Skip to content

Commit

Permalink
Split scripts test into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
rads authored and borkdude committed Jun 12, 2024
1 parent fb19322 commit ee15035
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 333 deletions.
91 changes: 91 additions & 0 deletions test/babashka/bbin/scripts/git_dir_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
(ns babashka.bbin.scripts.git-dir-test
(:require [babashka.bbin.dirs :as dirs]
[babashka.bbin.test-util :as tu]
[babashka.fs :as fs]
[clojure.test :refer [deftest is testing use-fixtures]]))

(use-fixtures :once
(tu/bbin-dirs-fixture)
(tu/bbin-private-keys-fixture))

(def bbin-test-lib
'{:lib io.github.rads/bbin-test-lib,
:coords {:git/url "https://github.com/rads/bbin-test-lib.git",
:git/tag "v0.0.1",
:git/sha "9140acfc12d8e1567fc6164a50d486de09433919"}})

(def bbin-test-lib-no-tag
'{:lib io.github.rads/bbin-test-lib-no-tag,
:coords {:git/url "https://github.com/rads/bbin-test-lib-no-tag.git",
:git/sha "cefb15e3320dd4c599e8be62f7a01a00b07e2e72"}})

(def bbin-test-lib-private
'{:lib io.bitbucket.radsmith/bbin-test-lib-private,
:coords {:git/url "[email protected]:radsmith/bbin-test-lib-private.git"
:git/tag "v0.0.1",
:git/sha "9140acfc12d8e1567fc6164a50d486de09433919"}})

(deftest install-from-qualified-lib-name-public-test
(testing "install */* (public Git repo)"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [cli-opts {:script/lib "io.github.rads/bbin-test-lib"}
out (tu/run-install cli-opts)
bin-file (fs/file (dirs/bin-dir nil) "hello")]
(is (= bbin-test-lib out))
(is (fs/exists? bin-file))
(is (= "Hello world!" (tu/run-bin-script 'hello))))))

(deftest install-from-qualified-lib-name-no-tag-test
(testing "install */* (public Git repo, no tags)"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [cli-opts {:script/lib "io.github.rads/bbin-test-lib-no-tag"}
out (tu/run-install cli-opts)
bin-file (fs/file (dirs/bin-dir nil) "hello")]
(is (= bbin-test-lib-no-tag out))
(is (fs/exists? bin-file))
(is (= "Hello world!" (tu/run-bin-script 'hello))))))

(deftest install-from-qualified-lib-name-private-test
(testing "install */* (private Git repo)"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [cli-opts {:script/lib "io.bitbucket.radsmith/bbin-test-lib-private"}
out (tu/run-install cli-opts)
bin-file (fs/file (dirs/bin-dir nil) "hello")]
(is (= bbin-test-lib-private out))
(is (fs/exists? bin-file))
(is (= "Hello world!" (tu/run-bin-script 'hello))))))

(def git-http-url-lib
'{:lib org.babashka.bbin/script-1039504783-https-jackfan.us.kg-rads-bbin-test-lib-git
:coords {:git/url "https://github.com/rads/bbin-test-lib.git"
:git/sha "cefb15e3320dd4c599e8be62f7a01a00b07e2e72"}})

(deftest install-from-git-http-url-test
(testing "install https://*.git"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [cli-opts {:script/lib (get-in git-http-url-lib [:coords :git/url])}
out (tu/run-install cli-opts)
bin-file (fs/file (dirs/bin-dir nil) "hello")]
(is (= git-http-url-lib out))
(is (fs/exists? bin-file))
(is (= "Hello world!" (tu/run-bin-script 'hello))))))

(def git-ssh-url-lib
'{:lib org.babashka.bbin/script-1166637990-git-bitbucket-org-radsmith-bbin-test-lib-private-git
:coords {:git/url "[email protected]:radsmith/bbin-test-lib-private.git"
:git/sha "cefb15e3320dd4c599e8be62f7a01a00b07e2e72"}})

(deftest install-from-git-ssh-url-test
(testing "install git@*:*.git"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [cli-opts {:script/lib (get-in git-ssh-url-lib [:coords :git/url])}
out (tu/run-install cli-opts)
bin-file (fs/file (dirs/bin-dir nil) "hello")]
(is (= git-ssh-url-lib out))
(is (fs/exists? bin-file))
(is (= "Hello world!" (tu/run-bin-script 'hello))))))
22 changes: 22 additions & 0 deletions test/babashka/bbin/scripts/http_file_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(ns babashka.bbin.scripts.http-file-test
(:require [babashka.bbin.dirs :as dirs]
[babashka.bbin.test-util :as tu]
[babashka.fs :as fs]
[clojure.test :refer [deftest is testing use-fixtures]]))

(use-fixtures :once (tu/bbin-dirs-fixture))

(def portal-script-url
(str "https://gist.githubusercontent.com"
"/rads/da8ecbce63fe305f3520637810ff9506"
"/raw/e83305656f2d145430085d5414e2c3bff776b6e8/portal.clj"))

(deftest install-from-url-clj-test
(testing "install https://*.clj"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [cli-opts {:script/lib portal-script-url}
out (tu/run-install cli-opts)]
(is (= {:coords {:bbin/url portal-script-url}} out))
(is (fs/exists? (fs/file (dirs/bin-dir nil) "portal")))
(is (= {'portal {:coords {:bbin/url portal-script-url}}} (tu/run-ls))))))
23 changes: 23 additions & 0 deletions test/babashka/bbin/scripts/http_jar_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns babashka.bbin.scripts.http-jar-test
(:require [babashka.bbin.dirs :as dirs]
[babashka.bbin.test-util :as tu]
[clojure.test :refer [deftest is testing use-fixtures]]))

(use-fixtures :once (tu/bbin-dirs-fixture))

(def hello-jar-url "https://raw.githubusercontent.com/rads/bbin-test-lib/main/hello.jar")

(deftest install-from-url-jar-test
(testing "install https://*.jar"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [cli-opts {:script/lib hello-jar-url}
out (tu/run-install cli-opts)]
(is (= {:coords {:bbin/url hello-jar-url}} out))
(is (= "Hello JAR" (tu/run-bin-script :hello)))))
(testing "install https://*.jar (reinstall)"
(let [cli-opts {:script/lib hello-jar-url}
out (tu/run-install cli-opts)]
(is (= {:coords {:bbin/url hello-jar-url}} out))
(is (= "Hello JAR" (tu/run-bin-script :hello)))
(is (= {'hello {:coords {:bbin/url hello-jar-url}}} (tu/run-ls))))))
74 changes: 74 additions & 0 deletions test/babashka/bbin/scripts/local_dir_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
(ns babashka.bbin.scripts.local-dir-test
(:require [babashka.bbin.dirs :as dirs]
[babashka.bbin.test-util :as tu]
[babashka.fs :as fs]
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]])
(:import (clojure.lang ExceptionInfo)))

(use-fixtures :once (tu/bbin-dirs-fixture))

(deftest install-from-lib-local-root-dir-test
(testing "install */* --local/root *"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [local-root (str (fs/file tu/test-dir "foo"))]
(fs/create-dir local-root)
(spit (fs/file local-root "bb.edn") (pr-str {}))
(spit (fs/file local-root "deps.edn") (pr-str {}))
(let [cli-opts {:script/lib "babashka/foo"
:local/root local-root}
out (tu/run-install cli-opts)]
(is (= {:lib 'babashka/foo
:coords {:local/root local-root}}
out))
(is (fs/exists? (fs/file (dirs/bin-dir nil) "foo")))))))

(deftest invalid-bin-config-test
(testing "install */* --local/root * (invalid bin config)"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [local-root (str (fs/file tu/test-dir "foo"))
invalid-config 123]
(fs/create-dir local-root)
(spit (fs/file local-root "bb.edn") (pr-str {:bbin/bin invalid-config}))
(spit (fs/file local-root "deps.edn") (pr-str {}))
(let [cli-opts {:script/lib "babashka/foo"
:local/root local-root}]
(is (thrown-with-msg? ExceptionInfo #"123 - failed: map\? spec: :bbin/bin"
(tu/run-install cli-opts)))))))

(deftest install-from-no-lib-local-root-dir-test
(testing "install ./"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [local-root (str (fs/file tu/test-dir "foo"))]
(fs/create-dir local-root)
(spit (fs/file local-root "bb.edn")
(pr-str {:bbin/bin {'foo {:main-opts ["-m" "babashka/foo"]}}}))
(spit (fs/file local-root "deps.edn") (pr-str {}))
(let [cli-opts {:script/lib local-root}
script-url (str "file://" local-root)
out (tu/run-install cli-opts)]
(is (= {:coords {:bbin/url script-url}} out))
(is (fs/exists? (fs/file (dirs/bin-dir nil) "foo")))
(is (= {'foo {:coords {:bbin/url script-url}}} (tu/run-ls)))))))

(deftest install-tool-from-local-root-test
(testing "install ./ --tool"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [opts {:script/lib "bbin/foo"
:local/root (str "test-resources" fs/file-separator "local-tool")
:as "footool"
:tool true}
full-path (str (fs/canonicalize (:local/root opts) {:nofollow-links true}))
_ (tu/run-install opts)]
(is (fs/exists? (fs/file (dirs/bin-dir nil) "footool")))
(let [usage-out (tu/run-bin-script "footool")]
(is (every? #(str/includes? usage-out %) ["`keys`" "`vals`"])))
(is (str/includes? (tu/run-bin-script "footool" "k" ":a" "1") "(:a)"))
(is (str/includes? (tu/run-bin-script "footool" "v" ":a" "1") "(1)"))
(is (= {'footool {:coords {:local/root full-path}
:lib 'bbin/foo}}
(tu/run-ls))))))
82 changes: 82 additions & 0 deletions test/babashka/bbin/scripts/local_file_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
(ns babashka.bbin.scripts.local-file-test
(:require [babashka.bbin.dirs :as dirs]
[babashka.bbin.test-util :as tu]
[babashka.fs :as fs]
[clojure.test :refer [deftest is testing use-fixtures]]))

(use-fixtures :once (tu/bbin-dirs-fixture))

(deftest install-from-local-root-clj-test
(testing "install ./*.clj (with shebang)"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [script-file (doto (fs/file tu/test-dir "hello.clj")
(spit "#!/usr/bin/env bb\n(println \"Hello world\")"))
script-url (str "file://" script-file)
cli-opts {:script/lib (str script-file)}
out (tu/run-install cli-opts)]
(is (= {:coords {:bbin/url (str "file://" script-file)}} out))
(is (= "Hello world" (tu/run-bin-script :hello)))
(is (= {'hello {:coords {:bbin/url script-url}}} (tu/run-ls)))))
(testing "install ./*.clj (without shebang)"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [script-file (doto (fs/file tu/test-dir "hello.clj")
(spit "(println \"Hello world\")"))
script-url (str "file://" script-file)
cli-opts {:script/lib (str script-file)}
out (tu/run-install cli-opts)]
(is (= {:coords {:bbin/url script-url}} out))
(is (= "Hello world" (tu/run-bin-script :hello)))
(is (= {'hello {:coords {:bbin/url script-url}}} (tu/run-ls))))))

(deftest install-from-local-root-bb-test
(testing "install ./*.bb (with shebang)"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [script-file (doto (fs/file tu/test-dir "hello.bb")
(spit "#!/usr/bin/env bb\n(println \"Hello world\")"))
script-url (str "file://" script-file)
cli-opts {:script/lib (str script-file)}
out (tu/run-install cli-opts)]
(is (= {:coords {:bbin/url (str "file://" script-file)}} out))
(is (= "Hello world" (tu/run-bin-script :hello)))
(is (= {'hello {:coords {:bbin/url script-url}}} (tu/run-ls)))))
(testing "install ./*.bb (without shebang)"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [script-file (doto (fs/file tu/test-dir "hello.bb")
(spit "(println \"Hello world\")"))
script-url (str "file://" script-file)
cli-opts {:script/lib (str script-file)}
out (tu/run-install cli-opts)]
(is (= {:coords {:bbin/url (str "file://" script-file)}} out))
(is (= "Hello world" (tu/run-bin-script :hello)))
(is (= {'hello {:coords {:bbin/url script-url}}} (tu/run-ls))))))

(deftest install-from-local-root-no-extension-test
(testing "install ./* (no extension, with shebang)"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [script-file (doto (fs/file tu/test-dir "hello")
(spit "#!/usr/bin/env bb\n(println \"Hello world\")"))
script-url (str "file://" script-file)
cli-opts {:script/lib (str script-file)}
out (tu/run-install cli-opts)]
(is (= {:coords {:bbin/url (str "file://" script-file)}} out))
(is (= "Hello world" (tu/run-bin-script :hello)))
(is (= {'hello {:coords {:bbin/url script-url}}} (tu/run-ls)))))
(testing "install ./* (no extension, without shebang)"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [script-file (doto (fs/file tu/test-dir "hello")
(spit "(println \"Hello world\")"))
script-url (str "file://" script-file)
cli-opts {:script/lib (str script-file)}
out (tu/run-install cli-opts)]
(is (= {:coords {:bbin/url (str "file://" script-file)}} out))
(is (= "Hello world" (tu/run-bin-script :hello)))
(is (= {'hello {:coords {:bbin/url script-url}}} (tu/run-ls))))))

(comment
(clojure.test/run-tests))
27 changes: 27 additions & 0 deletions test/babashka/bbin/scripts/local_jar_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns babashka.bbin.scripts.local-jar-test
(:require [babashka.bbin.dirs :as dirs]
[babashka.bbin.test-util :as tu]
[babashka.fs :as fs]
[clojure.test :refer [deftest is testing use-fixtures]])
(:import (clojure.lang ExceptionInfo)))

(use-fixtures :once (tu/bbin-dirs-fixture))

(deftest install-from-local-root-jar-test
(testing "install ./*.jar"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [script-jar (str "test-resources" fs/file-separator "hello.jar")
cli-opts {:script/lib script-jar}
out (tu/run-install cli-opts)]
(is (= {:coords {:bbin/url (str "file://" (fs/canonicalize script-jar {:nofollow-links true}))}}
out))
(is (= "Hello JAR" (tu/run-bin-script :hello)))))
(testing "install ./*.jar (no main class)"
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [script-jar (str "test-resources" fs/file-separator "hello-no-main-class.jar")
cli-opts {:script/lib script-jar}]
(is (thrown-with-msg? ExceptionInfo #"jar has no Main-Class" (tu/run-install cli-opts)))
(is (not (fs/exists? (fs/file (dirs/bin-dir nil) "hello-no-main-class"))))
(is (not (fs/exists? (fs/file (dirs/jars-dir nil) "hello-no-main-class.jar")))))))
38 changes: 6 additions & 32 deletions test/babashka/bbin/scripts/maven_jar_test.clj
Original file line number Diff line number Diff line change
@@ -1,53 +1,27 @@
(ns babashka.bbin.scripts.maven-jar-test
(:require [babashka.bbin.dirs :as dirs]
[babashka.bbin.scripts :as scripts]
[babashka.bbin.test-util
:refer [bbin-dirs-fixture
bbin-private-keys-fixture
reset-test-dir]]
[babashka.bbin.util :as util]
[babashka.bbin.test-util :as tu]
[babashka.fs :as fs]
[babashka.process :as p]
[clojure.edn :as edn]
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]))

(use-fixtures :once
(bbin-dirs-fixture)
(bbin-private-keys-fixture))

(defn run-install [cli-opts]
(some-> (with-out-str (scripts/install (assoc cli-opts :edn true)))
edn/read-string))

(defn run-ls []
(some-> (with-out-str (scripts/ls {:edn true}))
edn/read-string))

(defn exec-cmd-line [script-name]
(concat (when util/windows? ["cmd" "/c"])
[(str (fs/canonicalize (fs/file (dirs/bin-dir nil) (name script-name)) {:nofollow-links true}))]))

(defn run-bin-script [script-name & script-args]
(let [args (concat (exec-cmd-line script-name) script-args)
{:keys [out]} (p/sh args {:err :inherit})]
(str/trim out)))
(use-fixtures :once (tu/bbin-dirs-fixture))

(def maven-lib
{:lib 'org.babashka/http-server
:coords {:mvn/version "0.1.11"}})

(deftest install-from-mvn-version-test
(testing "install */* --mvn/version *"
(reset-test-dir)
(tu/reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [cli-opts {:script/lib (str (:lib maven-lib))
:mvn/version (-> maven-lib :coords :mvn/version)}
out (run-install cli-opts)]
out (tu/run-install cli-opts)]
(is (= maven-lib out))
(is (fs/exists? (fs/file (dirs/bin-dir nil) (name (:lib maven-lib)))))
(is (str/starts-with? (run-bin-script (:lib maven-lib) "--help")
(is (str/starts-with? (tu/run-bin-script (:lib maven-lib) "--help")
"Serves static assets using web server."))
(is (= '{http-server {:lib org.babashka/http-server
:coords {:mvn/version "0.1.11"}}}
(run-ls))))))
(tu/run-ls))))))
Loading

0 comments on commit ee15035

Please sign in to comment.