fix(bonfire): recognise cobra's unknown command error once in Execute (IN-03, quick-261028-rqd)

This commit is contained in:
Jakub Zych
2026-10-28 20:23:39 +01:00
parent 8476a65aa0
commit 39c59b8329
5 changed files with 75 additions and 13 deletions

View File

@@ -342,3 +342,34 @@ func TestSanitize(t *testing.T) {
t.Errorf("Sanitize(%q) = %q, want it unchanged", clean, got)
}
}
// TestPluginErrorWordedUnknownCommand (IN-03): only cobra's own unknown
// command error is a usage error. A command whose Run returns an error
// worded the same way exits 1 and is printed verbatim, while a mistyped
// command still exits 2 with `Command "x" is not defined.` and cobra's
// suggestions as Try lines.
func TestPluginErrorWordedUnknownCommand(t *testing.T) {
plainEnv(t)
commands := []Command{
{Name: "migrate", Description: "Migrate", Run: func(context.Context, Input, Output) error {
return errors.New(`unknown command "x" for "acme"`)
}},
}
_, errOut, code := runGolden(t, commands, "migrate")
if code != ExitFailure {
t.Errorf("plugin error: exit %d, want %d\n%s", code, ExitFailure, errOut)
}
if !strings.Contains(errOut, `unknown command "x" for "acme"`) || strings.Contains(errOut, "is not defined") {
t.Errorf("plugin error not printed verbatim:\n%s", errOut)
}
_, errOut, code = runGolden(t, commands, "migrat")
if code != ExitUsage {
t.Errorf("migrat: exit %d, want %d\n%s", code, ExitUsage, errOut)
}
for _, want := range []string{`Command "migrat" is not defined.`, "Try:", "bin/acme migrate"} {
if !strings.Contains(errOut, want) {
t.Errorf("migrat: block lacks %q:\n%s", want, errOut)
}
}
}