[#583] round-trip YAML using *input*

This commit is contained in:
Michiel Borkent 2020-09-23 11:15:29 +02:00
parent 49c2661cf6
commit 2f445658bf
2 changed files with 25 additions and 21 deletions

View file

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

View file

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