From 9fffa80defe08805775536403514b795c2e72698 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 17 Aug 2020 22:56:59 +0200 Subject: [PATCH] [#538] support top-level requires --- README.md | 4 ++++ sci | 2 +- .../uberscript/src/my/{impl.clj => impl1.clj} | 2 +- .../babashka/uberscript/src/my/impl2.cljc | 4 ++-- .../babashka/uberscript/src/my/main.clj | 11 ++++++---- test/babashka/classpath_test.clj | 11 ---------- test/babashka/uberscript_test.clj | 21 +++++++++++++++++++ 7 files changed, 36 insertions(+), 19 deletions(-) rename test-resources/babashka/uberscript/src/my/{impl.clj => impl1.clj} (83%) create mode 100644 test/babashka/uberscript_test.clj diff --git a/README.md b/README.md index 2f96a52c..1aef0537 100644 --- a/README.md +++ b/README.md @@ -616,6 +616,10 @@ $ bb my-script.clj Hello from gist script! ``` +Caveat: building uberscripts works by statically inspecting top-level `ns` forms +and `require` forms. The rest of the code is not evaluated. Code that relies on +dynamic requires may not work in an uberscript. + ## Parsing command line arguments Babashka ships with `clojure.tools.cli`: diff --git a/sci b/sci index 4f40b916..5e3a4d65 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 4f40b916faa394c37447c99da2e9f2605dc62d87 +Subproject commit 5e3a4d6556bd6fe5523fc0f60b36a9184508c740 diff --git a/test-resources/babashka/uberscript/src/my/impl.clj b/test-resources/babashka/uberscript/src/my/impl1.clj similarity index 83% rename from test-resources/babashka/uberscript/src/my/impl.clj rename to test-resources/babashka/uberscript/src/my/impl1.clj index 0dfd77e7..36aff679 100644 --- a/test-resources/babashka/uberscript/src/my/impl.clj +++ b/test-resources/babashka/uberscript/src/my/impl1.clj @@ -1,4 +1,4 @@ -(ns my.impl +(ns my.impl1 (:require [clojure.string])) (defn impl-fn diff --git a/test-resources/babashka/uberscript/src/my/impl2.cljc b/test-resources/babashka/uberscript/src/my/impl2.cljc index d5483e20..399b983a 100644 --- a/test-resources/babashka/uberscript/src/my/impl2.cljc +++ b/test-resources/babashka/uberscript/src/my/impl2.cljc @@ -1,4 +1,4 @@ (ns my.impl2 - (:require [my.impl :as impl])) + (:require [my.impl1 :as impl1])) -(def impl-fn impl/impl-fn) +(def impl-fn impl1/impl-fn) diff --git a/test-resources/babashka/uberscript/src/my/main.clj b/test-resources/babashka/uberscript/src/my/main.clj index 163e8e61..b0adbfdb 100644 --- a/test-resources/babashka/uberscript/src/my/main.clj +++ b/test-resources/babashka/uberscript/src/my/main.clj @@ -1,10 +1,13 @@ (ns my.main - (:require [my.impl :as impl] ;; my.impl is already loaded, so it will not be loaded again (normally) - [my.impl2] ;; but my.impl2 also loads my.impl - [my.impl3 :refer [foo]])) + (:require [my.impl1 :as impl1] ;; my.impl is already loaded, so it will not be loaded again (normally) + ;; but my.impl2 also loads my.impl + [my.impl2])) + +;; top-level requires are also supported +(require '[my.impl3 :refer [foo]]) (defn -main [& args] ;; this function is defined non-top-level and may cause problems (foo) ;; this should just return args - (impl/impl-fn args)) + (impl1/impl-fn args)) diff --git a/test/babashka/classpath_test.clj b/test/babashka/classpath_test.clj index e08d25e6..5784af52 100644 --- a/test/babashka/classpath_test.clj +++ b/test/babashka/classpath_test.clj @@ -34,17 +34,6 @@ (is (= "\"my.main2\"" (str/trim (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main2")))))) -(deftest uberscript-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" "-m" "my.main" "--uberscript" (.getPath tmp-file)))) - (is (= "(\"1\" \"2\" \"3\" \"4\")\n" - (tu/bb nil "--file" (.getPath tmp-file) "1" "2" "3" "4"))) - (testing "order of namespaces is correct" - (tu/bb nil "--classpath" "test-resources/babashka/uberscript/src" "-m" "my.main" "--uberscript" (.getPath tmp-file)) - (is (= "(\"1\" \"2\" \"3\" \"4\")\n" - (tu/bb nil "--file" (.getPath tmp-file) "1" "2" "3" "4")))))) - (deftest error-while-loading-test (is (true? (bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" diff --git a/test/babashka/uberscript_test.clj b/test/babashka/uberscript_test.clj new file mode 100644 index 00000000..efdedef2 --- /dev/null +++ b/test/babashka/uberscript_test.clj @@ -0,0 +1,21 @@ +(ns babashka.uberscript-test + (:require + [babashka.test-utils :as tu] + [clojure.edn :as edn] + [clojure.test :as t :refer [deftest is testing]])) + +(defn bb [input & args] + (edn/read-string (apply tu/bb (when (some? input) (str input)) (map str args)))) + +(deftest uberscript-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" "-m" "my.main" "--uberscript" (.getPath tmp-file)))) + (is (= "(\"1\" \"2\" \"3\" \"4\")\n" + (tu/bb nil "--file" (.getPath tmp-file) "1" "2" "3" "4"))) + (testing "order of namespaces is correct" + (tu/bb nil "--classpath" "test-resources/babashka/uberscript/src" "-m" "my.main" "--uberscript" (.getPath tmp-file)) + (spit "/tmp/foo.clj" (slurp (.getPath tmp-file))) + (is (= "(\"1\" \"2\" \"3\" \"4\")\n" + (tu/bb nil "--file" (.getPath tmp-file) "1" "2" "3" "4")))))) +