diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 8ddc66d7..81c789d8 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -216,19 +216,6 @@ opts-map))] opts)) -(defn edn-seq* - [^java.io.BufferedReader rdr] - (let [edn-val (edn/read {:eof ::EOF} rdr)] - (when (not (identical? ::EOF edn-val)) - (cons edn-val (lazy-seq (edn-seq* rdr)))))) - -(defn edn-seq - [in] - (edn-seq* in)) - -(defn shell-seq [in] - (line-seq (java.io.BufferedReader. in))) - (def version (str/trim (slurp (io/resource "BABASHKA_VERSION")))) (defn print-version [] @@ -448,6 +435,22 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that Throwable java.lang.Throwable}) (def input-var (sci/new-dynamic-var '*input* nil)) +(def edn-readers (cond-> {} + features/yaml? + (assoc 'ordered/map @(resolve 'flatland.ordered.map/ordered-map)))) + +(defn edn-seq* + [^java.io.BufferedReader rdr] + (let [edn-val (edn/read {:eof ::EOF :readers edn-readers} rdr)] + (when (not (identical? ::EOF edn-val)) + (cons edn-val (lazy-seq (edn-seq* rdr)))))) + +(defn edn-seq + [in] + (edn-seq* in)) + +(defn shell-seq [in] + (line-seq (java.io.BufferedReader. in))) (defn main [& args] @@ -474,14 +477,14 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that ::EOF (if stream? (if shell-in (or (read-line) ::EOF) - (edn/read {;;:readers *data-readers* + (edn/read {:readers edn-readers :eof ::EOF} *in*)) (delay (cond shell-in (shell-seq *in*) edn-in (edn-seq *in*) :else - (edn/read *in*)))))) + (edn/read {:readers edn-readers} *in*)))))) uberscript-sources (atom ()) env (atom {}) classpath (or classpath diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index d35cd5ff..5a7f608f 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -58,7 +58,7 @@ (is (thrown-with-msg? Exception #"java.lang.NullPointerException" (bb nil "(subs nil 0 0)")))) -(deftest main-test +(deftest input-test (testing "-io behaves as identity" (is (= "foo\nbar\n" (test-utils/bb "foo\nbar\n" "-io" "*input*")))) (testing "if and when" @@ -107,15 +107,16 @@ (bb "foo\n Clojure is nice. \nbar\n If you're nice to clojure. " "-i" "(map-indexed #(-> [%1 %2]) *input*)") - (bb "(keep #(when (re-find #\"(?i)clojure\" (second %)) (first %)) *input*)")))))) + (bb "(keep #(when (re-find #\"(?i)clojure\" (second %)) (first %)) *input*)"))))) + (testing "ordered/map data reader works" + (is (= "localhost" (bb "#ordered/map ([:test \"localhost\"])" + "(:test *input*)")))) + (testing "bb doesn't wait for input if *input* isn't used" + (is (= "2\n" (with-out-str (main/main "(inc 1)")))))) (deftest println-test (is (= "hello\n" (test-utils/bb nil "(println \"hello\")")))) -(deftest input-test - (testing "bb doesn't wait for input if *input* isn't used" - (is (= "2\n" (with-out-str (main/main "(inc 1)")))))) - (deftest System-test (let [res (bb nil "-f" "test/babashka/scripts/System.bb")] (is (= "bar" (second res)))