Skip to content

Commit

Permalink
Add tests for REPL and test commands (Issue #70).
Browse files Browse the repository at this point in the history
  • Loading branch information
emezeske committed Apr 11, 2012
1 parent f26a4a2 commit 51180b4
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 52 deletions.
8 changes: 4 additions & 4 deletions plugin/src/leiningen/cljsbuild.clj
Expand Up @@ -195,7 +195,7 @@
(apply ~task ~args)
(do ~@forms)))

(defn- compile-hook [task & args]
(defn compile-hook [task & args]
(skip-if-prepping task args
(let [compile-result (apply task args)
build-ids nil]
Expand All @@ -204,20 +204,20 @@
(run-compiler (first args) (config/extract-options (first args))
build-ids false)))))

(defn- test-hook [task & args]
(defn test-hook [task & args]
(skip-if-prepping task args
(let [test-results [(apply task args)
(run-tests (first args) (config/extract-options (first args)) [])]]
(if (every? #(= % exit-success) test-results)
exit-success
exit-failure))))

(defn- clean-hook [task & args]
(defn clean-hook [task & args]
(skip-if-prepping task args
(apply task args)
(clean (first args) (config/extract-options (first args)))))

(defn- jar-hook [task & [project out-file filespecs]]
(defn jar-hook [task & [project out-file filespecs]]
(skip-if-prepping task [project out-file filespecs]
(apply task [project out-file (concat filespecs (jar/get-filespecs project))])))

Expand Down
203 changes: 155 additions & 48 deletions plugin/test/leiningen/test/cljsbuild.clj
Expand Up @@ -3,7 +3,9 @@
leiningen.cljsbuild
midje.sweet)
(:require
[leiningen.trampoline :as ltrampoline]
[leiningen.cljsbuild.config :as config]
[leiningen.cljsbuild.jar :as jar]
[leiningen.cljsbuild.subproject :as subproject]))

; This crazy in-ns business is to mock out the namespaces that would normally
Expand Down Expand Up @@ -32,39 +34,32 @@
(clojure.core/use 'midje.sweet)
(unfinished run-tests)

(in-ns 'leiningen.test.cljsbuild)

(defmacro quietly [& body]
`(binding [*out* (new java.io.StringWriter)]
~@body))

(defn dummy-eval-in-project [_ _ _ body _]
(eval body))

(defn dummy-in-threads [f s]
(doseq [i s] (f i)))
(in-ns 'cljsbuild.repl.listen)
(clojure.core/use 'midje.sweet)
(unfinished run-repl-listen)
(unfinished run-repl-launch)

(defn dummy-once-every-bg [_ _ _]
; TODO
nil)
(in-ns 'cljsbuild.repl.rhino)
(clojure.core/use 'midje.sweet)
(unfinished run-repl-rhino)

; TODO: Could just define these in the namespaces directly?
(defmacro with-dummies [& forms]
`(binding [subproject/eval-in-project dummy-eval-in-project
cljsbuild.util/in-threads dummy-in-threads
cljsbuild.util/once-every-bg dummy-once-every-bg]
~@forms))
(in-ns 'leiningen.test.cljsbuild)

(def crossover-path "c")
(def repl-listen-port 10000)
(def repl-launch-command-id "launch-id")
(def repl-launch-command ["launch-me"])
(def test-command-id "test-id")
(def test-command ["test-me"])
(def crossover-path "crossover-path")
(def crossovers [])

(def build-id "1")
(def source-path "a")
(def build-id "build-id")
(def source-path "source-path")
(def warn-on-undeclared false)

(def compiler
{:output-to "h"
:output-dir "i"
{:output-to "output-to"
:output-dir "output-dir"
:optimizations :advanced
:pretty-print false})

Expand All @@ -73,7 +68,7 @@
{:id build-id
:source-path source-path
:jar true
:notify-command ["g"]
:notify-command ["notify"]
:warn-on-undeclared warn-on-undeclared
:compiler compiler}))

Expand All @@ -83,20 +78,43 @@
(def project
{:dependencies [['org.clojure/clojure "1.3.0"]]
:cljsbuild
{:repl-launch-commands {:a ["a"]}
:repl-listen-port 10000
:test-commands {:b ["b"]}
{:repl-listen-port repl-listen-port
:repl-launch-commands {repl-launch-command-id repl-launch-command}
:test-commands {test-command-id test-command}
:crossover-path crossover-path
:crossover-jar true
:crossovers crossovers
:builds builds}})

(defmacro quietly [& forms]
`(binding [*out* (new java.io.StringWriter)]
~@forms))

(defn run-cljsbuild [& args]
(quietly
(apply cljsbuild args)))

(defn hook-success [& args]
0)

(defn hook-failure [& args]
1)

(defn eval-locally [_ _ _ body _]
(eval body))

(defmacro with-compiler-bindings [& forms]
`(binding [subproject/eval-in-project eval-locally
cljsbuild.util/in-threads (fn [f# s#] (doseq [i# s#] (f# i#)))
cljsbuild.util/once-every-bg (fn [_# _# _#] nil)]
~@forms))

(fact "fail when no arguments present"
(quietly (cljsbuild project)) => 1)
(run-cljsbuild project) => 1)

(fact "once/auto call eval-in-project with the right args"
(doseq [command ["once" "auto"]]
(quietly (cljsbuild project command)) => 0
(run-cljsbuild project command) => 0
(provided
(subproject/eval-in-project
project
Expand All @@ -108,9 +126,8 @@
(fact "once/auto tasks call the compiler correctly"
(doseq [[command watch?] {"once" false "auto" true}
extra-args [[] [build-id]]]
(with-dummies
(quietly
(apply cljsbuild project command extra-args))) => 0
(with-compiler-bindings
(apply run-cljsbuild project command extra-args)) => 0
(provided
(cljsbuild.crossover/copy-crossovers
crossover-path
Expand All @@ -124,21 +141,25 @@
watch?) => nil :times 1)))

(fact "bad build IDs are detected"
(with-dummies
(quietly
(cljsbuild project "once" "wrong-build-id"))) => (throws Exception))
(with-compiler-bindings
(run-cljsbuild project "once" "wrong-build-id")) => (throws Exception))

(fact "clean calls cleanup-files"
(with-dummies
(with-compiler-bindings
(run-cljsbuild project "clean")) => 0
(with-compiler-bindings
(quietly
(cljsbuild project "clean"))) => 0
(provided
(clean-hook hook-success project))) => 0
(with-compiler-bindings
(quietly
(clean-hook hook-failure project))) => 0
(against-background
(cljsbuild.clean/cleanup-files compiler) => nil :times 1))

(fact "test calls run-tests"
(with-dummies
(fact "compile-hook calls through to the compiler when task succeeds"
(with-compiler-bindings
(quietly
(cljsbuild project "test"))) => 0
(compile-hook hook-success project))) => 0
(provided
(cljsbuild.crossover/copy-crossovers
crossover-path
Expand All @@ -149,9 +170,95 @@
compiler
anything
warn-on-undeclared
false) => nil :times 1
; TODO: Actually check the run-tests argument.
; TODO: Check that test ID selectorsw work.
(cljsbuild.test/run-tests anything) => nil :times 1))
false) => nil :times 1))

(fact "compile-hook does not call through to the compiler when task fails"
(with-compiler-bindings
(quietly
(compile-hook hook-failure project))) => 1
(provided
(cljsbuild.crossover/copy-crossovers
anything
anything) => nil :times 0
(cljsbuild.compiler/run-compiler
anything
anything
anything
anything
anything
anything) => nil :times 0))

; NOTE: This let has to be outside the fact, because against-background does not
; like being directly inside a let.
(let [parsed-commands [(config/parse-shell-command test-command)]]
(fact "tests work correctly"
(with-compiler-bindings
(run-cljsbuild project "test")) => 0
(with-compiler-bindings
(run-cljsbuild project "test" test-command-id)) => 0
(with-compiler-bindings
(quietly
(test-hook hook-success project))) => 0
(with-compiler-bindings
(quietly
(test-hook hook-failure project))) => 1
(against-background
(cljsbuild.crossover/copy-crossovers
crossover-path
crossovers) => nil :times 1
(cljsbuild.compiler/run-compiler
source-path
crossover-path
compiler
anything
warn-on-undeclared
false) => nil :times 1
(cljsbuild.test/run-tests parsed-commands) => 0 :times 1)))

(defmacro with-repl-bindings [& forms]
`(binding [subproject/eval-in-project eval-locally
ltrampoline/*trampoline?* true]
~@forms))

; TODO: Test REPLs, hooks.
(fact "repl-listen calls run-repl-listen"
(with-repl-bindings
(run-cljsbuild project "repl-listen")) => 0
(provided
(cljsbuild.repl.listen/run-repl-listen repl-listen-port anything) => nil :times 1))

(fact "repl-launch with no ID fails"
(with-repl-bindings
(run-cljsbuild project "repl-launch")) => (throws Exception))

(fact "repl-launch with bad ID fails"
(with-repl-bindings
(run-cljsbuild project "repl-launch" "wrong-repl-launch-id")) => (throws Exception))

(fact "repl-launch calls run-repl-launch"
(let [parsed-command (config/parse-shell-command repl-launch-command)]
(with-repl-bindings
(run-cljsbuild project "repl-launch" repl-launch-command-id)) => 0
(provided
(cljsbuild.repl.listen/run-repl-launch
repl-listen-port
anything
parsed-command) => nil :times 1)))

(fact "repl-rhino calls run-repl-rhino"
(with-repl-bindings
(run-cljsbuild project "repl-rhino")) => 0
(provided
(cljsbuild.repl.rhino/run-repl-rhino) => nil :times 1))

(unfinished jar-task)

(fact "jar-hook calls through to get-filespecs"
(let [out-file "out"
input-filespecs [{:type :bytes :path "/i/j" :bytes "fake-1"}]
project-filespecs [{:type :bytes :path "/a/b" :bytes "fake-2"}]
all-filespecs (concat input-filespecs project-filespecs)]
(quietly
(jar-hook jar-task project out-file input-filespecs)) => 0
(provided
(jar-task project out-file all-filespecs) => 0
(jar/get-filespecs project) => project-filespecs)))

0 comments on commit 51180b4

Please sign in to comment.