2020-08-19 14:39:42 +00:00
|
|
|
(ns babashka.uberjar-test
|
|
|
|
|
(:require
|
2022-04-02 15:09:34 +00:00
|
|
|
[babashka.fs :as fs]
|
|
|
|
|
[babashka.main :as main]
|
2023-10-12 11:12:29 +00:00
|
|
|
[babashka.process :refer [shell]]
|
2022-04-02 15:09:34 +00:00
|
|
|
[babashka.test-utils :as tu]
|
|
|
|
|
[clojure.edn :as edn]
|
2023-10-12 11:12:29 +00:00
|
|
|
[clojure.java.io :as io]
|
2022-04-02 15:09:34 +00:00
|
|
|
[clojure.string :as str]
|
|
|
|
|
[clojure.test :as t :refer [deftest is testing]])
|
|
|
|
|
(:import (java.io File InputStreamReader PushbackReader)
|
|
|
|
|
(java.util.jar JarFile)))
|
2022-04-02 14:51:08 +00:00
|
|
|
|
|
|
|
|
(defn jar-entries [jar]
|
|
|
|
|
(with-open [jar-file (JarFile. jar)]
|
|
|
|
|
(doall (enumeration-seq (.entries jar-file)))))
|
2020-08-19 14:39:42 +00:00
|
|
|
|
2021-04-22 10:33:17 +00:00
|
|
|
(defn count-entries [jar]
|
2022-04-02 14:51:08 +00:00
|
|
|
(-> jar jar-entries count))
|
|
|
|
|
|
|
|
|
|
(defn get-entry [^File jar entry-name]
|
|
|
|
|
(-> jar JarFile. (.getEntry entry-name)))
|
2021-04-22 10:33:17 +00:00
|
|
|
|
2021-06-24 20:34:57 +00:00
|
|
|
(deftest uberjar-test
|
2021-04-07 08:21:09 +00:00
|
|
|
(testing "uberjar with --main"
|
|
|
|
|
(let [tmp-file (java.io.File/createTempFile "uber" ".jar")
|
|
|
|
|
path (.getPath tmp-file)]
|
|
|
|
|
(.deleteOnExit tmp-file)
|
2023-10-12 11:12:29 +00:00
|
|
|
(tu/bb nil "--classpath" "test-resources/babashka/uberjar/src" "uberjar" path "-m" "my.main-main")
|
2020-08-19 14:39:42 +00:00
|
|
|
(is (= "(\"1\" \"2\" \"3\" \"4\")\n"
|
2023-10-12 11:12:29 +00:00
|
|
|
(tu/bb nil "--jar" path "1" "2" "3" "4")))
|
2020-08-19 14:39:42 +00:00
|
|
|
(is (= "(\"1\" \"2\" \"3\" \"4\")\n"
|
2023-10-12 11:12:29 +00:00
|
|
|
(tu/bb nil "-jar" path "1" "2" "3" "4")))
|
2020-08-19 14:39:42 +00:00
|
|
|
(is (= "(\"1\" \"2\" \"3\" \"4\")\n"
|
2023-10-12 11:12:29 +00:00
|
|
|
(tu/bb nil path "1" "2" "3" "4")))))
|
2020-08-19 14:39:42 +00:00
|
|
|
(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
|
|
|
|
|
;; main class in the manifest, even if we delete the tmp-file, which may
|
|
|
|
|
;; indicate a state-related bug somewhere!
|
|
|
|
|
(let [tmp-file (java.io.File/createTempFile "uber" ".jar")
|
|
|
|
|
path (.getPath tmp-file)]
|
|
|
|
|
(.deleteOnExit tmp-file)
|
2021-04-22 09:56:13 +00:00
|
|
|
(tu/bb nil "--classpath" "test-resources/babashka/uberjar/src" "uberjar" path)
|
2021-04-07 08:21:09 +00:00
|
|
|
(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
|
2023-10-12 11:12:29 +00:00
|
|
|
(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"))))))
|
2021-04-22 10:33:17 +00:00
|
|
|
(testing "ignore empty entries on classpath"
|
|
|
|
|
(let [tmp-file (java.io.File/createTempFile "uber" ".jar")
|
2021-06-20 07:23:58 +00:00
|
|
|
path (.getPath tmp-file)
|
|
|
|
|
empty-classpath (if main/windows? ";;;" ":::")]
|
2021-04-22 10:33:17 +00:00
|
|
|
(.deleteOnExit tmp-file)
|
2021-06-20 07:23:58 +00:00
|
|
|
(tu/bb nil "--classpath" empty-classpath "uberjar" path "-m" "my.main-main")
|
2021-04-22 10:33:17 +00:00
|
|
|
;; Only a manifest entry is added
|
|
|
|
|
(is (< (count-entries path) 3)))))
|
2021-12-26 16:26:35 +00:00
|
|
|
|
2022-04-02 14:51:08 +00:00
|
|
|
(deftest uberjar-with-pods-test
|
|
|
|
|
(testing "jar contains bb.edn w/ only :pods when bb.edn has :pods"
|
2022-04-02 15:47:27 +00:00
|
|
|
(let [tmp-file (java.io.File/createTempFile "uber" ".jar")
|
|
|
|
|
path (.getPath tmp-file)]
|
|
|
|
|
(.deleteOnExit tmp-file)
|
|
|
|
|
(let [config {:paths ["test-resources/babashka/uberjar/src"]
|
|
|
|
|
:deps '{local/deps {:local/root "test-resources/babashka/uberjar"}}
|
|
|
|
|
:pods (cond-> '{org.babashka/go-sqlite3 {:version "0.1.0"}}
|
|
|
|
|
(not (fs/windows?)) (assoc 'pod/test-pod {:path "test-resources/pod"}))}]
|
|
|
|
|
(tu/with-config config
|
|
|
|
|
(tu/bb nil "uberjar" path "-m" "my.main-pod")
|
2022-04-03 11:35:53 +00:00
|
|
|
(let [bb-edn-entry (get-entry tmp-file "META-INF/bb.edn")
|
2022-04-02 15:47:27 +00:00
|
|
|
bb-edn (-> path JarFile. (.getInputStream bb-edn-entry)
|
|
|
|
|
InputStreamReader. PushbackReader. edn/read)]
|
|
|
|
|
(is (= #{:pods} (-> bb-edn keys set)))
|
|
|
|
|
(is (= (:pods config) (:pods bb-edn))))
|
2023-01-21 14:38:50 +00:00
|
|
|
(is (str/includes? (tu/bb nil "--prn" "--jar" path) "3")))))))
|
2023-10-12 11:12:29 +00:00
|
|
|
|
|
|
|
|
(deftest uberjar-as-binary-test
|
|
|
|
|
(when tu/native?
|
|
|
|
|
(let [tmp-file (java.io.File/createTempFile "uber" ".jar")
|
|
|
|
|
path (.getPath tmp-file)
|
|
|
|
|
bin-file (if (fs/windows?) "my-binary.exe" "my-binary")]
|
|
|
|
|
(.deleteOnExit tmp-file)
|
|
|
|
|
(.deleteOnExit (io/file bin-file))
|
|
|
|
|
(tu/bb nil "--classpath" "test-resources/babashka/uberjar/src" "uberjar" path "-m" "my.main-main")
|
|
|
|
|
(shell {:out bin-file} "cat" "./bb" path)
|
|
|
|
|
(.setExecutable (io/file bin-file) true)
|
|
|
|
|
(is (str/includes? (:out (shell {:out :string} (str (io/file "." bin-file)) "1 2 3 4")) "1 2 3 4")))))
|