2017-07-30 08:42:35 +00:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-02-24 16:53:13 +00:00
|
|
|
"regexp"
|
2018-02-25 12:12:58 +00:00
|
|
|
"strings"
|
2017-07-30 08:42:35 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-02-24 16:53:13 +00:00
|
|
|
func TestGenZshCompletion(t *testing.T) {
|
|
|
|
var debug bool
|
|
|
|
var option string
|
|
|
|
|
2017-07-30 08:42:35 +00:00
|
|
|
tcs := []struct {
|
|
|
|
name string
|
|
|
|
root *Command
|
|
|
|
expectedExpressions []string
|
2018-03-04 22:46:00 +00:00
|
|
|
invocationArgs []string
|
|
|
|
skip string
|
2017-07-30 08:42:35 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-02-24 16:53:13 +00:00
|
|
|
name: "simple command",
|
|
|
|
root: func() *Command {
|
|
|
|
r := &Command{
|
|
|
|
Use: "mycommand",
|
|
|
|
Long: "My Command long description",
|
2018-02-25 06:20:34 +00:00
|
|
|
Run: emptyRun,
|
2018-02-24 16:53:13 +00:00
|
|
|
}
|
|
|
|
r.Flags().BoolVar(&debug, "debug", debug, "description")
|
|
|
|
return r
|
|
|
|
}(),
|
|
|
|
expectedExpressions: []string{
|
2018-03-02 06:42:52 +00:00
|
|
|
`(?s)function _mycommand {\s+_arguments \\\s+'--debug\[description\]'.*--help.*}`,
|
2018-02-24 16:53:13 +00:00
|
|
|
"#compdef _mycommand mycommand",
|
|
|
|
},
|
2017-07-30 08:42:35 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-24 16:53:13 +00:00
|
|
|
name: "flags with both long and short flags",
|
2017-07-30 08:42:35 +00:00
|
|
|
root: func() *Command {
|
2018-02-24 16:53:13 +00:00
|
|
|
r := &Command{
|
|
|
|
Use: "testcmd",
|
|
|
|
Long: "long description",
|
2018-02-25 06:20:34 +00:00
|
|
|
Run: emptyRun,
|
2018-02-24 16:53:13 +00:00
|
|
|
}
|
|
|
|
r.Flags().BoolVarP(&debug, "debug", "d", debug, "debug description")
|
2017-07-30 08:42:35 +00:00
|
|
|
return r
|
|
|
|
}(),
|
2018-02-24 16:53:13 +00:00
|
|
|
expectedExpressions: []string{
|
2018-03-02 06:42:52 +00:00
|
|
|
`'\(-d --debug\)'{-d,--debug}'\[debug description\]'`,
|
2018-02-24 16:53:13 +00:00
|
|
|
},
|
2017-07-30 08:42:35 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-28 15:04:17 +00:00
|
|
|
name: "command with subcommands and flags with values",
|
2017-07-30 08:42:35 +00:00
|
|
|
root: func() *Command {
|
2018-02-24 16:53:13 +00:00
|
|
|
r := &Command{
|
|
|
|
Use: "rootcmd",
|
|
|
|
Long: "Long rootcmd description",
|
|
|
|
}
|
|
|
|
d := &Command{
|
|
|
|
Use: "subcmd1",
|
|
|
|
Short: "Subcmd1 short descrition",
|
2018-02-25 06:20:34 +00:00
|
|
|
Run: emptyRun,
|
2018-02-24 16:53:13 +00:00
|
|
|
}
|
|
|
|
e := &Command{
|
|
|
|
Use: "subcmd2",
|
|
|
|
Long: "Subcmd2 short description",
|
2018-02-25 06:20:34 +00:00
|
|
|
Run: emptyRun,
|
2018-02-24 16:53:13 +00:00
|
|
|
}
|
|
|
|
r.PersistentFlags().BoolVar(&debug, "debug", debug, "description")
|
|
|
|
d.Flags().StringVarP(&option, "option", "o", option, "option description")
|
|
|
|
r.AddCommand(d, e)
|
2017-07-30 08:42:35 +00:00
|
|
|
return r
|
|
|
|
}(),
|
2018-02-24 16:53:13 +00:00
|
|
|
expectedExpressions: []string{
|
2018-02-26 20:31:06 +00:00
|
|
|
`commands=\(\n\s+"help:.*\n\s+"subcmd1:.*\n\s+"subcmd2:.*\n\s+\)`,
|
2018-03-02 06:42:52 +00:00
|
|
|
`_arguments \\\n.*'--debug\[description]'`,
|
|
|
|
`_arguments -C \\\n.*'--debug\[description]'`,
|
2018-02-24 16:53:13 +00:00
|
|
|
`function _rootcmd_subcmd1 {`,
|
|
|
|
`function _rootcmd_subcmd1 {`,
|
2018-03-02 06:42:52 +00:00
|
|
|
`_arguments \\\n.*'\(-o --option\)'{-o,--option}'\[option description]:' \\\n`,
|
2018-02-24 16:53:13 +00:00
|
|
|
},
|
2017-07-30 08:42:35 +00:00
|
|
|
},
|
|
|
|
{
|
2018-03-02 06:42:52 +00:00
|
|
|
name: "filename completion with and without globs",
|
2017-07-30 08:42:35 +00:00
|
|
|
root: func() *Command {
|
2018-02-24 16:53:13 +00:00
|
|
|
var file string
|
|
|
|
r := &Command{
|
|
|
|
Use: "mycmd",
|
|
|
|
Short: "my command short description",
|
2018-02-25 06:20:34 +00:00
|
|
|
Run: emptyRun,
|
2018-02-24 16:53:13 +00:00
|
|
|
}
|
|
|
|
r.Flags().StringVarP(&file, "config", "c", file, "config file")
|
2018-02-28 15:04:17 +00:00
|
|
|
r.MarkFlagFilename("config")
|
2018-03-02 06:42:52 +00:00
|
|
|
r.Flags().String("output", "", "output file")
|
|
|
|
r.MarkFlagFilename("output", "*.log", "*.txt")
|
2017-07-30 08:42:35 +00:00
|
|
|
return r
|
|
|
|
}(),
|
2018-02-24 16:53:13 +00:00
|
|
|
expectedExpressions: []string{
|
2018-03-02 06:42:52 +00:00
|
|
|
`\n +'\(-c --config\)'{-c,--config}'\[config file]:filename:_files'`,
|
|
|
|
`:_files -g "\*.log" -g "\*.txt"`,
|
2018-02-24 16:53:13 +00:00
|
|
|
},
|
2017-07-30 08:42:35 +00:00
|
|
|
},
|
2018-02-28 15:04:17 +00:00
|
|
|
{
|
|
|
|
name: "repeated variables both with and without value",
|
|
|
|
root: func() *Command {
|
|
|
|
r := genTestCommand("mycmd", true)
|
2018-02-28 19:52:35 +00:00
|
|
|
_ = r.Flags().BoolSliceP("debug", "d", []bool{}, "debug usage")
|
2018-02-28 15:04:17 +00:00
|
|
|
_ = r.Flags().StringArray("option", []string{}, "options")
|
|
|
|
return r
|
|
|
|
}(),
|
|
|
|
expectedExpressions: []string{
|
2018-03-02 06:42:52 +00:00
|
|
|
`'\*--option\[options]`,
|
|
|
|
`'\(\*-d \*--debug\)'{\\\*-d,\\\*--debug}`,
|
2018-02-28 15:04:17 +00:00
|
|
|
},
|
|
|
|
},
|
2018-03-04 21:56:31 +00:00
|
|
|
{
|
2018-03-04 22:46:00 +00:00
|
|
|
name: "generated flags --help and --version should be created even when not executing root cmd",
|
2018-03-04 21:56:31 +00:00
|
|
|
root: func() *Command {
|
|
|
|
r := &Command{
|
|
|
|
Use: "mycmd",
|
|
|
|
Short: "mycmd short description",
|
|
|
|
Version: "myversion",
|
|
|
|
}
|
|
|
|
s := genTestCommand("sub1", true)
|
|
|
|
r.AddCommand(s)
|
|
|
|
return s
|
|
|
|
}(),
|
|
|
|
expectedExpressions: []string{
|
|
|
|
"--version",
|
|
|
|
"--help",
|
|
|
|
},
|
2018-03-04 22:46:00 +00:00
|
|
|
invocationArgs: []string{
|
|
|
|
"sub1",
|
|
|
|
},
|
|
|
|
skip: "--version and --help are currently not generated when not running on root command",
|
2018-03-04 21:56:31 +00:00
|
|
|
},
|
2018-03-04 23:09:55 +00:00
|
|
|
{
|
|
|
|
name: "zsh generation should run on root commannd",
|
|
|
|
root: func() *Command {
|
|
|
|
r := genTestCommand("root", false)
|
|
|
|
s := genTestCommand("sub1", true)
|
|
|
|
r.AddCommand(s)
|
|
|
|
return s
|
|
|
|
}(),
|
|
|
|
expectedExpressions: []string{
|
|
|
|
"function _root {",
|
|
|
|
},
|
|
|
|
},
|
2018-03-17 18:55:27 +00:00
|
|
|
{
|
|
|
|
name: "flag description with single quote (') shouldn't break quotes in completion file",
|
|
|
|
root: func() *Command {
|
|
|
|
r := genTestCommand("root", true)
|
|
|
|
r.Flags().Bool("private", false, "Don't show public info")
|
|
|
|
return r
|
|
|
|
}(),
|
|
|
|
expectedExpressions: []string{
|
|
|
|
`--private\[Don'\\''t show public info]`,
|
|
|
|
},
|
|
|
|
},
|
2017-07-30 08:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2018-03-04 22:46:00 +00:00
|
|
|
if tc.skip != "" {
|
|
|
|
t.Skip(tc.skip)
|
|
|
|
}
|
|
|
|
tc.root.Root().SetArgs(tc.invocationArgs)
|
2018-02-25 06:20:34 +00:00
|
|
|
tc.root.Execute()
|
2017-07-30 08:42:35 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2018-02-28 15:04:17 +00:00
|
|
|
if err := tc.root.GenZshCompletion(buf); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-02-24 16:53:13 +00:00
|
|
|
output := buf.Bytes()
|
2017-10-31 18:58:37 +00:00
|
|
|
|
2018-02-24 16:53:13 +00:00
|
|
|
for _, expr := range tc.expectedExpressions {
|
|
|
|
rgx, err := regexp.Compile(expr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error compiling expression (%s): %v", expr, err)
|
|
|
|
}
|
|
|
|
if !rgx.Match(output) {
|
|
|
|
t.Errorf("expeced completion (%s) to match '%s'", buf.String(), expr)
|
2017-07-30 08:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-02-25 12:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenZshCompletionHidden(t *testing.T) {
|
|
|
|
tcs := []struct {
|
|
|
|
name string
|
|
|
|
root *Command
|
|
|
|
expectedExpressions []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "hidden commmands",
|
|
|
|
root: func() *Command {
|
|
|
|
r := &Command{
|
|
|
|
Use: "main",
|
|
|
|
Short: "main short description",
|
|
|
|
}
|
|
|
|
s1 := &Command{
|
|
|
|
Use: "sub1",
|
|
|
|
Hidden: true,
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
s2 := &Command{
|
|
|
|
Use: "sub2",
|
|
|
|
Short: "short sub2 description",
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
r.AddCommand(s1, s2)
|
2018-02-24 16:53:13 +00:00
|
|
|
|
2018-02-25 12:12:58 +00:00
|
|
|
return r
|
|
|
|
}(),
|
|
|
|
expectedExpressions: []string{
|
|
|
|
"sub1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hidden flags",
|
|
|
|
root: func() *Command {
|
|
|
|
var hidden string
|
|
|
|
r := &Command{
|
|
|
|
Use: "root",
|
|
|
|
Short: "root short description",
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
r.Flags().StringVarP(&hidden, "hidden", "H", hidden, "hidden usage")
|
|
|
|
if err := r.Flags().MarkHidden("hidden"); err != nil {
|
|
|
|
t.Errorf("Error setting flag hidden: %v\n", err)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}(),
|
|
|
|
expectedExpressions: []string{
|
|
|
|
"--hidden",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
tc.root.Execute()
|
|
|
|
buf := new(bytes.Buffer)
|
2018-02-28 15:04:17 +00:00
|
|
|
if err := tc.root.GenZshCompletion(buf); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-02-25 12:12:58 +00:00
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
for _, expr := range tc.expectedExpressions {
|
|
|
|
if strings.Contains(output, expr) {
|
|
|
|
t.Errorf("Expected completion (%s) not to contain '%s' but it does", output, expr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-02-24 16:53:13 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 20:31:06 +00:00
|
|
|
func BenchmarkMediumSizeConstruct(b *testing.B) {
|
|
|
|
root := constructLargeCommandHeirarchy()
|
|
|
|
// if err := root.GenZshCompletionFile("_mycmd"); err != nil {
|
|
|
|
// b.Error(err)
|
|
|
|
// }
|
|
|
|
|
2018-02-24 16:53:13 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2018-02-26 20:31:06 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := root.GenZshCompletion(buf)
|
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
2018-02-24 16:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractFlags(t *testing.T) {
|
|
|
|
var debug, cmdc, cmdd bool
|
|
|
|
c := &Command{
|
|
|
|
Use: "cmdC",
|
|
|
|
Long: "Command C",
|
|
|
|
}
|
|
|
|
c.PersistentFlags().BoolVarP(&debug, "debug", "d", debug, "debug mode")
|
|
|
|
c.Flags().BoolVar(&cmdc, "cmd-c", cmdc, "Command C")
|
|
|
|
d := &Command{
|
|
|
|
Use: "CmdD",
|
|
|
|
Long: "Command D",
|
|
|
|
}
|
|
|
|
d.Flags().BoolVar(&cmdd, "cmd-d", cmdd, "Command D")
|
|
|
|
c.AddCommand(d)
|
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
resC := zshCompExtractFlag(c)
|
|
|
|
resD := zshCompExtractFlag(d)
|
2018-02-24 16:53:13 +00:00
|
|
|
|
|
|
|
if len(resC) != 2 {
|
|
|
|
t.Errorf("expected Command C to return 2 flags, got %d", len(resC))
|
|
|
|
}
|
|
|
|
if len(resD) != 2 {
|
|
|
|
t.Errorf("expected Command D to return 2 flags, got %d", len(resD))
|
|
|
|
}
|
2017-07-30 08:42:35 +00:00
|
|
|
}
|
2018-02-26 20:31:06 +00:00
|
|
|
|
|
|
|
func constructLargeCommandHeirarchy() *Command {
|
|
|
|
var config, st1, st2 string
|
|
|
|
var long, debug bool
|
|
|
|
var in1, in2 int
|
2018-02-28 15:04:17 +00:00
|
|
|
var verbose []bool
|
2018-02-26 20:31:06 +00:00
|
|
|
|
|
|
|
r := genTestCommand("mycmd", false)
|
|
|
|
r.PersistentFlags().StringVarP(&config, "config", "c", config, "config usage")
|
|
|
|
if err := r.MarkPersistentFlagFilename("config", "*"); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
s1 := genTestCommand("sub1", true)
|
|
|
|
s1.Flags().BoolVar(&long, "long", long, "long descriptin")
|
2018-02-28 15:04:17 +00:00
|
|
|
s1.Flags().BoolSliceVar(&verbose, "verbose", verbose, "verbose description")
|
|
|
|
s1.Flags().StringArray("option", []string{}, "various options")
|
2018-02-26 20:31:06 +00:00
|
|
|
s2 := genTestCommand("sub2", true)
|
|
|
|
s2.PersistentFlags().BoolVar(&debug, "debug", debug, "debug description")
|
|
|
|
s3 := genTestCommand("sub3", true)
|
|
|
|
s3.Hidden = true
|
|
|
|
s1_1 := genTestCommand("sub1sub1", true)
|
|
|
|
s1_1.Flags().StringVar(&st1, "st1", st1, "st1 description")
|
|
|
|
s1_1.Flags().StringVar(&st2, "st2", st2, "st2 description")
|
|
|
|
s1_2 := genTestCommand("sub1sub2", true)
|
|
|
|
s1_3 := genTestCommand("sub1sub3", true)
|
|
|
|
s1_3.Flags().IntVar(&in1, "int1", in1, "int1 descriptionn")
|
|
|
|
s1_3.Flags().IntVar(&in2, "int2", in2, "int2 descriptionn")
|
2018-02-28 15:04:17 +00:00
|
|
|
s1_3.Flags().StringArrayP("option", "O", []string{}, "more options")
|
2018-02-26 20:31:06 +00:00
|
|
|
s2_1 := genTestCommand("sub2sub1", true)
|
|
|
|
s2_2 := genTestCommand("sub2sub2", true)
|
|
|
|
s2_3 := genTestCommand("sub2sub3", true)
|
|
|
|
s2_4 := genTestCommand("sub2sub4", true)
|
|
|
|
s2_5 := genTestCommand("sub2sub5", true)
|
|
|
|
|
|
|
|
s1.AddCommand(s1_1, s1_2, s1_3)
|
|
|
|
s2.AddCommand(s2_1, s2_2, s2_3, s2_4, s2_5)
|
|
|
|
r.AddCommand(s1, s2, s3)
|
|
|
|
r.Execute()
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func genTestCommand(name string, withRun bool) *Command {
|
|
|
|
r := &Command{
|
|
|
|
Use: name,
|
|
|
|
Short: name + " short description",
|
|
|
|
Long: "Long description for " + name,
|
|
|
|
}
|
|
|
|
if withRun {
|
|
|
|
r.Run = emptyRun
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|