2015-03-16 19:31:03 +00:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-02-08 21:34:46 +00:00
|
|
|
"fmt"
|
2015-03-16 19:31:03 +00:00
|
|
|
"os"
|
2016-04-02 20:45:01 +00:00
|
|
|
"os/exec"
|
2018-02-28 05:38:38 +00:00
|
|
|
"regexp"
|
2015-03-16 19:31:03 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-04-07 21:38:22 +00:00
|
|
|
func checkOmit(t *testing.T, found, unexpected string) {
|
|
|
|
if strings.Contains(found, unexpected) {
|
2017-10-31 18:58:37 +00:00
|
|
|
t.Errorf("Got: %q\nBut should not have!\n", unexpected)
|
2015-04-07 21:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 19:31:03 +00:00
|
|
|
func check(t *testing.T, found, expected string) {
|
|
|
|
if !strings.Contains(found, expected) {
|
2017-10-31 18:58:37 +00:00
|
|
|
t.Errorf("Expecting to contain: \n %q\nGot:\n %q\n", expected, found)
|
2015-03-16 19:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 05:38:38 +00:00
|
|
|
func checkRegex(t *testing.T, found, pattern string) {
|
|
|
|
matched, err := regexp.MatchString(pattern, found)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error thrown performing MatchString: \n %s\n", err)
|
|
|
|
}
|
|
|
|
if !matched {
|
|
|
|
t.Errorf("Expecting to match: \n %q\nGot:\n %q\n", pattern, found)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 20:45:01 +00:00
|
|
|
func runShellCheck(s string) error {
|
|
|
|
excluded := []string{
|
|
|
|
"SC2034", // PREFIX appears unused. Verify it or export it.
|
|
|
|
}
|
|
|
|
cmd := exec.Command("shellcheck", "-s", "bash", "-", "-e", strings.Join(excluded, ","))
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
stdin.Write([]byte(s))
|
2017-10-31 18:58:37 +00:00
|
|
|
stdin.Close()
|
2016-04-02 20:45:01 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
2015-03-16 19:31:03 +00:00
|
|
|
// World worst custom function, just keep telling you to enter hello!
|
2017-10-31 18:58:37 +00:00
|
|
|
const bashCompletionFunc = `__custom_func() {
|
|
|
|
COMPREPLY=( "hello" )
|
2015-03-16 19:31:03 +00:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestBashCompletions(t *testing.T) {
|
2017-10-31 18:58:37 +00:00
|
|
|
rootCmd := &Command{
|
|
|
|
Use: "root",
|
|
|
|
ArgAliases: []string{"pods", "nodes", "services", "replicationcontrollers", "po", "no", "svc", "rc"},
|
|
|
|
ValidArgs: []string{"pod", "node", "service", "replicationcontroller"},
|
|
|
|
BashCompletionFunction: bashCompletionFunc,
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot")
|
|
|
|
rootCmd.MarkFlagRequired("introot")
|
|
|
|
|
|
|
|
// Filename.
|
|
|
|
rootCmd.Flags().String("filename", "", "Enter a filename")
|
|
|
|
rootCmd.MarkFlagFilename("filename", "json", "yaml", "yml")
|
|
|
|
|
|
|
|
// Persistent filename.
|
|
|
|
rootCmd.PersistentFlags().String("persistent-filename", "", "Enter a filename")
|
|
|
|
rootCmd.MarkPersistentFlagFilename("persistent-filename")
|
|
|
|
rootCmd.MarkPersistentFlagRequired("persistent-filename")
|
|
|
|
|
|
|
|
// Filename extensions.
|
|
|
|
rootCmd.Flags().String("filename-ext", "", "Enter a filename (extension limited)")
|
|
|
|
rootCmd.MarkFlagFilename("filename-ext")
|
|
|
|
rootCmd.Flags().String("custom", "", "Enter a filename (extension limited)")
|
|
|
|
rootCmd.MarkFlagCustom("custom", "__complete_custom")
|
|
|
|
|
|
|
|
// Subdirectories in a given directory.
|
|
|
|
rootCmd.Flags().String("theme", "", "theme to use (located in /themes/THEMENAME/)")
|
|
|
|
rootCmd.Flags().SetAnnotation("theme", BashCompSubdirsInDir, []string{"themes"})
|
|
|
|
|
|
|
|
echoCmd := &Command{
|
|
|
|
Use: "echo [string to echo]",
|
|
|
|
Aliases: []string{"say"},
|
|
|
|
Short: "Echo anything to the screen",
|
|
|
|
Long: "an utterly useless command for testing.",
|
|
|
|
Example: "Just run cobra-test echo",
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
|
2018-02-28 05:38:38 +00:00
|
|
|
echoCmd.Flags().String("filename", "", "Enter a filename")
|
|
|
|
echoCmd.MarkFlagFilename("filename", "json", "yaml", "yml")
|
|
|
|
echoCmd.Flags().String("config", "", "config to use (located in /config/PROFILE/)")
|
|
|
|
echoCmd.Flags().SetAnnotation("config", BashCompSubdirsInDir, []string{"config"})
|
|
|
|
|
2017-10-31 18:58:37 +00:00
|
|
|
printCmd := &Command{
|
|
|
|
Use: "print [string to print]",
|
|
|
|
Args: MinimumNArgs(1),
|
|
|
|
Short: "Print anything to the screen",
|
|
|
|
Long: "an absolutely utterly useless command for testing.",
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
deprecatedCmd := &Command{
|
|
|
|
Use: "deprecated [can't do anything here]",
|
|
|
|
Args: NoArgs,
|
|
|
|
Short: "A command which is deprecated",
|
|
|
|
Long: "an absolutely utterly useless command for testing deprecation!.",
|
|
|
|
Deprecated: "Please use echo instead",
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
colonCmd := &Command{
|
|
|
|
Use: "cmd:colon",
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
timesCmd := &Command{
|
|
|
|
Use: "times [# times] [string to echo]",
|
|
|
|
SuggestFor: []string{"counts"},
|
|
|
|
Args: OnlyValidArgs,
|
|
|
|
ValidArgs: []string{"one", "two", "three", "four"},
|
|
|
|
Short: "Echo anything to the screen more times",
|
|
|
|
Long: "a slightly useless command for testing.",
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
echoCmd.AddCommand(timesCmd)
|
|
|
|
rootCmd.AddCommand(echoCmd, printCmd, deprecatedCmd, colonCmd)
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
rootCmd.GenBashCompletion(buf)
|
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
check(t, output, "_root")
|
|
|
|
check(t, output, "_root_echo")
|
|
|
|
check(t, output, "_root_echo_times")
|
|
|
|
check(t, output, "_root_print")
|
|
|
|
check(t, output, "_root_cmd__colon")
|
2015-03-16 19:31:03 +00:00
|
|
|
|
|
|
|
// check for required flags
|
2017-10-31 18:58:37 +00:00
|
|
|
check(t, output, `must_have_one_flag+=("--introot=")`)
|
|
|
|
check(t, output, `must_have_one_flag+=("--persistent-filename=")`)
|
2015-03-16 19:31:03 +00:00
|
|
|
// check for custom completion function
|
2017-10-31 18:58:37 +00:00
|
|
|
check(t, output, `COMPREPLY=( "hello" )`)
|
2015-03-16 19:31:03 +00:00
|
|
|
// check for required nouns
|
2017-10-31 18:58:37 +00:00
|
|
|
check(t, output, `must_have_one_noun+=("pod")`)
|
2016-03-25 15:05:56 +00:00
|
|
|
// check for noun aliases
|
2017-10-31 18:58:37 +00:00
|
|
|
check(t, output, `noun_aliases+=("pods")`)
|
|
|
|
check(t, output, `noun_aliases+=("rc")`)
|
|
|
|
checkOmit(t, output, `must_have_one_noun+=("pods")`)
|
2015-06-22 18:28:16 +00:00
|
|
|
// check for filename extension flags
|
2017-10-31 18:58:37 +00:00
|
|
|
check(t, output, `flags_completion+=("_filedir")`)
|
2015-06-22 18:28:16 +00:00
|
|
|
// check for filename extension flags
|
2017-10-31 18:58:37 +00:00
|
|
|
check(t, output, `must_have_one_noun+=("three")`)
|
2017-09-01 15:16:37 +00:00
|
|
|
// check for filename extension flags
|
2018-02-08 21:34:46 +00:00
|
|
|
check(t, output, fmt.Sprintf(`flags_completion+=("__%s_handle_filename_extension_flag json|yaml|yml")`, rootCmd.Name()))
|
2018-02-28 05:38:38 +00:00
|
|
|
// check for filename extension flags in a subcommand
|
|
|
|
checkRegex(t, output, fmt.Sprintf(`_root_echo\(\)\n{[^}]*flags_completion\+=\("__%s_handle_filename_extension_flag json\|yaml\|yml"\)`, rootCmd.Name()))
|
2016-03-20 20:05:18 +00:00
|
|
|
// check for custom flags
|
2017-10-31 18:58:37 +00:00
|
|
|
check(t, output, `flags_completion+=("__complete_custom")`)
|
2015-08-09 19:30:58 +00:00
|
|
|
// check for subdirs_in_dir flags
|
2018-02-08 21:34:46 +00:00
|
|
|
check(t, output, fmt.Sprintf(`flags_completion+=("__%s_handle_subdirs_in_dir_flag themes")`, rootCmd.Name()))
|
2018-02-28 05:38:38 +00:00
|
|
|
// check for subdirs_in_dir flags in a subcommand
|
|
|
|
checkRegex(t, output, fmt.Sprintf(`_root_echo\(\)\n{[^}]*flags_completion\+=\("__%s_handle_subdirs_in_dir_flag config"\)`, rootCmd.Name()))
|
2015-04-07 21:38:22 +00:00
|
|
|
|
2017-10-31 18:58:37 +00:00
|
|
|
checkOmit(t, output, deprecatedCmd.Name())
|
2016-04-02 20:45:01 +00:00
|
|
|
|
2017-10-31 18:58:37 +00:00
|
|
|
// If available, run shellcheck against the script.
|
2016-04-02 20:45:01 +00:00
|
|
|
if err := exec.Command("which", "shellcheck").Run(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-10-31 18:58:37 +00:00
|
|
|
if err := runShellCheck(output); err != nil {
|
2016-04-02 20:45:01 +00:00
|
|
|
t.Fatalf("shellcheck failed: %v", err)
|
|
|
|
}
|
2015-03-16 19:31:03 +00:00
|
|
|
}
|
2016-08-02 21:49:33 +00:00
|
|
|
|
|
|
|
func TestBashCompletionHiddenFlag(t *testing.T) {
|
2017-10-31 18:58:37 +00:00
|
|
|
c := &Command{Use: "c", Run: emptyRun}
|
2016-08-02 21:49:33 +00:00
|
|
|
|
2017-10-31 18:58:37 +00:00
|
|
|
const flagName = "hiddenFlag"
|
|
|
|
c.Flags().Bool(flagName, false, "")
|
|
|
|
c.Flags().MarkHidden(flagName)
|
2016-08-02 21:49:33 +00:00
|
|
|
|
2017-10-31 18:58:37 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
c.GenBashCompletion(buf)
|
|
|
|
output := buf.String()
|
2016-08-02 21:49:33 +00:00
|
|
|
|
2017-10-31 18:58:37 +00:00
|
|
|
if strings.Contains(output, flagName) {
|
|
|
|
t.Errorf("Expected completion to not include %q flag: Got %v", flagName, output)
|
2016-08-02 21:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-02 22:01:33 +00:00
|
|
|
|
|
|
|
func TestBashCompletionDeprecatedFlag(t *testing.T) {
|
2017-10-31 18:58:37 +00:00
|
|
|
c := &Command{Use: "c", Run: emptyRun}
|
2017-05-14 12:57:11 +00:00
|
|
|
|
2017-10-31 18:58:37 +00:00
|
|
|
const flagName = "deprecated-flag"
|
|
|
|
c.Flags().Bool(flagName, false, "")
|
|
|
|
c.Flags().MarkDeprecated(flagName, "use --not-deprecated instead")
|
2017-05-14 12:57:11 +00:00
|
|
|
|
2017-06-05 17:32:33 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2017-10-31 18:58:37 +00:00
|
|
|
c.GenBashCompletion(buf)
|
|
|
|
output := buf.String()
|
2017-05-14 12:57:11 +00:00
|
|
|
|
2017-10-31 18:58:37 +00:00
|
|
|
if strings.Contains(output, flagName) {
|
|
|
|
t.Errorf("expected completion to not include %q flag: Got %v", flagName, output)
|
2017-05-14 12:57:11 +00:00
|
|
|
}
|
|
|
|
}
|