[#776] Use get-classpath when build an uberjar

It was previously using the classpath coming from the parameters/env. We need to use
cp/get-classpath for taking into consideration the classpath coming from bb.edn.
This commit is contained in:
Andrea Richiardi 2021-04-07 01:21:09 -07:00 committed by GitHub
parent 8e30cd350e
commit a0ace650f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 19 deletions

View file

@ -729,7 +729,7 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
(when uberjar (when uberjar
(uberjar/run {:dest uberjar (uberjar/run {:dest uberjar
:jar :uber :jar :uber
:classpath classpath :classpath (cp/get-classpath)
:main-class main :main-class main
:verbose verbose?})) :verbose verbose?}))
exit-code)))) exit-code))))

View file

@ -1,6 +1,5 @@
(ns babashka.bb-edn-test (ns babashka.bb-edn-test
(:require (:require
[babashka.fs :as fs]
[babashka.test-utils :as test-utils] [babashka.test-utils :as test-utils]
[clojure.edn :as edn] [clojure.edn :as edn]
[clojure.string :as str] [clojure.string :as str]
@ -12,16 +11,8 @@
:eof nil} :eof nil}
(apply test-utils/bb nil (map str args)))) (apply test-utils/bb nil (map str args))))
(defmacro with-config [cfg & body]
`(let [temp-dir# (fs/create-temp-dir)
bb-edn-file# (fs/file temp-dir# "bb.edn")]
(binding [*print-meta* true]
(spit bb-edn-file# ~cfg))
(binding [test-utils/*bb-edn-path* (str bb-edn-file#)]
~@body)))
(deftest doc-test (deftest doc-test
(with-config {:paths ["test-resources/task_scripts"]} (test-utils/with-config {:paths ["test-resources/task_scripts"]}
(is (str/includes? (apply test-utils/bb nil (is (str/includes? (apply test-utils/bb nil
(map str ["doc" "tasks"])) (map str ["doc" "tasks"]))
"This is task ns docstring.")) "This is task ns docstring."))
@ -33,11 +24,11 @@
"Main docstring")))) "Main docstring"))))
(deftest deps-test (deftest deps-test
(with-config '{:deps {medley/medley {:mvn/version "1.3.0"}}} (test-utils/with-config '{:deps {medley/medley {:mvn/version "1.3.0"}}}
(is (= '{1 {:id 1}, 2 {:id 2}} (is (= '{1 {:id 1}, 2 {:id 2}}
(bb "-e" "(require 'medley.core)" "-e" "(medley.core/index-by :id [{:id 1} {:id 2}])")))) (bb "-e" "(require 'medley.core)" "-e" "(medley.core/index-by :id [{:id 1} {:id 2}])"))))
(testing "--classpath option overrides bb.edn" (testing "--classpath option overrides bb.edn"
(with-config '{:deps {medley/medley {:mvn/version "1.3.0"}}} (test-utils/with-config '{:deps {medley/medley {:mvn/version "1.3.0"}}}
(is (= "src" (is (= "src"
(bb "-cp" "src" "-e" "(babashka.classpath/get-classpath)")))))) (bb "-cp" "src" "-e" "(babashka.classpath/get-classpath)"))))))
@ -46,6 +37,6 @@
;; Or do we want `--aliases :foo:bar` ;; Or do we want `--aliases :foo:bar`
;; Let's wait for a good use case ;; Let's wait for a good use case
#_(deftest alias-deps-test #_(deftest alias-deps-test
(with-config '{:aliases {:medley {:deps {medley/medley {:mvn/version "1.3.0"}}}}} (test-utils/with-config '{:aliases {:medley {:deps {medley/medley {:mvn/version "1.3.0"}}}}}
(is (= '{1 {:id 1}, 2 {:id 2}} (is (= '{1 {:id 1}, 2 {:id 2}}
(bb "-A:medley" "-e" "(require 'medley.core)" "-e" "(medley.core/index-by :id [{:id 1} {:id 2}])"))))) (bb "-A:medley" "-e" "(require 'medley.core)" "-e" "(medley.core/index-by :id [{:id 1} {:id 2}])")))))

View file

@ -1,5 +1,6 @@
(ns babashka.test-utils (ns babashka.test-utils
(:require (:require
[babashka.fs :as fs]
[babashka.impl.classpath :as cp] [babashka.impl.classpath :as cp]
[babashka.main :as main] [babashka.main :as main]
[babashka.process :as p] [babashka.process :as p]
@ -106,3 +107,11 @@
(defn stop-server! [^java.net.ServerSocket server] (defn stop-server! [^java.net.ServerSocket server]
(.close server)) (.close server))
(defmacro with-config [cfg & body]
`(let [temp-dir# (fs/create-temp-dir)
bb-edn-file# (fs/file temp-dir# "bb.edn")]
(binding [*print-meta* true]
(spit bb-edn-file# ~cfg))
(binding [*bb-edn-path* (str bb-edn-file#)]
~@body)))

View file

@ -5,10 +5,10 @@
[clojure.test :as t :refer [deftest is testing]])) [clojure.test :as t :refer [deftest is testing]]))
(deftest uberjar-test (deftest uberjar-test
(let [tmp-file (java.io.File/createTempFile "uber" ".jar") (testing "uberjar with --main"
path (.getPath tmp-file)] (let [tmp-file (java.io.File/createTempFile "uber" ".jar")
(.deleteOnExit tmp-file) path (.getPath tmp-file)]
(testing "uberjar" (.deleteOnExit tmp-file)
(tu/bb nil "uberjar" path "--classpath" "test-resources/babashka/uberjar/src" "-m" "my.main-main") (tu/bb nil "uberjar" path "--classpath" "test-resources/babashka/uberjar/src" "-m" "my.main-main")
(is (= "(\"1\" \"2\" \"3\" \"4\")\n" (is (= "(\"1\" \"2\" \"3\" \"4\")\n"
(tu/bb nil "--jar" path "1" "2" "3" "4"))) (tu/bb nil "--jar" path "1" "2" "3" "4")))
@ -25,4 +25,15 @@
path (.getPath tmp-file)] path (.getPath tmp-file)]
(.deleteOnExit tmp-file) (.deleteOnExit tmp-file)
(tu/bb nil "uberjar" path "--classpath" "test-resources/babashka/uberjar/src") (tu/bb nil "uberjar" path "--classpath" "test-resources/babashka/uberjar/src")
(is (str/includes? (tu/bb "(+ 1 2 3)" path) "6"))))) (is (str/includes? (tu/bb "(+ 1 2 3)" path) "6"))))
(testing "use bb.edn classpath when no other --classpath"
(tu/with-config {:paths ["test-resources/babashka/uberjar/src"]}
(let [tmp-file (java.io.File/createTempFile "uber" ".jar")
path (.getPath tmp-file)]
(.deleteOnExit tmp-file)
;; 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")))))))