Skip to content
Browse files

Support namespace injection in eval-in-project.

  • Loading branch information...
1 parent e3c1847 commit f8831ed80f3982b67d44e048541ca238e3b6c9fe Clojure committed
Showing with 27 additions and 17 deletions.
  1. +20 −6 src/leiningen/compile.clj
  2. +7 −10 src/leiningen/test.clj
  3. +0 −1 test_projects/sample_no_aot/project.clj
View
26 src/leiningen/compile.clj
@@ -5,10 +5,11 @@
[leiningen.core :only [ns->path user-settings]]
[leiningen.javac :only [javac]]
[leiningen.classpath :only [make-path find-lib-jars get-classpath]]
- [clojure.java.io :only [file]]
+ [clojure.java.io :only [file resource reader]]
[leiningen.util.ns :only [namespaces-in-dir]])
(:refer-clojure :exclude [compile])
- (:import (org.apache.tools.ant.taskdefs Java)
+ (:import (java.io PushbackReader)
+ (org.apache.tools.ant.taskdefs Java)
(java.lang.management ManagementFactory)
(java.util.regex Pattern)
(org.apache.tools.ant.types Environment$Variable)))
@@ -19,6 +20,9 @@
(def *skip-auto-compile* false)
+(def ^{:doc "A list of namespaces to inject into project subprocesses."}
+ injected-namespaces (atom ['robert.hooke]))
+
(defn- regex?
"Returns true if we have regex class"
[str-or-re]
@@ -126,11 +130,21 @@
(defn- get-jvm-args [project]
(concat (get-input-args) (:jvm-opts project) (:jvm-opts (user-settings))))
+(defn- injected-namespace-forms [ns-name]
+ (with-open [rdr (-> ns-name ns->path resource reader PushbackReader.)]
+ (doall (take-while #(not= ::done %)
+ (repeatedly #(read rdr false ::done))))))
+
+(defn- get-injected-form [injected-namespaces]
+ ;; Note: there's a posibility of conflicts here if a project
+ ;; requires a newer (say) hooke than Leiningen's, but since this
+ ;; injection doesn't affect clojure.core/*loaded-libs*, if the
+ ;; project requires hooke, it will load over the injected copy.
+ (mapcat injected-namespace-forms injected-namespaces))
+
(defn get-readable-form [java project form init]
- (let [cp (if java
- (str (.getClasspath (.getCommandLine java)))
- (System/getProperty "java.class.path"))
- form `(do ~init
+ (let [form `(do ~init
+ ~@(get-injected-form @injected-namespaces)
(set! ~'*warn-on-reflection*
~(:warn-on-reflection project))
~form)]
View
17 src/leiningen/test.clj
@@ -10,13 +10,12 @@
(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 (assoc (meta var#) ::var var#)))
- false ~selectors)
- (test-var# var#))))
- (throw (Exception. "Test selectors require robert/hooke dep.")))))
+ (robert.hooke/add-hook
+ (resolve 'clojure.test/test-var)
+ (fn test-var-with-selector [test-var# var#]
+ (when (reduce #(or %1 (%2 (assoc (meta var#) ::var var#)))
+ false ~selectors)
+ (test-var# var#))))))
(defn form-for-testing-namespaces
"Return a form that when eval'd in the context of the project will test
@@ -65,9 +64,7 @@ selectors. With no arguments, runs all tests."
result (doto (File/createTempFile "lein" "result") .deleteOnExit)]
(eval-in-project project (form-for-testing-namespaces
nses (.getAbsolutePath result) (vec selectors))
- nil nil `(do (require '~'clojure.test)
- ~(when (seq selectors)
- '(require 'robert.hooke))))
+ nil nil '(require 'clojure.test))
(if (and (.exists result) (pos? (.length result)))
(let [summary (read-string (slurp (.getAbsolutePath result)))
success? (zero? (+ (:error summary) (:fail summary)))]
View
1 test_projects/sample_no_aot/project.clj
@@ -5,7 +5,6 @@
(defproject nomnomnom "0.5.0-SNAPSHOT"
:dependencies [[clojure "1.2.0"]
- [robert/hooke "1.0.2"]
[janino "2.5.15"]]
:uberjar-exclusions [#"DUMMY"]
:test-selectors {:default (fn [m] (not (:integration m)))

0 comments on commit f8831ed

Please sign in to comment.
Something went wrong with that request. Please try again.