cobracmd: standard completion and exit codes for cobra programs; bash install line via eval
New module yetano.dev/yetano/core/cobracmd (own go.mod, cobra stays out of the main module): Completion replaces cobra's default completion command, which exits 0 for an unknown shell, with one that accepts only bash|zsh|fish|powershell (exit 2 otherwise); Execute maps cobra's usage errors to exit 2, other errors to 1. completion: the bash script's install line is eval "$(prog completion bash)" -- bash 3.2 on macOS cannot source a process substitution. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fa52990e3e
commit
5288c4ce74
6 files changed
+317
-1
No files matched your search
@@ -0,0 +1,106 @@
|
||||
package cobracmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func newRoot() *cobra.Command {
|
||||
root := &cobra.Command{Use: "yatool", SilenceUsage: true, SilenceErrors: true}
|
||||
root.AddCommand(&cobra.Command{Use: "serve", RunE: func(*cobra.Command, []string) error { return nil }})
|
||||
root.AddCommand(&cobra.Command{Use: "one <arg>", Args: cobra.ExactArgs(1), RunE: func(*cobra.Command, []string) error { return nil }})
|
||||
root.AddCommand(&cobra.Command{Use: "fail", RunE: func(*cobra.Command, []string) error { return errors.New("broken") }})
|
||||
root.AddCommand(&cobra.Command{Use: "bad", RunE: func(*cobra.Command, []string) error { return Usage(errors.New("wrong")) }})
|
||||
Completion(root)
|
||||
return root
|
||||
}
|
||||
|
||||
func run(args ...string) (int, string) {
|
||||
root := newRoot()
|
||||
var out bytes.Buffer
|
||||
root.SetOut(&out)
|
||||
root.SetErr(&out)
|
||||
root.SetArgs(args)
|
||||
return Execute(root), out.String()
|
||||
}
|
||||
|
||||
func TestExitCodes(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
args []string
|
||||
want int
|
||||
}{
|
||||
{[]string{"serve"}, ExitOK},
|
||||
{[]string{"fail"}, ExitProblem},
|
||||
{[]string{"bad"}, ExitUsage},
|
||||
{[]string{"nosuch"}, ExitUsage},
|
||||
{[]string{"serve", "--nosuch"}, ExitUsage},
|
||||
{[]string{"one"}, ExitUsage},
|
||||
{[]string{"one", "a", "b"}, ExitUsage},
|
||||
{[]string{"completion"}, ExitUsage},
|
||||
{[]string{"completion", "tcsh"}, ExitUsage},
|
||||
{[]string{"completion", "bash", "zsh"}, ExitUsage},
|
||||
} {
|
||||
if got, out := run(tc.args...); got != tc.want {
|
||||
t.Errorf("%v: exit %d, want %d (%s)", tc.args, got, tc.want, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompletionScripts(t *testing.T) {
|
||||
for shell, want := range map[string][]string{
|
||||
"bash": {`eval "$(yatool completion bash)"`, "__start_yatool"},
|
||||
"zsh": {"#compdef yatool", "source <(yatool completion zsh)"},
|
||||
"fish": {"yatool completion fish | source", "complete -c yatool"},
|
||||
"powershell": {"Invoke-Expression", "Register-ArgumentCompleter"},
|
||||
} {
|
||||
code, out := run("completion", shell)
|
||||
if code != ExitOK {
|
||||
t.Fatalf("%s: exit %d", shell, code)
|
||||
}
|
||||
for _, w := range want {
|
||||
if !strings.Contains(out, w) {
|
||||
t.Errorf("%s script lacks %q", shell, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, out := run("completion", "zsh"); !strings.HasPrefix(out, "#compdef") {
|
||||
t.Errorf("zsh script must start with #compdef: %.40q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompletionReplacesDefault(t *testing.T) {
|
||||
root := newRoot()
|
||||
n := 0
|
||||
for _, c := range root.Commands() {
|
||||
if c.Name() == "completion" {
|
||||
n++
|
||||
}
|
||||
}
|
||||
if n != 1 {
|
||||
t.Fatalf("%d completion commands, want 1", n)
|
||||
}
|
||||
// Calling it twice (e.g. a shared root builder) still leaves one.
|
||||
Completion(root)
|
||||
n = 0
|
||||
for _, c := range root.Commands() {
|
||||
if c.Name() == "completion" {
|
||||
n++
|
||||
}
|
||||
}
|
||||
if n != 1 {
|
||||
t.Fatalf("after a second Completion: %d completion commands, want 1", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsUsage(t *testing.T) {
|
||||
if IsUsage(nil) || IsUsage(errors.New("disk full")) {
|
||||
t.Error("plain errors are no usage errors")
|
||||
}
|
||||
if !IsUsage(Usage(errors.New("x"))) || Usage(nil) != nil {
|
||||
t.Error("Usage wrapping")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user