[#538] support top-level requires

This commit is contained in:
Michiel Borkent 2020-08-17 22:56:59 +02:00
parent 4dd102ca19
commit 9fffa80def
7 changed files with 36 additions and 19 deletions

View file

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

2
sci

@ -1 +1 @@
Subproject commit 4f40b916faa394c37447c99da2e9f2605dc62d87
Subproject commit 5e3a4d6556bd6fe5523fc0f60b36a9184508c740

View file

@ -1,4 +1,4 @@
(ns my.impl
(ns my.impl1
(:require [clojure.string]))
(defn impl-fn

View file

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

View file

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

View file

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

View file

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