-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
node.cljs
98 lines (83 loc) · 2.87 KB
/
node.cljs
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
(ns shadow.test.node
{:dev/always true}
(:require
[shadow.test.env :as env]
[cljs.test :as ct]
[shadow.test :as st]
[clojure.string :as str]))
;; FIXME: add option to not exit the node process?
(defmethod ct/report [::ct/default :end-run-tests] [m]
(if (ct/successful? m)
(js/process.exit 0)
(js/process.exit 1)))
;; get-test-data is a macro so this namespace REQUIRES :dev/always hint ns so that it is always recompiled
(defn ^:dev/after-load reset-test-data! []
(-> (env/get-test-data)
(env/reset-test-data!)))
(defn parse-args [args]
(reduce
(fn [opts arg]
(cond
(= "--help" arg)
(assoc opts :help true)
(= "--list" arg)
(assoc opts :list true)
(str/starts-with? arg "--test=")
(let [test-arg (subs arg 7)
test-syms
(->> (str/split test-arg ",")
(map symbol))]
(update opts :test-syms into test-syms))
:else
(do (println (str "Unknown arg: " arg))
opts)
))
{:test-syms []}
args))
(defn find-matching-test-vars [test-syms]
;; FIXME: should have some kind of wildcard support
(let [test-namespaces
(->> test-syms (filter simple-symbol?) (set))
test-var-syms
(->> test-syms (filter qualified-symbol?) (set))]
(->> (env/get-test-vars)
(filter (fn [the-var]
(let [{:keys [name ns]} (meta the-var)]
(or (contains? test-namespaces ns)
(contains? test-var-syms (symbol ns name))))))
)))
(defn execute-cli [{:keys [test-syms help list] :as opts}]
(let [test-env
(-> (ct/empty-env)
;; can't think of a proper way to let CLI specify custom reporter?
;; :report-fn is mostly for UI purposes, CLI should be fine with default report
#_(assoc :report-fn
(fn [m]
(tap> [:test m (ct/get-current-env)])
(prn m))))]
(cond
help
(do (println "Usage:")
(println " --list (list known test names)")
(println " --test=<ns-to-test>,<fqn-symbol-to-test> (run test for namespace or single var, separated by comma)"))
list
(doseq [[ns ns-info]
(->> (env/get-tests)
(sort-by first))]
(println "Namespace:" ns)
(doseq [var (:vars ns-info)
:let [m (meta var)]]
(println (str " " (:ns m) "/" (:name m))))
(println "---------------------------------"))
(seq test-syms)
(let [test-vars (find-matching-test-vars test-syms)]
(st/run-test-vars test-env test-vars))
:else
(st/run-all-tests test-env nil)
)))
(defn main [& args]
(reset-test-data!)
(if env/UI-DRIVEN
(js/console.log "Waiting for UI ...")
(let [opts (parse-args args)]
(execute-cli opts))))