/
filter.clj
137 lines (119 loc) · 5.01 KB
/
filter.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
(ns kaocha.plugin.filter
(:refer-clojure :exclude [symbol])
(:require [kaocha.plugin :as plugin :refer [defplugin]]
[kaocha.testable :as testable]
[clojure.set :as set]
[kaocha.output :as output]
[kaocha.core-ext :refer [symbol]]))
(defn- accumulate [m k v]
(update m k (fnil conj []) v))
(defn matches? [{:as testable
::testable/keys [id meta aliases]}
filters meta-filters]
(or (some #(= (keyword %) id) filters)
(some #(= (str %) (namespace id)) filters)
(seq (filter (set aliases) (map keyword filters)))
(some #(get meta (keyword %)) meta-filters)))
(defn filters [{:as testable
:kaocha.filter/keys [skip focus skip-meta focus-meta]}]
{:skip skip
:focus focus
:skip-meta skip-meta
:focus-meta focus-meta})
(defn merge-filters [f1 f2]
{:skip (concat (:skip f1) (:skip f2))
:skip-meta (concat (:skip-meta f1) (:skip-meta f2))
:focus (if (seq (:focus f2))
(:focus f2)
(:focus f1))
:focus-meta (if (seq (:focus-meta f2))
(:focus-meta f2)
(:focus-meta f1))})
(defn truthy-keys [m]
(map key (filter val m)))
(defn remove-missing-metadata-keys [focus-meta testable]
(let [used-meta (into #{}
(comp
(map (comp truthy-keys ::testable/meta))
cat
(map keyword))
(testable/test-seq testable))
focus-meta (set focus-meta)
unused (set/difference focus-meta used-meta)]
(doseq [u unused]
(output/warn "No tests found with metadata key " u ". Ignoring --focus-meta " u "."))
(set/difference focus-meta unused)))
(defn filter-testable [testable opts]
(let [{:as opts
:keys [skip focus skip-meta focus-meta]} (merge-filters opts (filters testable))
recurse (fn recurse
([]
(recurse opts))
([opts]
(cond-> testable
(:kaocha.test-plan/tests testable)
(update :kaocha.test-plan/tests (partial map #(filter-testable % opts))))))
skip-test (fn []
(assoc testable ::testable/skip true))]
(cond
(or (seq focus) (seq focus-meta))
(cond
;; - postiive
;; - foo ^:a
;; - bar ^:b
;; --focus positive --focus-meta a
;;
(matches? testable focus focus-meta)
(recurse (dissoc opts :focus :focus-meta))
;; Is it better to split this?
;; e.g. focus-meta inside
;; (matches? testable focus #{})
;; (recurse (dissoc opts :focus))
;; (matches? testable #{} focus-meta)
;; (recurse (dissoc opts :focus-meta))
(some #(matches? % focus focus-meta) (testable/test-seq testable))
(recurse)
:else
(skip-test))
(matches? testable skip skip-meta)
(skip-test)
(:kaocha.test-plan/tests testable)
(recurse)
:else
testable)))
(defplugin kaocha.plugin/filter
(cli-options [opts]
(let [parse #(keyword (if (= \: (first %)) (subs % 1) %))]
(conj opts
[nil "--skip SYM" "Skip tests with this ID and their children."
:parse-fn parse
:assoc-fn accumulate]
[nil "--focus SYM" "Only run this test, skip others."
:parse-fn parse
:assoc-fn accumulate]
[nil "--skip-meta SYM" "Skip tests where this metadata key is truthy."
:parse-fn parse
:assoc-fn accumulate]
[nil "--focus-meta SYM" "Only run tests where this metadata key is truthy."
:parse-fn parse
:assoc-fn accumulate])))
(config [config]
(let [{:keys [skip focus skip-meta focus-meta]} (:kaocha/cli-options config)]
(cond-> config
(seq skip) (assoc :kaocha.filter/skip skip)
(seq focus) (assoc :kaocha.filter/focus focus)
(seq skip-meta) (assoc :kaocha.filter/skip-meta skip-meta)
(seq focus-meta) (assoc :kaocha.filter/focus-meta focus-meta))))
(post-load [test-plan]
(let [{:keys [focus focus-meta]} (:kaocha/cli-options test-plan)]
(when (and (seq focus) (empty? (filter #(matches? % focus nil) (testable/test-seq test-plan))))
(output/warn ":focus " focus " did not match any tests."))
(let [test-plan (update test-plan :kaocha.filter/focus-meta remove-missing-metadata-keys test-plan)
filter-suite (fn [suite]
(let [suite (update suite :kaocha.filter/focus-meta remove-missing-metadata-keys test-plan)]
(-> suite
(filter-testable {})
(dissoc :kaocha.filter/focus :kaocha.filter/focus-meta)
(filter-testable (filters test-plan)))))]
(-> test-plan
(update :kaocha.test-plan/tests (partial map filter-suite)))))))