mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
tests now leverage the built in test suite, and are much less custom
This commit is contained in:
parent
8be60dc5cd
commit
dfa8ea9685
2 changed files with 36 additions and 73 deletions
|
@ -18,8 +18,8 @@ var _ = os.Stderr
|
||||||
|
|
||||||
var tp, te, tt, t1, tr []string
|
var tp, te, tt, t1, tr []string
|
||||||
var rootPersPre, echoPre, echoPersPre, timesPersPre []string
|
var rootPersPre, echoPre, echoPersPre, timesPersPre []string
|
||||||
var flagb1, flagb2, flagb3, flagbr, flagbp bool
|
var flagb1, flagb2, flagb3, flagbr, flagbp, flagbh, flagbph bool
|
||||||
var flags1, flags2a, flags2b, flags3 string
|
var flags1, flags2a, flags2b, flags3, outs string
|
||||||
var flagi1, flagi2, flagi3, flagir int
|
var flagi1, flagi2, flagi3, flagir int
|
||||||
var globalFlag1 bool
|
var globalFlag1 bool
|
||||||
var flagEcho, rootcalled bool
|
var flagEcho, rootcalled bool
|
||||||
|
@ -28,6 +28,18 @@ var versionUsed int
|
||||||
const strtwoParentHelp = "help message for parent flag strtwo"
|
const strtwoParentHelp = "help message for parent flag strtwo"
|
||||||
const strtwoChildHelp = "help message for child flag strtwo"
|
const strtwoChildHelp = "help message for child flag strtwo"
|
||||||
|
|
||||||
|
var cmdHiddenFlags = &Command{
|
||||||
|
Use: "hidden [string to set]",
|
||||||
|
Short: "set a string value to string var",
|
||||||
|
Long: `an utterly useless command for testing.`,
|
||||||
|
Run: func(cmd *Command, args []string) {
|
||||||
|
outs = "visible"
|
||||||
|
if flagbh {
|
||||||
|
outs = "hidden"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
var cmdPrint = &Command{
|
var cmdPrint = &Command{
|
||||||
Use: "print [string to print]",
|
Use: "print [string to print]",
|
||||||
Short: "Print anything to the screen",
|
Short: "Print anything to the screen",
|
||||||
|
|
|
@ -5,82 +5,33 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// test to ensure hidden flags run as intended
|
func init() {
|
||||||
|
cmdHiddenFlags.Flags().BoolVarP(&flagbh, "boolh", "", false, "")
|
||||||
|
cmdHiddenFlags.Flags().MarkHidden("boolh")
|
||||||
|
|
||||||
|
cmdHiddenFlags.PersistentFlags().BoolVarP(&flagbph, "boolph", "", false, "")
|
||||||
|
cmdHiddenFlags.PersistentFlags().MarkHidden("boolph")
|
||||||
|
}
|
||||||
|
|
||||||
|
// test to ensure hidden flags run as intended; if the the hidden flag fails to
|
||||||
|
// run, the output will be incorrect
|
||||||
func TestHiddenFlagExecutes(t *testing.T) {
|
func TestHiddenFlagExecutes(t *testing.T) {
|
||||||
var out string
|
cmdHiddenFlags.execute([]string{"--boolh"})
|
||||||
var secretFlag bool
|
if outs != "hidden" {
|
||||||
|
|
||||||
boringCmd := &Command{
|
|
||||||
Use: "boring",
|
|
||||||
Short: "Do something boring...",
|
|
||||||
Long: `Not a flashy command, just does boring stuff`,
|
|
||||||
Run: func(cmd *Command, args []string) {
|
|
||||||
out = "boring output"
|
|
||||||
|
|
||||||
if secretFlag {
|
|
||||||
out = "super secret NOT boring output!"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
boringCmd.Flags().BoolVarP(&secretFlag, "secret", "s", false, "makes this command run in super secret mode")
|
|
||||||
boringCmd.Flags().MarkHidden("secret")
|
|
||||||
|
|
||||||
//
|
|
||||||
boringCmd.Execute()
|
|
||||||
|
|
||||||
if out != "boring output" {
|
|
||||||
t.Errorf("Command with hidden flag failed to run!")
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
boringCmd.execute([]string{"-s"})
|
|
||||||
|
|
||||||
if out != "super secret NOT boring output!" {
|
|
||||||
t.Errorf("Hidden flag failed to run!")
|
t.Errorf("Hidden flag failed to run!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// test to ensure hidden flags do not show up in usage/help text
|
// test to ensure hidden flags do not show up in usage/help text; if a flag is
|
||||||
|
// found by Lookup() it will be visible in usage/help text
|
||||||
func TestHiddenFlagsAreHidden(t *testing.T) {
|
func TestHiddenFlagsAreHidden(t *testing.T) {
|
||||||
var out string
|
|
||||||
var secretFlag bool
|
|
||||||
var persistentSecretFlag bool
|
|
||||||
|
|
||||||
boringCmd := &Command{
|
if cmdHiddenFlags.LocalFlags().Lookup("boolh") != nil {
|
||||||
Use: "boring",
|
t.Errorf("unexpected flag 'boolh'")
|
||||||
Short: "Do something boring...",
|
|
||||||
Long: `Not a flashy command, just does boring stuff`,
|
|
||||||
Run: func(cmd *Command, args []string) {
|
|
||||||
out = "boring output"
|
|
||||||
|
|
||||||
if secretFlag {
|
|
||||||
out = "super secret NOT boring output!"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if persistentSecretFlag {
|
if cmdHiddenFlags.InheritedFlags().Lookup("boolph") != nil {
|
||||||
out = "you have no idea what you're getting yourself into!"
|
t.Errorf("unexpected flag 'boolph'")
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
boringCmd.Flags().BoolVarP(&secretFlag, "secret", "s", false, "run this command in super secret mode")
|
|
||||||
boringCmd.Flags().MarkHidden("secret")
|
|
||||||
|
|
||||||
// if a command has local flags, they will appear in usage/help text
|
|
||||||
if boringCmd.HasLocalFlags() {
|
|
||||||
t.Errorf("Hidden flag found!")
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
boringCmd.PersistentFlags().BoolVarP(&persistentSecretFlag, "Secret", "S", false, "run any sub command in super secret mode")
|
|
||||||
boringCmd.PersistentFlags().MarkHidden("Secret")
|
|
||||||
|
|
||||||
// if a command has inherited flags, they will appear in usage/help text
|
|
||||||
if boringCmd.HasInheritedFlags() {
|
|
||||||
t.Errorf("Hidden flag found!")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue