From d2783bdb43673d1e2d1af06fa8258b4e4f069f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Neboj=C5=A1a=20Stri=C4=8Devi=C4=87?= Date: Mon, 8 Apr 2024 07:37:56 +0200 Subject: [PATCH 1/3] Refactor and fix print errors and exit status --- src/alas.janet | 30 +++++++++++++++++------------- src/commands/schedule_tasks.janet | 15 ++++++++------- src/errors.janet | 5 ++++- src/file_repository.janet | 3 ++- src/schedule_parser.janet | 2 +- test/alas_test.janet | 24 ++++++++++++++++++++++++ test/examples/unparsable-todo.md | 11 +++++++++++ 7 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 test/alas_test.janet create mode 100644 test/examples/unparsable-todo.md diff --git a/src/alas.janet b/src/alas.janet index 8185c17..27fda33 100644 --- a/src/alas.janet +++ b/src/alas.janet @@ -36,34 +36,37 @@ :help "Output version information."} :default {:kind :option}]) -(defn- run-with-file-path [arguments file-path] +(defn run-with-file-path [arguments file-path] (def load-file-result (file_repository/load file-path)) - (def errors (load-file-result :errors)) - (if errors - (errors/print-errors errors (errors/exit-status-codes :file-error)) + (def file-errors (load-file-result :errors)) + (if (any? file-errors) + [file-errors (errors/exit-status-codes :file-error)] (let [plan-string (load-file-result :text) parse-result (plan_parser/parse plan-string) parse-errors (parse-result :errors) plan (parse-result :plan)] - (if (empty? parse-errors) + (if (any? parse-errors) + [parse-errors (errors/exit-status-codes :parse-error)] (let [{:plan new-plan :errors run-errors} (run-commands plan file-path arguments)] - (if (empty? run-errors) + (if (any? run-errors) + [run-errors (errors/exit-status-codes :command-error)] (let [serialize-empty-inbox (plan_parser/serialize-empty-inbox? plan-string) new-plan-string (plan_serializer/serialize new-plan {:serialize-empty-inbox serialize-empty-inbox})] - (file_repository/save new-plan-string file-path)) - (errors/print-errors run-errors (errors/exit-status-codes :command-error))) - (errors/print-errors parse-errors (errors/exit-status-codes :parse-error))))))) + (file_repository/save new-plan-string file-path) + errors/no-error))))))) (defn- run-with-arguments [arguments] (def file-path (arguments :default)) (if file-path (run-with-file-path arguments file-path) (if (arguments "version") - (print-version) - (errors/print-errors ["Plan file path is missing"] - (errors/exit-status-codes :plan-path-missing))))) + (do + (print-version) + errors/no-error) + [["Plan file path is missing"] + (errors/exit-status-codes :plan-path-missing)]))) ## ————————————————————————————————————————————————————————————————————————————————————————————————— ## Public Interface @@ -71,4 +74,5 @@ (defn main [& args] (def arguments (argparse ;argparse-params)) (if arguments - (run-with-arguments arguments))) + (let [[errors exit-code] (run-with-arguments arguments)] + (errors/print-errors errors (errors/exit-status-codes exit-code))))) diff --git a/src/commands/schedule_tasks.janet b/src/commands/schedule_tasks.janet index 0f63c20..3aae488 100644 --- a/src/commands/schedule_tasks.janet +++ b/src/commands/schedule_tasks.janet @@ -83,12 +83,13 @@ (def argument (arguments "schedule-tasks")) (if argument (let [load-file-result (file_repository/load argument) - errors (load-file-result :errors)] - (if errors - {:errors (errors/format-command-errors command errors)} + file-errors (load-file-result :errors)] + (if (any? file-errors) + {:errors (errors/format-command-errors command file-errors)} (let [parse-result (schedule_parser/parse (load-file-result :text)) - errors (parse-result :errors)] - (if errors - {:errors (errors/format-command-errors command errors)} - {:command [schedule-tasks (parse-result :tasks) (date/today)]})))) + parse-errors (parse-result :errors)] + (if (any? parse-errors) + {:errors (errors/format-command-errors command parse-errors)} + {:command [schedule-tasks (parse-result :tasks) (date/today)] + :errors []})))) {})) diff --git a/src/errors.janet b/src/errors.janet index 781249d..b19f355 100644 --- a/src/errors.janet +++ b/src/errors.janet @@ -5,12 +5,15 @@ ## Public Interface (def exit-status-codes - {:error 1 + {:ok 0 + :error 1 :plan-path-missing 2 :file-error 3 :parse-error 4 :command-error 5}) +(def no-error [[] (exit-status-codes :ok)]) + (defn format-command-errors [command errors] (map (fn [error] (string command " " (string/ascii-lower error))) errors)) diff --git a/src/file_repository.janet b/src/file_repository.janet index 0d7c52c..e30fa9c 100644 --- a/src/file_repository.janet +++ b/src/file_repository.janet @@ -29,4 +29,5 @@ [path] (if (= (os/stat path) nil) {:errors ["File does not exist"]} - {:text (string (file/read (file/open path) :all))})) + {:text (string (file/read (file/open path) :all)) + :errors []})) diff --git a/src/schedule_parser.janet b/src/schedule_parser.janet index f9151e2..d765195 100644 --- a/src/schedule_parser.janet +++ b/src/schedule_parser.janet @@ -41,7 +41,7 @@ {:errors ["Schedule is empty"]} (do (if (= (length tasks) (task-lines-count schedule-string)) - {:tasks tasks} + {:tasks tasks :errors []} {:errors [(string "Schedule can not be parsed - last parsed task is \"" ((last tasks) :title) "\"" diff --git a/test/alas_test.janet b/test/alas_test.janet new file mode 100644 index 0000000..4fc6f2c --- /dev/null +++ b/test/alas_test.janet @@ -0,0 +1,24 @@ +(use judge) + +(import ../src/alas) + +## ————————————————————————————————————————————————————————————————————————————————————————————————— +## Test run-with-file-path + +(deftest "returns exit status 0 when there were no errors" + (let [arguments {"skip-backup" true "stats" true} + [errors exit-status] (alas/run-with-file-path arguments "./test/examples/todo.md")] + (test (empty? errors) true) + (test exit-status 0))) + +(deftest "returns exit status 3 when there are file errors" + (let [arguments {"skip-backup" true "stats" true} + [errors exit-status] (alas/run-with-file-path arguments "./test/examples/missing-todo.md")] + (test (empty? errors) false) + (test exit-status 3))) + +(deftest "returns exit status 4 when there are parsing errors" + (let [arguments {"skip-backup" true "stats" true} + [errors exit-status] (alas/run-with-file-path arguments "./test/examples/unparsable-todo.md")] + (test (empty? errors) false) + (test exit-status 4))) diff --git a/test/examples/unparsable-todo.md b/test/examples/unparsable-todo.md new file mode 100644 index 0000000..5663fed --- /dev/null +++ b/test/examples/unparsable-todo.md @@ -0,0 +1,11 @@ +# Main TODO + +# 2020-08-01, Saturday + +- [ ] Develop photos for the grandmother +- [X] Pay bills + +# 2020-07-31, Friday + +- [X] Review open pull requests +- [X] Fix flaky test From dc2b31bb084d8ab41f8803b31a752126e2730e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Neboj=C5=A1a=20Stri=C4=8Devi=C4=87?= Date: Wed, 10 Apr 2024 07:28:12 +0200 Subject: [PATCH 2/3] Add tests for run-with-arguments --- src/alas.janet | 2 +- test/alas_test.janet | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/alas.janet b/src/alas.janet index 27fda33..0383f00 100644 --- a/src/alas.janet +++ b/src/alas.janet @@ -57,7 +57,7 @@ (file_repository/save new-plan-string file-path) errors/no-error))))))) -(defn- run-with-arguments [arguments] +(defn run-with-arguments [arguments] (def file-path (arguments :default)) (if file-path (run-with-file-path arguments file-path) diff --git a/test/alas_test.janet b/test/alas_test.janet index 4fc6f2c..2261eaf 100644 --- a/test/alas_test.janet +++ b/test/alas_test.janet @@ -22,3 +22,18 @@ [errors exit-status] (alas/run-with-file-path arguments "./test/examples/unparsable-todo.md")] (test (empty? errors) false) (test exit-status 4))) + +## ————————————————————————————————————————————————————————————————————————————————————————————————— +## Test run-with-arguments + +(deftest "returns exit status 0 when there were no errors" + (let [arguments {"skip-backup" true "stats" true :default "./test/examples/todo.md"} + [errors exit-status] (alas/run-with-arguments arguments)] + (test (empty? errors) true) + (test exit-status 0))) + +(deftest "returns exit status 2 when when the file path is missing" + (let [arguments {"skip-backup" true "stats" true} + [errors exit-status] (alas/run-with-arguments arguments)] + (test (empty? errors) false) + (test exit-status 2))) From e4c35ef852d703d1fd687f758bb414bc9eb453b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Neboj=C5=A1a=20Stri=C4=8Devi=C4=87?= Date: Wed, 10 Apr 2024 07:29:43 +0200 Subject: [PATCH 3/3] Switch to Ubuntu 22.04 on Semaphore --- .semaphore/semaphore.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.semaphore/semaphore.yml b/.semaphore/semaphore.yml index eef529f..511a649 100644 --- a/.semaphore/semaphore.yml +++ b/.semaphore/semaphore.yml @@ -2,8 +2,8 @@ version: v1.0 name: Alas Tests agent: machine: - type: e1-standard-2 - os_image: ubuntu1804 + type: e2-standard-2 + os_image: ubuntu2204 blocks: - name: Test task: