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

Code before:
	rootCmd.AddCommand(&Command{Use: "yyy", Run: emptyRun})

	output, err := executeCommand(rootCmd, "--help")
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	// The yyy command should be in the additional command "group"
	checkStringContains(t, output, "\nAdditional Commands:\n  yyy")
}

func TestAddGroup(t *testing.T) {
	var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}

	rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
	rootCmd.AddCommand(&Command{Use: "cmd", GroupID: "group", Run: emptyRun})

	output, err := executeCommand(rootCmd, "--help")
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	checkStringContains(t, output, "\nTest group\n  cmd")
}

func TestMissingGroupFirstLevel(t *testing.T) {
	var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}

	rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
	// Use the wrong group ID
	rootCmd.AddCommand(&Command{Use: "cmd", GroupID: "wrong", Run: emptyRun})

	defer func() {
		if recover() == nil {
			t.Errorf("The code should have panicked due to a missing group")
		}
	}()
	_, err := executeCommand(rootCmd, "--help")
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}
}

func TestMissingGroupNestedLevel(t *testing.T) {
	var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
	var childCmd = &Command{Use: "child", Run: emptyRun}
	rootCmd.AddCommand(childCmd)

	childCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
	// Use the wrong group ID
	childCmd.AddCommand(&Command{Use: "cmd", GroupID: "wrong", Run: emptyRun})

Review comment (bug):
I think this should be named "TestWrongGroupFirstLevel". This also applies to the other negative tests below.

Code after:
	rootCmd.AddCommand(&Command{Use: "yyy", Run: emptyRun})

	output, err := executeCommand(rootCmd, "--help")
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	// The yyy command should be in the additional command "group"
	checkStringContains(t, output, "\nAdditional Commands:\n  yyy")
}

func TestAddGroup(t *testing.T) {
	var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}

	rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
	rootCmd.AddCommand(&Command{Use: "cmd", GroupID: "group", Run: emptyRun})

	output, err := executeCommand(rootCmd, "--help")
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	checkStringContains(t, output, "\nTest group\n  cmd")
}

func TestWrongGroupFirstLevel(t *testing.T) {
	var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}

	rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
	// Use the wrong group ID
	rootCmd.AddCommand(&Command{Use: "cmd", GroupID: "wrong", Run: emptyRun})

	defer func() {
		if recover() == nil {
			t.Errorf("The code should have panicked due to a missing group")
		}
	}()
	_, err := executeCommand(rootCmd, "--help")
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}
}

func TestWrongGroupNestedLevel(t *testing.T) {
	var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
	var childCmd = &Command{Use: "child", Run: emptyRun}
	rootCmd.AddCommand(childCmd)

	childCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
	// Use the wrong group ID
	childCmd.AddCommand(&Command{Use: "cmd", GroupID: "wrong", Run: emptyRun})