Skip to content

Commit

Permalink
Primitive test selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Sep 25, 2010
1 parent acc9a82 commit f72bdff
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/leiningen/compile.clj
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@
(when-not (re-matches #"^-Xbootclasspath.+" arg)
(.setValue (.createJvmarg java) arg)))
(.setClassname java "clojure.main")
(.setValue (.createArg java) "-e")
(.setValue (.createArg java) (get-readable-form java project form))
;; to allow plugins and other tasks to customize
(when handler (handler java))
(.setValue (.createArg java) "-e")
(.setValue (.createArg java) (get-readable-form java project form))
(.executeJava java)))

(defn- platform-nullsink []
Expand Down
64 changes: 35 additions & 29 deletions src/leiningen/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,35 @@
(:refer-clojure :exclude [test])
(:use [clojure.java.io :only [file]]
[leiningen.util.ns :only [namespaces-in-dir]]
[leiningen.compile :only [eval-in-project]]))
[leiningen.compile :only [eval-in-project]])
(:import [java.io File]))

(defn- with-version-guard
"Compatibility test to prevent use of clojure.test with Clojure 1.0."
[form]
`(if (and (= 1 (:major *clojure-version*))
(= 0 (:minor *clojure-version*)))
(println "\"lein test\" is not compatible with Clojure 1.0 projects.
Please consider upgrading to a newer version of Clojure or using"
"the lein-test-is plugin.")
~form))
(defn- init-args [java & files]
(doseq [f files]
(.setValue (.createArg java) "-i")
(.setValue (.createArg java) f)))

(defn- form-for-hook-selectors [selectors]
`(when (seq ~selectors)
(if-let [add-hook# (resolve 'robert.hooke/add-hook)]
(add-hook# (resolve 'clojure.test/test-var)
(fn test-var-with-selector [test-var# var#]
(when (reduce #(or %1 (%2 (meta var#))) false ~selectors)
(test-var# var#))))
(throw (Exception. "Test selectors require robert/hooke dep.")))))

(defn form-for-testing-namespaces
"Return a form that when eval'd in the context of the project will test
each namespace and print an overall summary."
([namespaces result-file]
(form-for-testing-namespaces namespaces 'clojure.test result-file))
([namespaces test-package result-file]
([namespaces result-file & [selectors]]
`(do
(require '~test-package)
(doseq [n# '~namespaces]
(require n#))
(let [resolver# (fn [fname#]
(ns-resolve
(find-ns '~test-package) fname#))
summary# (apply (resolver# ~''run-tests) '~namespaces)]
~(form-for-hook-selectors selectors)
(let [summary# (apply ~'clojure.test/run-tests '~namespaces)]
(when-not (= "1.5" (System/getProperty "java.specification.version"))
(shutdown-agents))
;; Stupid ant won't let us return anything, so write results to disk
(with-open [w# (-> (java.io.File. ~result-file)
(java.io.FileOutputStream.)
(java.io.OutputStreamWriter.))]
Expand All @@ -39,19 +40,24 @@ each namespace and print an overall summary."

(defn test
"Run the project's tests. Accepts a list of namespaces for which to run all
tests. If none are given, runs them all."
[project & namespaces]
(let [namespaces (if (empty? namespaces)
(sort (namespaces-in-dir (:test-path project)))
(map symbol namespaces))
result (java.io.File/createTempFile "lein" "result")]
(eval-in-project project
(with-version-guard
(form-for-testing-namespaces namespaces
(.getAbsolutePath result))))
tests. If none are given, runs them all." ; TODO: update
[project & tests]
(let [tests (map read-string tests)
nses (if (or (empty? tests) (every? keyword? tests))
(sort (namespaces-in-dir (:test-path project)))
tests)
result (doto (File/createTempFile "lein" "result") .deleteOnExit)
selectors (filter keyword? tests)]
(when-not (or (every? symbol? nses) (every? keyword? nses))
(throw (Exception. "Args must be either all namespaces or keywords.")))
(eval-in-project project (form-for-testing-namespaces
nses (.getAbsolutePath result) (vec selectors))
#(apply init-args %
(if (seq selectors)
["@clojure/test.clj" "@robert/hooke.clj"]
["@clojure/test.clj"])))
(if (and (.exists result) (pos? (.length result)))
(let [summary (read-string (slurp (.getAbsolutePath result)))
success? (zero? (+ (:error summary) (:fail summary)))]
(.delete result)
(if success? 0 1))
1)))
28 changes: 28 additions & 0 deletions test/test_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns test-test
(:refer-clojure :exclude [test])
(:use [leiningen.test]
[leiningen.core :only [read-project]] :reload)
(:use [clojure.test]))

(use-fixtures :each
(fn [f]
(.delete (java.io.File. "/tmp/lein-test-ran"))
(f)))

(def project (binding [*ns* (find-ns 'leiningen.core)]
(read-project "test_projects/sample_no_aot/project.clj")))

(defn ran []
(map read-string (.split (slurp "/tmp/lein-test-ran") "\n")))

(deftest test-no-selectors
(test project)
(is (= [:regular :integration :int2 :not-custom] (ran))))

(deftest test-basic-selector
(test project ":integration")
(is (= [:integration] (ran))))

(deftest test-two-selectors
(test project ":integration" ":int2")
(is (= [:integration :int2] (ran))))
6 changes: 3 additions & 3 deletions test_projects/sample_no_aot/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; just want a basic project to work from, generate a new one with
;; "lein new".

(def clj-version "1.1.0-master-SNAPSHOT")

(defproject nomnomnom "0.5.0-SNAPSHOT"
:dependencies [[~(symbol "org.clojure" "clojure") ~clj-version]])
:dependencies [[clojure "1.2.0"]
[robert/hooke "1.0.2"]]
:test-selectors {:default '(fn [m] (not (:integration m)))})
23 changes: 23 additions & 0 deletions test_projects/sample_no_aot/test/selectors.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns selectors
(:use [clojure.test]
[clojure.java.io]))

(defn record-ran [t]
(with-open [w (writer "/tmp/lein-test-ran" :append true)]
(.write w (str t "\n"))))

(deftest ^{:integration true} integration-test
(record-ran :integration)
(is true))

(deftest regular
(record-ran :regular)
(is true))

(deftest ^{:custom false} not-custom
(record-ran :not-custom)
(is true))

(deftest ^{:int2 true} integration-2
(record-ran :int2)
(is true))

0 comments on commit f72bdff

Please sign in to comment.