From 29914adefbe7018d2eb0604930a81924b2b8ba83 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Wed, 1 Dec 2021 14:44:45 -0500 Subject: [PATCH] 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. Signed-off-by: Marc Khouzam --- args_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/args_test.go b/args_test.go index ded3d0d59..6bd41c8fd 100644 --- a/args_test.go +++ b/args_test.go @@ -292,3 +292,32 @@ func TestMatchAll(t *testing.T) { }) } } + +// 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) + } +}