Skip to content

Commit dc4ada4

Browse files
authored
Adds a minimal Babashka tasks infrastructure (#45)
* Add minimal Babashka tasks infrastructure Adds three Babashka tasks: - test-jvm - test-cljs - new-test * Update README.md with info about Babashka tasks * Add :min-bb-version key to bb.edn * Babashka new-test task takes multiple arguments * Update test template * Update README.md with more information about tasks and naming tests * Rework mappings of special characters to conform to Jank's usage * Update namespaces with new file names
1 parent 970c417 commit dc4ada4

39 files changed

+185
-35
lines changed

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Anyone with Clojure knowledge can help out!
1414
Check out the latest progress and the steps for helping out here: https://github.com/jank-lang/clojure.core-test/issues/1
1515

1616
## Running the tests
17+
18+
Note: You can also run tests with Babashka tasks. See below.
19+
1720
For a one-off run, you can use the following:
1821

1922
```bash
@@ -70,3 +73,70 @@ separated.
7073
```bash
7174
npx nodemon -w target/js taget/js/node-tests.js --test=clojure.core-test.int-questionmark
7275
```
76+
77+
## Babashka Tasks
78+
79+
You can see which Babashka tasks are available with:
80+
```bash
81+
~$ bb tasks
82+
The following tasks are available:
83+
84+
test-jvm Runs JVM tests
85+
test-cljs Runs CLJS tests
86+
new-test Creates new test for the Clojure symbols named by <args>
87+
88+
```
89+
90+
Currently, there are tasks to run the Clojure JVM and Clojurescript test suites.
91+
92+
Another task, `new-test`, allows you to easily create new test files
93+
that have all the standard naming conventions already applied. If you
94+
wanted to test a function named `clojure.core/foo`, for instance, you
95+
would type:
96+
97+
```bash
98+
bb new-test foo
99+
```
100+
101+
will create a new file named `foo.cljc` in the test namespace. The
102+
test file will look like the following:
103+
104+
```
105+
(ns clojure.core-test.{{ns-name}}
106+
(:require [clojure.test :as t :refer [deftest testing is are]]
107+
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
108+
109+
(when-var-exists clojure.core/{{sym-name}}
110+
(deftest test-{{sym-name}}
111+
;; `testing` sections are optional, depending on how you want to
112+
;; structure your tests. If you have a lot of tests and they group
113+
;; together in subgroups, then use `testing`. The `testing` form
114+
;; can also be a nice way to group tests that only apply to a
115+
;; subset of Clojure implementations. These can then be guarded by
116+
;; reader conditionals.
117+
(testing "section name"
118+
(is (= 1 0)))))
119+
```
120+
121+
Simply fill in test assertions and you're off and running.
122+
123+
Note: `new-test` takes care of converting various characters that
124+
might be problematic in file names to expanded versions. For instance
125+
"?" is converted to "questionmark" and "*" is converted to
126+
"star". Thus, you should always provide the name of the `clojure.core`
127+
symbol you want to test, not the file name or other name. You may need
128+
to quote or escape special characters when executing the command in
129+
the shell, however, to prevent the shell from interpreting them before
130+
they are passed to the Babashka task.
131+
132+
The complete set of conversions of characters to names is:
133+
- "*" -> "star"
134+
- "+" -> "plus
135+
- "!" -> "bang"
136+
- "'" -> "squote"
137+
- "?" -> "qmark"
138+
- "<" -> "lt"
139+
- ">" -> "gt"
140+
- "=" -> "eq"
141+
- "%" -> "percent"))
142+

bb.edn

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{:min-bb-version "0.4.0"
2+
:paths ["bb"]
3+
:tasks
4+
{test-jvm {:doc "Runs JVM tests"
5+
:task (shell "lein test")}
6+
test-cljs {:doc "Runs CLJS tests"
7+
:task (shell "npx shadow-cljs compile test")}
8+
new-test {:doc "Creates new test for the Clojure symbols named by <args>"
9+
:requires ([new-test])
10+
:task (new-test/new-test *command-line-args*)}}}

bb/new_test.clj

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
(ns new-test
2+
"Creates a new test from a template"
3+
(:require [babashka.fs :as fs]
4+
[clojure.string :as str]
5+
[selmer.parser :as s]
6+
[selmer.util :as util]))
7+
8+
;;; *, +, !, -, _, ', ?, <, > and =
9+
(defn sym-name->ns-name
10+
"Replace special characters in symbol name to make an ns-name."
11+
[sym]
12+
(let [n (name sym)
13+
ns-sym (-> (if (str/starts-with? n "-")
14+
(str/replace-first n "-" "minus")
15+
n)
16+
(str/replace "*" "-star")
17+
(str/replace "+" "-plus")
18+
(str/replace "!" "-bang")
19+
(str/replace "'" "-squote")
20+
(str/replace "?" "-qmark")
21+
(str/replace "<" "-lt")
22+
(str/replace ">" "-gt")
23+
(str/replace "=" "-eq")
24+
(str/replace "%" "-percent"))]
25+
(if (str/starts-with? ns-sym "-")
26+
(subs ns-sym 1)
27+
ns-sym)))
28+
29+
(defn ns-name->file-name
30+
"Replace hyphens with underscores to create the file name."
31+
[ns-name]
32+
(str/replace ns-name "-" "_"))
33+
34+
(defn new-test
35+
"Create a new test file for the symbol which is the first command line argument."
36+
[args]
37+
(if (zero? (count args))
38+
(println "Please supply one or more Clojure symbols corresponding to the new tests.")
39+
(loop [[sym-name & args] args]
40+
(when sym-name
41+
(let [ns-name (sym-name->ns-name sym-name)
42+
file-name (ns-name->file-name ns-name)
43+
dest-file-name (str "test/clojure/core_test/" file-name ".cljc")]
44+
(if (fs/exists? dest-file-name)
45+
(println dest-file-name "already exists. No action taken.")
46+
(do (println "Creating" dest-file-name)
47+
(let [template (slurp "templates/test-template.cljc")]
48+
(spit dest-file-name
49+
(util/without-escaping
50+
(s/render template {:sym-name sym-name
51+
:ns-name ns-name
52+
:file-name file-name})))))))
53+
(recur args)))))
54+
55+
56+

templates/test-template.cljc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(ns clojure.core-test.{{ns-name}}
2+
(:require [clojure.test :as t :refer [deftest testing is are]]
3+
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
4+
5+
(when-var-exists clojure.core/{{sym-name}}
6+
(deftest test-{{sym-name}}
7+
;; `testing` sections are optional, depending on how you want to
8+
;; structure your tests. If you have a lot of tests and they group
9+
;; together in subgroups, then use `testing`. The `testing` form
10+
;; can also be a nice way to group tests that only apply to a
11+
;; subset of Clojure implementations. These can then be guarded by
12+
;; reader conditionals.
13+
(testing "section name"
14+
(is (= 1 0)))))

test/clojure/core_test/any_questionmark.cljc renamed to test/clojure/core_test/any_qmark.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(ns clojure.core-test.any-questionmark
1+
(ns clojure.core-test.any-qmark
22
(:require [clojure.test :as t :refer [deftest testing is are]]
33
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
44

test/clojure/core_test/char_questionmark.cljc renamed to test/clojure/core_test/char_qmark.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(ns clojure.core-test.char-questionmark
1+
(ns clojure.core-test.char-qmark
22
(:require [clojure.test :as t :refer [deftest testing is are]]
33
[clojure.core-test.number-range :as r]
44
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

test/clojure/core_test/decimal_questionmark.cljc renamed to test/clojure/core_test/decimal_qmark.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(ns clojure.core-test.decimal-questionmark
1+
(ns clojure.core-test.decimal-qmark
22
(:require [clojure.test :as t :refer [deftest testing is are]]
33
[clojure.core-test.number-range :as r]
44
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

test/clojure/core_test/double_questionmark.cljc renamed to test/clojure/core_test/double_qmark.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(ns clojure.core-test.double-questionmark
1+
(ns clojure.core-test.double-qmark
22
(:require [clojure.test :as t :refer [deftest testing is are]]
33
[clojure.core-test.number-range :as r]
44
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

test/clojure/core_test/even_questionmark.cljc renamed to test/clojure/core_test/even_qmark.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(ns clojure.core-test.even-questionmark
1+
(ns clojure.core-test.even-qmark
22
(:require [clojure.test :as t :refer [deftest testing is are]]
33
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
44

test/clojure/core_test/false_questionmark.cljc renamed to test/clojure/core_test/false_qmark.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(ns clojure.core-test.false-questionmark
1+
(ns clojure.core-test.false-qmark
22
(:require [clojure.test :as t :refer [deftest testing is are]]
33
[clojure.core-test.number-range :as r]
44
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

0 commit comments

Comments
 (0)