Skip to content
Browse files

Handle starting in a different ns

Allow either :init-ns and :main. Add tests for reply option
conversions.

refs #432
  • Loading branch information...
1 parent 6e5dee8 commit b48eec97c4aa9f2dee0ff1cc76259ed87944bafb @trptcolin trptcolin committed
Showing with 91 additions and 12 deletions.
  1. +9 −5 sample.project.clj
  2. +15 −7 src/leiningen/repl.clj
  3. +67 −0 test/leiningen/test/repl.clj
View
14 sample.project.clj
@@ -91,11 +91,15 @@
;; bringing in AOT if you don't need an executable uberjar.
:main org.example.sample
;; Options to change the way the REPL behaves
- :repl-options {;; These will get passed to clojure.main/repl; see
- ;; its docstring for details.
- :prompt (fn [ns] (print "your command, master?" ns) (flush))
- ;; This expression will be run when first opening a REPL.
- :init (in-ns 'foo.bar)
+ :repl-options {;; Specify the string to print when prompting for input.
+ ;; defaults to something like (fn [ns] (str *ns* "=> "))
+ :prompt (fn [ns] (str "your command for <" ns ">, master? " ))
+ ;; Specify the ns to start the REPL in (overrides :main in
+ ;; this case only)
+ :init-ns foo.bar
+ ;; This expression will run when first opening a REPL, in the
+ ;; namespace from :init-ns or :main if specified
+ :init (println "here we are in" *ns*)
;; Customize the socket the repl task listens on and
;; attaches to.
:host "0.0.0.0"
View
22 src/leiningen/repl.clj
@@ -51,6 +51,20 @@
(-> project :repl-options :ack-port))]
(Integer. p)))
+(defn options-for-reply [repl-port project]
+ (let [repl-options (:repl-options project)]
+ (clojure.set/rename-keys
+ (assoc repl-options
+ :attach (if-let [host (repl-host project)]
+ (str host ":" repl-port)
+ (str repl-port))
+ :init (if-let [init-ns (or (:init-ns repl-options) (:main project))]
+ `(do (require '~init-ns) (in-ns '~init-ns)
+ ~(:init repl-options))
+ (:init repl-options)))
+ {:prompt :custom-prompt
+ :init :custom-init})))
+
(defn ^:no-project-needed repl
"Start a repl session either with the current project or standalone.
@@ -84,13 +98,7 @@ and port."
:repl-options
:timeout)
30000))]
- (reply/launch-nrepl (clojure.set/rename-keys
- (assoc (:repl-options project)
- :attach (if-let [host (repl-host project)]
- (str host ":" repl-port)
- (str repl-port)))
- {:prompt :custom-prompt
- :init :custom-init}))
+ (reply/launch-nrepl (options-for-reply repl-port project))
(println "REPL server launch timed out."))))
([project flag & opts]
(case flag
View
67 test/leiningen/test/repl.clj
@@ -0,0 +1,67 @@
+(ns leiningen.test.repl
+ (:use [clojure.test]
+ [leiningen.repl]))
+
+(deftest test-options-for-reply-empty
+ (let [project {}]
+ (is (= {:attach "9876"
+ :custom-init nil}
+ (options-for-reply 9876 project)))))
+
+(deftest test-options-for-reply-host
+ (let [project {:repl-options {:host "127.0.0.1"}}]
+ (is (= {:attach "127.0.0.1:9876"
+ :host "127.0.0.1"
+ :custom-init nil}
+ (options-for-reply 9876 project)))))
+
+(deftest test-options-for-reply-prompt
+ (let [prompt-fn (fn [ns] "hi ")
+ project {:repl-options {:prompt prompt-fn}}]
+ (is (= {:attach "9876"
+ :custom-prompt prompt-fn
+ :custom-init nil}
+ (options-for-reply 9876 project)))))
+
+(deftest test-options-for-reply-init
+ (let [init-form '(println "ohai")
+ project {:repl-options {:init init-form}}]
+ (is (= {:attach "9876"
+ :custom-init init-form}
+ (options-for-reply 9876 project)))))
+
+(deftest test-options-for-reply-init-ns
+ (let [project {:repl-options {:init-ns 'foo.core}}]
+ (is (= {:attach "9876"
+ :init-ns 'foo.core
+ :custom-init '(do (clojure.core/require 'foo.core)
+ (clojure.core/in-ns 'foo.core)
+ nil)}
+ (options-for-reply 9876 project)))))
+
+(deftest test-options-for-reply-init-ns-and-init
+ (let [project {:repl-options {:init-ns 'foo.core :init '(println "ohai")}}]
+ (is (= {:attach "9876"
+ :init-ns 'foo.core
+ :custom-init '(do (clojure.core/require 'foo.core)
+ (clojure.core/in-ns 'foo.core)
+ (println "ohai"))}
+ (options-for-reply 9876 project)))))
+
+(deftest test-options-for-reply-main-ns
+ (let [project {:main 'foo.core}]
+ (is (= {:attach "9876"
+ :custom-init '(do (clojure.core/require 'foo.core)
+ (clojure.core/in-ns 'foo.core)
+ nil)}
+ (options-for-reply 9876 project)))))
+
+(deftest test-options-for-reply-init-ns-beats-main
+ (let [project {:main 'foo.core :repl-options {:init-ns 'winner.here}}]
+ (is (= {:attach "9876"
+ :init-ns 'winner.here
+ :custom-init '(do (clojure.core/require 'winner.here)
+ (clojure.core/in-ns 'winner.here)
+ nil)}
+ (options-for-reply 9876 project)))))
+

0 comments on commit b48eec9

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