diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d54f343..4d8ca73e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ A preview of the next release can be installed from ## Unreleased -- ... +- [#1467](https://github.com/babashka/babashka/issues/1467): avoid printing results, unless `--prn` is enabled (aside from `-e`, `-o` and `-O`) ## 1.0.170 (2023-01-19) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 2b6e2885..ee71da69 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -153,6 +153,7 @@ Global opts: --init Load file after any preloads and prior to evaluation/subcommands. --config Replacing bb.edn with file. Relative paths are resolved relative to file. --deps-root Treat dir as root of relative paths in config. + --prn Print result via clojure.core/prn -Sforce Force recalculation of the classpath (don't use the cache) -Sdeps Deps data to use as the last deps file to be merged @@ -569,10 +570,10 @@ Use bb run --help to show this help output. :edn-in true)) ("-o") (recur (next options) (assoc opts-map - :shell-out true)) + :shell-out true :prn true)) ("-O") (recur (next options) (assoc opts-map - :edn-out true)) + :edn-out true :prn true)) ("-io") (recur (next options) (assoc opts-map :shell-in true @@ -639,7 +640,8 @@ Use bb run --help to show this help output. (assoc opts-map :nrepl (or opt "1667")))) ("--eval", "-e") - (let [options (next options)] + (let [options (next options) + opts-map (assoc opts-map :prn true)] (recur (next options) (update opts-map :expressions (fnil conj []) (first options)))) ("--main", "-m",) @@ -675,6 +677,7 @@ Use bb run --help to show this help output. (case c (\( \{ \[ \* \@ \#) (-> opts-map + (assoc :prn true) (update :expressions (fnil conj []) (first options)) (assoc :command-line-args (next options))) (assoc opts-map @@ -710,6 +713,8 @@ Use bb run --help to show this help output. ("--deps-root") (recur (nnext options) (assoc opts-map :deps-root (second options))) + ("--prn") + (recur (next options) (assoc opts-map :prn true)) [options opts-map]) [options opts-map]))) @@ -790,6 +795,7 @@ Use bb run --help to show this help output. :print-deps :prepare] exec-fn :exec} cli-opts + print-result? (:prn cli-opts) _ (when debug (vreset! common/debug true)) _ (do ;; set properties (when main (System/setProperty "babashka.main" main)) @@ -992,8 +998,7 @@ Use bb run --help to show this help output. (sci/eval-string* sci-ctx expression))] ;; return value printing (when (and (some? res) - (or (not run) - (:prn cli-opts))) + print-result?) (if-let [pr-f (cond shell-out println edn-out sio/prn)] (if (sequential? res) diff --git a/test/babashka/bb_edn_test.clj b/test/babashka/bb_edn_test.clj index eb39a5c7..3e2142ef 100644 --- a/test/babashka/bb_edn_test.clj +++ b/test/babashka/bb_edn_test.clj @@ -457,7 +457,7 @@ even more stuff here\" (pr-str '{:paths ["test-resources"] :pods {retrogradeorbit/bootleg {:version "0.1.9"}}}) (is (= "\"

Test

\"\n" - (test-utils/bb nil "-m" "pod-tests.bootleg")))))) + (test-utils/bb nil "--prn" "-m" "pod-tests.bootleg")))))) (deftest ^:skip-windows local-pod-test (test-utils/with-config diff --git a/test/babashka/classpath_test.clj b/test/babashka/classpath_test.clj index ae04e945..61808c77 100644 --- a/test/babashka/classpath_test.clj +++ b/test/babashka/classpath_test.clj @@ -13,10 +13,10 @@ (deftest classpath-test (is (= :my-script/bb - (bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" + (bb nil "--prn" "--classpath" "test-resources/babashka/src_for_classpath_test" "(require '[my-script :as ms]) (ms/foo)"))) (is (= "hello from foo\n" - (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test/foo.jar" + (tu/bb nil "--prn" "--classpath" "test-resources/babashka/src_for_classpath_test/foo.jar" "(require '[foo :as f]) (f/foo)"))) (is (thrown-with-msg? Exception #"not find" (tu/bb nil @@ -42,10 +42,10 @@ (deftest main-test (is (= "(\"1\" \"2\" \"3\" \"4\")\n" - (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main" "1" "2" "3" "4"))) + (tu/bb nil "--prn" "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main" "1" "2" "3" "4"))) (testing "system property" (is (= "\"my.main2\"" - (str/trim (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main2")))))) + (str/trim (tu/bb nil "--prn" "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main2")))))) (deftest error-while-loading-test (is (true? diff --git a/test/babashka/exec_test.clj b/test/babashka/exec_test.clj index 4a990520..6af27024 100644 --- a/test/babashka/exec_test.clj +++ b/test/babashka/exec_test.clj @@ -13,7 +13,7 @@ (is (thrown? Exception (bb "-x" "json/generate-string" "--foo" "1"))) (is (= {:foo 1} (cheshire/parse-string (edn/read-string - (bb "-x" "cheshire.core/generate-string" "--foo" "1")) true)))) + (bb "--prn" "-x" "cheshire.core/generate-string" "--foo" "1")) true)))) (deftest tasks-exec-test (u/with-config diff --git a/test/babashka/file_var_test.clj b/test/babashka/file_var_test.clj index e601523a..bc5d058b 100644 --- a/test/babashka/file_var_test.clj +++ b/test/babashka/file_var_test.clj @@ -10,7 +10,7 @@ (deftest file-var-test (let [[f1 f2 f3 f4] - (str/split (bb nil "--classpath" "test/babashka/scripts" + (str/split (bb nil "--prn" "--classpath" "test/babashka/scripts" "test/babashka/scripts/file_var.bb") #"\n")] (is (str/ends-with? f1 "file_var_classpath.bb")) @@ -19,6 +19,6 @@ (is (str/ends-with? f4 "file_var.bb"))) (testing "file var uses absolute path" (is (str/includes? - (bb nil (io/file "test" ".." "test" - "babashka" "scripts" "simple_file_var.bb")) + (bb nil "--prn" (io/file "test" ".." "test" + "babashka" "scripts" "simple_file_var.bb")) "..")))) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 28102ba8..fe0adbd3 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -31,11 +31,11 @@ (parse-opts ["--nrepl-server" "-cp" "src"]))) (is (= {:nrepl "1667", :classpath "src"} (parse-opts ["-cp" "src" "nrepl-server"]))) - (is (= {:socket-repl "1666", :expressions ["123"]} + (is (= {:prn true :socket-repl "1666", :expressions ["123"]} (parse-opts ["--socket-repl" "-e" "123"]))) - (is (= {:socket-repl "1666", :expressions ["123"]} + (is (= {:prn true :socket-repl "1666", :expressions ["123"]} (parse-opts ["--socket-repl" "1666" "-e" "123"]))) - (is (= {:nrepl "1666", :expressions ["123"]} + (is (= {:prn true :nrepl "1666", :expressions ["123"]} (parse-opts ["--nrepl-server" "1666" "-e" "123"]))) (is (= {:classpath "src" :uberjar "foo.jar"} @@ -50,7 +50,7 @@ (is (= 123 (bb nil "-e" "(println 123)"))) (is (= 123 (bb nil "--eval" "(println 123)"))) (testing "distinguish automatically between expression or file name" - (is (= {:result 8080} (bb nil "test/babashka/scripts/tools.cli.bb"))) + (is (= {:result 8080} (bb nil "--prn" "test/babashka/scripts/tools.cli.bb"))) (is (thrown-with-msg? Exception #"does not exist" (bb nil "foo.clj"))) (is (thrown-with-msg? Exception #"does not exist" (bb nil "-help")))) (is (= "1 2 3" (bb nil "-e" "(require '[clojure.string :as str1])" "-e" "(str1/join \" \" [1 2 3])"))) @@ -138,7 +138,7 @@ (is (= "hello\n" (test-utils/bb nil "(println \"hello\")")))) (deftest System-test - (let [res (bb nil "-f" "test/babashka/scripts/System.bb")] + (let [res (bb nil "--prn" "-f" "test/babashka/scripts/System.bb")] (is (= "bar" (second res))) (doseq [s res] (is (not-empty s))))) @@ -202,21 +202,21 @@ (deftest init-test (testing "init with a file" - (is (= "foo" (bb nil "--init" "test-resources/babashka/init_test.clj" + (is (= "foo" (bb nil "--prn" "--init" "test-resources/babashka/init_test.clj" "-f" "test-resources/babashka/init_caller.clj")))) (testing "init with eval(s)" (is (= "foo" (bb nil "--init" "test-resources/babashka/init_test.clj" "-e" "(init-test/do-a-thing)")))) (testing "init with main from init'ed ns" - (is (= "Hello from init!" (bb nil "--init" "test-resources/babashka/init_test.clj" + (is (= "Hello from init!" (bb nil "--prn" "--init" "test-resources/babashka/init_test.clj" "-m" "init-test")))) (testing "init with main from another namespace" (test-utils/with-config '{:paths ["test-resources/babashka/src_for_classpath_test"]} - (is (= "foo" (bb nil "--init" "test-resources/babashka/init_test.clj" + (is (= "foo" (bb nil "--prn" "--init" "test-resources/babashka/init_test.clj" "-m" "call-init-main"))))) (testing "init with a qualified function passed to --main" (test-utils/with-config '{:paths ["test-resources/babashka/src_for_classpath_test"]} - (is (= "foobar" (bb nil "--init" "test-resources/babashka/init_test.clj" + (is (= "foobar" (bb nil "--prn" "--init" "test-resources/babashka/init_test.clj" "-m" "call-init-main/foobar"))))) (testing "init with a subcommand after it" (let [actual-output (test-utils/bb "(println (init-test/do-a-thing))" @@ -341,7 +341,7 @@ temp-dir-path)))))) (deftest tools-cli-test - (is (= {:result 8080} (bb nil "test/babashka/scripts/tools.cli.bb")))) + (is (= {:result 8080} (bb nil "--prn" "test/babashka/scripts/tools.cli.bb")))) (deftest try-catch-test (is (zero? (bb nil "(try (/ 1 0) (catch ArithmeticException _ 0))"))) @@ -366,7 +366,7 @@ (deftest csv-test (is (= '(["Adult" "87727"] ["Elderly" "43914"] ["Child" "33411"] ["Adolescent" "29849"] ["Infant" "15238"] ["Newborn" "10050"] ["In Utero" "1198"]) - (bb nil (.getPath (io/file "test" "babashka" "scripts" "csv.bb")))))) + (bb nil "--prn" (.getPath (io/file "test" "babashka" "scripts" "csv.bb")))))) (deftest assert-test ;; assert was first implemented in bb but moved to sci later (is (thrown-with-msg? Exception #"should-be-true" @@ -419,7 +419,7 @@ (java.nio.file.Files/copy p p' (into-array [java.nio.file.StandardCopyOption/REPLACE_EXISTING]))))))" temp-path)) (is (.exists f2)) - (let [v (bb nil "-f" (.getPath (io/file "test-resources" "babashka" "glob.clj")))] + (let [v (bb nil "--prn" "-f" (.getPath (io/file "test-resources" "babashka" "glob.clj")))] (is (vector? v)) (is (.exists (io/file (first v))))) (is (= :success (bb nil "(with-open [str (java.nio.file.Files/newDirectoryStream (.toPath (clojure.java.io/file \".\")))] :success)"))) @@ -477,7 +477,7 @@ (deftest clojure-data-xml-test (is (= "12" (bb nil "(let [xml (xml/parse-str \"12\")] (xml/emit-str xml))"))) - (is (= "0.0.87-SNAPSHOT" (bb nil "examples/pom_version_get.clj" (.getPath (io/file "test-resources" "pom.xml"))))) + (is (= "0.0.87-SNAPSHOT" (bb nil "--prn" "examples/pom_version_get.clj" (.getPath (io/file "test-resources" "pom.xml"))))) (is (= ":xmlns.DAV%3A/propfind" (bb nil "(clojure.data.xml/alias-uri :D \"DAV:\") (str ::D/propfind)")))) @@ -617,12 +617,12 @@ (deftest file-property-test (is (= "true\nfalse\n" - (test-utils/bb nil (.getPath (io/file "test-resources" "babashka" "file_property1.clj"))))) + (test-utils/bb nil "--prn" (.getPath (io/file "test-resources" "babashka" "file_property1.clj"))))) (is (= "true\n" - (test-utils/bb nil (.getPath (io/file "test-resources" "babashka" "file_property2.clj"))))) + (test-utils/bb nil "--prn" (.getPath (io/file "test-resources" "babashka" "file_property2.clj"))))) (is (apply = - (bb nil (.getPath (io/file "test" "babashka" "scripts" "simple_file_var.bb"))))) - (let [res (bb nil (.getPath (io/file "test" ".." "test" "babashka" + (bb nil "--prn" (.getPath (io/file "test" "babashka" "scripts" "simple_file_var.bb"))))) + (let [res (bb nil "--prn" (.getPath (io/file "test" ".." "test" "babashka" "scripts" "simple_file_var.bb")))] (is (apply = res)) (is (str/includes? (first res) "..")))) diff --git a/test/babashka/test_utils.clj b/test/babashka/test_utils.clj index 9a856a96..ce2ac58f 100644 --- a/test/babashka/test_utils.clj +++ b/test/babashka/test_utils.clj @@ -5,7 +5,6 @@ [babashka.impl.common :as common] [babashka.main :as main] [babashka.process :as p] - [clojure.edn :as edn] [clojure.string :as str] [clojure.test :as test :refer [*report-counters*]] [clojure.tools.reader.reader-types :as r] diff --git a/test/babashka/uberjar_test.clj b/test/babashka/uberjar_test.clj index 4476f45a..272c5b5b 100644 --- a/test/babashka/uberjar_test.clj +++ b/test/babashka/uberjar_test.clj @@ -24,13 +24,13 @@ (let [tmp-file (java.io.File/createTempFile "uber" ".jar") path (.getPath tmp-file)] (.deleteOnExit tmp-file) - (tu/bb nil "--classpath" "test-resources/babashka/uberjar/src" "uberjar" path "-m" "my.main-main") + (tu/bb nil "--prn" "--classpath" "test-resources/babashka/uberjar/src" "uberjar" path "-m" "my.main-main") (is (= "(\"1\" \"2\" \"3\" \"4\")\n" - (tu/bb nil "--jar" path "1" "2" "3" "4"))) + (tu/bb nil "--prn" "--jar" path "1" "2" "3" "4"))) (is (= "(\"1\" \"2\" \"3\" \"4\")\n" - (tu/bb nil "-jar" path "1" "2" "3" "4"))) + (tu/bb nil "--prn" "-jar" path "1" "2" "3" "4"))) (is (= "(\"1\" \"2\" \"3\" \"4\")\n" - (tu/bb nil path "1" "2" "3" "4"))))) + (tu/bb nil "--prn" path "1" "2" "3" "4"))))) (testing "without main, a REPL starts" ;; NOTE: if we choose the same tmp-file as above and doing this all in the ;; same JVM process, the below test fails because my.main-main will be the @@ -49,9 +49,9 @@ ;; building with no --classpath (tu/bb nil "uberjar" path "-m" "my.main-main") ;; running - (is (= "(\"42\")\n" (tu/bb nil "--jar" path "-m" "my.main-main" "42"))) - (is (= "(\"42\")\n" (tu/bb nil "--classpath" path "-m" "my.main-main" "42"))) - (is (= "(\"42\")\n" (tu/bb nil path "42")))))) + (is (= "(\"42\")\n" (tu/bb nil "--prn" "--jar" path "-m" "my.main-main" "42"))) + (is (= "(\"42\")\n" (tu/bb nil "--prn" "--classpath" path "-m" "my.main-main" "42"))) + (is (= "(\"42\")\n" (tu/bb nil "--prn" path "42")))))) (testing "ignore empty entries on classpath" (let [tmp-file (java.io.File/createTempFile "uber" ".jar") path (.getPath tmp-file) @@ -77,7 +77,7 @@ InputStreamReader. PushbackReader. edn/read)] (is (= #{:pods} (-> bb-edn keys set))) (is (= (:pods config) (:pods bb-edn)))) - (is (str/includes? (tu/bb nil "--jar" path) "3"))))))) + (is (str/includes? (tu/bb nil "--prn" "--jar" path) "3"))))))) (deftest throw-on-empty-classpath ;; this test fails the windows native test in CI diff --git a/test/babashka/uberscript_test.clj b/test/babashka/uberscript_test.clj index 9a111b18..e76511cd 100644 --- a/test/babashka/uberscript_test.clj +++ b/test/babashka/uberscript_test.clj @@ -1,15 +1,15 @@ (ns babashka.uberscript-test (:require [babashka.test-utils :as tu] - [clojure.test :as t :refer [deftest is]] - [clojure.string :as str])) + [clojure.string :as str] + [clojure.test :as t :refer [deftest is]])) (deftest basic-test (let [tmp-file (java.io.File/createTempFile "uberscript" ".clj")] (.deleteOnExit tmp-file) - (is (empty? (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" "uberscript" (.getPath tmp-file) "-m" "my.main"))) + (is (empty? (tu/bb nil "--prn" "--classpath" "test-resources/babashka/src_for_classpath_test" "uberscript" (.getPath tmp-file) "-m" "my.main"))) (is (= "(\"1\" \"2\" \"3\" \"4\")\n" - (tu/bb nil "--file" (.getPath tmp-file) "1" "2" "3" "4"))))) + (tu/bb nil "--prn" "--file" (.getPath tmp-file) "1" "2" "3" "4"))))) (when-not (= "aarch64" (System/getenv "BABASHKA_ARCH")) (deftest advanced-test @@ -20,16 +20,16 @@ ;; reader error for ::a/foo is swallowed ;; pod namespaces can be loaded without a problem ;; resulting program can be executed - (is (empty? (tu/bb nil "--classpath" "test-resources/babashka/uberscript/src" "uberscript" (.getPath tmp-file) "-m" "my.main"))) + (is (empty? (tu/bb nil "--prn" "--classpath" "test-resources/babashka/uberscript/src" "uberscript" (.getPath tmp-file) "-m" "my.main"))) (is (= ":clojure.string/foo\ntrue\n(\"1\" \"2\" \"3\" \"4\")\n" - (tu/bb nil "--file" (.getPath tmp-file) "1" "2" "3" "4")))))) + (tu/bb nil "--prn" "--file" (.getPath tmp-file) "1" "2" "3" "4")))))) (deftest pods-test (let [tmp-file (java.io.File/createTempFile "uberscript" ".clj")] (.deleteOnExit tmp-file) (tu/with-config (pr-str '{:paths ["test-resources/babashka/uberscript/src"] :pods {org.babashka/go-sqlite3 {:version "0.1.0"}}}) - (is (empty? (tu/bb nil "uberscript" (.getPath tmp-file) "-m" "my.main-pod"))) + (is (empty? (tu/bb nil "--prn" "uberscript" (.getPath tmp-file) "-m" "my.main-pod"))) (is (= 1 (count (re-seq #"load-pod 'org.babashka/go-sqlite3" (str/join (str/split-lines (slurp tmp-file)))))))) - (is (str/includes? (tu/bb nil "--file" (.getPath tmp-file)) "3")))) + (is (str/includes? (tu/bb nil "--prn" "--file" (.getPath tmp-file)) "3"))))