switch to symbols for built-in tasks

This commit is contained in:
Michiel Borkent 2021-03-27 16:14:52 +01:00
parent f02bf19e80
commit c4d8e4de4c
2 changed files with 35 additions and 36 deletions

View file

@ -166,7 +166,7 @@ Use -- to separate script command line args from bb command line args.
task-key :task} (decode-task task)] task-key :task} (decode-task task)]
(if-let [help-text (:doc task)] (if-let [help-text (:doc task)]
[(println help-text) 0] [(println help-text) 0]
(if-let [main (when (= :main task-key) (if-let [main (when (= 'main task-key)
(first args))] (first args))]
(let [main (if (simple-symbol? main) (let [main (if (simple-symbol? main)
(symbol (str main) "-main") (symbol (str main) "-main")
@ -450,7 +450,7 @@ Use -- to separate script command line args from bb command line args.
key? (when fst (str/starts-with? fst ":")) key? (when fst (str/starts-with? fst ":"))
keys (when key? (rest (str/split fst #":"))) keys (when key? (rest (str/split fst #":")))
expanded (when (and key? (> (count keys) 1)) expanded (when (and key? (> (count keys) 1))
(into [:do] (map (comp vector keyword) keys))) (into ['do] (map (comp vector keyword) keys)))
k (when (and key? (not expanded)) k (when (and key? (not expanded))
(keyword (first keys))) (keyword (first keys)))
bb-edn @bb-edn bb-edn @bb-edn
@ -603,10 +603,10 @@ Use -- to separate script command line args from bb command line args.
(let [{:keys [:task :opts :args]} (decode-task task)] (let [{:keys [:task :opts :args]} (decode-task task)]
opts ;; not used opts ;; not used
(case task (case task
:babashka babashka
(let [cmd-line-args args] (let [cmd-line-args args]
(parse-opts (seq (map str (concat cmd-line-args command-line-args))))) (parse-opts (seq (map str (concat cmd-line-args command-line-args)))))
:shell shell
(let [args (if (and (= 1 (count args)) (let [args (if (and (= 1 (count args))
(string? (first args))) (string? (first args)))
(p/tokenize (first args)) (p/tokenize (first args))
@ -617,11 +617,11 @@ Use -- to separate script command line args from bb command line args.
(-> (p/process args {:inherit true}) (-> (p/process args {:inherit true})
deref deref
:exit)])}) :exit)])})
:main main
(let [main-arg (first args) (let [main-arg (first args)
cmd-line-args (rest args)] cmd-line-args (rest args)]
(parse-opts (seq (map str (concat ["--main" main-arg] cmd-line-args command-line-args))))) (parse-opts (seq (map str (concat ["--main" main-arg] cmd-line-args command-line-args)))))
:do do
{:do (map #(resolve-task tasks % nil) args)} {:do (map #(resolve-task tasks % nil) args)}
;; default ;; default
(if-let [t (get tasks task)] (if-let [t (get tasks task)]

View file

@ -1,5 +1,4 @@
(ns babashka.bb-edn-test (ns babashka.bb-edn-test
{:clj-kondo/config '{:linters {:unresolved-symbol {:exclude [working?]}}}}
(:require (:require
[babashka.fs :as fs] [babashka.fs :as fs]
[babashka.test-utils :as test-utils] [babashka.test-utils :as test-utils]
@ -32,21 +31,21 @@
(bb "doc" :sum))))) (bb "doc" :sum)))))
(deftest babashka-task-test (deftest babashka-task-test
(with-config {:tasks {:sum [:babashka "-e" "(+ 1 2 3)"]}} (with-config {:tasks {:sum ['babashka "-e" "(+ 1 2 3)"]}}
(let [res (bb :sum)] (let [res (bb :sum)]
(is (= 6 res))))) (is (= 6 res)))))
(deftest shell-task-test (deftest shell-task-test
(let [temp-dir (fs/create-temp-dir) (let [temp-dir (fs/create-temp-dir)
temp-file (fs/create-file (fs/path temp-dir "temp-file.txt"))] temp-file (fs/create-file (fs/path temp-dir "temp-file.txt"))]
(with-config {:tasks {:clean [:shell "rm" (str temp-file)]}} (with-config {:tasks {:clean ['shell "rm" (str temp-file)]}}
(is (fs/exists? temp-file)) (is (fs/exists? temp-file))
(bb :clean) (bb :clean)
(is (not (fs/exists? temp-file))))) (is (not (fs/exists? temp-file)))))
(let [temp-dir (fs/create-temp-dir) (let [temp-dir (fs/create-temp-dir)
temp-file (fs/create-file (fs/path temp-dir "temp-file.txt"))] temp-file (fs/create-file (fs/path temp-dir "temp-file.txt"))]
(testing "tokenization" (testing "tokenization"
(with-config {:tasks {:clean [:shell (str "rm " (str temp-file))]}} (with-config {:tasks {:clean ['shell (str "rm " (str temp-file))]}}
(is (fs/exists? temp-file)) (is (fs/exists? temp-file))
(bb :clean) (bb :clean)
(is (not (fs/exists? temp-file))))))) (is (not (fs/exists? temp-file)))))))
@ -55,9 +54,9 @@
(testing ":and-do" (testing ":and-do"
(let [temp-dir (fs/create-temp-dir) (let [temp-dir (fs/create-temp-dir)
temp-file (fs/create-file (fs/path temp-dir "temp-file.txt"))] temp-file (fs/create-file (fs/path temp-dir "temp-file.txt"))]
(with-config {:tasks {:sum [:babashka "-e" "(+ 1 2 3)"] (with-config {:tasks {:sum ['babashka "-e" "(+ 1 2 3)"]
:all [:do :all ['do
[:shell "rm" (str temp-file)] ['shell "rm" (str temp-file)]
[:sum]]}} [:sum]]}}
(is (fs/exists? temp-file)) (is (fs/exists? temp-file))
(let [res (bb :all)] (let [res (bb :all)]
@ -66,42 +65,42 @@
#_(testing ":and-do shortcut" #_(testing ":and-do shortcut"
(let [temp-dir (fs/create-temp-dir) (let [temp-dir (fs/create-temp-dir)
temp-file (fs/create-file (fs/path temp-dir "temp-file.txt"))] temp-file (fs/create-file (fs/path temp-dir "temp-file.txt"))]
(with-config {:tasks {:clean [:shell :clean:sum] (with-config {:tasks {:clean [shell :clean:sum]
:sum [:babashka "-e" "(+ 1 2 3)"] :sum [babashka "-e" "(+ 1 2 3)"]
:all [:babashka :clean:sum]}} :all [babashka :clean:sum]}}
(is (fs/exists? temp-file)) (is (fs/exists? temp-file))
(let [res (bb :clean:sum)] (let [res (bb :clean:sum)]
(is (= 6 res))) (is (= 6 res)))
(is (not (fs/exists? temp-file))))))) (is (not (fs/exists? temp-file)))))))
#_(testing ":do always continuing" #_(testing "'do always continuing"
(with-config {:tasks {:sum-1 [:babashka "-e" "(do (+ 4 5 6) nil)"] (with-config {:tasks {:sum-1 [babashka "-e" "(do (+ 4 5 6) nil)"]
:sum-2 [:babashka "-e" "(+ 1 2 3)"] :sum-2 [babashka "-e" "(+ 1 2 3)"]
:all [:babashka :sum-1:sum2]}} :all [babashka :sum-1:sum2]}}
(is (= 6 (bb :all)))) (is (= 6 (bb :all))))
#_(with-config {:tasks {:div-by-zero [:babashka "-e" "(/ 1 0)"] #_(with-config {:tasks {:div-by-zero [babashka "-e" "(/ 1 0)"]
:sum [:babashka "-e" "(+ 1 2 3)"] :sum [babashka "-e" "(+ 1 2 3)"]
:all[:babashka :div-by-zero:sum] }} :all[babashka :div-by-zero:sum] }}
(is (= 6 (bb :all))))) (is (= 6 (bb :all)))))
(testing "task fails when one of subtask fails" (testing "task fails when one of subtask fails"
(with-config {:tasks {:div-by-zero [:babashka "-e" "(/ 1 0)"] (with-config {:tasks {:div-by-zero ['babashka "-e" "(/ 1 0)"]
:sum [:babashka "-e" "(+ 1 2 3)"] :sum ['babashka "-e" "(+ 1 2 3)"]
:all [:babashka :div-by-zero:sum]}} :all ['babashka :div-by-zero:sum]}}
(is (thrown-with-msg? Exception #"Divide" (is (thrown-with-msg? Exception #"Divide"
(bb :all))))) (bb :all)))))
#_(testing ":or-do short-cutting" #_(testing ":or-do short-cutting"
(with-config {:tasks {:sum-1 [:babashka "-e" "(+ 1 2 3)"] (with-config {:tasks {:sum-1 [babashka "-e" "(+ 1 2 3)"]
:sum-2 [:babashka "-e" "(+ 4 5 6)"] :sum-2 [babashka "-e" "(+ 4 5 6)"]
:all [:or [:sum1] [:sum2]]}} :all [:or [:sum1] [:sum2]]}}
(is (= 6 (bb :all))))) (is (= 6 (bb :all)))))
#_(testing ":or-do succeeding after failing" #_(testing ":or-do succeeding after failing"
(with-config {:tasks {:div-by-zero [:babashka "-e" "(/ 1 0)"] (with-config {:tasks {:div-by-zero [babashka "-e" "(/ 1 0)"]
:sum [:babashka "-e" "(+ 1 2 3)"] :sum [babashka "-e" "(+ 1 2 3)"]
:all [:babashka [:or [:div-by-zero] [:sum]]]}} :all [babashka [:or [:div-by-zero] [:sum]]]}}
(is (= 6 (bb :all)))))) (is (= 6 (bb :all))))))
(deftest prioritize-user-task-test (deftest prioritize-user-task-test
(is (map? (bb "describe"))) (is (map? (bb "describe")))
(with-config {:tasks {:describe [:babashka "-e" "(+ 1 2 3)"]}} (with-config {:tasks {:describe ['babashka "-e" "(+ 1 2 3)"]}}
(is (= 6 (bb :describe))))) (is (= 6 (bb :describe)))))
(deftest doc-task-test (deftest doc-task-test
@ -110,7 +109,7 @@
Addition is a pretty advanced topic. Let us start with the identity element Addition is a pretty advanced topic. Let us start with the identity element
0. ..." 0. ..."
:task [:babashka "-e" "(+ 1 2 3)"]}}} :task ['babashka "-e" "(+ 1 2 3)"]}}}
(is (str/includes? (apply test-utils/bb nil (is (str/includes? (apply test-utils/bb nil
(map str ["doc" :cool-task])) (map str ["doc" :cool-task]))
"Usage: bb :cool-task")))) "Usage: bb :cool-task"))))
@ -125,14 +124,14 @@
Addition is a pretty advanced topic. Let us start with the identity element Addition is a pretty advanced topic. Let us start with the identity element
0. ..."} 0. ..."}
:task [:babashka "-e" "(+ 1 2 3)"] :task ['babashka "-e" "(+ 1 2 3)"]
:cool-task-2 :cool-task-2
{:description "Return the sum of 4, 5 and 6." {:description "Return the sum of 4, 5 and 6."
:doc "Usage: bb :cool-task :doc "Usage: bb :cool-task
Addition is a pretty advanced topic. Let us start with the identity element Addition is a pretty advanced topic. Let us start with the identity element
0. ..." 0. ..."
:task [:babashka "-e" "(+ 4 5 6)"]}}} :task ['babashka "-e" "(+ 4 5 6)"]}}}
(let [res (test-utils/bb nil "tasks")] (let [res (test-utils/bb nil "tasks")]
(is (str/includes? res "The following tasks are available:")) (is (str/includes? res "The following tasks are available:"))
(is (str/includes? res ":task-1 Return the")) (is (str/includes? res ":task-1 Return the"))
@ -140,7 +139,7 @@ Addition is a pretty advanced topic. Let us start with the identity element
(deftest main-task-test (deftest main-task-test
(with-config {:paths ["test-resources/task_scripts"] (with-config {:paths ["test-resources/task_scripts"]
:tasks {:main-task [:main 'tasks 1 2 3]}} :tasks {:main-task ['main 'tasks 1 2 3]}}
(is (= '("1" "2" "3") (bb :main-task))) (is (= '("1" "2" "3") (bb :main-task)))
(let [res (apply test-utils/bb nil (let [res (apply test-utils/bb nil
(map str ["doc" :main-task]))] (map str ["doc" :main-task]))]