New tests (they don't all pass)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Eric Paris · committed by Eric Paris · parent 09b49c3 · Browse files · 1 file·+83 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+83 added−2 removed
- other:+83 added−2 removed
Provenance
- Commit
fe58172- Tree
3e2e18c- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent
09b49c3
1 file changed+83 added−2 removed
modified:cobra_test.go+83 added-2 removed
1 unmodified line2 3 import (4 "bytes"5 "fmt"6+ "os"7 "strings"8 "testing"9 )10 11 var _ = fmt.Println12+var _ = os.Stderr13 14 var tp, te, tt, t1 []string15 var flagb1, flagb2, flagb3, flagbr, flagbp bool16 var flags1, flags2a, flags2b, flags3 string6 unmodified lines23 24 var cmdPrint = &Command{25 Use: "print [string to print]",26 Short: "Print anything to the screen",25- Long: `an utterly useless command for testing.`,27+ Long: `an absolutely utterly useless command for testing.`,28 Run: func(cmd *Command, args []string) {29 tp = args30 },31 }10 unmodified lines42 43 var cmdTimes = &Command{44 Use: "times [# times] [string to echo]",45 Short: "Echo anything to the screen more times",44- Long: `an slightly useless command for testing.`,46+ Long: `a slightly useless command for testing.`,47 Run: func(cmd *Command, args []string) {48 tt = args49 },50 }367 unmodified lines418 t.Errorf("invalid flag should generate error")419 }420 }421 422+func TestInvalidSubCommandFlags(t *testing.T) {423+ cmd := initializeWithRootCmd()424+ cmd.AddCommand(cmdTimes)425+426+ result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")427+428+ checkResultContains(t, result, "unknown flag: --badflag")429+430+ if strings.Contains(result.Output, "unknown flag: --inttwo") {431+ t.Errorf("invalid --badflag flag shouldn't fail on 'unknown' --inttwo flag")432+ }433+434+}435+436 func TestPersistentFlags(t *testing.T) {437 fullSetupTest("echo -s something -p more here")438 439 // persistentFlag should act like normal flag on it's own command48 unmodified lines488 t.Errorf("Root Function was not called")489 }490 }491 492+func TestRunnableRootCommandNilInput(t *testing.T) {493+ empty_arg := make([]string, 0)494+ c := initializeWithRootCmd()495+496+ buf := new(bytes.Buffer)497+ // Testing flag with invalid input498+ c.SetOutput(buf)499+ cmdEcho.AddCommand(cmdTimes)500+ c.AddCommand(cmdPrint, cmdEcho)501+ c.SetArgs(empty_arg)502+503+ c.Execute()504+505+ if rootcalled != true {506+ t.Errorf("Root Function was not called")507+ }508+}509+510+func TestRunnableRootCommandEmptyInput(t *testing.T) {511+ args := make([]string, 3)512+ args[0] = ""513+ args[1] = "--introot=12"514+ args[2] = ""515+ c := initializeWithRootCmd()516+517+ buf := new(bytes.Buffer)518+ // Testing flag with invalid input519+ c.SetOutput(buf)520+ cmdEcho.AddCommand(cmdTimes)521+ c.AddCommand(cmdPrint, cmdEcho)522+ c.SetArgs(args)523+524+ c.Execute()525+526+ if rootcalled != true {527+ t.Errorf("Root Function was not called.\n\nOutput was:\n\n%s\n", buf)528+ }529+}530+531+func TestInvalidSubcommandWhenArgsAllowed(t *testing.T) {532+ fullSetupTest("echo invalid-sub")533+534+ if te[0] != "invalid-sub" {535+ t.Errorf("Subcommand didn't work...")536+ }537+}538+539 func TestRootFlags(t *testing.T) {540 fullSetupTest("-i 17 -b")541 542 if flagbr != true {53 unmodified lines596 597 }598 }599 600+func TestNoNRunnableRootCommandNilInput(t *testing.T) {601+ args := make([]string, 0)602+ c := initialize()603+604+ buf := new(bytes.Buffer)605+ // Testing flag with invalid input606+ c.SetOutput(buf)607+ cmdEcho.AddCommand(cmdTimes)608+ c.AddCommand(cmdPrint, cmdEcho)609+ c.SetArgs(args)610+611+ c.Execute()612+613+ if !strings.Contains(buf.String(), cmdRootNoRun.Long) {614+ t.Errorf("Expected to get help output, Got: \n %s", buf)615+ }616+}617+618 func TestRootNoCommandHelp(t *testing.T) {619 x := rootOnlySetupTest("--help")620 621 checkResultOmits(t, x, "Available Commands:")78 unmodified lines