Skip to content

Commit

Permalink
Add run task plus rudimentary tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Oct 29, 2010
1 parent b5adf25 commit 063baba
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/leiningen/run.clj
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."))))
29 changes: 29 additions & 0 deletions test/test_run.clj
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))))
4 changes: 3 additions & 1 deletion test_projects/tricky-name/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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})
5 changes: 5 additions & 0 deletions test_projects/tricky-name/src/org/domain/tricky_name/core.clj
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))))
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)))

0 comments on commit 063baba

Please sign in to comment.