babashka/test/babashka/classpath_test.clj
Bob 35e2cd9d05
make tests run on windows as the default (#235) (#898)
* make tests run on windows as the default

- change from selecting tests to run to selecting tests to skip (remove all :windows tags, add
  :skip-windows tag to tests that don't currently work on windows)
- handfuls of calls to `normalize` and `escape-file-paths` to handle platform differences
- split `task-test` to make most of the tests run on windows, and exclude just a couple of Unix-y tests

* make a binding name clearer

* skip nrepl-server-test on Windows

- test fails on CI, so disabling it for now

* unset bb environment vars after running tests

* unset bb environment var after running release-artifact

BABASHKA_EDN being set can interfere with some tests, so unset it before running the
native tests

* skip uberjar test on Windows

uberjar-test's 'throw on empty classpath' test failing on Windows native
 (but passes on JVM) - skip it for now
2021-06-20 09:23:58 +02:00

72 lines
3 KiB
Clojure

(ns babashka.classpath-test
(:require
[babashka.test-utils :as tu]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[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))))
(def path-sep (System/getProperty "path.separator"))
(deftest classpath-test
(is (= :my-script/bb
(bb nil "--classpath" "test-resources/babashka/src_for_classpath_test"
"(require '[my-script :as ms]) (ms/foo)")))
(is (= "hello from foo\n"
(tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test/foo.jar"
"(require '[foo :as f]) (f/foo)")))
(is (thrown-with-msg? Exception #"not find"
(tu/bb nil
"(require '[foo :as f])"))))
(deftest babashka-classpath-test
(is (= "test-resources"
(bb nil "--classpath" "test-resources"
"(require '[babashka.classpath :as cp]) (cp/get-classpath)")))
(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)")))
(is (= ["foo" "bar"]
(bb nil "--classpath" (str/join path-sep ["foo" "bar"])
"(require '[babashka.classpath :as cp]) (cp/split-classpath (cp/get-classpath))"))))
(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"
(tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main" "1" "2" "3" "4")))
(testing "system property"
(is (= "\"my.main2\""
(str/trim (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main2"))))))
(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))"))))
(deftest resource-test
(let [tmp-file (java.io.File/createTempFile "icon" ".png")]
(.deleteOnExit tmp-file)
(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))))
(is (= (.length (io/file "logo" "icon.png"))
(.length tmp-file))))
(testing "No exception on absolute path"
(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\")))) "))))))