Skip to content

Commit

Permalink
Add config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwhitaker committed Jul 27, 2024
1 parent 0b2fab2 commit 47d64a8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
5 changes: 0 additions & 5 deletions .runny.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,3 @@ commands:
needs:
- demo-broken
run: ls

demo-unknown-dependency:
needs:
- unknown
run: ls
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/dominikbraun/graph v0.23.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMU
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dominikbraun/graph v0.23.0 h1:TdZB4pPqCLFxYhdyMFb1TBdFxp8XLcJfTTBQucVPgCo=
github.com/dominikbraun/graph v0.23.0/go.mod h1:yOjYyogZLY1LSG9E33JWZJiq5k83Qy2C6POAuiViluc=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
Expand Down
7 changes: 7 additions & 0 deletions runny/fixtures/invalid-circular-dependency.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
commands:
foo:
needs:
- bar
bar:
needs:
- foo
28 changes: 27 additions & 1 deletion runny/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/dominikbraun/graph"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -37,5 +38,30 @@ func readConfig(path string) (Config, error) {
}
return conf, err
}
return conf, nil

return conf, conf.validate()
}

func (c *Config) validate() error {
hash := func(c CommandName) string {
return string(c)
}
g := graph.New(hash, graph.Directed(), graph.PreventCycles())

for cmdName := range c.Commands {
err := g.AddVertex(cmdName)
if err != nil {
return fmt.Errorf("error declaring command %s: %v", cmdName, err)
}
}

for cmdName, cmd := range c.Commands {
for _, needsName := range cmd.Needs {
err := g.AddEdge(hash(cmdName), hash(needsName))
if err != nil {
return fmt.Errorf("error declaring %s as dependency of %s: %v", needsName, cmdName, err)
}
}
}
return nil
}
11 changes: 11 additions & 0 deletions runny/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ func TestReadInvalidConfig(t *testing.T) {
t.Fatalf("unexpected error message: %v", err)
}
}

func TestConfigWithCircularDependency(t *testing.T) {
_, err := readConfig("fixtures/invalid-circular-dependency.yaml")
if err == nil {
t.Fatalf("Expected an error when reading invalid config, but reading was succeeded")
}

if !strings.Contains(err.Error(), "edge would create a cycle") {
t.Fatalf("unexpected error message: %v", err)
}
}

0 comments on commit 47d64a8

Please sign in to comment.