Skip to content

Commit

Permalink
Add backwards-compatibility tests for legacyArgs()
Browse files Browse the repository at this point in the history
These tests make sure we don't break backwards-compatibility with
respect to the current behaviour of legacyArgs().

See #1500 for the back-story.

Signed-off-by: Marc Khouzam <[email protected]>
  • Loading branch information
marckhouzam committed Dec 1, 2021
1 parent 9e1d6f1 commit 6cc0a16
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,32 @@ func TestChildTakesArgs(t *testing.T) {
t.Fatalf("Unexpected error: %v", err)
}
}

// This test make sure we keep backwards-compatibility with respect
// to the legacyArgs() function.
// It makes sure the root command accepts arguments if it does not have
// sub-commands.
func TestLegacyArgsRootAcceptsArgs(t *testing.T) {
rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}

_, err := executeCommand(rootCmd, "somearg")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}

// This test make sure we keep backwards-compatibility with respect
// to the legacyArgs() function.
// It makes sure a sub-command accepts arguments and further sub-commands
func TestLegacyArgsSubcmdAcceptsArgs(t *testing.T) {
rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}
childCmd := &Command{Use: "child", Args: nil, Run: emptyRun}
grandchildCmd := &Command{Use: "grandchild", Args: nil, Run: emptyRun}
rootCmd.AddCommand(childCmd)
childCmd.AddCommand(grandchildCmd)

_, err := executeCommand(rootCmd, "child", "somearg")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}

0 comments on commit 6cc0a16

Please sign in to comment.