Skip to content
Myr

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

1 file+83 added−2 removed

modified:cobra_test.go+83 added-2 removed
1 unmodified line
23import (4 "bytes"5 "fmt"6 "os"7 "strings"8 "testing"9)1011var _ = fmt.Println12var _ = os.Stderr1314var tp, te, tt, t1 []string15var flagb1, flagb2, flagb3, flagbr, flagbp bool16var flags1, flags2a, flags2b, flags3 string
6 unmodified lines
2324var 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 lines
4243var 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 lines
418 t.Errorf("invalid flag should generate error")419 }420}421422func TestInvalidSubCommandFlags(t *testing.T) {423 cmd := initializeWithRootCmd()424 cmd.AddCommand(cmdTimes)425426 result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")427428 checkResultContains(t, result, "unknown flag: --badflag")429430 if strings.Contains(result.Output, "unknown flag: --inttwo") {431 t.Errorf("invalid --badflag flag shouldn't fail on 'unknown' --inttwo flag")432 }433434}435436func TestPersistentFlags(t *testing.T) {437 fullSetupTest("echo -s something -p more here")438439 // persistentFlag should act like normal flag on it's own command
48 unmodified lines
488 t.Errorf("Root Function was not called")489 }490}491492func TestRunnableRootCommandNilInput(t *testing.T) {493 empty_arg := make([]string, 0)494 c := initializeWithRootCmd()495496 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)502503 c.Execute()504505 if rootcalled != true {506 t.Errorf("Root Function was not called")507 }508}509510func TestRunnableRootCommandEmptyInput(t *testing.T) {511 args := make([]string, 3)512 args[0] = ""513 args[1] = "--introot=12"514 args[2] = ""515 c := initializeWithRootCmd()516517 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)523524 c.Execute()525526 if rootcalled != true {527 t.Errorf("Root Function was not called.\n\nOutput was:\n\n%s\n", buf)528 }529}530531func TestInvalidSubcommandWhenArgsAllowed(t *testing.T) {532 fullSetupTest("echo invalid-sub")533534 if te[0] != "invalid-sub" {535 t.Errorf("Subcommand didn't work...")536 }537}538539func TestRootFlags(t *testing.T) {540 fullSetupTest("-i 17 -b")541542 if flagbr != true {
53 unmodified lines
596597 }598}599600func TestNoNRunnableRootCommandNilInput(t *testing.T) {601 args := make([]string, 0)602 c := initialize()603604 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)610611 c.Execute()612613 if !strings.Contains(buf.String(), cmdRootNoRun.Long) {614 t.Errorf("Expected to get help output, Got: \n %s", buf)615 }616}617618func TestRootNoCommandHelp(t *testing.T) {619 x := rootOnlySetupTest("--help")620621 checkResultOmits(t, x, "Available Commands:")
78 unmodified lines