Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ how different EDN forms are interpreted as our test runner:
| --- | --- | --- |
| Unqualified Symbol | `my.namespace-test` | Run all tests in this namespace |
| Qualified Symbol | `my.namespace-test/my-test` | Run one specific test |
| String | `'"test/metabase/api"'` | Run all tests in test namespaces in this directory (including subdirectories) |
| String (directory) | `'"test/metabase/api"'` | Run all tests in test namespaces in this directory (including subdirectories) |
| String (file) | `'"test/metabase/api/user_test.clj"'` | Run all tests in this specific test file |
| Vector of symbols/strings | `'[my.namespace "test/metabase/some_directory"]'` | Union of tests found by the individual items in the vector |

### Example commands:
Expand All @@ -47,6 +48,7 @@ how different EDN forms are interpreted as our test runner:
| Run tests in a specific namespace | `clojure -X:test :only my.namespace-test` |
| Run a specific test | `clojure -X:test :only my.namespace-test/my-test` |
| Run tests in a specific directory (including subdirectories) | `clojure -X:test :only '"test/metabase/api"'` |
| Run tests in a specific file | `clojure -X:test :only '"test/metabase/api/user_test.clj"'` |
| Run tests in 2 namespaces | `clojure -X:test :only '[my.namespace-test my.other.namespace-test]'` |


Expand Down
29 changes: 20 additions & 9 deletions src/mb/hawk/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[clojure.set :as set]
[clojure.string :as str]
[clojure.test :as t]
[clojure.tools.namespace.file :as ns.file]
[clojure.tools.namespace.find :as ns.find]
[eftest.report.pretty]
[eftest.report.progress]
Expand All @@ -27,7 +28,7 @@
;;;; Finding tests

(defmulti find-tests
"Find test vars in `arg`, which can be a string directory name, symbol naming a specific namespace or test, or a
"Find test vars in `arg`, which can be a string directory or file name, symbol naming a specific namespace or test, or a
collection of one or more of the above."
{:arglists '([arg options])}
(fn [arg _options]
Expand Down Expand Up @@ -55,16 +56,26 @@
(re-matches (re-pattern namespace-pattern) (name ns-symbol))
true))

;; directory
;; directory or file
(defmethod find-tests java.io.File
[^java.io.File file {:keys [namespace-pattern exclude-directories], :as options}]
(when (and (.isDirectory file)
(not (str/includes? (str file) ".gitlibs/libs"))
(not (exclude-directory? file exclude-directories)))
(println "Looking for test namespaces in directory" (str file))
(->> (ns.find/find-namespaces-in-dir file)
(filter #(include-namespace? % namespace-pattern))
(mapcat #(find-tests % options)))))
(cond
;; handle regular files
(.isFile file)
;; almost exact code from `ns.find/find-namespaces-in-dir`
(let [[_ nom :as decl] (try (ns.file/read-file-ns-decl file)
(catch Exception _ nil))]
(when (and decl nom (symbol? nom))
(find-tests nom options)))

;; handle directories
(.isDirectory file)
(when (and (not (str/includes? (str file) ".gitlibs/libs"))
(not (exclude-directory? file exclude-directories)))
(println "Looking for test namespaces in directory" (str file))
(->> (ns.find/find-namespaces-in-dir file)
(filter #(include-namespace? % namespace-pattern))
(mapcat #(find-tests % options))))))

(defn- load-test-namespace [ns-symb]
(binding [hawk.init/*test-namespace-being-loaded* ns-symb]
Expand Down
11 changes: 11 additions & 0 deletions test/mb/hawk/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,14 @@
(hawk/find-tests this-ns options))
{:only-tags [:exclude-this-test]}
{:only-tags #{:exclude-this-test}}))

(deftest only-test
(testing "Can find tests when supplied a namespace"
(is (some #{#'only-test}
(hawk/find-tests 'mb.hawk.core-test nil))))
(testing "Can find tests when supplied a dir"
(is (some #{#'only-test}
(hawk/find-tests "test/mb/hawk" nil))))
(testing "Can find tests when supplied a file path"
(is (some #{#'only-test}
(hawk/find-tests "test/mb/hawk/core_test.clj" nil)))))
Loading