Skip to content

Commit b23c77e

Browse files
mfikesdnolen
authored andcommitted
CLJS-2593: Integration test suite for cljs.main
1 parent b763d8d commit b23c77e

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
dist: trusty
22

3+
git:
4+
depth: 500
5+
36
language: node_js
47

58
node_js:
@@ -14,6 +17,7 @@ before_install:
1417

1518
before_script:
1619
- script/bootstrap
20+
- script/uberjar
1721
- mkdir -p builds/out-adv
1822
- bin/cljsc src/test/cljs "{:optimizations :advanced
1923
:output-wrapper true
@@ -53,3 +57,9 @@ script:
5357
- grep '0 failures, 0 errors.' test-out.txt
5458
- script/test-self-parity | tee test-out.txt
5559
- grep '0 failures, 0 errors.' test-out.txt
60+
- script/test-cli node | tee test-out.txt
61+
- grep '0 failures, 0 errors.' test-out.txt
62+
- script/test-cli nashorn | tee test-out.txt
63+
- grep '0 failures, 0 errors.' test-out.txt
64+
- script/test-cli rhino | tee test-out.txt
65+
- grep '0 failures, 0 errors.' test-out.txt

script/test-cli

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
if [ ! -f target/cljs.jar ]; then
4+
echo "Run script/uberjar first"
5+
exit 1
6+
fi
7+
8+
java -cp target/cljs.jar:src/test/cljs_cli clojure.main -m cljs-cli.test-runner "$@"

src/test/cljs_cli/cljs_cli/test.clj

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
(ns cljs-cli.test
2+
(:require
3+
[clojure.test :refer [deftest]]
4+
[clojure.java.io :as io]
5+
[clojure.java.shell :as shell :refer [with-sh-dir]]
6+
[cljs-cli.util :refer [cljs-main output-is with-sources with-post-condition with-repl-env-filter]]))
7+
8+
(deftest eval-test
9+
(-> (cljs-main "-e" 3 "-e" nil "-e" 4)
10+
(output-is 3 4)))
11+
12+
(deftest init-test
13+
(with-sources {"src/foo/core.cljs"
14+
"(ns foo.core) (def x 3)"}
15+
(-> (cljs-main "-i" "src/foo/core.cljs" "-e" 'foo.core/x)
16+
(output-is 3))))
17+
18+
(deftest main-test
19+
(with-sources {"src/foo/core.cljs"
20+
"(ns foo.core) (defn -main [] (prn :hi))"}
21+
(-> (cljs-main "-m" "foo.core")
22+
(output-is :hi))))
23+
24+
(deftest command-line-args-test
25+
(with-sources {"src/foo/core.cljs"
26+
"(ns foo.core) (prn *command-line-args*)"}
27+
(-> (cljs-main "src/foo/core.cljs" "alpha" "beta" "gamma")
28+
(output-is (pr-str '("alpha" "beta" "gamma"))))))
29+
30+
(deftest command-line-args-empty-test
31+
(with-sources {"src/foo/core.cljs"
32+
"(ns foo.core) (prn *command-line-args*)"}
33+
(-> (cljs-main "src/foo/core.cljs")
34+
(output-is nil))))
35+
36+
(deftest initial-ns-test
37+
(-> (cljs-main "-e" "::foo")
38+
(output-is ":cljs.user/foo")))
39+
40+
(deftest source-test
41+
(with-sources {"src/foo/core.cljs"
42+
"(ns foo.core) (prn :hi)"}
43+
(-> (cljs-main "src/foo/core.cljs")
44+
(output-is :hi))))
45+
46+
(deftest compile-test
47+
(with-sources {"src/foo/core.cljs"
48+
"(ns foo.core) (defn -main [] (prn :hi))"}
49+
(with-post-condition (fn [dir] (.exists (io/file dir "out" "main.js")))
50+
(-> (cljs-main "-o" "out/main.js" "-c" "foo.core")
51+
(output-is)))))
52+
53+
(deftest run-optimized-node-test
54+
(with-repl-env-filter #{"node"}
55+
(with-sources {"src/foo/core.cljs"
56+
"(ns foo.core) (prn :hello-from-node)"}
57+
(with-post-condition (fn [dir]
58+
(= {:exit 0, :out ":hello-from-node\n", :err ""}
59+
(with-sh-dir dir
60+
(shell/sh "node" (str (io/file dir "out" "main.js"))))))
61+
(-> (cljs-main "-t" "node" "-o" "out/main.js" "-O" "advanced" "-c" "foo.core")
62+
(output-is))))))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(ns cljs-cli.test-runner
2+
(:require
3+
[cljs-cli.test]
4+
[cljs-cli.util]))
5+
6+
(defn -main [& args]
7+
(try
8+
(binding [cljs-cli.util/*repl-env* (or (first args) "nashorn")
9+
cljs-cli.util/*repl-opts* (second args)]
10+
(clojure.test/run-tests 'cljs-cli.test))
11+
(finally
12+
(shutdown-agents))))

src/test/cljs_cli/cljs_cli/util.clj

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
(ns cljs-cli.util
2+
(:require
3+
[clojure.string :as string]
4+
[clojure.java.io :as io]
5+
[clojure.java.shell :as shell]
6+
[clojure.test :refer [is]])
7+
(:import
8+
(java.io File)
9+
(java.nio.file Files CopyOption)
10+
(java.nio.file.attribute FileAttribute)))
11+
12+
(def ^:dynamic *repl-env* "nashorn")
13+
(def ^:dynamic *repl-env-filter* (constantly true))
14+
(def ^:dynamic *repl-opts* nil)
15+
(def ^:dynamic *sources* nil)
16+
(def ^:dynamic *post-condition* nil)
17+
18+
(defmacro with-sources
19+
[sources & body]
20+
`(binding [*sources* ~sources]
21+
~@body))
22+
23+
(defmacro with-post-condition
24+
[post-condition & body]
25+
`(binding [*post-condition* ~post-condition]
26+
~@body))
27+
28+
(defmacro with-repl-env-filter
29+
[repl-env-filter & body]
30+
`(binding [*repl-env-filter* ~repl-env-filter]
31+
~@body))
32+
33+
(defn- ^File make-temp-dir []
34+
(.toFile (Files/createTempDirectory "cljs-cli-test" (make-array FileAttribute 0))))
35+
36+
(defn- delete-recursively [fname]
37+
(doseq [f (reverse (file-seq (io/file fname)))]
38+
(io/delete-file f)))
39+
40+
(defn- copy-uberjar [^File dest]
41+
(Files/copy (.toPath (io/file "target/cljs.jar")) (.toPath (io/file dest "cljs.jar")) (make-array CopyOption 0)))
42+
43+
(defn- write-sources [temp-dir]
44+
(let [qualified #(io/file temp-dir %)]
45+
(run! #(io/make-parents (qualified %)) (keys *sources*))
46+
(run! (fn [[file source]]
47+
(spit (qualified file) source))
48+
*sources*)))
49+
50+
(defn- run-in-temp-dir [args]
51+
(let [temp-dir (make-temp-dir)]
52+
(try
53+
(write-sources temp-dir)
54+
(copy-uberjar temp-dir)
55+
(let [result (shell/with-sh-dir temp-dir
56+
#_(apply println "running:" args)
57+
(apply shell/sh args))]
58+
(when *post-condition*
59+
(is (*post-condition* temp-dir)))
60+
result)
61+
(finally
62+
(delete-recursively temp-dir)))))
63+
64+
(defn form-cp []
65+
(string/join File/pathSeparator ["cljs.jar" "src"]))
66+
67+
(defn cljs-main [& args]
68+
(if (*repl-env-filter* *repl-env*)
69+
(let [command-line-args (map str args)]
70+
(run-in-temp-dir
71+
(keep (fn [arg]
72+
(when arg
73+
(str arg)))
74+
(into ["java" "-cp" (form-cp) "cljs.main"
75+
"-re" *repl-env*
76+
(when *repl-opts* "-ro") (when *repl-opts* *repl-opts*)]
77+
command-line-args))))
78+
{:exit 0 :out "" :err ""}))
79+
80+
(def ^:private expected-browser-err
81+
"Compiling client js ...\nServing HTTP on localhost port 9000\nListening for browser REPL connect ...\n")
82+
83+
(defn- maybe-print-result-err [{:keys [err]}]
84+
(when (and (not (empty? err))
85+
(not (= expected-browser-err err)))
86+
(binding [*out* *err*]
87+
(println err))))
88+
89+
(defn output-is [result & expected-lines]
90+
(is (zero? (:exit result)))
91+
(maybe-print-result-err result)
92+
(is (= (apply str (map print-str (interleave expected-lines (repeat "\n"))))
93+
(:out result))))

0 commit comments

Comments
 (0)