Skip to content

Commit

Permalink
Add tests for bbin ls
Browse files Browse the repository at this point in the history
  • Loading branch information
rads committed Oct 14, 2023
1 parent dda48c5 commit 6ea3f3a
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions test/babashka/bbin/scripts_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
(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}))]))
Expand Down Expand Up @@ -143,7 +147,10 @@
(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")
"Serves static assets using web server.")))))
"Serves static assets using web server."))
(is (= '{http-server {:lib org.babashka/http-server
:coords {:mvn/version "0.1.11"}}}
(run-ls))))))

(deftest install-from-lib-local-root-dir-test
(testing "install */* --local/root *"
Expand Down Expand Up @@ -185,50 +192,59 @@
(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 (run-install cli-opts)]
(is (= {:coords {:bbin/url (str "file://" local-root)}}
out))
(is (fs/exists? (fs/file (dirs/bin-dir nil) "foo")))))))
(is (= {:coords {:bbin/url script-url}} out))
(is (fs/exists? (fs/file (dirs/bin-dir nil) "foo")))
(is (= {'foo {:coords {:bbin/url script-url}}} (run-ls)))))))

(deftest install-from-local-root-clj-test
(testing "install ./*.clj (with shebang)"
(reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [script-file (doto (fs/file 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 (run-install cli-opts)]
(is (= {:coords {:bbin/url (str "file://" script-file)}} out))
(is (= "Hello world" (run-bin-script :hello)))))
(is (= "Hello world" (run-bin-script :hello)))
(is (= {'hello {:coords {:bbin/url script-url}}} (run-ls)))))
(testing "install ./*.clj (without shebang)"
(reset-test-dir)
(dirs/ensure-bbin-dirs {})
(let [script-file (doto (fs/file test-dir "hello.clj")
(spit "(println \"Hello world\")"))
script-url (str "file://" script-file)
cli-opts {:script/lib (str script-file)}
out (run-install cli-opts)]
(is (= {:coords {:bbin/url (str "file://" script-file)}} out))
(is (= "Hello world" (run-bin-script :hello))))))
(is (= {:coords {:bbin/url script-url}} out))
(is (= "Hello world" (run-bin-script :hello)))
(is (= {'hello {:coords {:bbin/url script-url}}} (run-ls))))))

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

(deftest install-from-local-root-jar-test
(testing "install ./*.jar"
Expand Down Expand Up @@ -256,7 +272,8 @@
(let [cli-opts {:script/lib portal-script-url}
out (run-install cli-opts)]
(is (= {:coords {:bbin/url portal-script-url}} out))
(is (fs/exists? (fs/file (dirs/bin-dir nil) "portal"))))))
(is (fs/exists? (fs/file (dirs/bin-dir nil) "portal")))
(is (= {'portal {:coords {:bbin/url portal-script-url}}} (run-ls))))))

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

Expand All @@ -272,7 +289,8 @@
(let [cli-opts {:script/lib hello-jar-url}
out (run-install cli-opts)]
(is (= {:coords {:bbin/url hello-jar-url}} out))
(is (= "Hello JAR" (run-bin-script :hello))))))
(is (= "Hello JAR" (run-bin-script :hello)))
(is (= {'hello {:coords {:bbin/url hello-jar-url}}} (run-ls))))))

(deftest install-tool-from-local-root-test
(testing "install ./ --tool"
Expand All @@ -282,12 +300,16 @@
: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}))
_ (run-install opts)]
(is (fs/exists? (fs/file (dirs/bin-dir nil) "footool")))
(let [usage-out (run-bin-script "footool")]
(is (every? #(str/includes? usage-out %) ["`keys`" "`vals`"])))
(is (str/includes? (run-bin-script "footool" "k" ":a" "1") "(:a)"))
(is (str/includes? (run-bin-script "footool" "v" ":a" "1") "(1)")))))
(is (str/includes? (run-bin-script "footool" "v" ":a" "1") "(1)"))
(is (= {'footool {:coords {:local/root full-path}
:lib 'bbin/foo}}
(run-ls))))))

(deftest uninstall-test
(testing "uninstall foo"
Expand Down

0 comments on commit 6ea3f3a

Please sign in to comment.