2019-12-12 22:07:35 +00:00
|
|
|
(ns babashka.classpath-test
|
|
|
|
|
(:require
|
|
|
|
|
[babashka.test-utils :as tu]
|
|
|
|
|
[clojure.edn :as edn]
|
2020-01-29 22:22:26 +00:00
|
|
|
[clojure.java.io :as io]
|
2020-08-13 09:20:32 +00:00
|
|
|
[clojure.string :as str]
|
|
|
|
|
[clojure.test :as t :refer [deftest is testing]]))
|
2019-12-12 22:07:35 +00:00
|
|
|
|
|
|
|
|
(defn bb [input & args]
|
|
|
|
|
(edn/read-string (apply tu/bb (when (some? input) (str input)) (map str args))))
|
|
|
|
|
|
2021-06-20 07:23:58 +00:00
|
|
|
(def path-sep (System/getProperty "path.separator"))
|
|
|
|
|
|
2019-12-12 22:07:35 +00:00
|
|
|
(deftest classpath-test
|
|
|
|
|
(is (= :my-script/bb
|
2023-01-21 14:38:50 +00:00
|
|
|
(bb nil "--prn" "--classpath" "test-resources/babashka/src_for_classpath_test"
|
2019-12-12 22:07:35 +00:00
|
|
|
"(require '[my-script :as ms]) (ms/foo)")))
|
|
|
|
|
(is (= "hello from foo\n"
|
2023-01-21 14:38:50 +00:00
|
|
|
(tu/bb nil "--prn" "--classpath" "test-resources/babashka/src_for_classpath_test/foo.jar"
|
2020-01-19 17:08:08 +00:00
|
|
|
"(require '[foo :as f]) (f/foo)")))
|
2023-08-16 18:19:20 +00:00
|
|
|
(is (thrown-with-msg? Exception #"not locate"
|
2020-01-19 17:08:08 +00:00
|
|
|
(tu/bb nil
|
|
|
|
|
"(require '[foo :as f])"))))
|
2019-12-12 22:07:35 +00:00
|
|
|
|
2020-12-06 09:57:24 +00:00
|
|
|
(deftest babashka-classpath-test
|
|
|
|
|
(is (= "test-resources"
|
|
|
|
|
(bb nil "--classpath" "test-resources"
|
2020-12-06 10:01:37 +00:00
|
|
|
"(require '[babashka.classpath :as cp]) (cp/get-classpath)")))
|
2021-06-20 07:23:58 +00:00
|
|
|
(is (= (str/join path-sep ["test-resources" "foobar"])
|
|
|
|
|
(bb nil "--classpath" "test-resources"
|
|
|
|
|
"(require '[babashka.classpath :as cp]) (cp/add-classpath \"foobar\") (cp/get-classpath)")))
|
2020-12-06 10:41:05 +00:00
|
|
|
(is (= ["foo" "bar"]
|
2021-06-20 07:23:58 +00:00
|
|
|
(bb nil "--classpath" (str/join path-sep ["foo" "bar"])
|
|
|
|
|
"(require '[babashka.classpath :as cp]) (cp/split-classpath (cp/get-classpath))"))))
|
2020-12-06 09:57:24 +00:00
|
|
|
|
2019-12-12 22:07:35 +00:00
|
|
|
(deftest classpath-env-test
|
|
|
|
|
;; for this test you have to set `BABASHKA_CLASSPATH` to test-resources/babashka/src_for_classpath_test/env
|
|
|
|
|
;; and `BABASHKA_PRELOADS` to "(require '[env-ns])"
|
|
|
|
|
(when (System/getenv "BABASHKA_CLASSPATH_TEST")
|
|
|
|
|
(println (System/getenv "BABASHKA_CLASSPATH"))
|
|
|
|
|
(is (= "env!" (bb nil "(env-ns/foo)")))))
|
|
|
|
|
|
|
|
|
|
(deftest main-test
|
|
|
|
|
(is (= "(\"1\" \"2\" \"3\" \"4\")\n"
|
2023-01-21 14:38:50 +00:00
|
|
|
(tu/bb nil "--prn" "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main" "1" "2" "3" "4")))
|
2020-04-13 14:49:28 +00:00
|
|
|
(testing "system property"
|
|
|
|
|
(is (= "\"my.main2\""
|
2023-01-21 14:38:50 +00:00
|
|
|
(str/trim (tu/bb nil "--prn" "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main2"))))))
|
2019-12-22 16:27:06 +00:00
|
|
|
|
|
|
|
|
(deftest error-while-loading-test
|
|
|
|
|
(is (true?
|
|
|
|
|
(bb nil "--classpath" "test-resources/babashka/src_for_classpath_test"
|
|
|
|
|
"
|
|
|
|
|
(try
|
|
|
|
|
(require '[ns-with-error])
|
|
|
|
|
(catch Exception nil))
|
|
|
|
|
(nil? (resolve 'ns-with-error/x))"))))
|
2019-12-29 22:37:08 +00:00
|
|
|
|
|
|
|
|
(deftest resource-test
|
|
|
|
|
(let [tmp-file (java.io.File/createTempFile "icon" ".png")]
|
|
|
|
|
(.deleteOnExit tmp-file)
|
2021-06-20 07:23:58 +00:00
|
|
|
(bb nil "--classpath" "logo" "-e" (format "(io/copy (io/input-stream (io/resource \"icon.png\")) (io/file \"%s\"))"
|
|
|
|
|
(tu/escape-file-paths (.getPath tmp-file))))
|
2019-12-29 22:37:08 +00:00
|
|
|
(is (= (.length (io/file "logo" "icon.png"))
|
2020-06-10 20:55:07 +00:00
|
|
|
(.length tmp-file))))
|
|
|
|
|
(testing "No exception on absolute path"
|
2020-08-12 07:34:38 +00:00
|
|
|
(is (nil? (bb nil "(io/resource \"/tmp\")"))))
|
|
|
|
|
(testing "Reading a resource from a .jar file"
|
|
|
|
|
(is (= "true"
|
|
|
|
|
(str/trim
|
|
|
|
|
(tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test/foo.jar"
|
|
|
|
|
"(pos? (count (slurp (io/resource \"foo.clj\")))) "))))))
|
2023-02-01 20:53:52 +00:00
|
|
|
|
|
|
|
|
(deftest classloader-test
|
|
|
|
|
(let [url
|
|
|
|
|
(tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test/foo.jar"
|
|
|
|
|
"(first (map str (.getURLs (clojure.lang.RT/baseLoader))))")]
|
|
|
|
|
(is (str/includes? url "file:"))
|
2023-02-03 12:08:55 +00:00
|
|
|
(is (str/includes? url "foo.jar")))
|
|
|
|
|
(let [results (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test/foo.jar"
|
|
|
|
|
"(map some? [(.getResource (clojure.lang.RT/baseLoader) \"foo.clj\")
|
|
|
|
|
(.getResourceAsStream (clojure.lang.RT/baseLoader) \"foo.clj\")
|
|
|
|
|
(.getResources (clojure.lang.RT/baseLoader) \"foo.clj\")])")]
|
|
|
|
|
(is (= [true true true] (edn/read-string results)))))
|
2023-02-03 20:21:56 +00:00
|
|
|
|
|
|
|
|
(deftest reader-tag-test
|
|
|
|
|
(is (= [[3 2 1] [1 2 3]]
|
|
|
|
|
(bb nil "--classpath" "test-resources/babashka/src_for_classpath_test"
|
|
|
|
|
"(require 'reader) [#r/reverse [1 2 3] #r/distinct [1 1 2 3]]"))))
|