Skip to content

Commit

Permalink
More tests and write usage to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
aburdulescu committed Dec 12, 2023
1 parent 240921f commit f4b9263
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 9 deletions.
12 changes: 6 additions & 6 deletions internal/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func printAppVersion() {

func (a *App) Run(args ...string) error {
fset := flag.NewFlagSet("pocryp", flag.ExitOnError)
fset.SetOutput(os.Stdout)
fset.Usage = a.Usage
fset.BoolVar(&a.printVersion, "version", false, "")
if err := fset.Parse(args); err != nil {
Expand Down Expand Up @@ -122,8 +123,7 @@ func (a App) maxCommandName(category string) int {
}

func (a App) Usage() {
w := os.Stderr
fmt.Fprint(w, `Usage: pocryp command [ARGS]
fmt.Print(`Usage: pocryp command [ARGS]
Flags:
-h, --help Print this message
Expand All @@ -133,13 +133,13 @@ Commands(by category):
`)
for _, v := range a.categories {
fmt.Fprintf(w, "%s:\n", v.name)
fmt.Printf("%s:\n", v.name)
mlen := a.maxCommandName(v.name)
for _, cmd := range v.commands {
padding := strings.Repeat(" ", mlen-len(cmd.Name))
fmt.Fprintf(w, " %s%s %s\n", cmd.Name, padding, cmd.Brief)
fmt.Printf(" %s%s %s\n", cmd.Name, padding, cmd.Brief)
}
fmt.Fprint(w, "\n")
fmt.Print("\n")
}
fmt.Fprint(w, "Run 'pocryp command -h' for more information about a command.\n")
fmt.Print("Run 'pocryp command -h' for more information about a command.\n")
}
7 changes: 4 additions & 3 deletions internal/cli/cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ func (c *Command) Init() {
panic("cmd: missing Brief")
}
c.Flags = flag.NewFlagSet(c.Name, flag.ContinueOnError)
c.Flags.SetOutput(os.Stdout)
c.Flags.Usage = func() {
fmt.Fprint(os.Stderr, c.Usage)
fmt.Fprintln(os.Stderr, "Options:")
fmt.Print(c.Usage)
fmt.Println("Options:")
c.Flags.PrintDefaults()
fmt.Fprint(os.Stderr, "\n")
fmt.Print("\n")
}
}
100 changes: 100 additions & 0 deletions internal/cli/cmd/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cmd

import (
"testing"
)

func TestParse(t *testing.T) {
c := Command{
Run: func(*Command) error { return nil },
Name: "foo",
Usage: "bar",
Brief: "baz",
}

c.Init()

t.Run("Err", func(t *testing.T) {
c.Args = []string{"-foo"}
if err := c.Parse(); err == nil {
t.Fatal("expected error")
}
})

t.Run("Ok", func(t *testing.T) {
c.Args = []string{"foo", "bar"}
if err := c.Parse(); err != nil {
t.Fatal(err)
}
})
}

func TestInit(t *testing.T) {
tests := []struct {
name string
msg string
cmd Command
}{
{
name: "NoRun",
msg: "cmd: missing Run",
cmd: Command{},
},

{
name: "NoName",
msg: "cmd: missing Name",
cmd: Command{
Run: func(*Command) error { return nil },
},
},

{
name: "NoUsage",
msg: "cmd: missing Usage",
cmd: Command{
Run: func(*Command) error { return nil },
Name: "foo",
},
},

{
name: "NoBrief",
msg: "cmd: missing Brief",
cmd: Command{
Run: func(*Command) error { return nil },
Name: "foo",
Usage: "bar",
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
defer expectPanic(t, test.msg)
test.cmd.Init()
})
}

t.Run("Ok", func(t *testing.T) {
c := Command{
Run: func(*Command) error { return nil },
Name: "foo",
Usage: "bar",
Brief: "baz",
}
c.Init()
c.Flags.Usage()
})
}

func expectPanic(t *testing.T, msg string) {
t.Helper()
r := recover()
if r == nil {
t.Fatal("expected panic")
}
if r != msg {
t.Fatalf("want %q, have %q", msg, r)
}
}
File renamed without changes.

0 comments on commit f4b9263

Please sign in to comment.