Skip to content

Commit

Permalink
Add tests for dupe flags and commands and other confusing things
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Aug 21, 2022
1 parent 317df91 commit c8a6060
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
8 changes: 6 additions & 2 deletions main.go
Expand Up @@ -20,7 +20,7 @@ type appModifier func(app *cli.App)

var appModifiers []appModifier

func cliMain(args ...string) error {
func buildApp() *cli.App {
app := cli.NewApp()

app.Usage = "A fast regex parser, extractor and realtime aggregator"
Expand Down Expand Up @@ -116,7 +116,11 @@ func cliMain(args ...string) error {
modifier(app)
}

return app.Run(args)
return app
}

func cliMain(args ...string) error {
return buildApp().Run(args)
}

func main() {
Expand Down
35 changes: 35 additions & 0 deletions main_test.go
Expand Up @@ -12,3 +12,38 @@ func TestMain(t *testing.T) {
assert.Error(t, cliMain("main"))
assert.NoError(t, cliMain("main", "--help"))
}

func TestDupeCommands(t *testing.T) {
commands := make(map[string]struct{})
app := buildApp()

for _, cmd := range app.Commands {
for _, name := range cmd.Names() {
assert.NotContains(t, commands, name)
commands[name] = struct{}{}
}
}
}

func TestDupeFlags(t *testing.T) {
app := buildApp()
for _, cmd := range app.Commands {
flags := make(map[string]struct{})

// Global (even though technically can dupe, it's confusing, so prevent)
for _, flag := range app.Flags {
for _, name := range flag.Names() {
assert.NotContains(t, flags, name)
flags[name] = struct{}{}
}
}

// And the specific flags
for _, flag := range cmd.Flags {
for _, name := range flag.Names() {
assert.NotContains(t, flags, name)
flags[name] = struct{}{}
}
}
}
}

0 comments on commit c8a6060

Please sign in to comment.