Add run task plus rudimentary tests.

This commit is contained in:
Phil Hagelberg 2010-10-28 20:02:39 -07:00
parent b5adf254a5
commit 063baba0aa
5 changed files with 74 additions and 1 deletions

33
src/leiningen/run.clj Normal file
View file

@ -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."))))

29
test/test_run.clj Normal file
View file

@ -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))))

View file

@ -2,4 +2,6 @@
:description "One with a tricky group and project name"
:dev-dependencies [[clojure "1.2.0"]]
:shell-wrapper true
:main org.domain.tricky-name.core)
:main org.domain.tricky-name.core
:run-aliases {:bbb org.domain.tricky-name.brunch
:mmm org.domain.tricky-name.munch})

View file

@ -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))))

View file

@ -0,0 +1,4 @@
(ns org.domain.tricky-name.munch)
(defn -main [& args]
(spit "/tmp/lein-test" (pr-str :munched args)))