diff --git a/README.md b/README.md index 93245ca..c766a99 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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]'` | diff --git a/src/mb/hawk/core.clj b/src/mb/hawk/core.clj index 1966bd6..b73ca1c 100644 --- a/src/mb/hawk/core.clj +++ b/src/mb/hawk/core.clj @@ -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] @@ -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] @@ -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] diff --git a/test/mb/hawk/core_test.clj b/test/mb/hawk/core_test.clj index c8713a8..dde3087 100644 --- a/test/mb/hawk/core_test.clj +++ b/test/mb/hawk/core_test.clj @@ -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)))))