Improvements to printing --time and System exit code (#18)

This commit is contained in:
Michiel Borkent 2019-08-17 14:50:02 +02:00 committed by GitHub
parent d4fee04df6
commit fc9c4c384f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 17 deletions

2
sci

@ -1 +1 @@
Subproject commit 6f3a1882cd03cbba23a35e5bb657772c4a045615 Subproject commit 1d41480d9dca2cd62f8cd341392919d1b64183ec

View file

@ -62,7 +62,7 @@
(defn print-help [] (defn print-help []
(println (str "babashka v" (str/trim (slurp (io/resource "BABASHKA_VERSION"))))) (println (str "babashka v" (str/trim (slurp (io/resource "BABASHKA_VERSION")))))
(println (str "sci v" (str/trim (slurp (io/resource "SCI_VERSION"))))) ;; (println (str "sci v" (str/trim (slurp (io/resource "SCI_VERSION")))))
(println) (println)
(print-usage) (print-usage)
(println) (println)
@ -82,7 +82,7 @@
(let [f (io/file file)] (let [f (io/file file)]
(if (.exists f) (if (.exists f)
(as-> (slurp file) x (as-> (slurp file) x
;; remove hashbang ;; remove shebang
(str/replace x #"^#!.*" "") (str/replace x #"^#!.*" "")
(format "(do %s)" x)) (format "(do %s)" x))
(throw (Exception. (str "File does not exist: " file)))))) (throw (Exception. (str "File does not exist: " file))))))
@ -101,7 +101,7 @@
(System/getProperties)) (System/getProperties))
(defn exit [n] (defn exit [n]
(System/exit n)) (throw (ex-info "" {:bb/exit-code n})))
(def bindings (def bindings
{'run! run! {'run! run!
@ -144,7 +144,8 @@
[(print-help) 0] [(print-help) 0]
:else :else
(try (try
(let [expr (if file (read-file file) expression)
(let [expr (if file (read-file file) (format "(do %s)" expression))
read-next #(if stream? read-next #(if stream?
(if raw-in (or (read-line) ::EOF) (if raw-in (or (read-line) ::EOF)
(read-edn)) (read-edn))
@ -156,7 +157,7 @@
(if (identical? ::EOF in) (if (identical? ::EOF in)
[nil 0] ;; done streaming [nil 0] ;; done streaming
(let [res [(do (when-not (or expression file) (let [res [(do (when-not (or expression file)
(throw (Exception. "Missing expression."))) (throw (Exception. (str args "Babashka expected an expression. Type --help to print help."))))
(let [res (sci/eval-string (let [res (sci/eval-string
expr expr
{:bindings (assoc bindings {:bindings (assoc bindings
@ -176,18 +177,23 @@
res))))) res)))))
(catch Exception e (catch Exception e
(binding [*out* *err*] (binding [*out* *err*]
(when-let [msg (or (:stderr (ex-data e)) (let [d (ex-data e)
(.getMessage e))] exit-code (:bb/exit-code d)]
(println (str/trim msg) ))) (if exit-code [nil exit-code]
[nil 1])))) (do (when-let [msg (or (:stderr d )
(.getMessage e))]
(println (str/trim msg)))
[nil 1]))))))))
1) 1)
t1 (System/currentTimeMillis)] t1 (System/currentTimeMillis)]
(when time? (println "bb took" (str (- t1 t0) "ms."))) (when time? (binding [*out* *err*]
(println "bb took" (str (- t1 t0) "ms."))))
exit-code)) exit-code))
(defn -main (defn -main
[& args] [& args]
(System/exit (apply main args))) (let [exit-code (apply main args)]
(System/exit exit-code)))
;;;; Scratch ;;;; Scratch

View file

@ -68,12 +68,19 @@
(let [res (bb nil "-f" "test/babashka/scripts/System.bb")] (let [res (bb nil "-f" "test/babashka/scripts/System.bb")]
(is (= "bar" (second res))) (is (= "bar" (second res)))
(doseq [s res] (doseq [s res]
(is (not-empty s))))) (is (not-empty s))))
(let [out (java.io.StringWriter.)
err (java.io.StringWriter.)
exit-code (binding [*out* out *err* err]
(main/main "--time" "(println \"Hello world!\") (System/exit 42)"))]
(is (= (str out) "Hello world!\n"))
(is (re-find #"took" (str err)))
(is (= 42 exit-code))))
(deftest malformed-command-line-args-test (deftest malformed-command-line-args-test
(is (thrown-with-msg? Exception #"File does not exist: non-existing\n" (is (thrown-with-msg? Exception #"File does not exist: non-existing\n"
(bb nil "-f" "non-existing"))) (bb nil "-f" "non-existing")))
(is (thrown-with-msg? Exception #"Missing expression.\n" (is (thrown-with-msg? Exception #"expression"
(bb nil)))) (bb nil))))
#_(deftest raw-in-test #_(deftest raw-in-test

View file

@ -13,7 +13,7 @@
(with-in-str input (with-in-str input
(apply main/main args)) (apply main/main args))
(apply main/main input args))))] (apply main/main input args))))]
(if-let [err ^String (not-empty (str sw))] (if-let [err ^String (not-empty (str sw))]
(throw (Exception. err)) res))) (throw (Exception. err)) res)))
(defn bb-native [input & args] (defn bb-native [input & args]
@ -23,8 +23,9 @@
{:in input})) {:in input}))
(apply bb input args)) (apply bb input args))
(catch Exception e (catch Exception e
(let [err-msg (or (:stderr (ex-data e)) "")] (let [d (ex-data e)
(throw (Exception. ^String err-msg))))))) err-msg (or (:stderr (ex-data e)) "")]
(throw (ex-info err-msg d)))))))
(def bb (def bb
(case (System/getenv "BABASHKA_TEST_ENV") (case (System/getenv "BABASHKA_TEST_ENV")