Skip to content

Commit

Permalink
error on invalid kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
bwireman committed Jul 6, 2024
1 parent 3b0cc1d commit da0507d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
1 change: 0 additions & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ GREEN='\033[0;32m'
NC='\033[0m'

gleam check
gleam update
gleam build
gleam format

Expand Down
29 changes: 19 additions & 10 deletions src/cactus/run.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import tom.{type Toml}

const actions = "actions"

const kind = "kind"

const gleam = "gleam"

pub type ActionKind {
Expand All @@ -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")

Expand All @@ -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"))
}
}

Expand Down Expand Up @@ -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"))
}
}
5 changes: 3 additions & 2 deletions src/cactus/util.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) ->
Expand Down
7 changes: 7 additions & 0 deletions test/run_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand Down
2 changes: 2 additions & 0 deletions test/testdata/gleam/invalid.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ actions = true
[cactus.no-command]
actions = [{}]

[cactus.kind-wrong-type]
actions = [{command = "echo", kind = "foo"}]

0 comments on commit da0507d

Please sign in to comment.