Skip to content

Commit

Permalink
Merge pull request #135 from hackberrydev/exit-code
Browse files Browse the repository at this point in the history
Refactor and fix print errors and exit status
  • Loading branch information
strika authored Apr 10, 2024
2 parents d80d85f + e4c35ef commit fb7d9f8
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 18 additions & 14 deletions src/alas.janet
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,43 @@
: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]
(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

(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)))))
15 changes: 8 additions & 7 deletions src/commands/schedule_tasks.janet
Original file line number Diff line number Diff line change
Expand Up @@ -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 []}))))
{}))
5 changes: 4 additions & 1 deletion src/errors.janet
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
3 changes: 2 additions & 1 deletion src/file_repository.janet
Original file line number Diff line number Diff line change
Expand Up @@ -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 []}))
2 changes: 1 addition & 1 deletion src/schedule_parser.janet
Original file line number Diff line number Diff line change
Expand Up @@ -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)
"\""
Expand Down
39 changes: 39 additions & 0 deletions test/alas_test.janet
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(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)))

## —————————————————————————————————————————————————————————————————————————————————————————————————
## 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)))
11 changes: 11 additions & 0 deletions test/examples/unparsable-todo.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fb7d9f8

Please sign in to comment.