Skip to content

Commit e55aff1

Browse files
marckhouzamhoshsadiq
authored andcommitted
Add backwards-compatibility tests for legacyArgs()
These tests make sure we don't break backwards-compatibility with respect to the current behaviour of legacyArgs(). See #1500 for the back-story. Merge spf13/cobra#1547 Signed-off-by: Marc Khouzam <[email protected]>
1 parent 212ce0c commit e55aff1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

args_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,32 @@ func TestMatchAll(t *testing.T) {
240240
})
241241
}
242242
}
243+
244+
// This test make sure we keep backwards-compatibility with respect
245+
// to the legacyArgs() function.
246+
// It makes sure the root command accepts arguments if it does not have
247+
// sub-commands.
248+
func TestLegacyArgsRootAcceptsArgs(t *testing.T) {
249+
rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}
250+
251+
_, err := executeCommand(rootCmd, "somearg")
252+
if err != nil {
253+
t.Fatalf("Unexpected error: %v", err)
254+
}
255+
}
256+
257+
// This test make sure we keep backwards-compatibility with respect
258+
// to the legacyArgs() function.
259+
// It makes sure a sub-command accepts arguments and further sub-commands
260+
func TestLegacyArgsSubcmdAcceptsArgs(t *testing.T) {
261+
rootCmd := &Command{Use: "root", Args: nil, Run: emptyRun}
262+
childCmd := &Command{Use: "child", Args: nil, Run: emptyRun}
263+
grandchildCmd := &Command{Use: "grandchild", Args: nil, Run: emptyRun}
264+
rootCmd.AddCommand(childCmd)
265+
childCmd.AddCommand(grandchildCmd)
266+
267+
_, err := executeCommand(rootCmd, "child", "somearg")
268+
if err != nil {
269+
t.Fatalf("Unexpected error: %v", err)
270+
}
271+
}

0 commit comments

Comments
 (0)