Skip to content

Commit

Permalink
Suggestions cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Dec 7, 2024
1 parent 83e6c43 commit 239fddc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
4 changes: 3 additions & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strings"
"unicode"
"unicode/utf8"

"github.com/xo/ox/text"
)

// Command is a command.
Expand Down Expand Up @@ -153,7 +155,7 @@ func (cmd *Command) Suggest(args ...string) error {
}
}
}
return fmt.Errorf("%w %q", ErrUnknownCommand, args[0])
return fmt.Errorf(text.SuggestionErrorMessage, ErrUnknownCommand, args[0], cmd.Name)
}

// Lookup returns the furthest matching command in the command tree.
Expand Down
17 changes: 9 additions & 8 deletions ox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,34 @@ func TestSuggestions(t *testing.T) {
args []string
exp string
}{
{ss("on"), `error: unknown command "on" for "cmd"
{
ss("subfoo"), `error: unknown command "subfoo" for "cmd"`,
},
{
ss("on"), `error: unknown command "on" for "cmd"
Did you mean this?
one
`},
`,
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
var code int
c := testContext(t, &code, "on")
c := testContext(t, &code, test.args...)
if err := c.Parse(); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
err := c.Run(context.Background())
if err == nil {
t.Fatalf("expected non-nil error")
}
if s, exp := err.Error(), `unknown command "on" for "cmd"`; s != exp {
t.Fatalf("expected %q, got: %q", exp, s)
}
if !c.Handler(err) {
t.Fatalf("expected Handler to return true")
}
if code == 0 {
t.Fatalf("expected Handler to set non-zero code")
}
if s := c.Stderr.(*bytes.Buffer).String(); s != test.exp {
if s := strings.TrimSuffix(c.Stderr.(*bytes.Buffer).String(), "\n"); s != test.exp {
t.Errorf("\nexpected:\n%s\ngot:\n%s", test.exp, s)
}
})
Expand Down

0 comments on commit 239fddc

Please sign in to comment.