diff --git a/CHANGELOG.md b/CHANGELOG.md index 12fd0ed3..107b4e57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ A preview of the next release can be installed from [Babashka](https://github.com/babashka/babashka): Native, fast starting Clojure interpreter for scripting +## Unreleased + +- [#1430](https://github.com/babashka/babashka/issues/1430): Fix issue with `bb tasks` throwing on empty display tasks list. + ## 1.0.166 (2022-11-24) See the [Testing babashka scripts](https://blog.michielborkent.nl/babashka-test-runner.html) blog post for how to run tests with this release. diff --git a/src/babashka/impl/tasks.clj b/src/babashka/impl/tasks.clj index 99c25162..f4f1ad6b 100644 --- a/src/babashka/impl/tasks.clj +++ b/src/babashka/impl/tasks.clj @@ -429,15 +429,22 @@ (iterate zip/right loc)))) (defn list-tasks + "Prints out the task names found in BB-EDN in the original order + alongside their documentation as retrieved with SCI-CTX. + + For a task to be listed + - its name has to be a symbol but should not start with `-`, and + - should not be `:private`." [sci-ctx] - (let [tasks (:tasks @bb-edn)] - (if (seq tasks) - (let [raw-edn (:raw @bb-edn) - names (key-order raw-edn) - names (map str names) - names (remove #(str/starts-with? % "-") names) - names (remove #(:private (get tasks (symbol %))) names) - longest (apply max (map count names)) + (let [tasks (:tasks @bb-edn) + raw-edn (:raw @bb-edn) + names (when (seq tasks) + (->> (key-order raw-edn) + (map str) + (remove #(str/starts-with? % "-")) + (remove #(:private (get tasks (symbol %))))))] + (if (seq names) + (let [longest (apply max (map count names)) fmt (str "%1$-" longest "s")] (println "The following tasks are available:") (println) diff --git a/test/babashka/bb_edn_test.clj b/test/babashka/bb_edn_test.clj index 0422fe2b..eb39a5c7 100644 --- a/test/babashka/bb_edn_test.clj +++ b/test/babashka/bb_edn_test.clj @@ -307,6 +307,25 @@ (test-utils/with-config {} (let [res (test-utils/bb nil "tasks")] (is (str/includes? res "No tasks found.")))) + (test-utils/with-config '{:tasks {:x 1}} + (let [res (test-utils/bb nil "tasks")] + (is (str/includes? res "No tasks found.")))) + (test-utils/with-config '{:tasks {-xyz 5}} + (let [res (test-utils/bb nil "tasks")] + (is (str/includes? res "No tasks found.")))) + (test-utils/with-config '{:tasks {xyz {:private true}}} + (let [res (test-utils/bb nil "tasks")] + (is (str/includes? res "No tasks found.")))) + (test-utils/with-config '{:tasks {abc 1 xyz 2}} + (let [res (test-utils/bb nil "tasks")] + (is (= "The following tasks are available:\n\nabc\nxyz\n" res)))) + (test-utils/with-config '{:tasks {abc 1 xyz {:doc "some text" :tasks 5} + -xyz 3 qrs {:private true}}} + (let [res (test-utils/bb nil "tasks")] + (is (= "The following tasks are available:\n\nabc\nxyz some text\n" res)))) + (test-utils/with-config '{:tasks {xyz 1 abc 2}} + (let [res (test-utils/bb nil "tasks")] + (is (= "The following tasks are available:\n\nxyz\nabc\n" res)))) (test-utils/with-config "{:paths [\"test-resources/task_scripts\"] :tasks {:requires ([tasks :as t]) task1