forked from technomancy/leiningen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add run task plus rudimentary tests.
- Loading branch information
1 parent
b5adf25
commit 063baba
Showing
5 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
(ns leiningen.run | ||
(:use [leiningen.compile :only [eval-in-project]] | ||
[leiningen.core :only [abort]])) | ||
|
||
(defn- run-main | ||
"Loads the project namespaces as well as all its dependencies and then calls | ||
ns/f, passing it the args." | ||
([project ns & args] | ||
(eval-in-project project `((ns-resolve '~(symbol ns) '~'-main) ~@args) | ||
nil nil `(require '~(symbol ns))))) | ||
|
||
;; TODO: use subtask help? | ||
(defn run | ||
"Call a function in a new process or run a .clj file. | ||
USAGE: lein run [ARGS...] | ||
Calls the -main function in the namespace specified as :main in project.clj. | ||
USAGE: lein run -m NAMESPACE [ARGS...] | ||
Calls the namespace/function in a new process; function defaults to -main. | ||
USAGE: lein run :alias [ARGS...] | ||
Aliases can be defined in project.clj as | ||
:run-aliases {:alias a.namespace | ||
:alias2 another.namespace}" | ||
[project & [first-arg & args]] | ||
(let [first-arg (read-string first-arg) | ||
alias (and (keyword? first-arg) (first-arg (:run-aliases project)))] | ||
(cond alias (apply run project "-m" (cons alias args)) | ||
(= first-arg '-m) (apply run-main project args) | ||
(:main project) (apply run-main project (:main project) | ||
first-arg args) | ||
:else (abort "No :main namespace specified in project.clj.")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
(ns test-run | ||
(:use [clojure.test] | ||
[clojure.java.io :only [delete-file]] | ||
[leiningen.core :only [read-project]] | ||
[leiningen.run])) | ||
|
||
(def out-file "/tmp/lein-test") | ||
|
||
(def project (binding [*ns* (find-ns 'leiningen.core)] | ||
(read-project "test_projects/tricky-name/project.clj"))) | ||
|
||
(use-fixtures :each (fn [f] | ||
(delete-file out-file :silently) | ||
(f))) | ||
|
||
(deftest test-basic | ||
(is (zero? (run project "1"))) | ||
(is (= "nom:1" (slurp out-file)))) | ||
|
||
(deftest test-alt-main | ||
(is (zero? (run project "-m" "org.domain.tricky-name.munch" "1"))) | ||
(is (= ":munched (\"1\")" (slurp out-file)))) | ||
|
||
(deftest test-aliases | ||
(is (zero? (run project ":bbb" "1"))) | ||
(is (= "BRUNCH" (slurp out-file))) | ||
(delete-file out-file :silently) | ||
(is (zero? (run project ":mmm" "1"))) | ||
(is (= ":munched (\"1\")" (slurp out-file)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
test_projects/tricky-name/src/org/domain/tricky_name/core.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
(ns org.domain.tricky-name.core) | ||
|
||
(defn -main [& args] | ||
(when-not (empty? args) | ||
(spit "/tmp/lein-test" (str "nom:" (first args))) | ||
(recur (rest args)))) |
4 changes: 4 additions & 0 deletions
4
test_projects/tricky-name/src/org/domain/tricky_name/munch.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(ns org.domain.tricky-name.munch) | ||
|
||
(defn -main [& args] | ||
(spit "/tmp/lein-test" (pr-str :munched args))) |