Skip to content

Commit 00baaa5

Browse files
Add :only-tags option (#32)
* Improved test partitioning * Update dox * Code cleanup * Modernize GH Actions * Appease Kondo * Ok partitioning does need to sort namespaces (but not vars) * Support :only-tags * Turns out I don't know how to use the GH conflict resolver --------- Co-authored-by: Chris Truter <[email protected]> Co-authored-by: Chris Truter <[email protected]>
1 parent 199d5f5 commit 00baaa5

File tree

3 files changed

+76
-28
lines changed

3 files changed

+76
-28
lines changed

README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ please submit a PR.
153153
:exec-args {:exclude-directories ["src" "resources" "shared/src"]}}}}
154154
```
155155

156-
## Skipping namespaces
156+
## Skipping namespaces or vars with tags
157157

158158
You can optionally exclude tests in namespaces with certain tags by specifying the `:exclude-tags` option:
159159

@@ -177,7 +177,30 @@ And adding it to namespaces like
177177
...)
178178
```
179179

180-
Currently only supported on namespaces! It would be nice to support this on individual tests as well -- PRs are welcome!
180+
## Only running tests against namespaces or vars with tags
181+
182+
The opposite of `:exclude-tags` -- you can only run tests against a certain set of tags with `:only-tags`. If multiple
183+
`:only-tags` are specified, only namespaces or vars that have all of those tags will be run.
184+
185+
`:only-tags` can be combined with `:only` and/or `:exclude-tags`.
186+
187+
```
188+
clj -X:test :only-tags [:my-project/e2e-test]
189+
```
190+
191+
will only run tests in namespaces like
192+
193+
```clj
194+
(ns ^:my-project/e2e-test my-namespace
195+
...)
196+
```
197+
198+
or ones individually marked `:my-project/e2e-test` like
199+
200+
```clj
201+
(deftest ^:my-project/e2e-test my-test
202+
...)
203+
```
181204

182205
## Whole-Suite Hooks
183206

src/mb/hawk/core.clj

+30-26
Original file line numberDiff line numberDiff line change
@@ -76,36 +76,40 @@
7676
[(or (resolve symb)
7777
(throw (ex-info (format "Unable to resolve test named %s" symb) {:test-symbol symb})))])
7878

79-
(defn- excluded-tags
80-
"Return the set of all tags in an element's metadata that are also in the `:exclude-tags` options."
81-
[element options]
82-
(when-let [excluded-tags (not-empty (set (:exclude-tags options)))]
83-
(let [ns-tags (-> element meta keys set)]
84-
(not-empty (set/intersection excluded-tags ns-tags)))))
85-
86-
(defn- filter-tests-by-tag
87-
"Filter out the test cases with tags that are also in the `:exclude-tags` options."
88-
[tests options]
89-
(filter (fn [test]
90-
(if-let [excluded-tags (not-empty (excluded-tags test options))]
91-
(println (format
92-
"Skipping test `%s` due to excluded tag(s): %s"
93-
(:name (meta test))
94-
(->> excluded-tags sort (str/join ","))))
95-
test))
96-
tests))
79+
(defn- skip-by-tags?
80+
"Whether we should skip a namespace or test var because it has tags in `:exclude-tags` or is missing tags in
81+
`:only-tags`. Prints debug message as a side-effect."
82+
[ns-or-var options]
83+
(let [tags-set (fn [ns-or-var]
84+
(not-empty (set (keys (meta ns-or-var)))))
85+
excluded-tag? (when-let [exclude-tags (not-empty (set (:exclude-tags options)))]
86+
(when-let [disallowed-tags (not-empty (set/intersection exclude-tags (tags-set ns-or-var)))]
87+
(printf
88+
"Skipping `%s` due to excluded tag(s): %s\n"
89+
(if (var? ns-or-var)
90+
(:name (meta ns-or-var))
91+
(ns-name ns-or-var))
92+
(->> disallowed-tags sort (str/join ",")))
93+
true))
94+
missing-tag? (when (var? ns-or-var)
95+
(let [varr ns-or-var]
96+
(when-let [only-tags (not-empty (set (:only-tags options)))]
97+
(when-let [missing-tags (not-empty (set/difference only-tags
98+
(tags-set (:ns (meta varr)))
99+
(tags-set varr)))]
100+
(printf
101+
"Skipping `%s` due to missing only tag(s): %s\n"
102+
(:name (meta varr))
103+
(->> missing-tags sort (str/join ",")))
104+
true))))]
105+
(or excluded-tag? missing-tag?)))
97106

98107
(defn- find-tests-for-namespace-symbol
99108
[ns-symb options]
100109
(load-test-namespace ns-symb)
101-
(if-let [excluded-tags (not-empty (excluded-tags (find-ns ns-symb) options))]
102-
(println (format
103-
"Skipping tests in `%s` due to excluded tag(s): %s"
104-
ns-symb
105-
(->> excluded-tags sort (str/join ","))))
106-
(filter-tests-by-tag
107-
(eftest.runner/find-tests ns-symb)
108-
options)))
110+
(when-not (skip-by-tags? (find-ns ns-symb) options)
111+
(remove #(skip-by-tags? % options)
112+
(eftest.runner/find-tests ns-symb))))
109113

110114
;; a test namespace or individual test
111115
(defmethod find-tests clojure.lang.Symbol

test/mb/hawk/core_test.clj

+21
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,24 @@
5858
{:exclude-tags [:exclude-this-test]}
5959
{:exclude-tags #{:exclude-this-test}}
6060
{:exclude-tags [:exclude-this-test :another/tag]}))
61+
62+
(deftest only-tags-test
63+
(are [options] (= []
64+
(hawk/find-tests this-ns options))
65+
{:only-tags [:another/tag]}
66+
{:only-tags [:another/tag :exclude-tags-test]}
67+
{:only-tags [:another/tag :exclude-this-test]})
68+
(are [options] (= (hawk/find-tests this-ns {})
69+
(hawk/find-tests this-ns options))
70+
{:only-tags [:exclude-tags-test]}
71+
{:only-tags #{:exclude-tags-test}})
72+
(are [options] (= [#'find-tests-test]
73+
(hawk/find-tests this-ns options))
74+
{:only-tags [:exclude-this-test]}
75+
{:only-tags #{:exclude-this-test}}
76+
{:only-tags [:exclude-this-test :exclude-tags-test]}
77+
{:only-tags #{:exclude-this-test :exclude-tags-test}})
78+
(are [options] (= [#'find-tests-test]
79+
(hawk/find-tests this-ns options))
80+
{:only-tags [:exclude-this-test]}
81+
{:only-tags #{:exclude-this-test}}))

0 commit comments

Comments
 (0)