Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

make bin/mage run-tests able to run a single test (#67450)

Oleksandr Solovyove597be0c10b3
diff --git a/bb.edn b/bb.edn
index deaa880a171..0b8b89f025a 100644
--- a/bb.edn
+++ b/bb.edn
@@ -209,18 +209,20 @@
(nrepl-eval "metabase.comments.core" code)))}
run-tests
- {:doc "Quickly run tests locally. Pass dirs/files as arguments to run them."
+ {:doc "Quickly run tests locally. Pass dirs, files, namespaces, or single tests as arguments."
:examples [["./bin/mage run-tests" "launch interactive test picker"]
["./bin/mage run-tests test/metabase/db" "run tests in test/metabase/db"]
- ["./bin/mage run-tests test/metabase/logger test/metabase/api_keys" "run tests in test/metabase/logger and src/metabase/api_keys"]
- ["./bin/mage run-tests test/metabase/config_test.clj" "run all tests in the metabase.config-test namespace"]]
+ ["./bin/mage run-tests test/metabase/logger test/metabase/api_keys" "run tests in test/metabase/logger and test/metabase/api_keys"]
+ ["./bin/mage run-tests test/metabase/config_test.clj" "run all tests in the metabase.config-test namespace"]
+ ["./bin/mage run-tests metabase.config-test" "run all tests in the metabase.config-test namespace (by name)"]
+ ["./bin/mage run-tests metabase.config-test/config-test" "run a single test by namespace/test-name"]]
;; TODO: add option to select files as well as dirs from fzf with styles
:options [["-s" "--selecting STYLE" "Style of test selection" :default "all"]
;; all, file, dir
["-p" "--port PORT" "Port to use for the task, defaults to value in .nrepl-port"]]
:arg-schema [:sequential
{:details "If none are given, you will be prompted to select directories recursively from test/ and enterprise/backend/test/"}
- [:string {:name "file-or-dir" :description "Files or directories (recursive) to test"}]]
+ [:string {:name "file-dir-ns-or-test" :description "Files, directories (recursive), namespaces, or namespace/test-name to test"}]]
:requires [[mage.quick-test-runner :as qtr]]
:task (task! (qtr/go parsed))}
diff --git a/mage/src/mage/be_dev.clj b/mage/src/mage/be_dev.clj
index ca8846a4f1b..7438cf00713 100644
--- a/mage/src/mage/be_dev.clj
+++ b/mage/src/mage/be_dev.clj
@@ -88,7 +88,7 @@
_ (u/debug "Code:\n-----\n" code-str "\n-----")
_ (bencode/write-bencode out {:op "eval" :code code-str})
final-value (atom nil)
- safe-print (fn [& msg] (when-not *quiet-nrepl-eval* (apply print (map #(str/replace % #"\n$" "") msg)) (flush)))]
+ safe-print (fn [& msg] (when-not *quiet-nrepl-eval* (run! print msg) (flush)))]
(loop []
(let [response (->> (bencode/read-bencode in) (walk/postwalk consume))]
(u/debug "Response:\n-----\n" (with-out-str (pp/pprint response)) "-----")
diff --git a/mage/src/mage/quick_test_runner.clj b/mage/src/mage/quick_test_runner.clj
index df640597602..16ba1021435 100644
--- a/mage/src/mage/quick_test_runner.clj
+++ b/mage/src/mage/quick_test_runner.clj
@@ -58,13 +58,17 @@
(str/join (repeat (:fail out-data) "❌"))
(str/join (repeat (:error out-data) "⚠️"))))
+(defn- only-edn [test-dirs]
+ (str "'" (pr-str test-dirs)))
+
(defn- run-tests-over-nrepl [test-dirs options]
(let [start (u/start-timer)
the-ns "mb.hawk.core"
+ only-arg (only-edn test-dirs)
the-cmd (str "(do (require (quote metabase.test-runner)) "
"((requiring-resolve 'dev.reload/reload!)) "
"(metabase.test-runner/find-and-run-tests-repl "
- "{:only " (pr-str test-dirs) "}))")]
+ "{:only " only-arg "}))")]
(println "Running Code over nrepl:" (c/bold the-cmd))
(bling/callout
{:type :info
@@ -101,43 +105,39 @@
(u/exit exit-code)))))
;; namespaces will be converted to their file paths, so this check will work.
+(def ^:private namespace-prefixes
+ ["metabase." "metabase-enterprise." "hooks."])
+
+(defn- namespace-like? [s]
+ (some #(str/starts-with? s %) namespace-prefixes))
+
(defn- check-arg [arg]
- (or (str/includes? arg ".clj") (str/includes? arg "/")))
-
-(defn- add-cljy-suffix-or-throw
- "Given a namespace, we can't tell if it's a clj or cljc file without looking for the file itself."
- [partial-file-path maybe-ns]
- (or (first
- (keep (fn [suffix]
- (let [file-path (str partial-file-path suffix)]
- (cond
- ;; src/metabase/...
- (str/starts-with? maybe-ns "metabase.")
- (let [path (str "src/" file-path)]
- (and (fs/exists? (str u/project-root-directory "/" path)) path))
-
- ;; enterprise/backend/src/metabase_enterprise/...
- (str/starts-with? maybe-ns "metabase-enterprise.")
- (let [path (str "enterprise/backend/" file-path)]
- (and (fs/exists? (str u/project-root-directory "/" path)) path)))))
- [".clj" ".cljc" ".bb"]))
- (throw
- (ex-info "" {:mage/error (str "Could not find a file for namespace: "
- (c/yellow partial-file-path)
- ". Tried appending .clj, .cljs, .cljc, and .bb -- is that a real namespace?")
- :babashka/exit 1}))))
-
-(defn- maybe-convert-ns-to-path [maybe-ns]
- (if (or (str/starts-with? maybe-ns "metabase.")
- (str/starts-with? maybe-ns "metabase-enterprise."))
- (-> maybe-ns
- str
- (str/replace "." "/")
- (str/replace "-" "_")
- ;; Cannot tell from the namespace alone if it's a clj, or cljc file. :melty-face:
- ;; Try to find the file by appending each suffix until we find one that exists:
- (add-cljy-suffix-or-throw maybe-ns))
- maybe-ns))
+ (cond
+ (symbol? arg) true
+ (string? arg) (or (str/includes? arg ".clj")
+ (str/includes? arg "/")
+ (namespace-like? arg))
+ :else false))
+
+(defn- normalize-test-arg [arg]
+ (cond
+ (symbol? arg)
+ arg
+
+ (not (string? arg))
+ arg
+
+ :else
+ (let [[ns-part test-name] (str/split arg #"/" 2)]
+ (cond
+ (and test-name (namespace-like? ns-part))
+ (symbol (str ns-part "/" test-name))
+
+ (namespace-like? arg)
+ (symbol arg)
+
+ :else
+ arg))))
(defn- setup-test-files [arguments {:keys [selecting] :as _options}]
(let [tests (if (seq arguments)
@@ -153,7 +153,7 @@
"--header-border" "rounded"
"--preview" (str "'" u/project-root-directory "/mage/cmd/fzf_preview.clj {}'")]))
str/split-lines))
- test-dir-or-nss (mapv maybe-convert-ns-to-path tests)]
+ test-dir-or-nss (mapv normalize-test-arg tests)]
(when-not (every? check-arg test-dir-or-nss)
(throw (ex-info "" {:mage/error (str
"When providing arguments, they must be file paths or directories, got: "
diff --git a/mage/test/mage/quick_test_runner_test.clj b/mage/test/mage/quick_test_runner_test.clj
new file mode 100644
index 00000000000..5eecb34dce9
--- /dev/null
+++ b/mage/test/mage/quick_test_runner_test.clj
@@ -0,0 +1,41 @@
+(ns mage.quick-test-runner-test
+ (:require
+ [clojure.test :refer [deftest is testing]]
+ [mage.quick-test-runner :as qtr]))
+
+(deftest only-edn-test
+ (testing "quotes symbols for nrepl eval"
+ (is (= "'[metabase.foo-test]"
+ (#'qtr/only-edn ['metabase.foo-test])))
+ (is (= "'[\"test/metabase/db\"]"
+ (#'qtr/only-edn ["test/metabase/db"])))
+ (is (= "'[metabase.foo-test/bar-test \"test/metabase/db\"]"
+ (#'qtr/only-edn ['metabase.foo-test/bar-test "test/metabase/db"])))))
+
+(deftest normalize-test-arg-test
+ (testing "namespace args"
+ (is (= 'metabase.foo-test
+ (#'qtr/normalize-test-arg "metabase.foo-test")))
+ (is (= 'metabase-enterprise.transforms.models-test/table-with-db-and-fields-hydration-test
+ (#'qtr/normalize-test-arg
+ "metabase-enterprise.transforms.models-test/table-with-db-and-fields-hydration-test")))
+ (is (= 'hooks.foo-test
+ (#'qtr/normalize-test-arg "hooks.foo-test")))
+ (is (= 'metabase.foo-test
+ (#'qtr/normalize-test-arg 'metabase.foo-test))))
+ (testing "path args"
+ (is (= "test/metabase/db"
+ (#'qtr/normalize-test-arg "test/metabase/db")))
+ (is (= "test/metabase/db/sync_test.clj"
+ (#'qtr/normalize-test-arg "test/metabase/db/sync_test.clj")))))
+
+(deftest check-arg-test
+ (testing "accepted variants"
+ (is (#'qtr/check-arg "metabase.foo-test"))
+ (is (#'qtr/check-arg "metabase.foo-test/bar-test"))
+ (is (#'qtr/check-arg "hooks.foo-test"))
+ (is (#'qtr/check-arg "test/metabase/db"))
+ (is (#'qtr/check-arg "test/metabase/db/sync_test.clj"))
+ (is (#'qtr/check-arg 'metabase.foo-test)))
+ (testing "rejected variants"
+ (is (not (#'qtr/check-arg "foo")))))
diff --git a/mage/test/mage/runtests.clj b/mage/test/mage/runtests.clj
index 17487a21c75..7ffd1113eaa 100644
--- a/mage/test/mage/runtests.clj
+++ b/mage/test/mage/runtests.clj
@@ -5,7 +5,9 @@
(set! *warn-on-reflection* true)
(def ^:private test-ns
- ['mage.core-test 'mage.start-db-test])
+ ['mage.core-test
+ 'mage.quick-test-runner-test
+ 'mage.start-db-test])
(when (= *file* (System/getProperty "babashka.file"))
(doseq [tns test-ns]