2015-03-16 22:31:03 +03:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = fmt.Println
|
|
|
|
var _ = os.Stderr
|
|
|
|
|
2015-04-08 00:38:22 +03:00
|
|
|
func checkOmit(t *testing.T, found, unexpected string) {
|
|
|
|
if strings.Contains(found, unexpected) {
|
|
|
|
t.Errorf("Unexpected response.\nGot: %q\nBut should not have!\n", unexpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:31:03 +03:00
|
|
|
func check(t *testing.T, found, expected string) {
|
|
|
|
if !strings.Contains(found, expected) {
|
|
|
|
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// World worst custom function, just keep telling you to enter hello!
|
|
|
|
const (
|
|
|
|
bash_completion_func = `__custom_func() {
|
|
|
|
COMPREPLY=( "hello" )
|
|
|
|
}
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBashCompletions(t *testing.T) {
|
|
|
|
c := initializeWithRootCmd()
|
|
|
|
cmdEcho.AddCommand(cmdTimes)
|
2015-12-10 07:57:45 +03:00
|
|
|
c.AddCommand(cmdEcho, cmdPrint, cmdDeprecated, cmdColon)
|
2015-03-16 22:31:03 +03:00
|
|
|
|
|
|
|
// custom completion function
|
|
|
|
c.BashCompletionFunction = bash_completion_func
|
|
|
|
|
|
|
|
// required flag
|
|
|
|
c.MarkFlagRequired("introot")
|
|
|
|
|
2015-06-22 21:28:16 +03:00
|
|
|
// valid nouns
|
2015-03-16 22:31:03 +03:00
|
|
|
validArgs := []string{"pods", "nodes", "services", "replicationControllers"}
|
|
|
|
c.ValidArgs = validArgs
|
|
|
|
|
2015-06-22 21:28:16 +03:00
|
|
|
// filename
|
2015-03-16 22:31:03 +03:00
|
|
|
var flagval string
|
|
|
|
c.Flags().StringVar(&flagval, "filename", "", "Enter a filename")
|
2015-06-22 21:28:16 +03:00
|
|
|
c.MarkFlagFilename("filename", "json", "yaml", "yml")
|
|
|
|
|
2015-11-04 22:42:43 +03:00
|
|
|
// persistent filename
|
|
|
|
var flagvalPersistent string
|
|
|
|
c.PersistentFlags().StringVar(&flagvalPersistent, "persistent-filename", "", "Enter a filename")
|
|
|
|
c.MarkPersistentFlagFilename("persistent-filename")
|
|
|
|
c.MarkPersistentFlagRequired("persistent-filename")
|
|
|
|
|
2015-06-22 21:28:16 +03:00
|
|
|
// filename extensions
|
|
|
|
var flagvalExt string
|
|
|
|
c.Flags().StringVar(&flagvalExt, "filename-ext", "", "Enter a filename (extension limited)")
|
|
|
|
c.MarkFlagFilename("filename-ext")
|
2015-03-16 22:31:03 +03:00
|
|
|
|
2015-08-09 22:30:58 +03:00
|
|
|
// subdirectories in a given directory
|
|
|
|
var flagvalTheme string
|
|
|
|
c.Flags().StringVar(&flagvalTheme, "theme", "", "theme to use (located in /themes/THEMENAME/)")
|
|
|
|
c.Flags().SetAnnotation("theme", BashCompSubdirsInDir, []string{"themes"})
|
|
|
|
|
2015-03-16 22:31:03 +03:00
|
|
|
out := new(bytes.Buffer)
|
|
|
|
c.GenBashCompletion(out)
|
|
|
|
str := out.String()
|
|
|
|
|
|
|
|
check(t, str, "_cobra-test")
|
|
|
|
check(t, str, "_cobra-test_echo")
|
|
|
|
check(t, str, "_cobra-test_echo_times")
|
|
|
|
check(t, str, "_cobra-test_print")
|
2015-12-10 07:57:45 +03:00
|
|
|
check(t, str, "_cobra-test_cmd__colon")
|
2015-03-16 22:31:03 +03:00
|
|
|
|
|
|
|
// check for required flags
|
|
|
|
check(t, str, `must_have_one_flag+=("--introot=")`)
|
2015-11-04 22:42:43 +03:00
|
|
|
check(t, str, `must_have_one_flag+=("--persistent-filename=")`)
|
2015-03-16 22:31:03 +03:00
|
|
|
// check for custom completion function
|
|
|
|
check(t, str, `COMPREPLY=( "hello" )`)
|
|
|
|
// check for required nouns
|
|
|
|
check(t, str, `must_have_one_noun+=("pods")`)
|
2015-06-22 21:28:16 +03:00
|
|
|
// check for filename extension flags
|
|
|
|
check(t, str, `flags_completion+=("_filedir")`)
|
|
|
|
// check for filename extension flags
|
2015-05-04 22:44:07 +03:00
|
|
|
check(t, str, `flags_completion+=("__handle_filename_extension_flag json|yaml|yml")`)
|
2015-08-09 22:30:58 +03:00
|
|
|
// check for subdirs_in_dir flags
|
|
|
|
check(t, str, `flags_completion+=("__handle_subdirs_in_dir_flag themes")`)
|
2015-04-08 00:38:22 +03:00
|
|
|
|
|
|
|
checkOmit(t, str, cmdDeprecated.Name())
|
2015-03-16 22:31:03 +03:00
|
|
|
}
|