Repository: spf13/cobra
File: command_test.go (Go)

Code before:
		Long: `Help provides help for any command in the application.
	Simply type ` + c.Name() + ` help [path to command] for full details.`,
		Run: func(c *Command, args []string) { c.Print(correctMessage) },
	})

	if err := c.Execute(); err != nil {
		t.Error("Unexpected error:", err)
	}

	if output.String() != correctMessage {
		t.Errorf("Expected to contain %q message, but got %q", correctMessage, output.String())
	}
}

func TestRequiredFlags(t *testing.T) {
	c := &Command{Use: "c", Run: func(*Command, []string) {}}
	output := new(bytes.Buffer)
	c.SetOutput(output)
	c.Flags().String("foo1", "", "required foo1")
	c.MarkFlagRequired("foo1")
	c.Flags().String("foo2", "", "required foo2")
	c.MarkFlagRequired("foo2")
	c.Flags().String("bar", "", "optional bar")

	expectedFmt := "Required flags %s have/has not been set"
	expected := fmt.Sprintf(expectedFmt, fmt.Sprintf("%q, %q", "foo1", "foo2"))

	if err := c.Execute(); err != nil {
		if err.Error() != expected {
			t.Errorf("expected %v, got %v", expected, err.Error())
		}
	}
}

func TestPersistentRequiredFlags(t *testing.T) {
	parent := &Command{Use: "parent", Run: func(*Command, []string) {}}
	output := new(bytes.Buffer)
	parent.SetOutput(output)
	parent.PersistentFlags().String("foo1", "", "required foo1")
	parent.MarkPersistentFlagRequired("foo1")
	parent.PersistentFlags().String("foo2", "", "required foo2")
	parent.MarkPersistentFlagRequired("foo2")
	parent.Flags().String("foo3", "", "optional foo3")

	child := &Command{Use: "child", Run: func(*Command, []string) {}}
	child.Flags().String("bar1", "", "required bar1")
	child.MarkFlagRequired("bar1")
	child.Flags().String("bar2", "", "required bar2")
	child.MarkFlagRequired("bar2")
	child.Flags().String("bar3", "", "optional bar3")


Review comment (suggestion):
I think, it would be simpler to write `expected := fmt.Sprintf(Required flags %q, %q have/has not been set, "foo1", "foo2")`

Code after:
		Long: `Help provides help for any command in the application.
	Simply type ` + c.Name() + ` help [path to command] for full details.`,
		Run: func(c *Command, args []string) { c.Print(correctMessage) },
	})

	if err := c.Execute(); err != nil {
		t.Error("Unexpected error:", err)
	}

	if output.String() != correctMessage {
		t.Errorf("Expected to contain %q message, but got %q", correctMessage, output.String())
	}
}

func TestTraverseWithParentFlags(t *testing.T) {
	cmd := &Command{
		Use:              "do",
		TraverseChildren: true,
	}
	cmd.Flags().String("foo", "", "foo things")
	cmd.Flags().BoolP("goo", "g", false, "foo things")

	sub := &Command{Use: "next"}
	sub.Flags().String("add", "", "add things")
	cmd.AddCommand(sub)

	c, args, err := cmd.Traverse([]string{"-g", "--foo", "ok", "next", "--add"})
	if err != nil {
		t.Fatalf("Expected no error: %s", err)
	}
	if len(args) != 1 && args[0] != "--add" {
		t.Fatalf("wrong args %s", args)
	}
	if c.Name() != sub.Name() {
		t.Fatalf("wrong command %q expected %q", c.Name(), sub.Name())
	}
}

func TestTraverseNoParentFlags(t *testing.T) {
	cmd := &Command{
		Use:              "do",
		TraverseChildren: true,
	}
	cmd.Flags().String("foo", "", "foo things")

	sub := &Command{Use: "next"}
	sub.Flags().String("add", "", "add things")
	cmd.AddCommand(sub)

	c, args, err := cmd.Traverse([]string{"next"})
	if err != nil {