Skip to content

Commit fb7d9f8

Browse files
authored
Merge pull request #135 from hackberrydev/exit-code
Refactor and fix print errors and exit status
2 parents d80d85f + e4c35ef commit fb7d9f8

8 files changed

Lines changed: 85 additions & 26 deletions

File tree

.semaphore/semaphore.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ version: v1.0
22
name: Alas Tests
33
agent:
44
machine:
5-
type: e1-standard-2
6-
os_image: ubuntu1804
5+
type: e2-standard-2
6+
os_image: ubuntu2204
77
blocks:
88
- name: Test
99
task:

src/alas.janet

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,43 @@
3636
:help "Output version information."}
3737
:default {:kind :option}])
3838

39-
(defn- run-with-file-path [arguments file-path]
39+
(defn run-with-file-path [arguments file-path]
4040
(def load-file-result (file_repository/load file-path))
41-
(def errors (load-file-result :errors))
42-
(if errors
43-
(errors/print-errors errors (errors/exit-status-codes :file-error))
41+
(def file-errors (load-file-result :errors))
42+
(if (any? file-errors)
43+
[file-errors (errors/exit-status-codes :file-error)]
4444
(let [plan-string (load-file-result :text)
4545
parse-result (plan_parser/parse plan-string)
4646
parse-errors (parse-result :errors)
4747
plan (parse-result :plan)]
48-
(if (empty? parse-errors)
48+
(if (any? parse-errors)
49+
[parse-errors (errors/exit-status-codes :parse-error)]
4950
(let [{:plan new-plan :errors run-errors} (run-commands plan file-path arguments)]
50-
(if (empty? run-errors)
51+
(if (any? run-errors)
52+
[run-errors (errors/exit-status-codes :command-error)]
5153
(let [serialize-empty-inbox (plan_parser/serialize-empty-inbox? plan-string)
5254
new-plan-string (plan_serializer/serialize
5355
new-plan
5456
{:serialize-empty-inbox serialize-empty-inbox})]
55-
(file_repository/save new-plan-string file-path))
56-
(errors/print-errors run-errors (errors/exit-status-codes :command-error)))
57-
(errors/print-errors parse-errors (errors/exit-status-codes :parse-error)))))))
57+
(file_repository/save new-plan-string file-path)
58+
errors/no-error)))))))
5859

59-
(defn- run-with-arguments [arguments]
60+
(defn run-with-arguments [arguments]
6061
(def file-path (arguments :default))
6162
(if file-path
6263
(run-with-file-path arguments file-path)
6364
(if (arguments "version")
64-
(print-version)
65-
(errors/print-errors ["Plan file path is missing"]
66-
(errors/exit-status-codes :plan-path-missing)))))
65+
(do
66+
(print-version)
67+
errors/no-error)
68+
[["Plan file path is missing"]
69+
(errors/exit-status-codes :plan-path-missing)])))
6770

6871
## —————————————————————————————————————————————————————————————————————————————————————————————————
6972
## Public Interface
7073

7174
(defn main [& args]
7275
(def arguments (argparse ;argparse-params))
7376
(if arguments
74-
(run-with-arguments arguments)))
77+
(let [[errors exit-code] (run-with-arguments arguments)]
78+
(errors/print-errors errors (errors/exit-status-codes exit-code)))))

src/commands/schedule_tasks.janet

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@
8383
(def argument (arguments "schedule-tasks"))
8484
(if argument
8585
(let [load-file-result (file_repository/load argument)
86-
errors (load-file-result :errors)]
87-
(if errors
88-
{:errors (errors/format-command-errors command errors)}
86+
file-errors (load-file-result :errors)]
87+
(if (any? file-errors)
88+
{:errors (errors/format-command-errors command file-errors)}
8989
(let [parse-result (schedule_parser/parse (load-file-result :text))
90-
errors (parse-result :errors)]
91-
(if errors
92-
{:errors (errors/format-command-errors command errors)}
93-
{:command [schedule-tasks (parse-result :tasks) (date/today)]}))))
90+
parse-errors (parse-result :errors)]
91+
(if (any? parse-errors)
92+
{:errors (errors/format-command-errors command parse-errors)}
93+
{:command [schedule-tasks (parse-result :tasks) (date/today)]
94+
:errors []}))))
9495
{}))

src/errors.janet

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
## Public Interface
66

77
(def exit-status-codes
8-
{:error 1
8+
{:ok 0
9+
:error 1
910
:plan-path-missing 2
1011
:file-error 3
1112
:parse-error 4
1213
:command-error 5})
1314

15+
(def no-error [[] (exit-status-codes :ok)])
16+
1417
(defn format-command-errors [command errors]
1518
(map (fn [error] (string command " " (string/ascii-lower error))) errors))
1619

src/file_repository.janet

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@
2929
[path]
3030
(if (= (os/stat path) nil)
3131
{:errors ["File does not exist"]}
32-
{:text (string (file/read (file/open path) :all))}))
32+
{:text (string (file/read (file/open path) :all))
33+
:errors []}))

src/schedule_parser.janet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
{:errors ["Schedule is empty"]}
4242
(do
4343
(if (= (length tasks) (task-lines-count schedule-string))
44-
{:tasks tasks}
44+
{:tasks tasks :errors []}
4545
{:errors [(string "Schedule can not be parsed - last parsed task is \""
4646
((last tasks) :title)
4747
"\""

test/alas_test.janet

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
(use judge)
2+
3+
(import ../src/alas)
4+
5+
## —————————————————————————————————————————————————————————————————————————————————————————————————
6+
## Test run-with-file-path
7+
8+
(deftest "returns exit status 0 when there were no errors"
9+
(let [arguments {"skip-backup" true "stats" true}
10+
[errors exit-status] (alas/run-with-file-path arguments "./test/examples/todo.md")]
11+
(test (empty? errors) true)
12+
(test exit-status 0)))
13+
14+
(deftest "returns exit status 3 when there are file errors"
15+
(let [arguments {"skip-backup" true "stats" true}
16+
[errors exit-status] (alas/run-with-file-path arguments "./test/examples/missing-todo.md")]
17+
(test (empty? errors) false)
18+
(test exit-status 3)))
19+
20+
(deftest "returns exit status 4 when there are parsing errors"
21+
(let [arguments {"skip-backup" true "stats" true}
22+
[errors exit-status] (alas/run-with-file-path arguments "./test/examples/unparsable-todo.md")]
23+
(test (empty? errors) false)
24+
(test exit-status 4)))
25+
26+
## —————————————————————————————————————————————————————————————————————————————————————————————————
27+
## Test run-with-arguments
28+
29+
(deftest "returns exit status 0 when there were no errors"
30+
(let [arguments {"skip-backup" true "stats" true :default "./test/examples/todo.md"}
31+
[errors exit-status] (alas/run-with-arguments arguments)]
32+
(test (empty? errors) true)
33+
(test exit-status 0)))
34+
35+
(deftest "returns exit status 2 when when the file path is missing"
36+
(let [arguments {"skip-backup" true "stats" true}
37+
[errors exit-status] (alas/run-with-arguments arguments)]
38+
(test (empty? errors) false)
39+
(test exit-status 2)))

test/examples/unparsable-todo.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Main TODO
2+
3+
# 2020-08-01, Saturday
4+
5+
- [ ] Develop photos for the grandmother
6+
- [X] Pay bills
7+
8+
# 2020-07-31, Friday
9+
10+
- [X] Review open pull requests
11+
- [X] Fix flaky test

0 commit comments

Comments
 (0)