From da0507db0118e2f039dc84733d45f695be5109e6 Mon Sep 17 00:00:00 2001 From: bwireman Date: Sat, 6 Jul 2024 11:20:28 -0500 Subject: [PATCH] error on invalid kinds --- scripts/test.sh | 1 - src/cactus/run.gleam | 29 +++++++++++++++++++---------- src/cactus/util.gleam | 5 +++-- test/run_test.gleam | 7 +++++++ test/testdata/gleam/invalid.toml | 2 ++ 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/scripts/test.sh b/scripts/test.sh index ac0bdd4..b60aa41 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -6,7 +6,6 @@ GREEN='\033[0;32m' NC='\033[0m' gleam check -gleam update gleam build gleam format diff --git a/src/cactus/run.gleam b/src/cactus/run.gleam index 4eccf80..11bc976 100644 --- a/src/cactus/run.gleam +++ b/src/cactus/run.gleam @@ -11,6 +11,8 @@ import tom.{type Toml} const actions = "actions" +const kind = "kind" + const gleam = "gleam" pub type ActionKind { @@ -28,7 +30,7 @@ pub fn parse_action(raw: Toml) -> Result(Action, CactusErr) { tom.InlineTable(t) -> { use command <- try(as_invalid_field_err(tom.get_string(t, ["command"]))) let kind = - tom.get_string(t, ["kind"]) + tom.get_string(t, [kind]) |> result.map(string.lowercase) |> result.unwrap("module") @@ -39,15 +41,23 @@ pub fn parse_action(raw: Toml) -> Result(Action, CactusErr) { |> result.all(), ) - let action_kind = case kind { - "sub_command" -> SubCommand - "binary" -> Binary - _ -> Module - } + use action_kind <- try(case kind { + "sub_command" -> Ok(SubCommand) + "binary" -> Ok(Binary) + "module" -> Ok(Module) + _ -> + Error(InvalidFieldCustomErr( + kind, + "got: " + <> util.quote(kind) + <> "expected: one of 'sub_command', 'binary', 'module'", + )) + }) Ok(Action(command: command, kind: action_kind, args: args)) } - _ -> Error(InvalidFieldCustomErr(actions)) + _ -> + Error(InvalidFieldCustomErr(actions, "'actions' was not an InlineTable")) } } @@ -90,8 +100,7 @@ pub fn run(path: String, action: String) -> Result(List(String), CactusErr) { pub fn as_string(t: Toml) -> Result(String, CactusErr) { case t { tom.String(v) -> Ok(v) - _ -> { - Error(InvalidFieldCustomErr("args")) - } + _ -> + Error(InvalidFieldCustomErr("args", "'args' was not a list of strings")) } } diff --git a/src/cactus/util.gleam b/src/cactus/util.gleam index a4fc5cd..aca4c88 100644 --- a/src/cactus/util.gleam +++ b/src/cactus/util.gleam @@ -6,7 +6,7 @@ import tom.{type GetError, type Toml, NotFound, WrongType} pub type CactusErr { InvalidFieldErr(err: GetError) - InvalidFieldCustomErr(field: String) + InvalidFieldCustomErr(field: String, err: String) InvalidTomlErr ActionFailedErr(output: String) FSErr(path: String, err: FileError) @@ -46,7 +46,8 @@ pub fn str(err: CactusErr) -> String { <> quote(expected) <> " got " <> quote(got) - InvalidFieldCustomErr(field) -> "Invalid field in config: " <> field + InvalidFieldCustomErr(field, err) -> + "Invalid field in config: " <> field <> " " <> err InvalidTomlErr -> "Invalid Toml Error" ActionFailedErr(output) -> "Action Failed Error:\n" <> output FSErr(path, err) -> diff --git a/test/run_test.gleam b/test/run_test.gleam index 536cd84..c665926 100644 --- a/test/run_test.gleam +++ b/test/run_test.gleam @@ -41,6 +41,13 @@ pub fn parse_action_test() { should.be_error(d) + let assert [e] = + run.get_actions("test/testdata/gleam/invalid.toml", "kind-wrong-type") + |> should.be_ok + |> list.map(run.parse_action) + + should.be_error(e) + run.get_actions("test/testdata/gleam/too_many.toml", "pre-merge-commit") |> should.be_ok |> should.equal([]) diff --git a/test/testdata/gleam/invalid.toml b/test/testdata/gleam/invalid.toml index 0b8b760..9e7fa01 100644 --- a/test/testdata/gleam/invalid.toml +++ b/test/testdata/gleam/invalid.toml @@ -5,4 +5,6 @@ actions = true [cactus.no-command] actions = [{}] +[cactus.kind-wrong-type] +actions = [{command = "echo", kind = "foo"}]