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]
|
2021-04-25 11:34:26 +00:00
|
|
|
(let [args (map str args)
|
|
|
|
|
ret (apply test-utils/bb nil args)]
|
|
|
|
|
;; (.println System/out :args)
|
|
|
|
|
;; (.println System/out (vec args))
|
|
|
|
|
;; (.println System/out :ret)
|
|
|
|
|
;; (.println System/out ret)
|
|
|
|
|
(edn/read-string
|
|
|
|
|
{:readers *data-readers*
|
|
|
|
|
:eof nil}
|
|
|
|
|
ret)))
|
2021-03-28 15:30:44 +00:00
|
|
|
|
|
|
|
|
(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}])"))))
|
2021-04-28 10:09:05 +00:00
|
|
|
(test-utils/with-config '{:deps {medley/medley {:mvn/version "1.3.0"}}}
|
|
|
|
|
(let [cp (bb "-e" "(do (require '[babashka.classpath :as cp])
|
|
|
|
|
(cp/split-classpath (cp/get-classpath)))")]
|
|
|
|
|
(is (= 1 (count cp)))
|
|
|
|
|
(is (str/includes? (first cp) "medley"))))
|
2021-03-30 16:06:56 +00:00
|
|
|
(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)}}
|
2021-04-25 11:34:26 +00:00
|
|
|
(is (= 6 (bb "run" "--prn" "foo"))))
|
2021-04-10 13:16:12 +00:00
|
|
|
(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-25 21:28:53 +00:00
|
|
|
(fs/delete out)
|
2021-04-24 22:24:33 +00:00
|
|
|
(testing "shell test with :continue"
|
|
|
|
|
(test-utils/with-config {:tasks {'foo (list 'shell {:out out
|
|
|
|
|
:err out
|
|
|
|
|
:continue true}
|
|
|
|
|
"ls foobar")}}
|
|
|
|
|
(bb "foo")
|
|
|
|
|
(is (str/includes? (slurp out)
|
|
|
|
|
"foobar"))))
|
2021-04-25 21:28:53 +00:00
|
|
|
(fs/delete out)
|
2021-04-25 11:34:26 +00:00
|
|
|
(testing "shell test with :continue fn"
|
2021-04-25 21:28:53 +00:00
|
|
|
(test-utils/with-config {:tasks {'foo (list '-> (list 'shell {:out out
|
|
|
|
|
:err out
|
|
|
|
|
:continue '(fn [proc]
|
|
|
|
|
(contains? proc :exit))}
|
|
|
|
|
"ls foobar")
|
|
|
|
|
:exit)}}
|
2021-04-25 21:31:39 +00:00
|
|
|
(is (pos? (bb "run" "--prn" "foo")))))
|
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-25 11:34:26 +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")
|
|
|
|
|
(is (= "quux\nbaz\nbar\nfoo\n" (slurp out))))))
|
|
|
|
|
(testing "init test"
|
2021-04-12 07:47:28 +00:00
|
|
|
(test-utils/with-config '{:tasks {:init (def x 1)
|
|
|
|
|
foo x}}
|
2021-04-25 11:34:26 +00:00
|
|
|
(is (= 1 (bb "run" "--prn" "foo")))))
|
|
|
|
|
(testing "requires test"
|
2021-04-12 20:55:10 +00:00
|
|
|
(test-utils/with-config '{:tasks {:requires ([babashka.fs :as fs])
|
|
|
|
|
foo (fs/exists? ".")}}
|
2021-04-25 11:34:26 +00:00
|
|
|
(is (= true (bb "run" "--prn" "foo"))))
|
2021-04-12 20:55:10 +00:00
|
|
|
(test-utils/with-config '{:tasks {foo {:requires ([babashka.fs :as fs])
|
|
|
|
|
:task (fs/exists? ".")}}}
|
2021-04-25 11:34:26 +00:00
|
|
|
(is (= true (bb "run" "--prn" "foo"))))
|
2021-04-12 20:55:10 +00:00
|
|
|
(test-utils/with-config '{:tasks {bar {:requires ([babashka.fs :as fs])}
|
|
|
|
|
foo {:depends [bar]
|
|
|
|
|
:task (fs/exists? ".")}}}
|
2021-04-25 11:34:26 +00:00
|
|
|
(is (= true (bb "run" "--prn" "foo")))))
|
|
|
|
|
(testing "map returned from task"
|
2021-04-24 11:05:27 +00:00
|
|
|
(test-utils/with-config '{:tasks {foo {:task {:a 1 :b 2}}}}
|
2021-04-25 11:34:26 +00:00
|
|
|
(is (= {:a 1 :b 2} (bb "run" "--prn" "foo")))))
|
|
|
|
|
(testing "fully qualified symbol execution"
|
2021-04-25 09:42:11 +00:00
|
|
|
(test-utils/with-config {:paths ["test-resources/task_scripts"]
|
|
|
|
|
:tasks '{foo tasks/foo}}
|
2021-04-25 11:34:26 +00:00
|
|
|
(is (= :foo (bb "run" "--prn" "foo"))))
|
2021-04-25 09:42:11 +00:00
|
|
|
(test-utils/with-config {:paths ["test-resources/task_scripts"]
|
|
|
|
|
:tasks '{:requires ([tasks :as t])
|
|
|
|
|
foo t/foo}}
|
2021-04-25 11:34:26 +00:00
|
|
|
(is (= :foo (bb "run" "--prn" "foo"))))
|
2021-04-25 09:42:11 +00:00
|
|
|
(test-utils/with-config {:paths ["test-resources/task_scripts"]
|
|
|
|
|
:tasks '{foo {:requires ([tasks :as t])
|
|
|
|
|
:task t/foo}}}
|
2021-04-27 21:32:25 +00:00
|
|
|
(is (= :foo (bb "run" "--prn" "foo")))))
|
|
|
|
|
(testing "extra-paths"
|
|
|
|
|
(test-utils/with-config {:paths ["test-resources/task_scripts"]
|
|
|
|
|
:tasks '{:requires ([tasks :as t])
|
|
|
|
|
foo {:extra-paths ["test-resources/task_test_scripts"]
|
|
|
|
|
:requires ([task-test :as tt])
|
|
|
|
|
:task tt/task-test-fn}}}
|
|
|
|
|
(is (= :task-test-fn (bb "run" "--prn" "foo")))))
|
|
|
|
|
(testing "extra-deps"
|
|
|
|
|
(test-utils/with-config {:tasks '{foo {:extra-deps {medley/medley {:mvn/version "1.3.0"}}
|
|
|
|
|
:requires ([medley.core :as m])
|
|
|
|
|
:task (m/index-by :id [{:id 1} {:id 2}])}}}
|
2021-04-30 09:11:12 +00:00
|
|
|
(is (= {1 {:id 1}, 2 {:id 2}} (bb "run" "--prn" "foo")))))
|
|
|
|
|
(testing "enter / leave"
|
|
|
|
|
(test-utils/with-config '{:tasks {:init (do (def enter-ctx (atom []))
|
|
|
|
|
(def leave-ctx (atom [])))
|
|
|
|
|
:enter (swap! enter-ctx conj (:name (current-task)))
|
|
|
|
|
:leave (swap! leave-ctx conj (:name (current-task)))
|
|
|
|
|
foo {:depends [bar]
|
|
|
|
|
:task [@enter-ctx @leave-ctx]}
|
|
|
|
|
bar {:depends [baz]}
|
|
|
|
|
baz {:enter nil
|
|
|
|
|
:leave nil}}}
|
|
|
|
|
(is (= '[[bar foo] [bar]] (bb "run" "--prn" "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-25 19:51:00 +00:00
|
|
|
(test-utils/with-config "{:paths [\"test-resources/task_scripts\"]
|
|
|
|
|
:tasks {:requires ([tasks :as t])
|
|
|
|
|
task1
|
|
|
|
|
{: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)
|
|
|
|
|
:private true}
|
|
|
|
|
foo tasks/foo
|
|
|
|
|
bar t/foo
|
|
|
|
|
baz non-existing/bar
|
|
|
|
|
quux {:requires ([tasks :as t2])
|
|
|
|
|
:task t2/foo}}}"
|
2021-04-10 14:07:06 +00:00
|
|
|
(let [res (test-utils/bb nil "tasks")]
|
2021-04-25 19:51:00 +00:00
|
|
|
(is (= "The following tasks are available:\n\ntask1 task1 doc\ntask2 task2 doc\nfoo Foo docstring\nbar Foo docstring\nbaz \nquux Foo docstring\n"
|
2021-04-26 08:59:05 +00:00
|
|
|
res))))
|
|
|
|
|
(testing ":tasks is the first node"
|
|
|
|
|
(test-utils/with-config "{:tasks {task1
|
|
|
|
|
{:doc \"task1 doc\"
|
|
|
|
|
:task (+ 1 2 3)}}}"
|
|
|
|
|
(let [res (test-utils/bb nil "tasks")]
|
|
|
|
|
(is (= "The following tasks are available:\n\ntask1 task1 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-25 11:34:26 +00:00
|
|
|
(test-utils/with-config '{:aliases {:medley {:deps {medley/medley {:mvn/version "1.3.0"}}}}}
|
|
|
|
|
(is (= '{1 {:id 1}, 2 {:id 2}}
|
|
|
|
|
(bb "-A:medley" "-e" "(require 'medley.core)" "-e" "(medley.core/index-by :id [{:id 1} {:id 2}])")))))
|