Skip to content

Commit

Permalink
Convert all tests to use Midje (Issue #70).
Browse files Browse the repository at this point in the history
  • Loading branch information
emezeske committed Apr 9, 2012
1 parent d2835b0 commit 5816773
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 118 deletions.
2 changes: 1 addition & 1 deletion bin/test-install.sh
Expand Up @@ -14,7 +14,7 @@ pushd $project_root
rm -rf ~/.m2/repository/lein-cljsbuild ~/.m2/repository/cljsbuild/
for d in support plugin; do
pushd $d
lein install, test
lein install, midje
popd
done
for d in $projects; do
Expand Down
Expand Up @@ -4,4 +4,4 @@

(defn make-example-text []
(macros/reverse-eval
("code" "shared " "from the " "Hello " str)))
("code" "shared " "from the " "Hello " str)))
4 changes: 2 additions & 2 deletions example-projects/advanced/src-cljs/example/hello.cljs
Expand Up @@ -2,8 +2,8 @@
(:require
[example.crossover.shared :as shared]))

(defn ^:export say-hello []
(js/alert (shared/make-example-text)))
(defn ^:export say-hello []
(js/alert (shared/make-example-text)))

(defn add-some-numbers [& numbers]
(apply + numbers))
4 changes: 2 additions & 2 deletions example-projects/advanced/test-cljs/example/test/hello.cljs
Expand Up @@ -2,6 +2,6 @@
(:use [example.hello :only [add-some-numbers]]))

(defn run []
(assert (= (add-some-numbers 2 2) 4))
(assert (= (add-some-numbers 1 2 3) 6))
(assert (= (add-some-numbers 2 2) 4))
(assert (= (add-some-numbers 1 2 3) 6))
(assert (= (add-some-numbers 4 5 6) 15)))
4 changes: 4 additions & 0 deletions plugin/project.clj
Expand Up @@ -5,4 +5,8 @@
:url "http://www.eclipse.org/legal/epl-v10.html"
:distribution :repo}
:dependencies [[fs "1.1.2"]]
:dev-dependencies [[midje "1.3.1"]
; NOTE: lein-midje requires different versions to be
; installed for lein1 vs lein2 compatibility :(.
[lein-midje "1.0.9"]]
:eval-in-leiningen true)
3 changes: 2 additions & 1 deletion plugin/src/leiningen/cljsbuild.clj
Expand Up @@ -56,7 +56,8 @@
'~crossovers))]
(copy-crossovers#)
(when ~watch?
(cljsbuild.util/once-every 1000 "copying crossovers" copy-crossovers#))
(future
(cljsbuild.util/once-every 1000 "copying crossovers" copy-crossovers#)))
(cljsbuild.util/in-threads
(fn [opts#]
(cljsbuild.compiler/run-compiler
Expand Down
118 changes: 68 additions & 50 deletions plugin/test/leiningen/test/cljsbuild/config.clj
@@ -1,66 +1,84 @@
(ns leiningen.test.cljsbuild.config
(:use
leiningen.cljsbuild.config
clojure.test))
midje.sweet))

(deftest test-backwards-compatibility
(fact
(let [config-0-0-x-early {:source-path "a"
:compiler {:output-to "hello.js"}}
config-0-0-x-late [{:source-path "a"
:compiler {:output-to "hello.js"}}]
config-backwards {:builds
[{:source-path "a"
:compiler {:output-to "hello.js"}}]}]
(is (= config-backwards (backwards-compat config-0-0-x-early)))
(is (= config-backwards (backwards-compat config-0-0-x-late)))))
(backwards-compat config-0-0-x-early) => config-backwards
(backwards-compat config-0-0-x-late) => config-backwards))

(deftest test-ids
(fact
(let [config-vec {:builds [{:id "a"} {:id "b"}]}
config-map {:builds {:a {} :b {}}}]
(is (= config-vec (convert-builds-map config-vec)))
(is (= config-vec (convert-builds-map config-map)))))
(convert-builds-map config-vec) => config-vec
(convert-builds-map config-map) => config-vec))

(deftest test-shell-command
(is (= {:shell []}
(parse-shell-command [])))
(is (= {:shell ["a"]}
(parse-shell-command ["a"])))
(is (= {:shell ["a" "b" "c"] :x 1 :y 2}
(parse-shell-command ["a" "b" "c" :x 1 :y 2]))))
(fact
(parse-shell-command []) => {:shell []}
(parse-shell-command ["a"]) => {:shell ["a"]}
(parse-shell-command ["a" "b" "c" :x 1 :y 2]) => {:shell ["a" "b" "c"] :x 1 :y 2})

(deftest test-default-options
(let [config-in {:repl-launch-commands {:a ["a"]}
:repl-listen-port 10000
:test-commands {:b ["b"]}
:crossover-path "c"
:crossover-jar true
:crossovers ["d" "e"]
:builds
'({:source-path "f"
:jar true
:notify-command ["g"]
:warn-on-undeclared false
:compiler
{:output-to "h"
:output-dir "i"
:optimizations :advanced
:pretty-print false}})}]
; Ensure that none of our custom settings are overwritten by defaults.
(is (= config-in (set-default-options config-in)))
; Ensure that if any custom setting is missing, a default is provided.
(doseq [option (keys config-in)]
(is (contains? (set-default-options (dissoc config-in option)) option)))
(let [build (first (:builds config-in))]
(doseq [build-option (keys build)]
(let [defaulted (set-default-options
(assoc config-in :builds
(list (dissoc build build-option))))]
(is (contains? (first (:builds defaulted)) build-option))))
(let [compiler (:compiler build)]
(doseq [compiler-option (keys compiler)]
(let [defaulted (set-default-options
(assoc config-in :builds
(list
(assoc build :compiler
(dissoc compiler :compiler-option)))))]
(is (contains? (:compiler (first (:builds defaulted))) compiler-option))))))))
(def config-in
{:repl-launch-commands {:a ["a"]}
:repl-listen-port 10000
:test-commands {:b ["b"]}
:crossover-path "c"
:crossover-jar true
:crossovers ["d" "e"]
:builds
'({:source-path "f"
:jar true
:notify-command ["g"]
:warn-on-undeclared false
:compiler
{:output-to "h"
:output-dir "i"
:optimizations :advanced
:pretty-print false}})})

(fact "custom settings are not overwritten by defaults"
(set-default-options config-in) => config-in)

(fact "missing settings have defaults provided"
(doseq [option (keys config-in)]
(set-default-options (dissoc config-in option)) => (contains {option anything})))

(defn- get-build [config]
(first (:builds config)))

(defn- default-build-option [config build option]
(set-default-options
(assoc config :builds
(list (dissoc build option)))))

(fact "missing build settings have defaults provided"
(let [build (get-build config-in)]
(doseq [build-option (keys build)]
(let [defaulted (default-build-option config-in build build-option)]
(get-build defaulted) => (contains {build-option anything})))))

(defn- get-compiler [config]
(:compiler (get-build config)))

(defn- default-compiler-option [config compiler option]
(set-default-options
(assoc config :builds
(list
(assoc compiler :compiler
(dissoc compiler option))))))

(fact "missing compiler settings have defaults provided"
(let [compiler (:compiler (get-build config-in))]
(doseq [compiler-option (keys compiler)]
(let [defaulted (default-compiler-option config-in compiler compiler-option)]
(get-compiler defaulted) => (contains {compiler-option anything})))))

(fact
(extract-options {:cljsbuild config-in}) => config-in)
12 changes: 6 additions & 6 deletions plugin/test/leiningen/test/cljsbuild/jar.clj
@@ -1,12 +1,12 @@
(ns leiningen.test.cljsbuild.jar
(:use
leiningen.cljsbuild.jar
clojure.test))
midje.sweet))

(deftest test-relative-path
(is (= "a" (relative-path "/" "/a")))
(is (= "d/e" (relative-path "/a/b/c" "/a/b/c/d/e")))
(is (thrown? Exception (relative-path "" "a")))
(is (thrown? Exception (relative-path "/a/b/c" "/a/b"))))
(fact
(relative-path "/" "/a") => "a"
(relative-path "/a/b/c" "/a/b/c/d/e") => "d/e"
(relative-path "" "a") => (throws Exception)
(relative-path "/a/b/c" "/a/b") => (throws Exception))

; TODO: More tests!
70 changes: 37 additions & 33 deletions plugin/test/leiningen/test/cljsbuild/subproject.clj
@@ -1,22 +1,28 @@
(ns leiningen.test.cljsbuild.subproject
(:use
leiningen.cljsbuild.subproject
clojure.test))
midje.sweet))

(deftest test-check-clojure-version
(are [x] (= nil x)
(check-clojure-version {'org.clojure/clojure ["1.3.0"]})
(check-clojure-version {'org.clojure/clojure ["1.4.0"]}))
(are [x] (thrown? Exception x)
(check-clojure-version {'org.clojure/clojure ["1.2.0"]})
(check-clojure-version {})))
(fact
(check-clojure-version {'org.clojure/clojure ["1.3.0"]}) => nil
(check-clojure-version {'org.clojure/clojure ["1.4.0"]}) => nil
(check-clojure-version {'org.clojure/clojure ["1.2.0"]}) => (throws Exception)
(check-clojure-version {}) => (throws Exception))

(deftest test-merge-dependencies
(are [x] (apply =
(map dependency-map [(concat x cljsbuild-dependencies)
(merge-dependencies x)]))
[['org.clojure/clojure "1.3.0"] ['a "1"] ['b "2"]]
[['org.clojure/clojure "1.3.0"] ['cljsbuild "9.9.9"]]))
(defn- merge-dependencies-map [dependencies]
(-> dependencies
merge-dependencies
dependency-map))

(defn- expected-dependencies-map [dependencies]
(-> dependencies
(concat cljsbuild-dependencies)
dependency-map))

(fact
(for [dependencies [[['org.clojure/clojure "1.3.0"] ['a "1"] ['b "2"]]
[['org.clojure/clojure "1.3.0"] ['cljsbuild "9.9.9"]]]]
(merge-dependencies-map dependencies) => (expected-dependencies-map dependencies)))

(def lein-crossover ".crossovers")
(def lein-build-path "src-cljs-a")
Expand All @@ -34,16 +40,15 @@
:extra-classpath-dirs lein-extra-classpath-dirs
:repositories lein-repositories})

(deftest test-make-subproject-lein1
(fact
(let [subproject (make-subproject-lein1 lein1-project lein-crossover lein-builds)]
(doseq [dir (concat lein-extra-classpath-dirs [lein-build-path lein-crossover])]
(is (some #{dir} (:extra-classpath-dirs subproject))))
(is (= lein-source-path (:source-path subproject)))
(is (:local-repo-classpath subproject))
(is (= lein-dev-dependencies (:dev-dependencies subproject)))
(is (= lein-repositories (:repositories subproject)))
(is (apply = (map dependency-map [(concat lein-dependencies cljsbuild-dependencies)
(:dependencies subproject)])))))
(:extra-classpath-dirs subproject) => (contains dir))
(:source-path subproject) => lein-source-path
(:local-repo-classpath subproject) => truthy
(:dev-dependencies subproject) => lein-dev-dependencies
(:repositories subproject) => lein-repositories
(dependency-map (:dependencies subproject)) => (expected-dependencies-map lein-dependencies)))

(def lein2-metadata {:test-metadata "testing 1 2 3"})
(def lein2-eval-in :trampoline)
Expand All @@ -54,19 +59,18 @@
:dev-dependencies lein-dev-dependencies
:source-paths (concat [lein-source-path] lein-extra-classpath-dirs)
:repositories lein-repositories
:eval-in lein2-eval-in
:eval-in lein2-eval-in
:resources-path lein2-resources-path}
lein2-metadata))

(deftest test-make-subproject-lein2
(fact
(let [subproject (make-subproject-lein2 lein2-project lein-crossover lein-builds)]
(is (= lein2-metadata (meta subproject)))
(meta subproject) => lein2-metadata
(doseq [dir (concat lein-extra-classpath-dirs [lein-source-path lein-build-path lein-crossover])]
(is (some #{dir} (:source-paths subproject))))
(is (:local-repo-classpath subproject))
(is (= lein-dev-dependencies (:dev-dependencies subproject)))
(is (= lein-repositories (:repositories subproject)))
(is (= lein2-eval-in (:eval-in subproject)))
(is (= lein2-resources-path (:resources-path subproject)))
(is (apply = (map dependency-map [(concat lein-dependencies cljsbuild-dependencies)
(:dependencies subproject)])))))
(:source-paths subproject) => (contains dir))
(:local-repo-classpath subproject)
(:dev-dependencies subproject) => lein-dev-dependencies
(:repositories subproject) => lein-repositories
(:eval-in subproject) => lein2-eval-in
(:resources-path subproject) => lein2-resources-path
(dependency-map (:dependencies subproject)) => (expected-dependencies-map lein-dependencies)))
6 changes: 5 additions & 1 deletion support/project.clj
Expand Up @@ -8,4 +8,8 @@
[org.clojure/clojurescript "0.0-1011"
:exclusions [org.apache.ant/ant]]
[fs "1.1.2"]
[clj-stacktrace "0.2.4"]])
[clj-stacktrace "0.2.4"]]
:dev-dependencies [[midje "1.3.1"]
; NOTE: lein-midje requires different versions to be
; installed for lein1 vs lein2 compatibility :(.
[lein-midje "1.0.9"]])
2 changes: 1 addition & 1 deletion support/src/cljsbuild/crossover.clj
Expand Up @@ -95,7 +95,7 @@
(let [temp-file (str to-file ".tmp")]
; Write a temp file and atomically rename to the real file
; to prevent the compiler from reading a half-written file
(spit temp-file (filtered-crossover-file from-resource))
(spit temp-file (filtered-crossover-file from-resource))
(fs/rename temp-file to-file)
; Mark the file as read-only, to hopefully warn the user not to modify it.
(fs/chmod "-w" to-file))))))
17 changes: 11 additions & 6 deletions support/src/cljsbuild/util.clj
Expand Up @@ -30,14 +30,19 @@ and returns a seq of the results. Launches all the threads at once."
[f s]
(doall (map deref (doall (map #(future (f %)) s)))))

(defn once-every [ms desc f]
(future
(while true
(Thread/sleep ms)
(defn sleep [ms]
(Thread/sleep ms))

(defn once-every
([ms desc f keep-going]
(while (keep-going)
(try
(f)
(catch Exception e
(println (str "Error " desc ": " e)))))))
(println (str "Error " desc ": " e))))
(sleep ms)))
([ms desc f]
(once-every ms desc f (fn [] true))))

(defn- pump-file [reader out]
(let [buffer (make-array Character/TYPE 1000)]
Expand All @@ -56,7 +61,7 @@ and returns a seq of the results. Launches all the threads at once."
(.join pump-out)
(.join pump-err))))

(defn- maybe-writer [file fallback]
(defn maybe-writer [file fallback]
(if file
(do
(fs/delete file)
Expand Down
13 changes: 13 additions & 0 deletions support/test/cljsbuild/test/clean.clj
@@ -0,0 +1,13 @@
(ns cljsbuild.test.clean
(:use
cljsbuild.clean
midje.sweet)
(:require
[fs.core :as fs]))

(fact
(let [output-to "a"
output-dir "b"]
(cleanup-files {:output-to output-to :output-dir output-dir}) => nil
(provided (fs/delete output-to) => nil :times 1)
(provided (fs/delete-dir output-dir) => nil :times 1)))
24 changes: 24 additions & 0 deletions support/test/cljsbuild/test/test.clj
@@ -0,0 +1,24 @@
(ns cljsbuild.test.test
(:use
cljsbuild.test
midje.sweet)
(:require
[cljsbuild.util :as util]))

(fact
(let [command-1 {:shell ["command1"]}
command-2 {:shell ["command2"]}
command-3 {:shell ["command3"]}
commands [command-1 command-2 command-3]]
(run-tests commands) => nil
(provided (util/sh command-1) => 0 :times 1)
(provided (util/sh command-2) => 0 :times 1)
(provided (util/sh command-3) => 0 :times 1)))

(fact
(let [command-1 {:shell ["command1"]}
command-2 {:shell ["command2"]}
commands [command-1 command-2]]
(run-tests commands) => (throws Exception)
(provided (util/sh command-1) => 0 :times 1)
(provided (util/sh command-2) => 1 :times 1)))

0 comments on commit 5816773

Please sign in to comment.