106 lines
2.9 KiB
Go
106 lines
2.9 KiB
Go
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")
|
||
|
|
}
|
||
|
|
}
|