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

View file

@ -68,12 +68,19 @@
(let [res (bb nil "-f" "test/babashka/scripts/System.bb")]
(is (= "bar" (second 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
(is (thrown-with-msg? Exception #"File does not exist: non-existing\n"
(bb nil "-f" "non-existing")))
(is (thrown-with-msg? Exception #"Missing expression.\n"
(is (thrown-with-msg? Exception #"expression"
(bb nil))))
#_(deftest raw-in-test

View file

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