2021-03-28 15:30:44 +00:00
|
|
|
(ns babashka.bb-edn-test
|
|
|
|
|
(:require
|
2021-04-10 13:16:12 +00:00
|
|
|
[babashka.fs :as fs]
|
2021-04-24 09:51:03 +00:00
|
|
|
[babashka.impl.common :as common]
|
|
|
|
|
[babashka.main :as main]
|
2021-03-28 15:30:44 +00:00
|
|
|
[babashka.test-utils :as test-utils]
|
|
|
|
|
[clojure.edn :as edn]
|
|
|
|
|
[clojure.string :as str]
|
2021-03-30 16:06:56 +00:00
|
|
|
[clojure.test :as test :refer [deftest is testing]]))
|
2021-03-28 15:30:44 +00:00
|
|
|
|
|
|
|
|
(defn bb [& args]
|
|
|
|
|
(edn/read-string
|
|
|
|
|
{:readers *data-readers*
|
|
|
|
|
:eof nil}
|
|
|
|
|
(apply test-utils/bb nil (map str args))))
|
|
|
|
|
|
|
|
|
|
(deftest doc-test
|
2021-04-07 08:21:09 +00:00
|
|
|
(test-utils/with-config {:paths ["test-resources/task_scripts"]}
|
2021-03-28 15:30:44 +00:00
|
|
|
(is (str/includes? (apply test-utils/bb nil
|
|
|
|
|
(map str ["doc" "tasks"]))
|
|
|
|
|
"This is task ns docstring."))
|
|
|
|
|
(is (str/includes? (apply test-utils/bb nil
|
|
|
|
|
(map str ["doc" "tasks/foo"]))
|
|
|
|
|
"Foo docstring"))
|
|
|
|
|
(is (str/includes? (apply test-utils/bb nil
|
|
|
|
|
(map str ["doc" "tasks/-main"]))
|
|
|
|
|
"Main docstring"))))
|
|
|
|
|
|
|
|
|
|
(deftest deps-test
|
2021-04-07 08:21:09 +00:00
|
|
|
(test-utils/with-config '{:deps {medley/medley {:mvn/version "1.3.0"}}}
|
2021-03-28 15:30:44 +00:00
|
|
|
(is (= '{1 {:id 1}, 2 {:id 2}}
|
2021-03-30 16:06:56 +00:00
|
|
|
(bb "-e" "(require 'medley.core)" "-e" "(medley.core/index-by :id [{:id 1} {:id 2}])"))))
|
|
|
|
|
(testing "--classpath option overrides bb.edn"
|
2021-04-07 08:21:09 +00:00
|
|
|
(test-utils/with-config '{:deps {medley/medley {:mvn/version "1.3.0"}}}
|
2021-03-30 16:06:56 +00:00
|
|
|
(is (= "src"
|
|
|
|
|
(bb "-cp" "src" "-e" "(babashka.classpath/get-classpath)"))))))
|
2021-03-29 21:57:13 +00:00
|
|
|
|
2021-04-10 13:16:12 +00:00
|
|
|
(deftest task-test
|
|
|
|
|
(test-utils/with-config '{:tasks {foo (+ 1 2 3)}}
|
|
|
|
|
(is (= 6 (bb "foo"))))
|
|
|
|
|
(let [tmp-dir (fs/create-temp-dir)
|
|
|
|
|
out (str (fs/file tmp-dir "out.txt"))]
|
2021-04-10 14:12:30 +00:00
|
|
|
(testing "shell test"
|
|
|
|
|
(test-utils/with-config {:tasks {'foo (list 'shell {:out out}
|
|
|
|
|
"echo hello")}}
|
|
|
|
|
(bb "foo")
|
|
|
|
|
(is (= "hello\n" (slurp out)))))
|
2021-04-10 14:50:20 +00:00
|
|
|
(fs/delete out)
|
2021-04-10 14:12:30 +00:00
|
|
|
(testing "clojure test"
|
|
|
|
|
(test-utils/with-config {:tasks {'foo (list 'clojure {:out out}
|
|
|
|
|
"-M -e" "(println :yolo)")}}
|
|
|
|
|
(bb "foo")
|
|
|
|
|
(is (= ":yolo\n" (slurp out)))))
|
2021-04-10 14:50:20 +00:00
|
|
|
(fs/delete out)
|
|
|
|
|
(testing "depends"
|
|
|
|
|
(test-utils/with-config {:tasks {'quux (list 'spit out "quux\n")
|
|
|
|
|
'baz (list 'spit out "baz\n" :append true)
|
|
|
|
|
'bar {:depends ['baz]
|
|
|
|
|
:task (list 'spit out "bar\n" :append true)}
|
|
|
|
|
'foo {:depends ['quux 'bar 'baz]
|
|
|
|
|
:task (list 'spit out "foo\n" :append true)}}}
|
|
|
|
|
(bb "foo")
|
|
|
|
|
(is (= "quux\nbaz\nbar\nfoo\n" (slurp out)))))
|
|
|
|
|
(fs/delete out)
|
2021-04-12 07:47:28 +00:00
|
|
|
(testing "init test"
|
|
|
|
|
(test-utils/with-config '{:tasks {:init (def x 1)
|
|
|
|
|
foo x}}
|
|
|
|
|
(is (= 1 (bb "foo")))))
|
2021-04-12 20:55:10 +00:00
|
|
|
(testing "requires test"
|
|
|
|
|
(test-utils/with-config '{:tasks {:requires ([babashka.fs :as fs])
|
|
|
|
|
foo (fs/exists? ".")}}
|
|
|
|
|
(is (= true (bb "foo"))))
|
|
|
|
|
(test-utils/with-config '{:tasks {foo {:requires ([babashka.fs :as fs])
|
|
|
|
|
:task (fs/exists? ".")}}}
|
|
|
|
|
(is (= true (bb "foo"))))
|
|
|
|
|
(test-utils/with-config '{:tasks {bar {:requires ([babashka.fs :as fs])}
|
|
|
|
|
foo {:depends [bar]
|
|
|
|
|
:task (fs/exists? ".")}}}
|
|
|
|
|
(is (= true (bb "foo")))))
|
2021-04-10 14:50:20 +00:00
|
|
|
;; This is why we don't support :when for now
|
|
|
|
|
#_(testing "depends with :when"
|
|
|
|
|
(test-utils/with-config {:tasks {'quux (list 'spit out "quux\n")
|
|
|
|
|
'baz (list 'spit out "baz\n" :append true)
|
|
|
|
|
'bar {:when false
|
|
|
|
|
:depends ['baz]
|
|
|
|
|
:task (list 'spit out "bar\n" :append true)}
|
|
|
|
|
'foo {:depends ['quux 'bar]
|
|
|
|
|
:task (list 'spit out "foo\n" :append true)}}}
|
|
|
|
|
(bb "foo")
|
2021-04-24 11:05:27 +00:00
|
|
|
(is (= "quux\nbaz\nbar\nfoo\n" (slurp out)))))
|
|
|
|
|
(testing "map returned from task"
|
|
|
|
|
(test-utils/with-config '{:tasks {foo {:task {:a 1 :b 2}}}}
|
|
|
|
|
(is (= {:a 1 :b 2} (bb "foo")))))))
|
2021-04-10 13:16:12 +00:00
|
|
|
|
2021-04-10 14:07:06 +00:00
|
|
|
(deftest list-tasks-test
|
|
|
|
|
(test-utils/with-config {}
|
|
|
|
|
(let [res (test-utils/bb nil "tasks")]
|
|
|
|
|
(is (str/includes? res "No tasks found."))))
|
2021-04-24 21:19:37 +00:00
|
|
|
(test-utils/with-config {:paths ["test-resources/task_scripts"]
|
|
|
|
|
:tasks {'task1
|
2021-04-10 14:07:06 +00:00
|
|
|
{:doc "task1 doc"
|
|
|
|
|
:task '(+ 1 2 3)}
|
|
|
|
|
'task2
|
|
|
|
|
{:doc "task2 doc"
|
|
|
|
|
:task '(+ 4 5 6)}
|
|
|
|
|
'-task3
|
|
|
|
|
{:task '(+ 1 2 3)}
|
|
|
|
|
'task4
|
|
|
|
|
{:task '(+ 1 2 3)
|
2021-04-24 21:19:37 +00:00
|
|
|
:private true}
|
|
|
|
|
'foo 'tasks/foo
|
|
|
|
|
'bar 'non-existing/bar}}
|
2021-04-10 14:07:06 +00:00
|
|
|
(let [res (test-utils/bb nil "tasks")]
|
2021-04-24 21:19:37 +00:00
|
|
|
(is (= "The following tasks are available:\n\nbar \nfoo Foo docstring\ntask1 task1 doc\ntask2 task2 doc\n"
|
|
|
|
|
res)))))
|
2021-04-10 14:07:06 +00:00
|
|
|
|
2021-04-24 09:51:03 +00:00
|
|
|
(deftest task-priority-test
|
2021-04-24 10:05:15 +00:00
|
|
|
(when-not test-utils/native?
|
|
|
|
|
(testing "FILE > TASK > SUBCOMMAND"
|
|
|
|
|
(is (= "foo.jar" (:uberjar (main/parse-opts ["uberjar" "foo.jar"]))))
|
2021-04-24 10:54:41 +00:00
|
|
|
(vreset! common/bb-edn '{:tasks {uberjar (+ 1 2 3)}})
|
|
|
|
|
(is (= "uberjar" (:run (main/parse-opts ["uberjar"]))))
|
2021-04-24 10:05:15 +00:00
|
|
|
(try
|
2021-04-24 10:54:41 +00:00
|
|
|
(spit "uberjar" "#!/usr/bin/env bb\n(+ 1 2 3)")
|
|
|
|
|
(vreset! common/bb-edn '{:tasks {uberjar (+ 1 2 3)}})
|
|
|
|
|
(is (= "uberjar" (:file (main/parse-opts ["uberjar"]))))
|
2021-04-24 10:05:15 +00:00
|
|
|
(finally (fs/delete "uberjar"))))))
|
2021-04-24 09:51:03 +00:00
|
|
|
|
2021-04-24 10:54:41 +00:00
|
|
|
(deftest min-bb-version
|
|
|
|
|
(when-not test-utils/native?
|
|
|
|
|
(vreset! common/bb-edn '{:min-bb-version "300.0.0"})
|
|
|
|
|
(let [sw (java.io.StringWriter.)]
|
|
|
|
|
(binding [*err* sw]
|
|
|
|
|
(main/main "-e" "nil"))
|
|
|
|
|
(is (str/includes? (str sw)
|
|
|
|
|
"WARNING: this project requires babashka 300.0.0 or newer, but you have: ")))))
|
|
|
|
|
|
2021-03-29 21:57:13 +00:00
|
|
|
;; TODO:
|
|
|
|
|
;; Do we want to support the same parsing as the clj CLI?
|
|
|
|
|
;; Or do we want `--aliases :foo:bar`
|
|
|
|
|
;; Let's wait for a good use case
|
|
|
|
|
#_(deftest alias-deps-test
|
2021-04-07 08:21:09 +00:00
|
|
|
(test-utils/with-config '{:aliases {:medley {:deps {medley/medley {:mvn/version "1.3.0"}}}}}
|
2021-03-29 21:57:13 +00:00
|
|
|
(is (= '{1 {:id 1}, 2 {:id 2}}
|
|
|
|
|
(bb "-A:medley" "-e" "(require 'medley.core)" "-e" "(medley.core/index-by :id [{:id 1} {:id 2}])")))))
|