From 5c899f970ba8ea4a13efba8a26a1c5ba92be1a9e Mon Sep 17 00:00:00 2001 From: Hosh Sadiq Date: Sun, 13 Mar 2022 06:55:49 +0000 Subject: [PATCH] Move tests to *_test package --- args_test.go | 106 +++---- bash_completions_test.go | 30 +- command_test.go | 573 +++++++++++++++++++------------------ completions_test.go | 529 +++++++++++++++++----------------- doc/adoc_docs_test.go | 15 +- doc/cmd_test.go | 2 +- doc/exports_test.go | 3 + doc/man_docs_test.go | 41 +-- doc/md_docs_test.go | 15 +- doc/rest_docs_test.go | 13 +- doc/yaml_docs_test.go | 16 +- exports_test.go | 9 + fish_completions_test.go | 30 +- internal/util/util_test.go | 6 +- zulu_test.go | 10 +- 15 files changed, 715 insertions(+), 683 deletions(-) create mode 100644 doc/exports_test.go create mode 100644 exports_test.go diff --git a/args_test.go b/args_test.go index 4e53356..3073992 100644 --- a/args_test.go +++ b/args_test.go @@ -1,16 +1,18 @@ -package zulu +package zulu_test import ( "fmt" "strings" "testing" + + "github.com/gowarden/zulu" ) type argsTestcase struct { - exerr string // Expected error key (see map[string][string]) - args PositionalArgs // Args validator - wValid bool // Define `ValidArgs` in the command - rargs []string // Runtime args + exerr string // Expected error key (see map[string][string]) + args zulu.PositionalArgs // Args validator + wValid bool // Define `ValidArgs` in the command + rargs []string // Runtime args } var errStrings = map[string]string{ @@ -23,7 +25,7 @@ var errStrings = map[string]string{ } func (tc *argsTestcase) test(t *testing.T) { - c := &Command{ + c := &zulu.Command{ Use: "c", Args: tc.args, RunE: emptyRun, @@ -66,9 +68,9 @@ func testArgs(t *testing.T, tests map[string]argsTestcase) { func TestArgs_No(t *testing.T) { testArgs(t, map[string]argsTestcase{ - " | ": {"", NoArgs, false, []string{}}, - " | Arb": {"unknown", NoArgs, false, []string{"one"}}, - "Valid | Valid": {"unknown", NoArgs, true, []string{"one"}}, + " | ": {"", zulu.NoArgs, false, []string{}}, + " | Arb": {"unknown", zulu.NoArgs, false, []string{"one"}}, + "Valid | Valid": {"unknown", zulu.NoArgs, true, []string{"one"}}, }) } func TestArgs_Nil(t *testing.T) { @@ -80,57 +82,57 @@ func TestArgs_Nil(t *testing.T) { } func TestArgs_Arbitrary(t *testing.T) { testArgs(t, map[string]argsTestcase{ - " | Arb": {"", ArbitraryArgs, false, []string{"a", "b"}}, - "Valid | Valid": {"", ArbitraryArgs, true, []string{"one", "two"}}, - "Valid | Invalid": {"invalid", ArbitraryArgs, true, []string{"a"}}, + " | Arb": {"", zulu.ArbitraryArgs, false, []string{"a", "b"}}, + "Valid | Valid": {"", zulu.ArbitraryArgs, true, []string{"one", "two"}}, + "Valid | Invalid": {"invalid", zulu.ArbitraryArgs, true, []string{"a"}}, }) } func TestArgs_MinimumN(t *testing.T) { testArgs(t, map[string]argsTestcase{ - " | Arb": {"", MinimumNArgs(2), false, []string{"a", "b", "c"}}, - "Valid | Valid": {"", MinimumNArgs(2), true, []string{"one", "three"}}, - "Valid | Invalid": {"invalid", MinimumNArgs(2), true, []string{"a", "b"}}, - " | Less": {"less", MinimumNArgs(2), false, []string{"a"}}, - "Valid | Less": {"less", MinimumNArgs(2), true, []string{"one"}}, - "Valid | LessInvalid": {"invalid", MinimumNArgs(2), true, []string{"a"}}, + " | Arb": {"", zulu.MinimumNArgs(2), false, []string{"a", "b", "c"}}, + "Valid | Valid": {"", zulu.MinimumNArgs(2), true, []string{"one", "three"}}, + "Valid | Invalid": {"invalid", zulu.MinimumNArgs(2), true, []string{"a", "b"}}, + " | Less": {"less", zulu.MinimumNArgs(2), false, []string{"a"}}, + "Valid | Less": {"less", zulu.MinimumNArgs(2), true, []string{"one"}}, + "Valid | LessInvalid": {"invalid", zulu.MinimumNArgs(2), true, []string{"a"}}, }) } func TestArgs_MaximumN(t *testing.T) { testArgs(t, map[string]argsTestcase{ - " | Arb": {"", MaximumNArgs(3), false, []string{"a", "b"}}, - "Valid | Valid": {"", MaximumNArgs(2), true, []string{"one", "three"}}, - "Valid | Invalid": {"invalid", MaximumNArgs(2), true, []string{"a", "b"}}, - " | More": {"more", MaximumNArgs(2), false, []string{"a", "b", "c"}}, - "Valid | More": {"more", MaximumNArgs(2), true, []string{"one", "three", "two"}}, - "Valid | MoreInvalid": {"invalid", MaximumNArgs(2), true, []string{"a", "b", "c"}}, + " | Arb": {"", zulu.MaximumNArgs(3), false, []string{"a", "b"}}, + "Valid | Valid": {"", zulu.MaximumNArgs(2), true, []string{"one", "three"}}, + "Valid | Invalid": {"invalid", zulu.MaximumNArgs(2), true, []string{"a", "b"}}, + " | More": {"more", zulu.MaximumNArgs(2), false, []string{"a", "b", "c"}}, + "Valid | More": {"more", zulu.MaximumNArgs(2), true, []string{"one", "three", "two"}}, + "Valid | MoreInvalid": {"invalid", zulu.MaximumNArgs(2), true, []string{"a", "b", "c"}}, }) } func TestArgs_Exact(t *testing.T) { testArgs(t, map[string]argsTestcase{ - " | Arb": {"", ExactArgs(3), false, []string{"a", "b", "c"}}, - "Valid | Valid": {"", ExactArgs(3), true, []string{"three", "one", "two"}}, - "Valid | Invalid": {"invalid", ExactArgs(3), true, []string{"three", "a", "two"}}, - " | InvalidCount": {"notexact", ExactArgs(2), false, []string{"a", "b", "c"}}, - "Valid | InvalidCount": {"notexact", ExactArgs(2), true, []string{"three", "one", "two"}}, - "Valid | InvalidCountInvalid": {"invalid", ExactArgs(2), true, []string{"three", "a", "two"}}, + " | Arb": {"", zulu.ExactArgs(3), false, []string{"a", "b", "c"}}, + "Valid | Valid": {"", zulu.ExactArgs(3), true, []string{"three", "one", "two"}}, + "Valid | Invalid": {"invalid", zulu.ExactArgs(3), true, []string{"three", "a", "two"}}, + " | InvalidCount": {"notexact", zulu.ExactArgs(2), false, []string{"a", "b", "c"}}, + "Valid | InvalidCount": {"notexact", zulu.ExactArgs(2), true, []string{"three", "one", "two"}}, + "Valid | InvalidCountInvalid": {"invalid", zulu.ExactArgs(2), true, []string{"three", "a", "two"}}, }) } func TestArgs_Range(t *testing.T) { testArgs(t, map[string]argsTestcase{ - " | Arb": {"", RangeArgs(2, 4), false, []string{"a", "b", "c"}}, - "Valid | Valid": {"", RangeArgs(2, 4), true, []string{"three", "one", "two"}}, - "Valid | Invalid": {"invalid", RangeArgs(2, 4), true, []string{"three", "a", "two"}}, - " | InvalidCount": {"notinrange", RangeArgs(2, 4), false, []string{"a"}}, - "Valid | InvalidCount": {"notinrange", RangeArgs(2, 4), true, []string{"two"}}, - "Valid | InvalidCountInvalid": {"invalid", RangeArgs(2, 4), true, []string{"a"}}, + " | Arb": {"", zulu.RangeArgs(2, 4), false, []string{"a", "b", "c"}}, + "Valid | Valid": {"", zulu.RangeArgs(2, 4), true, []string{"three", "one", "two"}}, + "Valid | Invalid": {"invalid", zulu.RangeArgs(2, 4), true, []string{"three", "a", "two"}}, + " | InvalidCount": {"notinrange", zulu.RangeArgs(2, 4), false, []string{"a"}}, + "Valid | InvalidCount": {"notinrange", zulu.RangeArgs(2, 4), true, []string{"two"}}, + "Valid | InvalidCountInvalid": {"invalid", zulu.RangeArgs(2, 4), true, []string{"a"}}, }) } // Takes(No)Args func TestRootTakesNoArgs(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{Use: "child", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", RunE: emptyRun} rootCmd.AddCommand(childCmd) _, err := executeCommand(rootCmd, "illegal", "args") @@ -146,8 +148,8 @@ func TestRootTakesNoArgs(t *testing.T) { } func TestRootTakesArgs(t *testing.T) { - rootCmd := &Command{Use: "root", Args: ArbitraryArgs, RunE: emptyRun} - childCmd := &Command{Use: "child", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Args: zulu.ArbitraryArgs, RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", RunE: emptyRun} rootCmd.AddCommand(childCmd) _, err := executeCommand(rootCmd, "legal", "args") @@ -157,8 +159,8 @@ func TestRootTakesArgs(t *testing.T) { } func TestChildTakesNoArgs(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{Use: "child", Args: NoArgs, RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", Args: zulu.NoArgs, RunE: emptyRun} rootCmd.AddCommand(childCmd) _, err := executeCommand(rootCmd, "child", "illegal", "args") @@ -174,8 +176,8 @@ func TestChildTakesNoArgs(t *testing.T) { } func TestChildTakesArgs(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{Use: "child", Args: ArbitraryArgs, RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", Args: zulu.ArbitraryArgs, RunE: emptyRun} rootCmd.AddCommand(childCmd) _, err := executeCommand(rootCmd, "child", "legal", "args") @@ -187,9 +189,9 @@ func TestChildTakesArgs(t *testing.T) { func TestMatchAll(t *testing.T) { // Somewhat contrived example check that ensures there are exactly 3 // arguments, and each argument is exactly 2 bytes long. - pargs := MatchAll( - ExactArgs(3), - func(cmd *Command, args []string) error { + pargs := zulu.MatchAll( + zulu.ExactArgs(3), + func(cmd *zulu.Command, args []string) error { for _, arg := range args { if len([]byte(arg)) != 2 { return fmt.Errorf("expected to be exactly 2 bytes long") @@ -217,7 +219,7 @@ func TestMatchAll(t *testing.T) { }, } - rootCmd := &Command{Use: "root", Args: pargs, RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Args: pargs, RunE: emptyRun} for name, tc := range testCases { t.Run(name, func(t *testing.T) { @@ -237,7 +239,7 @@ func TestMatchAll(t *testing.T) { // 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, RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Args: nil, RunE: emptyRun} _, err := executeCommand(rootCmd, "somearg") if err != nil { @@ -249,9 +251,9 @@ func TestLegacyArgsRootAcceptsArgs(t *testing.T) { // 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, RunE: emptyRun} - childCmd := &Command{Use: "child", Args: nil, RunE: emptyRun} - grandchildCmd := &Command{Use: "grandchild", Args: nil, RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Args: nil, RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", Args: nil, RunE: emptyRun} + grandchildCmd := &zulu.Command{Use: "grandchild", Args: nil, RunE: emptyRun} rootCmd.AddCommand(childCmd) childCmd.AddCommand(grandchildCmd) diff --git a/bash_completions_test.go b/bash_completions_test.go index 00bebbf..08ceaa6 100644 --- a/bash_completions_test.go +++ b/bash_completions_test.go @@ -1,4 +1,4 @@ -package zulu +package zulu_test import ( "bytes" @@ -6,6 +6,8 @@ import ( "os" "strings" "testing" + + "github.com/gowarden/zulu" ) func checkOmit(t *testing.T, found, unexpected string) { @@ -21,8 +23,8 @@ func check(t *testing.T, found, expected string) { } func TestCompleteNoDesCmdInBashScript(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child := &zulu.Command{ Use: "child", ValidArgsFunction: validArgsFunc, RunE: emptyRun, @@ -33,12 +35,12 @@ func TestCompleteNoDesCmdInBashScript(t *testing.T) { assertNoErr(t, rootCmd.GenBashCompletion(buf, false)) output := buf.String() - check(t, output, ShellCompNoDescRequestCmd) + check(t, output, zulu.ShellCompNoDescRequestCmd) } func TestCompleteCmdInBashScript(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child := &zulu.Command{ Use: "child", ValidArgsFunction: validArgsFunc, RunE: emptyRun, @@ -49,12 +51,12 @@ func TestCompleteCmdInBashScript(t *testing.T) { assertNoErr(t, rootCmd.GenBashCompletion(buf, true)) output := buf.String() - check(t, output, ShellCompRequestCmd) - checkOmit(t, output, ShellCompNoDescRequestCmd) + check(t, output, zulu.ShellCompRequestCmd) + checkOmit(t, output, zulu.ShellCompNoDescRequestCmd) } func TestBashProgWithDash(t *testing.T) { - rootCmd := &Command{Use: "root-dash", Args: NoArgs, RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root-dash", Args: zulu.NoArgs, RunE: emptyRun} buf := new(bytes.Buffer) assertNoErr(t, rootCmd.GenBashCompletion(buf, false)) output := buf.String() @@ -69,7 +71,7 @@ func TestBashProgWithDash(t *testing.T) { } func TestBashProgWithColon(t *testing.T) { - rootCmd := &Command{Use: "root:colon", Args: NoArgs, RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root:colon", Args: zulu.NoArgs, RunE: emptyRun} buf := new(bytes.Buffer) assertNoErr(t, rootCmd.GenBashCompletion(buf, false)) output := buf.String() @@ -91,8 +93,8 @@ func TestGenBashCompletionFile(t *testing.T) { defer os.RemoveAll("./tmp") - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child := &zulu.Command{ Use: "child", ValidArgsFunction: validArgsFunc, RunE: emptyRun, @@ -113,8 +115,8 @@ func TestFailGenBashCompletionFile(t *testing.T) { f, _ := os.OpenFile("./tmp/test", os.O_CREATE, 0400) defer f.Close() - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child := &zulu.Command{ Use: "child", ValidArgsFunction: validArgsFunc, RunE: emptyRun, diff --git a/command_test.go b/command_test.go index 450b96c..3be1199 100644 --- a/command_test.go +++ b/command_test.go @@ -1,4 +1,4 @@ -package zulu +package zulu_test import ( "bytes" @@ -13,16 +13,17 @@ import ( "testing" "github.com/gowarden/zflag" + "github.com/gowarden/zulu" ) -func emptyRun(*Command, []string) error { return nil } +func emptyRun(*zulu.Command, []string) error { return nil } -func executeCommand(root *Command, args ...string) (output string, err error) { +func executeCommand(root *zulu.Command, args ...string) (output string, err error) { _, output, err = executeCommandC(root, args...) return output, err } -func executeCommandWithContext(ctx context.Context, root *Command, args ...string) (output string, err error) { +func executeCommandWithContext(ctx context.Context, root *zulu.Command, args ...string) (output string, err error) { buf := new(bytes.Buffer) root.SetOut(buf) root.SetErr(buf) @@ -33,7 +34,7 @@ func executeCommandWithContext(ctx context.Context, root *Command, args ...strin return buf.String(), err } -func executeCommandC(root *Command, args ...string) (c *Command, output string, err error) { +func executeCommandC(root *zulu.Command, args ...string) (c *zulu.Command, output string, err error) { buf := new(bytes.Buffer) root.SetOut(buf) root.SetErr(buf) @@ -44,7 +45,7 @@ func executeCommandC(root *Command, args ...string) (c *Command, output string, return c, buf.String(), err } -func executeCommandWithContextC(ctx context.Context, root *Command, args ...string) (c *Command, output string, err error) { +func executeCommandWithContextC(ctx context.Context, root *zulu.Command, args ...string) (c *zulu.Command, output string, err error) { buf := new(bytes.Buffer) root.SetOut(buf) root.SetErr(buf) @@ -75,13 +76,13 @@ const onetwo = "one two" func TestSingleCommand(t *testing.T) { var rootCmdArgs []string - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", - Args: ExactArgs(2), - RunE: func(_ *Command, args []string) error { rootCmdArgs = args; return nil }, + Args: zulu.ExactArgs(2), + RunE: func(_ *zulu.Command, args []string) error { rootCmdArgs = args; return nil }, } - aCmd := &Command{Use: "a", Args: NoArgs, RunE: emptyRun} - bCmd := &Command{Use: "b", Args: NoArgs, RunE: emptyRun} + aCmd := &zulu.Command{Use: "a", Args: zulu.NoArgs, RunE: emptyRun} + bCmd := &zulu.Command{Use: "b", Args: zulu.NoArgs, RunE: emptyRun} rootCmd.AddCommand(aCmd, bCmd) output, err := executeCommand(rootCmd, "one", "two") @@ -100,13 +101,13 @@ func TestSingleCommand(t *testing.T) { func TestChildCommand(t *testing.T) { var child1CmdArgs []string - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child1Cmd := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child1Cmd := &zulu.Command{ Use: "child1", - Args: ExactArgs(2), - RunE: func(_ *Command, args []string) error { child1CmdArgs = args; return nil }, + Args: zulu.ExactArgs(2), + RunE: func(_ *zulu.Command, args []string) error { child1CmdArgs = args; return nil }, } - child2Cmd := &Command{Use: "child2", Args: NoArgs, RunE: emptyRun} + child2Cmd := &zulu.Command{Use: "child2", Args: zulu.NoArgs, RunE: emptyRun} rootCmd.AddCommand(child1Cmd, child2Cmd) output, err := executeCommand(rootCmd, "child1", "one", "two") @@ -124,7 +125,7 @@ func TestChildCommand(t *testing.T) { } func TestCallCommandWithoutSubcommands(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} _, err := executeCommand(rootCmd) if err != nil { t.Errorf("Calling command without subcommands should not have error: %v", err) @@ -132,8 +133,8 @@ func TestCallCommandWithoutSubcommands(t *testing.T) { } func TestRootExecuteUnknownCommand(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - rootCmd.AddCommand(&Command{Use: "child", RunE: emptyRun}) + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + rootCmd.AddCommand(&zulu.Command{Use: "child", RunE: emptyRun}) output, _ := executeCommand(rootCmd, "unknown") @@ -145,8 +146,8 @@ func TestRootExecuteUnknownCommand(t *testing.T) { } func TestSubcommandExecuteC(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{Use: "child", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", RunE: emptyRun} rootCmd.AddCommand(childCmd) c, output, err := executeCommandC(rootCmd, "child") @@ -165,16 +166,16 @@ func TestSubcommandExecuteC(t *testing.T) { func TestExecuteContext(t *testing.T) { ctx := context.TODO() - ctxRun := func(cmd *Command, args []string) error { + ctxRun := func(cmd *zulu.Command, args []string) error { if cmd.Context() != ctx { t.Errorf("Command %q must have context when called with ExecuteContext", cmd.Use) } return nil } - rootCmd := &Command{Use: "root", RunE: ctxRun, PreRunE: ctxRun} - childCmd := &Command{Use: "child", RunE: ctxRun, PreRunE: ctxRun} - granchildCmd := &Command{Use: "grandchild", RunE: ctxRun, PreRunE: ctxRun} + rootCmd := &zulu.Command{Use: "root", RunE: ctxRun, PreRunE: ctxRun} + childCmd := &zulu.Command{Use: "child", RunE: ctxRun, PreRunE: ctxRun} + granchildCmd := &zulu.Command{Use: "grandchild", RunE: ctxRun, PreRunE: ctxRun} childCmd.AddCommand(granchildCmd) rootCmd.AddCommand(childCmd) @@ -195,7 +196,7 @@ func TestExecuteContext(t *testing.T) { func TestExecuteContextC(t *testing.T) { ctx := context.TODO() - ctxRun := func(cmd *Command, args []string) error { + ctxRun := func(cmd *zulu.Command, args []string) error { if cmd.Context() != ctx { t.Errorf("Command %q must have context when called with ExecuteContext", cmd.Use) } @@ -203,9 +204,9 @@ func TestExecuteContextC(t *testing.T) { return nil } - rootCmd := &Command{Use: "root", RunE: ctxRun, PreRunE: ctxRun} - childCmd := &Command{Use: "child", RunE: ctxRun, PreRunE: ctxRun} - granchildCmd := &Command{Use: "grandchild", RunE: ctxRun, PreRunE: ctxRun} + rootCmd := &zulu.Command{Use: "root", RunE: ctxRun, PreRunE: ctxRun} + childCmd := &zulu.Command{Use: "child", RunE: ctxRun, PreRunE: ctxRun} + granchildCmd := &zulu.Command{Use: "grandchild", RunE: ctxRun, PreRunE: ctxRun} childCmd.AddCommand(granchildCmd) rootCmd.AddCommand(childCmd) @@ -224,16 +225,16 @@ func TestExecuteContextC(t *testing.T) { } func TestExecute_NoContext(t *testing.T) { - run := func(cmd *Command, args []string) error { + run := func(cmd *zulu.Command, args []string) error { if cmd.Context() != context.Background() { t.Errorf("Command %s must have background context", cmd.Use) } return nil } - rootCmd := &Command{Use: "root", RunE: run, PreRunE: run} - childCmd := &Command{Use: "child", RunE: run, PreRunE: run} - granchildCmd := &Command{Use: "grandchild", RunE: run, PreRunE: run} + rootCmd := &zulu.Command{Use: "root", RunE: run, PreRunE: run} + childCmd := &zulu.Command{Use: "child", RunE: run, PreRunE: run} + granchildCmd := &zulu.Command{Use: "grandchild", RunE: run, PreRunE: run} childCmd.AddCommand(granchildCmd) rootCmd.AddCommand(childCmd) @@ -252,10 +253,10 @@ func TestExecute_NoContext(t *testing.T) { } func TestRootUnknownCommandSilenced(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} rootCmd.SilenceErrors = true rootCmd.SilenceUsage = true - rootCmd.AddCommand(&Command{Use: "child", RunE: emptyRun}) + rootCmd.AddCommand(&zulu.Command{Use: "child", RunE: emptyRun}) output, _ := executeCommand(rootCmd, "unknown") if output != "" { @@ -265,17 +266,17 @@ func TestRootUnknownCommandSilenced(t *testing.T) { func TestCommandAlias(t *testing.T) { var timesCmdArgs []string - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - echoCmd := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + echoCmd := &zulu.Command{ Use: "echo", Aliases: []string{"say", "tell"}, - Args: NoArgs, + Args: zulu.NoArgs, RunE: emptyRun, } - timesCmd := &Command{ + timesCmd := &zulu.Command{ Use: "times", - Args: ExactArgs(2), - RunE: func(_ *Command, args []string) error { timesCmdArgs = args; return nil }, + Args: zulu.ExactArgs(2), + RunE: func(_ *zulu.Command, args []string) error { timesCmdArgs = args; return nil }, } echoCmd.AddCommand(timesCmd) rootCmd.AddCommand(echoCmd) @@ -295,16 +296,16 @@ func TestCommandAlias(t *testing.T) { } func TestEnablePrefixMatching(t *testing.T) { - EnablePrefixMatching = true + zulu.EnablePrefixMatching = true var aCmdArgs []string - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - aCmd := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + aCmd := &zulu.Command{ Use: "aCmd", - Args: ExactArgs(2), - RunE: func(_ *Command, args []string) error { aCmdArgs = args; return nil }, + Args: zulu.ExactArgs(2), + RunE: func(_ *zulu.Command, args []string) error { aCmdArgs = args; return nil }, } - bCmd := &Command{Use: "bCmd", Args: NoArgs, RunE: emptyRun} + bCmd := &zulu.Command{Use: "bCmd", Args: zulu.NoArgs, RunE: emptyRun} rootCmd.AddCommand(aCmd, bCmd) output, err := executeCommand(rootCmd, "a", "one", "two") @@ -320,24 +321,24 @@ func TestEnablePrefixMatching(t *testing.T) { t.Errorf("aCmdArgs expected: %q, got: %q", onetwo, got) } - EnablePrefixMatching = false + zulu.EnablePrefixMatching = false } func TestAliasPrefixMatching(t *testing.T) { - EnablePrefixMatching = true + zulu.EnablePrefixMatching = true var timesCmdArgs []string - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - echoCmd := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + echoCmd := &zulu.Command{ Use: "echo", Aliases: []string{"say", "tell"}, - Args: NoArgs, + Args: zulu.NoArgs, RunE: emptyRun, } - timesCmd := &Command{ + timesCmd := &zulu.Command{ Use: "times", - Args: ExactArgs(2), - RunE: func(_ *Command, args []string) error { timesCmdArgs = args; return nil }, + Args: zulu.ExactArgs(2), + RunE: func(_ *zulu.Command, args []string) error { timesCmdArgs = args; return nil }, } echoCmd.AddCommand(timesCmd) rootCmd.AddCommand(echoCmd) @@ -355,7 +356,7 @@ func TestAliasPrefixMatching(t *testing.T) { t.Errorf("timesCmdArgs expected: %v, got: %v", onetwo, got) } - EnablePrefixMatching = false + zulu.EnablePrefixMatching = false } // TestChildSameName checks the correct behaviour of zulu in cases, @@ -363,13 +364,13 @@ func TestAliasPrefixMatching(t *testing.T) { // is executed with args "foo foo". func TestChildSameName(t *testing.T) { var fooCmdArgs []string - rootCmd := &Command{Use: "foo", Args: NoArgs, RunE: emptyRun} - fooCmd := &Command{ + rootCmd := &zulu.Command{Use: "foo", Args: zulu.NoArgs, RunE: emptyRun} + fooCmd := &zulu.Command{ Use: "foo", - Args: ExactArgs(2), - RunE: func(_ *Command, args []string) error { fooCmdArgs = args; return nil }, + Args: zulu.ExactArgs(2), + RunE: func(_ *zulu.Command, args []string) error { fooCmdArgs = args; return nil }, } - barCmd := &Command{Use: "bar", Args: NoArgs, RunE: emptyRun} + barCmd := &zulu.Command{Use: "bar", Args: zulu.NoArgs, RunE: emptyRun} rootCmd.AddCommand(fooCmd, barCmd) output, err := executeCommand(rootCmd, "foo", "one", "two") @@ -391,12 +392,12 @@ func TestChildSameName(t *testing.T) { // with the same name. func TestGrandChildSameName(t *testing.T) { var fooCmdArgs []string - rootCmd := &Command{Use: "foo", Args: NoArgs, RunE: emptyRun} - barCmd := &Command{Use: "bar", Args: NoArgs, RunE: emptyRun} - fooCmd := &Command{ + rootCmd := &zulu.Command{Use: "foo", Args: zulu.NoArgs, RunE: emptyRun} + barCmd := &zulu.Command{Use: "bar", Args: zulu.NoArgs, RunE: emptyRun} + fooCmd := &zulu.Command{ Use: "foo", - Args: ExactArgs(2), - RunE: func(_ *Command, args []string) error { fooCmdArgs = args; return nil }, + Args: zulu.ExactArgs(2), + RunE: func(_ *zulu.Command, args []string) error { fooCmdArgs = args; return nil }, } barCmd.AddCommand(fooCmd) rootCmd.AddCommand(barCmd) @@ -417,10 +418,10 @@ func TestGrandChildSameName(t *testing.T) { func TestFlagLong(t *testing.T) { var cArgs []string - c := &Command{ + c := &zulu.Command{ Use: "c", - Args: ArbitraryArgs, - RunE: func(_ *Command, args []string) error { cArgs = args; return nil }, + Args: zulu.ArbitraryArgs, + RunE: func(_ *zulu.Command, args []string) error { cArgs = args; return nil }, } var intFlagValue int @@ -454,10 +455,10 @@ func TestFlagLong(t *testing.T) { func TestFlagShort(t *testing.T) { var cArgs []string - c := &Command{ + c := &zulu.Command{ Use: "c", - Args: ArbitraryArgs, - RunE: func(_ *Command, args []string) error { cArgs = args; return nil }, + Args: zulu.ArbitraryArgs, + RunE: func(_ *zulu.Command, args []string) error { cArgs = args; return nil }, } var intFlagValue int @@ -487,8 +488,8 @@ func TestFlagShort(t *testing.T) { } func TestChildFlag(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{Use: "child", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", RunE: emptyRun} rootCmd.AddCommand(childCmd) var intFlagValue int @@ -508,8 +509,8 @@ func TestChildFlag(t *testing.T) { } func TestChildFlagWithParentLocalFlag(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{Use: "child", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", RunE: emptyRun} rootCmd.AddCommand(childCmd) var intFlagValue int @@ -529,7 +530,7 @@ func TestChildFlagWithParentLocalFlag(t *testing.T) { } func TestFlagInvalidInput(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} rootCmd.Flags().Int("intf", -1, "", zflag.OptShorthand('i')) _, err := executeCommand(rootCmd, "-iabc") @@ -541,8 +542,8 @@ func TestFlagInvalidInput(t *testing.T) { } func TestFlagBeforeCommand(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{Use: "child", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", RunE: emptyRun} rootCmd.AddCommand(childCmd) var flagValue int @@ -626,14 +627,14 @@ func TestStripFlags(t *testing.T) { }, } - c := &Command{Use: "c", RunE: emptyRun} + c := &zulu.Command{Use: "c", RunE: emptyRun} c.PersistentFlags().Bool("persist", false, "", zflag.OptShorthand('p')) c.Flags().Int("int", -1, "", zflag.OptShorthand('i')) c.Flags().String("str", "", "", zflag.OptShorthand('s')) c.Flags().Bool("bool", false, "", zflag.OptShorthand('b')) for i, test := range tests { - got := stripFlags(test.input, c) + got := zulu.StripFlags(test.input, c) if !reflect.DeepEqual(test.output, got) { t.Errorf("(%v) Expected: %v, got: %v", i, test.output, got) } @@ -642,10 +643,10 @@ func TestStripFlags(t *testing.T) { func TestDisableFlagParsing(t *testing.T) { var cArgs []string - c := &Command{ + c := &zulu.Command{ Use: "c", DisableFlagParsing: true, - RunE: func(_ *Command, args []string) error { cArgs = args; return nil }, + RunE: func(_ *zulu.Command, args []string) error { cArgs = args; return nil }, } args := []string{"cmd", "-v", "-race", "-file", "foo.go"} @@ -664,10 +665,10 @@ func TestDisableFlagParsing(t *testing.T) { func TestPersistentFlagsOnSameCommand(t *testing.T) { var rootCmdArgs []string - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", - Args: ArbitraryArgs, - RunE: func(_ *Command, args []string) error { rootCmdArgs = args; return nil }, + Args: zulu.ArbitraryArgs, + RunE: func(_ *zulu.Command, args []string) error { rootCmdArgs = args; return nil }, } var flagValue int @@ -693,7 +694,7 @@ func TestPersistentFlagsOnSameCommand(t *testing.T) { // TestEmptyInputs checks, // if flags correctly parsed with blank strings in args. func TestEmptyInputs(t *testing.T) { - c := &Command{Use: "c", RunE: emptyRun} + c := &zulu.Command{Use: "c", RunE: emptyRun} var flagValue int c.Flags().IntVar(&flagValue, "intf", -1, "", zflag.OptShorthand('i')) @@ -715,8 +716,8 @@ func TestOverwrittenFlag(t *testing.T) { // TODO: This test fails, but should work. t.Skip() - parent := &Command{Use: "parent", RunE: emptyRun} - child := &Command{Use: "child", RunE: emptyRun} + parent := &zulu.Command{Use: "parent", RunE: emptyRun} + child := &zulu.Command{Use: "child", RunE: emptyRun} parent.PersistentFlags().Bool("boolf", false, "") parent.PersistentFlags().Int("intf", -1, "") @@ -745,11 +746,11 @@ func TestOverwrittenFlag(t *testing.T) { func TestPersistentFlagsOnChild(t *testing.T) { var childCmdArgs []string - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{ + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{ Use: "child", - Args: ArbitraryArgs, - RunE: func(_ *Command, args []string) error { childCmdArgs = args; return nil }, + Args: zulu.ArbitraryArgs, + RunE: func(_ *zulu.Command, args []string) error { childCmdArgs = args; return nil }, } rootCmd.AddCommand(childCmd) @@ -779,9 +780,9 @@ func TestPersistentFlagsOnChild(t *testing.T) { } func TestRequiredFlags(t *testing.T) { - c := &Command{Use: "c", RunE: emptyRun} - c.Flags().String("foo1", "", "", FlagOptRequired()) - c.Flags().String("foo2", "", "", FlagOptRequired()) + c := &zulu.Command{Use: "c", RunE: emptyRun} + c.Flags().String("foo1", "", "", zulu.FlagOptRequired()) + c.Flags().String("foo2", "", "", zulu.FlagOptRequired()) c.Flags().String("bar", "", "") expected := fmt.Sprintf("required flag(s) %q, %q not set", "foo1", "foo2") @@ -794,10 +795,10 @@ func TestRequiredFlags(t *testing.T) { } func TestRequiredFlagsWithCustomFlagErrorFunc(t *testing.T) { - c := &Command{Use: "c", RunE: emptyRun} - c.Flags().String("foo1", "", "", FlagOptRequired()) + c := &zulu.Command{Use: "c", RunE: emptyRun} + c.Flags().String("foo1", "", "", zulu.FlagOptRequired()) silentError := "failed flag parsing" - c.SetFlagErrorFunc(func(c *Command, err error) error { + c.SetFlagErrorFunc(func(c *zulu.Command, err error) error { c.Println(err) c.Println(c.UsageString()) return errors.New(silentError) @@ -815,14 +816,14 @@ func TestRequiredFlagsWithCustomFlagErrorFunc(t *testing.T) { } func TestPersistentRequiredFlags(t *testing.T) { - parent := &Command{Use: "parent", RunE: emptyRun} - parent.PersistentFlags().String("foo1", "", "", FlagOptRequired()) - parent.PersistentFlags().String("foo2", "", "", FlagOptRequired()) + parent := &zulu.Command{Use: "parent", RunE: emptyRun} + parent.PersistentFlags().String("foo1", "", "", zulu.FlagOptRequired()) + parent.PersistentFlags().String("foo2", "", "", zulu.FlagOptRequired()) parent.Flags().String("foo3", "", "") - child := &Command{Use: "child", RunE: emptyRun} - child.Flags().String("bar1", "", "", FlagOptRequired()) - child.Flags().String("bar2", "", "", FlagOptRequired()) + child := &zulu.Command{Use: "child", RunE: emptyRun} + child.Flags().String("bar1", "", "", zulu.FlagOptRequired()) + child.Flags().String("bar2", "", "", zulu.FlagOptRequired()) child.Flags().String("bar3", "", "") parent.AddCommand(child) @@ -839,11 +840,11 @@ func TestPersistentRequiredFlagsWithDisableFlagParsing(t *testing.T) { // Make sure a required persistent flag does not break // commands that disable flag parsing - parent := &Command{Use: "parent", RunE: emptyRun} - parent.PersistentFlags().Bool("foo", false, "", FlagOptRequired()) + parent := &zulu.Command{Use: "parent", RunE: emptyRun} + parent.PersistentFlags().Bool("foo", false, "", zulu.FlagOptRequired()) flag := parent.PersistentFlags().Lookup("foo") - child := &Command{Use: "child", RunE: emptyRun} + child := &zulu.Command{Use: "child", RunE: emptyRun} child.DisableFlagParsing = true parent.AddCommand(child) @@ -867,9 +868,9 @@ func TestPersistentRequiredFlagsWithDisableFlagParsing(t *testing.T) { func TestInitHelpFlagMergesFlags(t *testing.T) { usage := "custom flag" - rootCmd := &Command{Use: "root"} + rootCmd := &zulu.Command{Use: "root"} rootCmd.PersistentFlags().Bool("help", false, "custom flag") - childCmd := &Command{Use: "child"} + childCmd := &zulu.Command{Use: "child"} rootCmd.AddCommand(childCmd) childCmd.InitDefaultHelpFlag() @@ -880,8 +881,8 @@ func TestInitHelpFlagMergesFlags(t *testing.T) { } func TestHelpCommandExecuted(t *testing.T) { - rootCmd := &Command{Use: "root", Long: "Long description", RunE: emptyRun} - rootCmd.AddCommand(&Command{Use: "child", RunE: emptyRun}) + rootCmd := &zulu.Command{Use: "root", Long: "Long description", RunE: emptyRun} + rootCmd.AddCommand(&zulu.Command{Use: "child", RunE: emptyRun}) output, err := executeCommand(rootCmd, "help") if err != nil { @@ -892,8 +893,8 @@ func TestHelpCommandExecuted(t *testing.T) { } func TestHelpCommandExecutedOnChild(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{Use: "child", Long: "Long description", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", Long: "Long description", RunE: emptyRun} rootCmd.AddCommand(childCmd) output, err := executeCommand(rootCmd, "help", "child") @@ -905,16 +906,16 @@ func TestHelpCommandExecutedOnChild(t *testing.T) { } func TestSetHelpCommand(t *testing.T) { - c := &Command{Use: "c", RunE: emptyRun} - c.AddCommand(&Command{Use: "empty", RunE: emptyRun}) + c := &zulu.Command{Use: "c", RunE: emptyRun} + c.AddCommand(&zulu.Command{Use: "empty", RunE: emptyRun}) expected := "WORKS" - c.SetHelpCommand(&Command{ + c.SetHelpCommand(&zulu.Command{ Use: "help [command]", Short: "Help about any command", Long: `Help provides help for any command in the application. Simply type ` + c.Name() + ` help [path to command] for full details.`, - RunE: func(c *Command, _ []string) error { c.Print(expected); return nil }, + RunE: func(c *zulu.Command, _ []string) error { c.Print(expected); return nil }, }) got, err := executeCommand(c, "help") @@ -928,7 +929,7 @@ func TestSetHelpCommand(t *testing.T) { } func TestHelpFlagExecuted(t *testing.T) { - rootCmd := &Command{Use: "root", Long: "Long description", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Long: "Long description", RunE: emptyRun} output, err := executeCommand(rootCmd, "--help") if err != nil { @@ -939,8 +940,8 @@ func TestHelpFlagExecuted(t *testing.T) { } func TestHelpFlagExecutedOnChild(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{Use: "child", Long: "Long description", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", Long: "Long description", RunE: emptyRun} rootCmd.AddCommand(childCmd) output, err := executeCommand(rootCmd, "child", "--help") @@ -956,9 +957,9 @@ func TestHelpFlagExecutedOnChild(t *testing.T) { // that has no other flags. // Related to https://github.com/spf13/cobra/issues/302. func TestHelpFlagInHelp(t *testing.T) { - parentCmd := &Command{Use: "parent", RunE: func(*Command, []string) error { return nil }} + parentCmd := &zulu.Command{Use: "parent", RunE: func(*zulu.Command, []string) error { return nil }} - childCmd := &Command{Use: "child", RunE: func(*Command, []string) error { return nil }} + childCmd := &zulu.Command{Use: "child", RunE: func(*zulu.Command, []string) error { return nil }} parentCmd.AddCommand(childCmd) output, err := executeCommand(parentCmd, "help", "child") @@ -970,7 +971,7 @@ func TestHelpFlagInHelp(t *testing.T) { } func TestFlagsInUsage(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: func(*Command, []string) error { return nil }} + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: func(*zulu.Command, []string) error { return nil }} output, err := executeCommand(rootCmd, "--help") if err != nil { t.Errorf("Unexpected error: %v", err) @@ -980,8 +981,8 @@ func TestFlagsInUsage(t *testing.T) { } func TestHelpExecutedOnNonRunnableChild(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{Use: "child", Long: "Long description"} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", Long: "Long description"} rootCmd.AddCommand(childCmd) output, err := executeCommand(rootCmd, "child") @@ -993,7 +994,7 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) { } func TestVersionFlagExecuted(t *testing.T) { - rootCmd := &Command{Use: "root", Version: "1.0.0", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Version: "1.0.0", RunE: emptyRun} output, err := executeCommand(rootCmd, "--version", "arg1") if err != nil { @@ -1004,7 +1005,7 @@ func TestVersionFlagExecuted(t *testing.T) { } func TestVersionFlagExecutedWithNoName(t *testing.T) { - rootCmd := &Command{Version: "1.0.0", RunE: emptyRun} + rootCmd := &zulu.Command{Version: "1.0.0", RunE: emptyRun} output, err := executeCommand(rootCmd, "--version", "arg1") if err != nil { @@ -1015,7 +1016,7 @@ func TestVersionFlagExecutedWithNoName(t *testing.T) { } func TestShortAndLongVersionFlagInHelp(t *testing.T) { - rootCmd := &Command{Use: "root", Version: "1.0.0", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Version: "1.0.0", RunE: emptyRun} output, err := executeCommand(rootCmd, "--help") if err != nil { @@ -1026,7 +1027,7 @@ func TestShortAndLongVersionFlagInHelp(t *testing.T) { } func TestLongVersionFlagOnlyInHelpWhenShortPredefined(t *testing.T) { - rootCmd := &Command{Use: "root", Version: "1.0.0", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Version: "1.0.0", RunE: emptyRun} rootCmd.Flags().String("foo", "", "not a version flag", zflag.OptShorthand('v')) output, err := executeCommand(rootCmd, "--help") @@ -1039,7 +1040,7 @@ func TestLongVersionFlagOnlyInHelpWhenShortPredefined(t *testing.T) { } func TestShorthandVersionFlagExecuted(t *testing.T) { - rootCmd := &Command{Use: "root", Version: "1.0.0", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Version: "1.0.0", RunE: emptyRun} output, err := executeCommand(rootCmd, "-v", "arg1") if err != nil { @@ -1050,7 +1051,7 @@ func TestShorthandVersionFlagExecuted(t *testing.T) { } func TestVersionTemplate(t *testing.T) { - rootCmd := &Command{Use: "root", Version: "1.0.0", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Version: "1.0.0", RunE: emptyRun} rootCmd.SetVersionTemplate(`customized version: {{.Version}}`) output, err := executeCommand(rootCmd, "--version", "arg1") @@ -1062,7 +1063,7 @@ func TestVersionTemplate(t *testing.T) { } func TestShorthandVersionTemplate(t *testing.T) { - rootCmd := &Command{Use: "root", Version: "1.0.0", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Version: "1.0.0", RunE: emptyRun} rootCmd.SetVersionTemplate(`customized version: {{.Version}}`) output, err := executeCommand(rootCmd, "-v", "arg1") @@ -1074,8 +1075,8 @@ func TestShorthandVersionTemplate(t *testing.T) { } func TestVersionFlagExecutedOnSubcommand(t *testing.T) { - rootCmd := &Command{Use: "root", Version: "1.0.0"} - rootCmd.AddCommand(&Command{Use: "sub", RunE: emptyRun}) + rootCmd := &zulu.Command{Use: "root", Version: "1.0.0"} + rootCmd.AddCommand(&zulu.Command{Use: "sub", RunE: emptyRun}) output, err := executeCommand(rootCmd, "--version", "sub") if err != nil { @@ -1086,8 +1087,8 @@ func TestVersionFlagExecutedOnSubcommand(t *testing.T) { } func TestShorthandVersionFlagExecutedOnSubcommand(t *testing.T) { - rootCmd := &Command{Use: "root", Version: "1.0.0"} - rootCmd.AddCommand(&Command{Use: "sub", RunE: emptyRun}) + rootCmd := &zulu.Command{Use: "root", Version: "1.0.0"} + rootCmd.AddCommand(&zulu.Command{Use: "sub", RunE: emptyRun}) output, err := executeCommand(rootCmd, "-v", "sub") if err != nil { @@ -1098,8 +1099,8 @@ func TestShorthandVersionFlagExecutedOnSubcommand(t *testing.T) { } func TestVersionFlagOnlyAddedToRoot(t *testing.T) { - rootCmd := &Command{Use: "root", Version: "1.0.0", RunE: emptyRun} - rootCmd.AddCommand(&Command{Use: "sub", RunE: emptyRun}) + rootCmd := &zulu.Command{Use: "root", Version: "1.0.0", RunE: emptyRun} + rootCmd.AddCommand(&zulu.Command{Use: "sub", RunE: emptyRun}) _, err := executeCommand(rootCmd, "sub", "--version") if err == nil { @@ -1110,8 +1111,8 @@ func TestVersionFlagOnlyAddedToRoot(t *testing.T) { } func TestShortVersionFlagOnlyAddedToRoot(t *testing.T) { - rootCmd := &Command{Use: "root", Version: "1.0.0", RunE: emptyRun} - rootCmd.AddCommand(&Command{Use: "sub", RunE: emptyRun}) + rootCmd := &zulu.Command{Use: "root", Version: "1.0.0", RunE: emptyRun} + rootCmd.AddCommand(&zulu.Command{Use: "sub", RunE: emptyRun}) _, err := executeCommand(rootCmd, "sub", "-v") if err == nil { @@ -1122,7 +1123,7 @@ func TestShortVersionFlagOnlyAddedToRoot(t *testing.T) { } func TestVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} _, err := executeCommand(rootCmd, "--version") if err == nil { @@ -1132,7 +1133,7 @@ func TestVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) { } func TestShorthandVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} _, err := executeCommand(rootCmd, "-v") if err == nil { @@ -1142,7 +1143,7 @@ func TestShorthandVersionFlagOnlyExistsIfVersionNonEmpty(t *testing.T) { } func TestShorthandVersionFlagOnlyAddedIfShorthandNotDefined(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun, Version: "1.2.3"} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun, Version: "1.2.3"} rootCmd.Flags().String("notversion", "", "not a version flag", zflag.OptShorthand('v')) _, err := executeCommand(rootCmd, "-v") @@ -1154,7 +1155,7 @@ func TestShorthandVersionFlagOnlyAddedIfShorthandNotDefined(t *testing.T) { } func TestShorthandVersionFlagOnlyAddedIfVersionNotDefined(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun, Version: "1.2.3"} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun, Version: "1.2.3"} rootCmd.Flags().Bool("version", false, "a different kind of version flag") _, err := executeCommand(rootCmd, "-v") @@ -1165,8 +1166,8 @@ func TestShorthandVersionFlagOnlyAddedIfVersionNotDefined(t *testing.T) { } func TestUsageIsNotPrintedTwice(t *testing.T) { - var cmd = &Command{Use: "root"} - var sub = &Command{Use: "sub"} + var cmd = &zulu.Command{Use: "root"} + var sub = &zulu.Command{Use: "sub"} cmd.AddCommand(sub) output, _ := executeCommand(cmd, "") @@ -1176,14 +1177,14 @@ func TestUsageIsNotPrintedTwice(t *testing.T) { } func TestVisitParents(t *testing.T) { - c := &Command{Use: "app"} - sub := &Command{Use: "sub"} - dsub := &Command{Use: "dsub"} + c := &zulu.Command{Use: "app"} + sub := &zulu.Command{Use: "sub"} + dsub := &zulu.Command{Use: "dsub"} sub.AddCommand(dsub) c.AddCommand(sub) total := 0 - add := func(x *Command) { + add := func(x *zulu.Command) { total++ } sub.VisitParents(add) @@ -1205,8 +1206,8 @@ func TestVisitParents(t *testing.T) { } func TestSuggestions(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - timesCmd := &Command{ + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + timesCmd := &zulu.Command{ Use: "times", SuggestFor: []string{"counts"}, RunE: emptyRun, @@ -1252,8 +1253,8 @@ func TestSuggestions(t *testing.T) { } func TestRemoveCommand(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - childCmd := &Command{Use: "child", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", RunE: emptyRun} rootCmd.AddCommand(childCmd) rootCmd.RemoveCommand(childCmd) @@ -1265,14 +1266,14 @@ func TestRemoveCommand(t *testing.T) { func TestReplaceCommandWithRemove(t *testing.T) { childUsed := 0 - rootCmd := &Command{Use: "root", RunE: emptyRun} - child1Cmd := &Command{ + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + child1Cmd := &zulu.Command{ Use: "child", - RunE: func(*Command, []string) error { childUsed = 1; return nil }, + RunE: func(*zulu.Command, []string) error { childUsed = 1; return nil }, } - child2Cmd := &Command{ + child2Cmd := &zulu.Command{ Use: "child", - RunE: func(*Command, []string) error { childUsed = 2; return nil }, + RunE: func(*zulu.Command, []string) error { childUsed = 2; return nil }, } rootCmd.AddCommand(child1Cmd) rootCmd.RemoveCommand(child1Cmd) @@ -1295,8 +1296,8 @@ func TestReplaceCommandWithRemove(t *testing.T) { } func TestDeprecatedCommand(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - deprecatedCmd := &Command{ + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + deprecatedCmd := &zulu.Command{ Use: "deprecated", Deprecated: "This command is deprecated", RunE: emptyRun, @@ -1320,25 +1321,25 @@ func TestHooks(t *testing.T) { persPostArgs string ) - c := &Command{ + c := &zulu.Command{ Use: "c", - PersistentPreRunE: func(_ *Command, args []string) error { + PersistentPreRunE: func(_ *zulu.Command, args []string) error { persPreArgs = strings.Join(args, " ") return nil }, - PreRunE: func(_ *Command, args []string) error { + PreRunE: func(_ *zulu.Command, args []string) error { preArgs = strings.Join(args, " ") return nil }, - RunE: func(_ *Command, args []string) error { + RunE: func(_ *zulu.Command, args []string) error { runArgs = strings.Join(args, " ") return nil }, - PostRunE: func(_ *Command, args []string) error { + PostRunE: func(_ *zulu.Command, args []string) error { postArgs = strings.Join(args, " ") return nil }, - PersistentPostRunE: func(_ *Command, args []string) error { + PersistentPostRunE: func(_ *zulu.Command, args []string) error { persPostArgs = strings.Join(args, " ") return nil }, @@ -1371,13 +1372,13 @@ func TestHooks(t *testing.T) { func TestPersistentHooks(t *testing.T) { hooksArgs := map[string]string{} - getTestHookFn := func(key string, extras ...string) HookFuncE { - return func(cmd *Command, args []string) error { + getTestHookFn := func(key string, extras ...string) zulu.HookFuncE { + return func(cmd *zulu.Command, args []string) error { hooksArgs[key] = strings.Join(args, " ") + strings.Join(extras, "") return nil } } - parentCmd := &Command{ + parentCmd := &zulu.Command{ Use: "parent", PersistentInitializeE: getTestHookFn("parentPersInitArgs"), InitializeE: getTestHookFn("parentInitArgs"), @@ -1390,7 +1391,7 @@ func TestPersistentHooks(t *testing.T) { PersistentFinalizeE: getTestHookFn("parentPersFinArgs"), } - childCmd := &Command{ + childCmd := &zulu.Command{ Use: "child", PersistentInitializeE: getTestHookFn("childPersInitArgs"), InitializeE: getTestHookFn("childInitArgs"), @@ -1494,8 +1495,8 @@ func TestGlobalNormFuncPropagation(t *testing.T) { return zflag.NormalizedName(name) } - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{Use: "child", RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{Use: "child", RunE: emptyRun} rootCmd.AddCommand(childCmd) rootCmd.SetGlobalNormalizationFunc(normFunc) @@ -1514,7 +1515,7 @@ func TestNormPassedOnLocal(t *testing.T) { return zflag.NormalizedName(strings.ToUpper(name)) } - c := &Command{} + c := &zulu.Command{} c.Flags().Bool("flagname", true, "this is a dummy flag") c.SetGlobalNormalizationFunc(toUpper) if c.LocalFlags().Lookup("flagname") != c.LocalFlags().Lookup("FLAGNAME") { @@ -1528,15 +1529,15 @@ func TestNormPassedOnInherited(t *testing.T) { return zflag.NormalizedName(strings.ToUpper(name)) } - c := &Command{} + c := &zulu.Command{} c.SetGlobalNormalizationFunc(toUpper) - child1 := &Command{} + child1 := &zulu.Command{} c.AddCommand(child1) c.PersistentFlags().Bool("flagname", true, "") - child2 := &Command{} + child2 := &zulu.Command{} c.AddCommand(child2) inherited := child1.InheritedFlags() @@ -1559,7 +1560,7 @@ func TestConsistentNormalizedName(t *testing.T) { return zflag.NormalizedName(name) } - c := &Command{} + c := &zulu.Command{} c.Flags().Bool("flagname", true, "") c.SetGlobalNormalizationFunc(toUpper) c.SetGlobalNormalizationFunc(n) @@ -1573,8 +1574,8 @@ func TestFlagOnZflagCommandLine(t *testing.T) { flagName := "flagOnCommandLine" zflag.String(flagName, "", "about my flag") - c := &Command{Use: "c", RunE: emptyRun} - c.AddCommand(&Command{Use: "child", RunE: emptyRun}) + c := &zulu.Command{Use: "c", RunE: emptyRun} + c.AddCommand(&zulu.Command{Use: "child", RunE: emptyRun}) output, _ := executeCommand(c, "--help") checkStringContains(t, output, flagName) @@ -1586,10 +1587,10 @@ func TestFlagOnZflagCommandLine(t *testing.T) { // if hidden commands run as intended. func TestHiddenCommandExecutes(t *testing.T) { executed := false - c := &Command{ + c := &zulu.Command{ Use: "c", Hidden: true, - RunE: func(*Command, []string) error { executed = true; return nil }, + RunE: func(*zulu.Command, []string) error { executed = true; return nil }, } output, err := executeCommand(c) @@ -1607,22 +1608,22 @@ func TestHiddenCommandExecutes(t *testing.T) { // test to ensure hidden commands do not show up in usage/help text func TestHiddenCommandIsHidden(t *testing.T) { - c := &Command{Use: "c", Hidden: true, RunE: emptyRun} + c := &zulu.Command{Use: "c", Hidden: true, RunE: emptyRun} if c.IsAvailableCommand() { t.Errorf("Hidden command should be unavailable") } } func TestCommandsAreSorted(t *testing.T) { - EnableCommandSorting = true + zulu.EnableCommandSorting = true originalNames := []string{"middle", "zlast", "afirst"} expectedNames := []string{"afirst", "middle", "zlast"} - var rootCmd = &Command{Use: "root"} + var rootCmd = &zulu.Command{Use: "root"} for _, name := range originalNames { - rootCmd.AddCommand(&Command{Use: name}) + rootCmd.AddCommand(&zulu.Command{Use: name}) } for i, c := range rootCmd.Commands() { @@ -1632,18 +1633,18 @@ func TestCommandsAreSorted(t *testing.T) { } } - EnableCommandSorting = true + zulu.EnableCommandSorting = true } func TestEnableCommandSortingIsDisabled(t *testing.T) { - EnableCommandSorting = false + zulu.EnableCommandSorting = false originalNames := []string{"middle", "zlast", "afirst"} - var rootCmd = &Command{Use: "root"} + var rootCmd = &zulu.Command{Use: "root"} for _, name := range originalNames { - rootCmd.AddCommand(&Command{Use: name}) + rootCmd.AddCommand(&zulu.Command{Use: name}) } for i, c := range rootCmd.Commands() { @@ -1653,14 +1654,14 @@ func TestEnableCommandSortingIsDisabled(t *testing.T) { } } - EnableCommandSorting = true + zulu.EnableCommandSorting = true } func TestUsageWithGroup(t *testing.T) { - var rootCmd = &Command{Use: "root", Short: "test", CompletionOptions: CompletionOptions{DisableDefaultCmd: true}, RunE: emptyRun} + var rootCmd = &zulu.Command{Use: "root", Short: "test", CompletionOptions: zulu.CompletionOptions{DisableDefaultCmd: true}, RunE: emptyRun} - rootCmd.AddCommand(&Command{Use: "cmd1", Group: "group1", RunE: emptyRun}) - rootCmd.AddCommand(&Command{Use: "cmd2", Group: "group2", RunE: emptyRun}) + rootCmd.AddCommand(&zulu.Command{Use: "cmd1", Group: "group1", RunE: emptyRun}) + rootCmd.AddCommand(&zulu.Command{Use: "cmd2", Group: "group2", RunE: emptyRun}) output, err := executeCommand(rootCmd, "--help") if err != nil { @@ -1674,9 +1675,9 @@ func TestUsageWithGroup(t *testing.T) { } func TestUsageHelpGroup(t *testing.T) { - var rootCmd = &Command{Use: "root", Short: "test", CompletionOptions: CompletionOptions{DisableDefaultCmd: true}, RunE: emptyRun} + var rootCmd = &zulu.Command{Use: "root", Short: "test", CompletionOptions: zulu.CompletionOptions{DisableDefaultCmd: true}, RunE: emptyRun} - rootCmd.AddCommand(&Command{Use: "xxx", Group: "group", RunE: emptyRun}) + rootCmd.AddCommand(&zulu.Command{Use: "xxx", Group: "group", RunE: emptyRun}) rootCmd.SetHelpCommandGroup("group") output, err := executeCommand(rootCmd, "--help") @@ -1690,10 +1691,10 @@ func TestUsageHelpGroup(t *testing.T) { } func TestAddGroup(t *testing.T) { - var rootCmd = &Command{Use: "root", Short: "test", RunE: emptyRun} + var rootCmd = &zulu.Command{Use: "root", Short: "test", RunE: emptyRun} - rootCmd.AddGroup(&Group{Group: "group", Title: "Test group"}) - rootCmd.AddCommand(&Command{Use: "cmd", Group: "group", RunE: emptyRun}) + rootCmd.AddGroup(&zulu.Group{Group: "group", Title: "Test group"}) + rootCmd.AddCommand(&zulu.Command{Use: "cmd", Group: "group", RunE: emptyRun}) output, err := executeCommand(rootCmd, "--help") if err != nil { @@ -1704,7 +1705,7 @@ func TestAddGroup(t *testing.T) { } func TestSetOut(t *testing.T) { - c := &Command{} + c := &zulu.Command{} c.SetOut(nil) if out := c.OutOrStdout(); out != os.Stdout { t.Errorf("Expected setting output to nil to revert back to stdout") @@ -1712,7 +1713,7 @@ func TestSetOut(t *testing.T) { } func TestSetErr(t *testing.T) { - c := &Command{} + c := &zulu.Command{} c.SetErr(nil) if out := c.ErrOrStderr(); out != os.Stderr { t.Errorf("Expected setting error to nil to revert back to stderr") @@ -1720,7 +1721,7 @@ func TestSetErr(t *testing.T) { } func TestSetIn(t *testing.T) { - c := &Command{} + c := &zulu.Command{} c.SetIn(nil) if out := c.InOrStdin(); out != os.Stdin { t.Errorf("Expected setting input to nil to revert back to stdin") @@ -1728,14 +1729,14 @@ func TestSetIn(t *testing.T) { } func TestUsageStringRedirected(t *testing.T) { - c := &Command{} + c := &zulu.Command{} - c.usageFunc = func(cmd *Command) error { + c.SetUsageFunc(func(cmd *zulu.Command) error { cmd.Print("[stdout1]") cmd.PrintErr("[stderr2]") cmd.Print("[stdout3]") return nil - } + }) expected := "[stdout1][stderr2][stdout3]" if got := c.UsageString(); got != expected { @@ -1745,8 +1746,8 @@ func TestUsageStringRedirected(t *testing.T) { func TestCommandPrintRedirection(t *testing.T) { errBuff, outBuff := bytes.NewBuffer(nil), bytes.NewBuffer(nil) - root := &Command{ - RunE: func(cmd *Command, args []string) error { + root := &zulu.Command{ + RunE: func(cmd *zulu.Command, args []string) error { cmd.PrintErr("PrintErr") cmd.PrintErrln("PrintErr", "line") @@ -1786,10 +1787,10 @@ func TestCommandPrintRedirection(t *testing.T) { } func TestFlagErrorFunc(t *testing.T) { - c := &Command{Use: "c", RunE: emptyRun} + c := &zulu.Command{Use: "c", RunE: emptyRun} expectedFmt := "This is expected: %v" - c.SetFlagErrorFunc(func(_ *Command, err error) error { + c.SetFlagErrorFunc(func(_ *zulu.Command, err error) error { return fmt.Errorf(expectedFmt, err) }) @@ -1806,7 +1807,7 @@ func TestFlagErrorFunc(t *testing.T) { // if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false. // Related to https://github.com/spf13/cobra/issues/404. func TestSortedFlags(t *testing.T) { - c := &Command{} + c := &zulu.Command{} c.Flags().SortFlags = false names := []string{"C", "B", "A", "D"} for _, name := range names { @@ -1818,7 +1819,7 @@ func TestSortedFlags(t *testing.T) { if i == len(names) { return } - if stringInSlice(f.Name, names) { + if zulu.StringInSlice(f.Name, names) { if names[i] != f.Name { t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name) } @@ -1833,8 +1834,10 @@ func TestSortedFlags(t *testing.T) { // Related to https://github.com/spf13/cobra/issues/443. func TestMergeCommandLineToFlags(t *testing.T) { zflag.Bool("boolflag", false, "") - c := &Command{Use: "c", RunE: emptyRun} - c.mergePersistentFlags() + c := &zulu.Command{Use: "c", RunE: emptyRun} + // help flag is not actually needed here, it's a way to enforce + // zulu.Command.mergePersistentFlags is called. + c.InitDefaultHelpFlag() if c.Flags().Lookup("boolflag") == nil { t.Fatal("Expecting to have flag from CommandLine in c.Flags()") } @@ -1846,7 +1849,7 @@ func TestMergeCommandLineToFlags(t *testing.T) { // if zulu.Execute() prints a message, if a deprecated flag is used. // Related to https://github.com/spf13/cobra/issues/463. func TestUseDeprecatedFlags(t *testing.T) { - c := &Command{Use: "c", RunE: emptyRun} + c := &zulu.Command{Use: "c", RunE: emptyRun} c.Flags().Bool("deprecated", false, "deprecated flag", zflag.OptShorthand('d'), zflag.OptDeprecated("This flag is deprecated")) output, err := executeCommand(c, "c", "-d") @@ -1857,11 +1860,11 @@ func TestUseDeprecatedFlags(t *testing.T) { } func TestTraverseWithParentFlags(t *testing.T) { - rootCmd := &Command{Use: "root", TraverseChildren: true} + rootCmd := &zulu.Command{Use: "root", TraverseChildren: true} rootCmd.Flags().String("str", "", "") rootCmd.Flags().Bool("bool", false, "", zflag.OptShorthand('b')) - childCmd := &Command{Use: "child"} + childCmd := &zulu.Command{Use: "child"} childCmd.Flags().Int("int", -1, "") rootCmd.AddCommand(childCmd) @@ -1879,10 +1882,10 @@ func TestTraverseWithParentFlags(t *testing.T) { } func TestTraverseNoParentFlags(t *testing.T) { - rootCmd := &Command{Use: "root", TraverseChildren: true} + rootCmd := &zulu.Command{Use: "root", TraverseChildren: true} rootCmd.Flags().String("foo", "", "foo things") - childCmd := &Command{Use: "child"} + childCmd := &zulu.Command{Use: "child"} childCmd.Flags().String("str", "", "") rootCmd.AddCommand(childCmd) @@ -1899,9 +1902,9 @@ func TestTraverseNoParentFlags(t *testing.T) { } func TestTraverseWithBadParentFlags(t *testing.T) { - rootCmd := &Command{Use: "root", TraverseChildren: true} + rootCmd := &zulu.Command{Use: "root", TraverseChildren: true} - childCmd := &Command{Use: "child"} + childCmd := &zulu.Command{Use: "child"} childCmd.Flags().String("str", "", "") rootCmd.AddCommand(childCmd) @@ -1917,10 +1920,10 @@ func TestTraverseWithBadParentFlags(t *testing.T) { } func TestTraverseWithBadChildFlag(t *testing.T) { - rootCmd := &Command{Use: "root", TraverseChildren: true} + rootCmd := &zulu.Command{Use: "root", TraverseChildren: true} rootCmd.Flags().String("str", "", "") - childCmd := &Command{Use: "child"} + childCmd := &zulu.Command{Use: "child"} rootCmd.AddCommand(childCmd) // Expect no error because the last commands args shouldn't be parsed in @@ -1938,12 +1941,12 @@ func TestTraverseWithBadChildFlag(t *testing.T) { } func TestTraverseWithTwoSubcommands(t *testing.T) { - rootCmd := &Command{Use: "root", TraverseChildren: true} + rootCmd := &zulu.Command{Use: "root", TraverseChildren: true} - subCmd := &Command{Use: "sub", TraverseChildren: true} + subCmd := &zulu.Command{Use: "sub", TraverseChildren: true} rootCmd.AddCommand(subCmd) - subsubCmd := &Command{ + subsubCmd := &zulu.Command{ Use: "subsub", } subCmd.AddCommand(subsubCmd) @@ -1960,7 +1963,7 @@ func TestTraverseWithTwoSubcommands(t *testing.T) { // TestUpdateName checks if c.Name() updates on changed c.Use. // Related to https://github.com/spf13/cobra/pull/422#discussion_r143918343. func TestUpdateName(t *testing.T) { - c := &Command{Use: "name xyz"} + c := &zulu.Command{Use: "name xyz"} originalName := c.Name() c.Use = "changedName abc" @@ -1977,15 +1980,15 @@ type calledAsTestcase struct { } func (tc *calledAsTestcase) test(t *testing.T) { - defer func(ov bool) { EnablePrefixMatching = ov }(EnablePrefixMatching) - EnablePrefixMatching = tc.epm + defer func(ov bool) { zulu.EnablePrefixMatching = ov }(zulu.EnablePrefixMatching) + zulu.EnablePrefixMatching = tc.epm - var called *Command - run := func(c *Command, _ []string) error { t.Logf("called: %q", c.Name()); called = c; return nil } + var called *zulu.Command + run := func(c *zulu.Command, _ []string) error { t.Logf("called: %q", c.Name()); called = c; return nil } - parent := &Command{Use: "parent", RunE: run} - child1 := &Command{Use: "child1", RunE: run, Aliases: []string{"this"}} - child2 := &Command{Use: "child2", RunE: run, Aliases: []string{"that"}} + parent := &zulu.Command{Use: "parent", RunE: run} + child1 := &zulu.Command{Use: "child1", RunE: run, Aliases: []string{"this"}} + child2 := &zulu.Command{Use: "child2", RunE: run, Aliases: []string{"that"}} parent.AddCommand(child1) parent.AddCommand(child2) @@ -2033,7 +2036,7 @@ func TestCalledAs(t *testing.T) { } func TestFParseErrWhitelistBackwardCompatibility(t *testing.T) { - c := &Command{Use: "c", RunE: emptyRun} + c := &zulu.Command{Use: "c", RunE: emptyRun} c.Flags().Bool("boola", false, "a boolean flag", zflag.OptShorthand('a')) output, err := executeCommand(c, "c", "-a", "--unknown", "flag") @@ -2044,10 +2047,10 @@ func TestFParseErrWhitelistBackwardCompatibility(t *testing.T) { } func TestFParseErrWhitelistSameCommand(t *testing.T) { - c := &Command{ + c := &zulu.Command{ Use: "c", RunE: emptyRun, - FParseErrAllowList: FParseErrAllowList{ + FParseErrAllowList: zulu.FParseErrAllowList{ UnknownFlags: true, }, } @@ -2060,15 +2063,15 @@ func TestFParseErrWhitelistSameCommand(t *testing.T) { } func TestFParseErrWhitelistParentCommand(t *testing.T) { - root := &Command{ + root := &zulu.Command{ Use: "root", RunE: emptyRun, - FParseErrAllowList: FParseErrAllowList{ + FParseErrAllowList: zulu.FParseErrAllowList{ UnknownFlags: true, }, } - c := &Command{ + c := &zulu.Command{ Use: "child", RunE: emptyRun, } @@ -2084,15 +2087,15 @@ func TestFParseErrWhitelistParentCommand(t *testing.T) { } func TestFParseErrWhitelistChildCommand(t *testing.T) { - root := &Command{ + root := &zulu.Command{ Use: "root", RunE: emptyRun, } - c := &Command{ + c := &zulu.Command{ Use: "child", RunE: emptyRun, - FParseErrAllowList: FParseErrAllowList{ + FParseErrAllowList: zulu.FParseErrAllowList{ UnknownFlags: true, }, } @@ -2107,21 +2110,21 @@ func TestFParseErrWhitelistChildCommand(t *testing.T) { } func TestFParseErrWhitelistSiblingCommand(t *testing.T) { - root := &Command{ + root := &zulu.Command{ Use: "root", RunE: emptyRun, } - c := &Command{ + c := &zulu.Command{ Use: "child", RunE: emptyRun, - FParseErrAllowList: FParseErrAllowList{ + FParseErrAllowList: zulu.FParseErrAllowList{ UnknownFlags: true, }, } c.Flags().Bool("boola", false, "a boolean flag", zflag.OptShorthand('a')) - s := &Command{ + s := &zulu.Command{ Use: "sibling", RunE: emptyRun, } @@ -2138,7 +2141,7 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) { } func TestContext(t *testing.T) { - root := &Command{} + root := &zulu.Command{} if root.Context() == nil { t.Error("expected root.Context() != nil") } @@ -2146,9 +2149,9 @@ func TestContext(t *testing.T) { func TestSetContext(t *testing.T) { key, val := "foo", "bar" - root := &Command{ + root := &zulu.Command{ Use: "root", - RunE: func(cmd *Command, args []string) error { + RunE: func(cmd *zulu.Command, args []string) error { key := cmd.Context().Value(key) got, ok := key.(string) if !ok { @@ -2170,14 +2173,14 @@ func TestSetContext(t *testing.T) { func TestSetContextPreRun(t *testing.T) { key, val := "foo", "bar" - root := &Command{ + root := &zulu.Command{ Use: "root", - PreRunE: func(cmd *Command, args []string) error { + PreRunE: func(cmd *zulu.Command, args []string) error { ctx := context.WithValue(cmd.Context(), key, val) cmd.SetContext(ctx) return nil }, - RunE: func(cmd *Command, args []string) error { + RunE: func(cmd *zulu.Command, args []string) error { key := cmd.Context().Value(key) got, ok := key.(string) if !ok { @@ -2197,9 +2200,9 @@ func TestSetContextPreRun(t *testing.T) { func TestSetContextPreRunOverwrite(t *testing.T) { key, val := "foo", "bar" - root := &Command{ + root := &zulu.Command{ Use: "root", - RunE: func(cmd *Command, args []string) error { + RunE: func(cmd *zulu.Command, args []string) error { key := cmd.Context().Value(key) _, ok := key.(string) if ok { @@ -2218,17 +2221,17 @@ func TestSetContextPreRunOverwrite(t *testing.T) { func TestSetContextPersistentPreRun(t *testing.T) { key, val := "foo", "bar" - root := &Command{ + root := &zulu.Command{ Use: "root", - PersistentPreRunE: func(cmd *Command, args []string) error { + PersistentPreRunE: func(cmd *zulu.Command, args []string) error { ctx := context.WithValue(cmd.Context(), key, val) cmd.SetContext(ctx) return nil }, } - child := &Command{ + child := &zulu.Command{ Use: "child", - RunE: func(cmd *Command, args []string) error { + RunE: func(cmd *zulu.Command, args []string) error { key := cmd.Context().Value(key) got, ok := key.(string) if !ok { @@ -2249,13 +2252,13 @@ func TestSetContextPersistentPreRun(t *testing.T) { } func TestUsageTemplate(t *testing.T) { - createCmd := func() (*Command, *Command) { - root := &Command{ + createCmd := func() (*zulu.Command, *zulu.Command) { + root := &zulu.Command{ Use: "root", } - child := &Command{ + child := &zulu.Command{ Use: "child", - RunE: func(cmd *Command, args []string) error { return nil }, + RunE: func(cmd *zulu.Command, args []string) error { return nil }, } root.AddCommand(child) return root, child @@ -2264,7 +2267,7 @@ func TestUsageTemplate(t *testing.T) { tests := []struct { name string expectedUsage string - testCmd func(newOut io.Writer) *Command + testCmd func(newOut io.Writer) *zulu.Command }{ { name: "basic test", @@ -2276,7 +2279,7 @@ Available Commands: Use "root [command] --help" for more information about a command. `, - testCmd: func(newOut io.Writer) *Command { + testCmd: func(newOut io.Writer) *zulu.Command { root, child := createCmd() child.Short = "child I AM THE CHILD NOW" root.SetOut(newOut) @@ -2288,7 +2291,7 @@ Use "root [command] --help" for more information about a command. expectedUsage: `Usage: root child `, - testCmd: func(newOut io.Writer) *Command { + testCmd: func(newOut io.Writer) *zulu.Command { root, child := createCmd() root.SetOut(newOut) return child @@ -2302,7 +2305,7 @@ Use "root [command] --help" for more information about a command. Aliases: child, c `, - testCmd: func(newOut io.Writer) *Command { + testCmd: func(newOut io.Writer) *zulu.Command { root, child := createCmd() root.SetOut(newOut) @@ -2318,7 +2321,7 @@ Aliases: Examples: child sub --int 0 `, - testCmd: func(newOut io.Writer) *Command { + testCmd: func(newOut io.Writer) *zulu.Command { root, child := createCmd() root.SetOut(newOut) child.Example = "child sub --int 0" @@ -2372,7 +2375,7 @@ Additional help topics: Use "root child [command] --help" for more information about a command. `, - testCmd: func(newOut io.Writer) *Command { + testCmd: func(newOut io.Writer) *zulu.Command { root, child := createCmd() root.SetOut(newOut) @@ -2380,60 +2383,60 @@ Use "root child [command] --help" for more information about a command. child.Example = "child sub --int 0" pfs := root.PersistentFlags() - pfs.Int("pint", 1, "persistent int usage", zflag.OptShorthand('q'), zflag.OptGroup("group1"), FlagOptRequired()) + pfs.Int("pint", 1, "persistent int usage", zflag.OptShorthand('q'), zflag.OptGroup("group1"), zulu.FlagOptRequired()) pfs.Bool("pbool", false, "persistent bool usage", zflag.OptShorthand('c'), zflag.OptGroup("group2")) fs := child.Flags() fs.String("string1", "some", "string1 usage", zflag.OptShorthand('s')) fs.Bool("bool1", false, "bool1 usage", zflag.OptShorthand('b')) - fs.String("string2", "some", "string2 usage in group1", zflag.OptGroup("group1"), FlagOptRequired()) + fs.String("string2", "some", "string2 usage in group1", zflag.OptGroup("group1"), zulu.FlagOptRequired()) fs.Bool("bool2", false, "bool2 usage in group1", zflag.OptGroup("group1")) fs.String("string3", "some", "string3 usage in group2", zflag.OptGroup("group2")) - fs.Bool("bool3", false, "bool3 usage in group2", zflag.OptGroup("group2"), FlagOptRequired()) + fs.Bool("bool3", false, "bool3 usage in group2", zflag.OptGroup("group2"), zulu.FlagOptRequired()) - sub1 := &Command{ + sub1 := &zulu.Command{ Use: "sub1", Short: "sub1 short", - RunE: func(cmd *Command, args []string) error { return nil }, + RunE: func(cmd *zulu.Command, args []string) error { return nil }, } - sub2 := &Command{ + sub2 := &zulu.Command{ Use: "sub2", Short: "sub2 short", - RunE: func(cmd *Command, args []string) error { return nil }, + RunE: func(cmd *zulu.Command, args []string) error { return nil }, } - sub3 := &Command{ + sub3 := &zulu.Command{ Use: "sub3", Short: "sub3 short in group1", Group: "group1", - RunE: func(cmd *Command, args []string) error { return nil }, + RunE: func(cmd *zulu.Command, args []string) error { return nil }, } - sub4 := &Command{ + sub4 := &zulu.Command{ Use: "sub4", Short: "sub4 short in group1", Group: "group1", - RunE: func(cmd *Command, args []string) error { return nil }, + RunE: func(cmd *zulu.Command, args []string) error { return nil }, } - sub5 := &Command{ + sub5 := &zulu.Command{ Use: "sub5", Short: "sub5 short in group2", Group: "group2", - RunE: func(cmd *Command, args []string) error { return nil }, + RunE: func(cmd *zulu.Command, args []string) error { return nil }, } - sub6 := &Command{ + sub6 := &zulu.Command{ Use: "sub6", Short: "sub6 short in group2", Group: "group2", - RunE: func(cmd *Command, args []string) error { return nil }, + RunE: func(cmd *zulu.Command, args []string) error { return nil }, } - sub7 := &Command{ + sub7 := &zulu.Command{ Use: "sub7", Short: "short", } diff --git a/completions_test.go b/completions_test.go index 43635f5..4fb7a8e 100644 --- a/completions_test.go +++ b/completions_test.go @@ -1,4 +1,4 @@ -package zulu +package zulu_test import ( "bytes" @@ -7,11 +7,12 @@ import ( "testing" "github.com/gowarden/zflag" + "github.com/gowarden/zulu" ) -func validArgsFunc(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { +func validArgsFunc(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { if len(args) != 0 { - return nil, ShellCompDirectiveNoFileComp + return nil, zulu.ShellCompDirectiveNoFileComp } var completions []string @@ -20,12 +21,12 @@ func validArgsFunc(cmd *Command, args []string, toComplete string) ([]string, Sh completions = append(completions, comp) } } - return completions, ShellCompDirectiveDefault + return completions, zulu.ShellCompDirectiveDefault } -func validArgsFunc2(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { +func validArgsFunc2(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { if len(args) != 0 { - return nil, ShellCompDirectiveNoFileComp + return nil, zulu.ShellCompDirectiveNoFileComp } var completions []string @@ -34,34 +35,34 @@ func validArgsFunc2(cmd *Command, args []string, toComplete string) ([]string, S completions = append(completions, comp) } } - return completions, ShellCompDirectiveDefault + return completions, zulu.ShellCompDirectiveDefault } func TestCmdNameCompletionInGo(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", RunE: emptyRun, } - childCmd1 := &Command{ + childCmd1 := &zulu.Command{ Use: "firstChild", Short: "First command", RunE: emptyRun, } - childCmd2 := &Command{ + childCmd2 := &zulu.Command{ Use: "secondChild", RunE: emptyRun, } - hiddenCmd := &Command{ + hiddenCmd := &zulu.Command{ Use: "testHidden", Hidden: true, // Not completed RunE: emptyRun, } - deprecatedCmd := &Command{ + deprecatedCmd := &zulu.Command{ Use: "testDeprecated", Deprecated: "deprecated", // Not completed RunE: emptyRun, } - aliasedCmd := &Command{ + aliasedCmd := &zulu.Command{ Use: "aliased", Short: "A command with aliases", Aliases: []string{"testAlias", "testSynonym"}, // Not completed @@ -71,7 +72,7 @@ func TestCmdNameCompletionInGo(t *testing.T) { rootCmd.AddCommand(childCmd1, childCmd2, hiddenCmd, deprecatedCmd, aliasedCmd) // Test that sub-command names are completed - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -90,7 +91,7 @@ func TestCmdNameCompletionInGo(t *testing.T) { } // Test that sub-command names are completed with prefix - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "s") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "s") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -106,7 +107,7 @@ func TestCmdNameCompletionInGo(t *testing.T) { // Test that even with no valid sub-command matches, hidden, deprecated and // aliases are not completed - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "test") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "test") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -120,7 +121,7 @@ func TestCmdNameCompletionInGo(t *testing.T) { } // Test that sub-command names are completed with description - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -140,16 +141,16 @@ func TestCmdNameCompletionInGo(t *testing.T) { } func TestNoCmdNameCompletionInGo(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", RunE: emptyRun, } rootCmd.Flags().String("localroot", "", "local root flag") - childCmd1 := &Command{ + childCmd1 := &zulu.Command{ Use: "childCmd1", Short: "First command", - Args: MinimumNArgs(0), + Args: zulu.MinimumNArgs(0), RunE: emptyRun, } rootCmd.AddCommand(childCmd1) @@ -158,14 +159,14 @@ func TestNoCmdNameCompletionInGo(t *testing.T) { childCmd1.Flags().String("nonPersistent", "", "non-persistent flag", zflag.OptShorthand('n')) nonPersistentFlag := childCmd1.Flags().Lookup("nonPersistent") - childCmd2 := &Command{ + childCmd2 := &zulu.Command{ Use: "childCmd2", RunE: emptyRun, } childCmd1.AddCommand(childCmd2) // Test that sub-command names are not completed if there is an argument already - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "arg1", "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "childCmd1", "arg1", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -179,7 +180,7 @@ func TestNoCmdNameCompletionInGo(t *testing.T) { } // Test that sub-command names are not completed if a local non-persistent flag is present - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "--nonPersistent", "value", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "childCmd1", "--nonPersistent", "value", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -198,7 +199,7 @@ func TestNoCmdNameCompletionInGo(t *testing.T) { // set TraverseChildren to true on the root cmd rootCmd.TraverseChildren = true - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--localroot", "value", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--localroot", "value", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -220,7 +221,7 @@ func TestNoCmdNameCompletionInGo(t *testing.T) { // and TraverseChildren is set to true on the root cmd rootCmd.TraverseChildren = true - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--localroot", "value", "childCmd1", "--nonPersistent", "value", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--localroot", "value", "childCmd1", "--nonPersistent", "value", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -240,7 +241,7 @@ func TestNoCmdNameCompletionInGo(t *testing.T) { // Test that we don't use Traverse when we shouldn't. // This command should not return a completion since the command line is invalid without TraverseChildren. - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--localroot", "value", "childCmd1", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--localroot", "value", "childCmd1", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -254,7 +255,7 @@ func TestNoCmdNameCompletionInGo(t *testing.T) { } // Test that sub-command names are not completed if a local non-persistent short flag is present - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "-n", "value", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "childCmd1", "-n", "value", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -270,7 +271,7 @@ func TestNoCmdNameCompletionInGo(t *testing.T) { } // Test that sub-command names are completed with a persistent flag - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "--persistent", "value", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "childCmd1", "--persistent", "value", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -287,7 +288,7 @@ func TestNoCmdNameCompletionInGo(t *testing.T) { } // Test that sub-command names are completed with a persistent short flag - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "-p", "value", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "childCmd1", "-p", "value", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -305,14 +306,14 @@ func TestNoCmdNameCompletionInGo(t *testing.T) { } func TestValidArgsCompletionInGo(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", ValidArgs: []string{"one", "two", "three"}, - Args: MinimumNArgs(1), + Args: zulu.MinimumNArgs(1), } // Test that validArgs are completed - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -329,7 +330,7 @@ func TestValidArgsCompletionInGo(t *testing.T) { } // Test that validArgs are completed with prefix - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "o") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "o") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -344,7 +345,7 @@ func TestValidArgsCompletionInGo(t *testing.T) { } // Test that validArgs don't repeat - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "one", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "one", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -359,13 +360,13 @@ func TestValidArgsCompletionInGo(t *testing.T) { } func TestValidArgsAndCmdCompletionInGo(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", ValidArgs: []string{"one", "two"}, RunE: emptyRun, } - childCmd := &Command{ + childCmd := &zulu.Command{ Use: "thechild", RunE: emptyRun, } @@ -373,7 +374,7 @@ func TestValidArgsAndCmdCompletionInGo(t *testing.T) { rootCmd.AddCommand(childCmd) // Test that both sub-commands and validArgs are completed - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -392,7 +393,7 @@ func TestValidArgsAndCmdCompletionInGo(t *testing.T) { } // Test that both sub-commands and validArgs are completed with prefix - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -409,13 +410,13 @@ func TestValidArgsAndCmdCompletionInGo(t *testing.T) { } func TestValidArgsFuncAndCmdCompletionInGo(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", ValidArgsFunction: validArgsFunc, RunE: emptyRun, } - childCmd := &Command{ + childCmd := &zulu.Command{ Use: "thechild", Short: "The child command", RunE: emptyRun, @@ -424,7 +425,7 @@ func TestValidArgsFuncAndCmdCompletionInGo(t *testing.T) { rootCmd.AddCommand(childCmd) // Test that both sub-commands and validArgsFunction are completed - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -443,7 +444,7 @@ func TestValidArgsFuncAndCmdCompletionInGo(t *testing.T) { } // Test that both sub-commands and validArgs are completed with prefix - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -459,7 +460,7 @@ func TestValidArgsFuncAndCmdCompletionInGo(t *testing.T) { } // Test that both sub-commands and validArgs are completed with description - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "t") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -476,11 +477,11 @@ func TestValidArgsFuncAndCmdCompletionInGo(t *testing.T) { } func TestFlagNameCompletionInGo(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", RunE: emptyRun, } - childCmd := &Command{ + childCmd := &zulu.Command{ Use: "childCmd", RunE: emptyRun, } @@ -491,7 +492,7 @@ func TestFlagNameCompletionInGo(t *testing.T) { childCmd.Flags().String("subFlag", "", "sub flag") // Test that flag names are not shown if the user has not given the '-' prefix - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -508,7 +509,7 @@ func TestFlagNameCompletionInGo(t *testing.T) { } // Test that flag names are completed - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -526,7 +527,7 @@ func TestFlagNameCompletionInGo(t *testing.T) { } // Test that flag names are completed when a prefix is given - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--f") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--f") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -541,7 +542,7 @@ func TestFlagNameCompletionInGo(t *testing.T) { } // Test that flag names are completed in a sub-cmd - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "-") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "childCmd", "-") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -559,11 +560,11 @@ func TestFlagNameCompletionInGo(t *testing.T) { } func TestFlagNameCompletionInGoWithDesc(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", RunE: emptyRun, } - childCmd := &Command{ + childCmd := &zulu.Command{ Use: "childCmd", Short: "first command", RunE: emptyRun, @@ -575,7 +576,7 @@ func TestFlagNameCompletionInGoWithDesc(t *testing.T) { childCmd.Flags().String("subFlag", "", "sub flag") // Test that flag names are not shown if the user has not given the '-' prefix - output, err := executeCommand(rootCmd, ShellCompRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -592,7 +593,7 @@ func TestFlagNameCompletionInGoWithDesc(t *testing.T) { } // Test that flag names are completed - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "-") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "-") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -610,7 +611,7 @@ func TestFlagNameCompletionInGoWithDesc(t *testing.T) { } // Test that flag names are completed when a prefix is given - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--f") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "--f") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -625,7 +626,7 @@ func TestFlagNameCompletionInGoWithDesc(t *testing.T) { } // Test that flag names are completed in a sub-cmd - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "childCmd", "-") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "childCmd", "-") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -643,11 +644,11 @@ func TestFlagNameCompletionInGoWithDesc(t *testing.T) { } func TestFlagNameCompletionRepeat(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", RunE: emptyRun, } - childCmd := &Command{ + childCmd := &zulu.Command{ Use: "childCmd", Short: "first command", RunE: emptyRun, @@ -664,7 +665,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) { bsliceFlag := rootCmd.Flags().Lookup("bslice") // Test that flag names are not repeated unless they are an array or slice - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--first", "1", "--") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--first", "1", "--") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -683,7 +684,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) { } // Test that flag names are not repeated unless they are an array or slice - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--first", "1", "--second=false", "--") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--first", "1", "--second=false", "--") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -702,7 +703,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) { } // Test that flag names are not repeated unless they are an array or slice - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--slice", "1", "--slice=2", "--bslice", "true", "--") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--slice", "1", "--slice=2", "--bslice", "true", "--") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -723,7 +724,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) { } // Test that flag names are not repeated unless they are an array or slice, using shortname - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-l", "1", "-l=2", "-") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-l", "1", "-l=2", "-") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -747,7 +748,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) { } // Test that flag names are not repeated unless they are an array or slice, using shortname with prefix - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-l", "1", "-l=2", "-a") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-l", "1", "-l=2", "-a") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -764,33 +765,33 @@ func TestFlagNameCompletionRepeat(t *testing.T) { } func TestRequiredFlagNameCompletionInGo(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", ValidArgs: []string{"realArg"}, RunE: emptyRun, } - childCmd := &Command{ + childCmd := &zulu.Command{ Use: "childCmd", - ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { - return []string{"subArg"}, ShellCompDirectiveNoFileComp + ValidArgsFunction: func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { + return []string{"subArg"}, zulu.ShellCompDirectiveNoFileComp }, RunE: emptyRun, } rootCmd.AddCommand(childCmd) - rootCmd.Flags().Int("requiredFlag", -1, "required flag", zflag.OptShorthand('r'), FlagOptRequired()) + rootCmd.Flags().Int("requiredFlag", -1, "required flag", zflag.OptShorthand('r'), zulu.FlagOptRequired()) requiredFlag := rootCmd.Flags().Lookup("requiredFlag") - rootCmd.PersistentFlags().Int("requiredPersistent", -1, "required persistent", zflag.OptShorthand('p'), FlagOptRequired()) + rootCmd.PersistentFlags().Int("requiredPersistent", -1, "required persistent", zflag.OptShorthand('p'), zulu.FlagOptRequired()) requiredPersistent := rootCmd.PersistentFlags().Lookup("requiredPersistent") rootCmd.Flags().String("release", "", "Release name", zflag.OptShorthand('R')) - childCmd.Flags().Bool("subRequired", false, "sub required flag", zflag.OptShorthand('s'), FlagOptRequired()) + childCmd.Flags().Bool("subRequired", false, "sub required flag", zflag.OptShorthand('s'), zulu.FlagOptRequired()) childCmd.Flags().Bool("subNotRequired", false, "sub not required flag", zflag.OptShorthand('n')) // Test that a required flag is suggested even without the - prefix - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -812,7 +813,7 @@ func TestRequiredFlagNameCompletionInGo(t *testing.T) { } // Test that a required flag is suggested without other flags when using the '-' prefix - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -830,7 +831,7 @@ func TestRequiredFlagNameCompletionInGo(t *testing.T) { } // Test that if no required flag matches, the normal flags are suggested - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--relea") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--relea") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -845,7 +846,7 @@ func TestRequiredFlagNameCompletionInGo(t *testing.T) { } // Test required flags for sub-commands - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "childCmd", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -863,7 +864,7 @@ func TestRequiredFlagNameCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "-") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "childCmd", "-") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -880,7 +881,7 @@ func TestRequiredFlagNameCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "--subNot") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "childCmd", "--subNot") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -895,7 +896,7 @@ func TestRequiredFlagNameCompletionInGo(t *testing.T) { } // Test that when a required flag is present, it is not suggested anymore - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--requiredFlag", "1", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--requiredFlag", "1", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -914,7 +915,7 @@ func TestRequiredFlagNameCompletionInGo(t *testing.T) { } // Test that when a persistent required flag is present, it is not suggested anymore - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--requiredPersistent", "1", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--requiredPersistent", "1", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -936,7 +937,7 @@ func TestRequiredFlagNameCompletionInGo(t *testing.T) { } // Test that when all required flags are present, normal completion is done - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--requiredFlag", "1", "--requiredPersistent", "1", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--requiredFlag", "1", "--requiredPersistent", "1", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -955,26 +956,26 @@ func TestRequiredFlagNameCompletionInGo(t *testing.T) { } func TestFlagFileExtFilterCompletionInGo(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", RunE: emptyRun, } // No extensions. Should be ignored. - rootCmd.Flags().String("file", "", "file flag", zflag.OptShorthand('f'), FlagOptFilename()) + rootCmd.Flags().String("file", "", "file flag", zflag.OptShorthand('f'), zulu.FlagOptFilename()) // Single extension - rootCmd.Flags().String("log", "", "log flag", zflag.OptShorthand('l'), FlagOptFilename("log")) + rootCmd.Flags().String("log", "", "log flag", zflag.OptShorthand('l'), zulu.FlagOptFilename("log")) // Multiple extensions - rootCmd.Flags().String("yaml", "", "yaml flag", zflag.OptShorthand('y'), FlagOptFilename("yaml", "yml")) + rootCmd.Flags().String("yaml", "", "yaml flag", zflag.OptShorthand('y'), zulu.FlagOptFilename("yaml", "yml")) // Directly using annotation - rootCmd.Flags().String("text", "", "text flag", zflag.OptShorthand('t'), zflag.OptAnnotation(BashCompFilenameExt, []string{"txt"})) + rootCmd.Flags().String("text", "", "text flag", zflag.OptShorthand('t'), zflag.OptAnnotation(zulu.BashCompFilenameExt, []string{"txt"})) // Test that the completion logic returns the proper info for the completion // script to handle the file filtering - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--file", "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--file", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -987,7 +988,7 @@ func TestFlagFileExtFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--log", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--log", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1001,7 +1002,7 @@ func TestFlagFileExtFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--yaml", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--yaml", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1015,7 +1016,7 @@ func TestFlagFileExtFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--yaml=") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--yaml=") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1029,7 +1030,7 @@ func TestFlagFileExtFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-y", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-y", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1043,7 +1044,7 @@ func TestFlagFileExtFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-y=") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-y=") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1057,7 +1058,7 @@ func TestFlagFileExtFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--text", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--text", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1073,23 +1074,23 @@ func TestFlagFileExtFilterCompletionInGo(t *testing.T) { } func TestFlagDirFilterCompletionInGo(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", RunE: emptyRun, } // Filter directories - rootCmd.Flags().String("dir", "", "dir flag", zflag.OptShorthand('d'), FlagOptDirname()) + rootCmd.Flags().String("dir", "", "dir flag", zflag.OptShorthand('d'), zulu.FlagOptDirname()) // Filter directories within a directory - rootCmd.Flags().String("subdir", "", "subdir", zflag.OptShorthand('s'), FlagOptDirname("themes")) + rootCmd.Flags().String("subdir", "", "subdir", zflag.OptShorthand('s'), zulu.FlagOptDirname("themes")) // Multiple directory specification get ignored - rootCmd.Flags().String("manydir", "", "manydir", zflag.OptShorthand('m'), zflag.OptAnnotation(BashCompSubdirsInDir, []string{"themes", "colors"})) + rootCmd.Flags().String("manydir", "", "manydir", zflag.OptShorthand('m'), zflag.OptAnnotation(zulu.BashCompSubdirsInDir, []string{"themes", "colors"})) // Test that the completion logic returns the proper info for the completion // script to handle the directory filtering - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--dir", "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--dir", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1102,7 +1103,7 @@ func TestFlagDirFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-d", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-d", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1115,7 +1116,7 @@ func TestFlagDirFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--subdir", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--subdir", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1129,7 +1130,7 @@ func TestFlagDirFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--subdir=") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--subdir=") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1143,7 +1144,7 @@ func TestFlagDirFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-s", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-s", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1157,7 +1158,7 @@ func TestFlagDirFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-s=") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-s=") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1171,7 +1172,7 @@ func TestFlagDirFilterCompletionInGo(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--manydir", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--manydir", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1186,7 +1187,7 @@ func TestFlagDirFilterCompletionInGo(t *testing.T) { } func TestValidArgsFuncCmdContext(t *testing.T) { - validArgsFunc := func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + validArgsFunc := func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { ctx := cmd.Context() if ctx == nil { @@ -1195,14 +1196,14 @@ func TestValidArgsFuncCmdContext(t *testing.T) { t.Error("Received invalid context") } - return nil, ShellCompDirectiveDefault + return nil, zulu.ShellCompDirectiveDefault } - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", RunE: emptyRun, } - childCmd := &Command{ + childCmd := &zulu.Command{ Use: "childCmd", ValidArgsFunction: validArgsFunc, RunE: emptyRun, @@ -1213,7 +1214,7 @@ func TestValidArgsFuncCmdContext(t *testing.T) { ctx := context.WithValue(context.Background(), "testKey", "123") // Test completing an empty string on the childCmd - _, output, err := executeCommandWithContextC(ctx, rootCmd, ShellCompNoDescRequestCmd, "childCmd", "") + _, output, err := executeCommandWithContextC(ctx, rootCmd, zulu.ShellCompNoDescRequestCmd, "childCmd", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1228,14 +1229,14 @@ func TestValidArgsFuncCmdContext(t *testing.T) { } func TestValidArgsFuncSingleCmd(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", ValidArgsFunction: validArgsFunc, RunE: emptyRun, } // Test completing an empty string - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1251,7 +1252,7 @@ func TestValidArgsFuncSingleCmd(t *testing.T) { } // Check completing with a prefix - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1267,19 +1268,19 @@ func TestValidArgsFuncSingleCmd(t *testing.T) { } func TestValidArgsFuncSingleCmdInvalidArg(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", // If we don't specify a value for Args, this test fails. // This is only true for a root command without any subcommands, and is caused // by the fact that the __complete command becomes a subcommand when there should not be one. // The problem is in the implementation of legacyArgs(). - Args: MinimumNArgs(1), + Args: zulu.MinimumNArgs(1), ValidArgsFunction: validArgsFunc, RunE: emptyRun, } // Check completing with wrong number of args - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "unexpectedArg", "t") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "unexpectedArg", "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1294,13 +1295,13 @@ func TestValidArgsFuncSingleCmdInvalidArg(t *testing.T) { } func TestValidArgsFuncChildCmds(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child1Cmd := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child1Cmd := &zulu.Command{ Use: "child1", ValidArgsFunction: validArgsFunc, RunE: emptyRun, } - child2Cmd := &Command{ + child2Cmd := &zulu.Command{ Use: "child2", ValidArgsFunction: validArgsFunc2, RunE: emptyRun, @@ -1308,7 +1309,7 @@ func TestValidArgsFuncChildCmds(t *testing.T) { rootCmd.AddCommand(child1Cmd, child2Cmd) // Test completion of first sub-command with empty argument - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child1", "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "child1", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1324,7 +1325,7 @@ func TestValidArgsFuncChildCmds(t *testing.T) { } // Test completion of first sub-command with a prefix to complete - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child1", "t") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "child1", "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1339,7 +1340,7 @@ func TestValidArgsFuncChildCmds(t *testing.T) { } // Check completing with wrong number of args - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child1", "unexpectedArg", "t") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "child1", "unexpectedArg", "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1353,7 +1354,7 @@ func TestValidArgsFuncChildCmds(t *testing.T) { } // Test completion of second sub-command with empty argument - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child2", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "child2", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1368,7 +1369,7 @@ func TestValidArgsFuncChildCmds(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child2", "t") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "child2", "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1383,7 +1384,7 @@ func TestValidArgsFuncChildCmds(t *testing.T) { } // Check completing with wrong number of args - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child2", "unexpectedArg", "t") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "child2", "unexpectedArg", "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1398,8 +1399,8 @@ func TestValidArgsFuncChildCmds(t *testing.T) { } func TestValidArgsFuncAliases(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child := &zulu.Command{ Use: "child", Aliases: []string{"son", "daughter"}, ValidArgsFunction: validArgsFunc, @@ -1408,7 +1409,7 @@ func TestValidArgsFuncAliases(t *testing.T) { rootCmd.AddCommand(child) // Test completion of first sub-command with empty argument - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "son", "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "son", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1424,7 +1425,7 @@ func TestValidArgsFuncAliases(t *testing.T) { } // Test completion of first sub-command with a prefix to complete - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "daughter", "t") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "daughter", "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1439,7 +1440,7 @@ func TestValidArgsFuncAliases(t *testing.T) { } // Check completing with wrong number of args - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "son", "unexpectedArg", "t") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "son", "unexpectedArg", "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1454,8 +1455,8 @@ func TestValidArgsFuncAliases(t *testing.T) { } func TestCompleteNoDesCmdInZshScript(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child := &zulu.Command{ Use: "child", ValidArgsFunction: validArgsFunc, RunE: emptyRun, @@ -1466,12 +1467,12 @@ func TestCompleteNoDesCmdInZshScript(t *testing.T) { assertNoErr(t, rootCmd.GenZshCompletionNoDesc(buf)) output := buf.String() - check(t, output, ShellCompNoDescRequestCmd) + check(t, output, zulu.ShellCompNoDescRequestCmd) } func TestCompleteCmdInZshScript(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child := &zulu.Command{ Use: "child", ValidArgsFunction: validArgsFunc, RunE: emptyRun, @@ -1482,40 +1483,40 @@ func TestCompleteCmdInZshScript(t *testing.T) { assertNoErr(t, rootCmd.GenZshCompletion(buf)) output := buf.String() - check(t, output, ShellCompRequestCmd) - checkOmit(t, output, ShellCompNoDescRequestCmd) + check(t, output, zulu.ShellCompRequestCmd) + checkOmit(t, output, zulu.ShellCompNoDescRequestCmd) } func TestFlagCompletionInGo(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", RunE: emptyRun, } rootCmd.Flags().Int("introot", -1, "help message for flag introot", zflag.OptShorthand('i'), - FlagOptCompletionFunc(func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + zulu.FlagOptCompletionFunc(func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { completions := make([]string, 0) for _, comp := range []string{"1\tThe first", "2\tThe second", "10\tThe tenth"} { if strings.HasPrefix(comp, toComplete) { completions = append(completions, comp) } } - return completions, ShellCompDirectiveDefault + return completions, zulu.ShellCompDirectiveDefault }), ) rootCmd.Flags().String("filename", "", "Enter a filename", - FlagOptCompletionFunc(func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + zulu.FlagOptCompletionFunc(func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { completions := make([]string, 0) for _, comp := range []string{"file.yaml\tYAML format", "myfile.json\tJSON format", "file.xml\tXML format"} { if strings.HasPrefix(comp, toComplete) { completions = append(completions, comp) } } - return completions, ShellCompDirectiveNoSpace | ShellCompDirectiveNoFileComp + return completions, zulu.ShellCompDirectiveNoSpace | zulu.ShellCompDirectiveNoFileComp }), ) // Test completing an empty string - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--introot", "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--introot", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1532,7 +1533,7 @@ func TestFlagCompletionInGo(t *testing.T) { } // Check completing with a prefix - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--introot", "1") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--introot", "1") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1548,7 +1549,7 @@ func TestFlagCompletionInGo(t *testing.T) { } // Test completing an empty string - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--filename", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--filename", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1565,7 +1566,7 @@ func TestFlagCompletionInGo(t *testing.T) { } // Check completing with a prefix - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--filename", "f") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "--filename", "f") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1582,13 +1583,13 @@ func TestFlagCompletionInGo(t *testing.T) { } func TestValidArgsFuncChildCmdsWithDesc(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child1Cmd := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child1Cmd := &zulu.Command{ Use: "child1", ValidArgsFunction: validArgsFunc, RunE: emptyRun, } - child2Cmd := &Command{ + child2Cmd := &zulu.Command{ Use: "child2", ValidArgsFunction: validArgsFunc2, RunE: emptyRun, @@ -1596,7 +1597,7 @@ func TestValidArgsFuncChildCmdsWithDesc(t *testing.T) { rootCmd.AddCommand(child1Cmd, child2Cmd) // Test completion of first sub-command with empty argument - output, err := executeCommand(rootCmd, ShellCompRequestCmd, "child1", "") + output, err := executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child1", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1612,7 +1613,7 @@ func TestValidArgsFuncChildCmdsWithDesc(t *testing.T) { } // Test completion of first sub-command with a prefix to complete - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child1", "t") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child1", "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1627,7 +1628,7 @@ func TestValidArgsFuncChildCmdsWithDesc(t *testing.T) { } // Check completing with wrong number of args - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child1", "unexpectedArg", "t") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child1", "unexpectedArg", "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1641,7 +1642,7 @@ func TestValidArgsFuncChildCmdsWithDesc(t *testing.T) { } // Test completion of second sub-command with empty argument - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child2", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1656,7 +1657,7 @@ func TestValidArgsFuncChildCmdsWithDesc(t *testing.T) { t.Errorf("expected: %q, got: %q", expected, output) } - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "t") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child2", "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1671,7 +1672,7 @@ func TestValidArgsFuncChildCmdsWithDesc(t *testing.T) { } // Check completing with wrong number of args - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "unexpectedArg", "t") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child2", "unexpectedArg", "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1686,15 +1687,15 @@ func TestValidArgsFuncChildCmdsWithDesc(t *testing.T) { } func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{ + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{ Use: "child", RunE: emptyRun, - ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { - return []string{"--validarg", "test"}, ShellCompDirectiveDefault + ValidArgsFunction: func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { + return []string{"--validarg", "test"}, zulu.ShellCompDirectiveDefault }, } - childCmd2 := &Command{ + childCmd2 := &zulu.Command{ Use: "child2", RunE: emptyRun, ValidArgs: []string{"arg1", "arg2"}, @@ -1702,13 +1703,13 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { rootCmd.AddCommand(childCmd, childCmd2) childCmd.Flags().Bool("bool", false, "test bool flag") childCmd.Flags().String("string", "", "test string flag", - FlagOptCompletionFunc(func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { - return []string{"myval"}, ShellCompDirectiveDefault + zulu.FlagOptCompletionFunc(func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { + return []string{"myval"}, zulu.ShellCompDirectiveDefault }), ) // Test flag completion with no argument - output, err := executeCommand(rootCmd, ShellCompRequestCmd, "child", "--") + output, err := executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "--") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1724,7 +1725,7 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { } // Test that no flags are completed after the -- arg - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "-") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "--", "-") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1740,7 +1741,7 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { } // Test that no flags are completed after the -- arg with a flag set - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--bool", "--", "-") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "--bool", "--", "-") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1759,7 +1760,7 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { childCmd.Flags().SetInterspersed(false) // Test that no flags are completed after the first arg - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "arg", "--") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "arg", "--") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1775,7 +1776,7 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { } // Test that no flags are completed after the fist arg with a flag set - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--string", "t", "arg", "--") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "--string", "t", "arg", "--") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1791,7 +1792,7 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { } // Check that args are still completed after -- - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "--", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1807,7 +1808,7 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { } // Check that args are still completed even if flagname with ValidArgsFunction exists - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "--string", "") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "--", "--string", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1823,7 +1824,7 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { } // Check that args are still completed even if flagname with ValidArgsFunction exists - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "--", "a") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child2", "--", "a") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1839,7 +1840,7 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { } // Check that --validarg is not parsed as flag after -- - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "--validarg", "") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "--", "--validarg", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1855,7 +1856,7 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { } // Check that --validarg is not parsed as flag after an arg - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "arg", "--validarg", "") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "arg", "--validarg", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1871,10 +1872,10 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { } // Check that --validarg is added to args for the ValidArgsFunction - childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { - return args, ShellCompDirectiveDefault + childCmd.ValidArgsFunction = func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { + return args, zulu.ShellCompDirectiveDefault } - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "--validarg", "") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "--", "--validarg", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1889,10 +1890,10 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { } // Check that --validarg is added to args for the ValidArgsFunction and toComplete is also set correctly - childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { - return append(args, toComplete), ShellCompDirectiveDefault + childCmd.ValidArgsFunction = func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { + return append(args, toComplete), zulu.ShellCompDirectiveDefault } - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "--validarg", "--toComp=ab") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "--", "--validarg", "--toComp=ab") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1909,18 +1910,18 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) { } func TestFlagCompletionWorksRootCommandAddedAfterFlags(t *testing.T) { - rootCmd := &Command{Use: "root", RunE: emptyRun} - childCmd := &Command{ + rootCmd := &zulu.Command{Use: "root", RunE: emptyRun} + childCmd := &zulu.Command{ Use: "child", RunE: emptyRun, - ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { - return []string{"--validarg", "test"}, ShellCompDirectiveDefault + ValidArgsFunction: func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { + return []string{"--validarg", "test"}, zulu.ShellCompDirectiveDefault }, } childCmd.Flags().Bool("bool", false, "test bool flag") childCmd.Flags().String("string", "", "test string flag", - FlagOptCompletionFunc(func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { - return []string{"myval"}, ShellCompDirectiveDefault + zulu.FlagOptCompletionFunc(func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { + return []string{"myval"}, zulu.ShellCompDirectiveDefault }), ) @@ -1929,7 +1930,7 @@ func TestFlagCompletionWorksRootCommandAddedAfterFlags(t *testing.T) { rootCmd.AddCommand(childCmd) // Test that flag completion works for the subcmd - output, err := executeCommand(rootCmd, ShellCompRequestCmd, "child", "--string", "") + output, err := executeCommand(rootCmd, zulu.ShellCompRequestCmd, "child", "--string", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1945,35 +1946,35 @@ func TestFlagCompletionWorksRootCommandAddedAfterFlags(t *testing.T) { } func TestFlagCompletionInGoWithDesc(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", RunE: emptyRun, } rootCmd.Flags().Int("introot", -1, "help message for flag introot", zflag.OptShorthand('i'), - FlagOptCompletionFunc(func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + zulu.FlagOptCompletionFunc(func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { completions := []string{} for _, comp := range []string{"1\tThe first", "2\tThe second", "10\tThe tenth"} { if strings.HasPrefix(comp, toComplete) { completions = append(completions, comp) } } - return completions, ShellCompDirectiveDefault + return completions, zulu.ShellCompDirectiveDefault }), ) rootCmd.Flags().String("filename", "", "Enter a filename", - FlagOptCompletionFunc(func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { + zulu.FlagOptCompletionFunc(func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { completions := []string{} for _, comp := range []string{"file.yaml\tYAML format", "myfile.json\tJSON format", "file.xml\tXML format"} { if strings.HasPrefix(comp, toComplete) { completions = append(completions, comp) } } - return completions, ShellCompDirectiveNoSpace | ShellCompDirectiveNoFileComp + return completions, zulu.ShellCompDirectiveNoSpace | zulu.ShellCompDirectiveNoFileComp }), ) // Test completing an empty string - output, err := executeCommand(rootCmd, ShellCompRequestCmd, "--introot", "") + output, err := executeCommand(rootCmd, zulu.ShellCompRequestCmd, "--introot", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -1990,7 +1991,7 @@ func TestFlagCompletionInGoWithDesc(t *testing.T) { } // Check completing with a prefix - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--introot", "1") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "--introot", "1") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2006,7 +2007,7 @@ func TestFlagCompletionInGoWithDesc(t *testing.T) { } // Test completing an empty string - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--filename", "") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "--filename", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2023,7 +2024,7 @@ func TestFlagCompletionInGoWithDesc(t *testing.T) { } // Check completing with a prefix - output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--filename", "f") + output, err = executeCommand(rootCmd, zulu.ShellCompRequestCmd, "--filename", "f") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2040,18 +2041,18 @@ func TestFlagCompletionInGoWithDesc(t *testing.T) { } func TestValidArgsNotValidArgsFunc(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", ValidArgs: []string{"one", "two"}, - ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { - return []string{"three", "four"}, ShellCompDirectiveNoFileComp + ValidArgsFunction: func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { + return []string{"three", "four"}, zulu.ShellCompDirectiveNoFileComp }, RunE: emptyRun, } // Test that if both ValidArgs and ValidArgsFunction are present // only ValidArgs is considered - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2067,7 +2068,7 @@ func TestValidArgsNotValidArgsFunc(t *testing.T) { } // Check completing with a prefix - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2083,16 +2084,16 @@ func TestValidArgsNotValidArgsFunc(t *testing.T) { } func TestArgAliasesCompletionInGo(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", - Args: ArbitraryArgs, + Args: zulu.ArbitraryArgs, ValidArgs: []string{"one", "two", "three"}, ArgAliases: []string{"un", "deux", "trois"}, RunE: emptyRun, } // Test that argaliases are not completed when there are validargs that match - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2109,7 +2110,7 @@ func TestArgAliasesCompletionInGo(t *testing.T) { } // Test that argaliases are not completed when there are validargs that match using a prefix - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "t") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2125,7 +2126,7 @@ func TestArgAliasesCompletionInGo(t *testing.T) { } // Test that argaliases are completed when there are no validargs that match - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "tr") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "tr") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2141,25 +2142,25 @@ func TestArgAliasesCompletionInGo(t *testing.T) { } func TestCompleteHelp(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child1Cmd := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child1Cmd := &zulu.Command{ Use: "child1", RunE: emptyRun, } - child2Cmd := &Command{ + child2Cmd := &zulu.Command{ Use: "child2", RunE: emptyRun, } rootCmd.AddCommand(child1Cmd, child2Cmd) - child3Cmd := &Command{ + child3Cmd := &zulu.Command{ Use: "child3", RunE: emptyRun, } child1Cmd.AddCommand(child3Cmd) // Test that completion includes the help command - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2177,7 +2178,7 @@ func TestCompleteHelp(t *testing.T) { } // Test sub-commands are completed on first level of help command - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "help", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "help", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2195,7 +2196,7 @@ func TestCompleteHelp(t *testing.T) { } // Test sub-commands are completed on first level of help command - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "help", "child1", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "help", "child1", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2210,10 +2211,10 @@ func TestCompleteHelp(t *testing.T) { } } -func removeCompCmd(rootCmd *Command) { +func removeCompCmd(rootCmd *zulu.Command) { // Remove completion command for the next test - for _, cmd := range rootCmd.commands { - if cmd.Name() == compCmdName { + for _, cmd := range rootCmd.Commands() { + if cmd.Name() == zulu.CompCmdName { rootCmd.RemoveCommand(cmd) return } @@ -2221,22 +2222,22 @@ func removeCompCmd(rootCmd *Command) { } func TestDefaultCompletionCmd(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", - Args: NoArgs, + Args: zulu.NoArgs, RunE: emptyRun, } // Test that no completion command is created if there are not other sub-commands assertNoErr(t, rootCmd.Execute()) - for _, cmd := range rootCmd.commands { - if cmd.Name() == compCmdName { + for _, cmd := range rootCmd.Commands() { + if cmd.Name() == zulu.CompCmdName { t.Errorf("Should not have a 'completion' command when there are no other sub-commands of root") break } } - subCmd := &Command{ + subCmd := &zulu.Command{ Use: "sub", RunE: emptyRun, } @@ -2245,8 +2246,8 @@ func TestDefaultCompletionCmd(t *testing.T) { // Test that a completion command is created if there are other sub-commands found := false assertNoErr(t, rootCmd.Execute()) - for _, cmd := range rootCmd.commands { - if cmd.Name() == compCmdName { + for _, cmd := range rootCmd.Commands() { + if cmd.Name() == zulu.CompCmdName { found = true break } @@ -2260,8 +2261,8 @@ func TestDefaultCompletionCmd(t *testing.T) { // Test that the default completion command can be disabled rootCmd.CompletionOptions.DisableDefaultCmd = true assertNoErr(t, rootCmd.Execute()) - for _, cmd := range rootCmd.commands { - if cmd.Name() == compCmdName { + for _, cmd := range rootCmd.Commands() { + if cmd.Name() == zulu.CompCmdName { t.Errorf("Should not have a 'completion' command when the feature is disabled") break } @@ -2270,38 +2271,38 @@ func TestDefaultCompletionCmd(t *testing.T) { rootCmd.CompletionOptions.DisableDefaultCmd = false // Test that completion descriptions are enabled by default - output, err := executeCommand(rootCmd, compCmdName, "zsh") + output, err := executeCommand(rootCmd, zulu.CompCmdName, "zsh") if err != nil { t.Errorf("Unexpected error: %v", err) } - check(t, output, ShellCompRequestCmd) - checkOmit(t, output, ShellCompNoDescRequestCmd) + check(t, output, zulu.ShellCompRequestCmd) + checkOmit(t, output, zulu.ShellCompNoDescRequestCmd) // Remove completion command for the next test removeCompCmd(rootCmd) // Test that completion descriptions can be disabled completely rootCmd.CompletionOptions.DisableDescriptions = true - output, err = executeCommand(rootCmd, compCmdName, "zsh") + output, err = executeCommand(rootCmd, zulu.CompCmdName, "zsh") if err != nil { t.Errorf("Unexpected error: %v", err) } - check(t, output, ShellCompNoDescRequestCmd) + check(t, output, zulu.ShellCompNoDescRequestCmd) // Re-enable for next test rootCmd.CompletionOptions.DisableDescriptions = false // Remove completion command for the next test removeCompCmd(rootCmd) - var compCmd *Command + var compCmd *zulu.Command // Test that the --no-descriptions flag is present on all shells assertNoErr(t, rootCmd.Execute()) for _, shell := range []string{"bash", "fish", "powershell", "zsh"} { - if compCmd, _, err = rootCmd.Find([]string{compCmdName, shell}); err != nil { + if compCmd, _, err = rootCmd.Find([]string{zulu.CompCmdName, shell}); err != nil { t.Errorf("Unexpected error: %v", err) } - if flag := compCmd.Flags().Lookup(compCmdNoDescFlagName); flag == nil { - t.Errorf("Missing --%s flag for %s shell", compCmdNoDescFlagName, shell) + if flag := compCmd.Flags().Lookup(zulu.CompCmdNoDescFlagName); flag == nil { + t.Errorf("Missing --%s flag for %s shell", zulu.CompCmdNoDescFlagName, shell) } } // Remove completion command for the next test @@ -2311,11 +2312,11 @@ func TestDefaultCompletionCmd(t *testing.T) { rootCmd.CompletionOptions.DisableNoDescFlag = true assertNoErr(t, rootCmd.Execute()) for _, shell := range []string{"fish", "zsh", "bash", "powershell"} { - if compCmd, _, err = rootCmd.Find([]string{compCmdName, shell}); err != nil { + if compCmd, _, err = rootCmd.Find([]string{zulu.CompCmdName, shell}); err != nil { t.Errorf("Unexpected error: %v", err) } - if flag := compCmd.Flags().Lookup(compCmdNoDescFlagName); flag != nil { - t.Errorf("Unexpected --%s flag for %s shell", compCmdNoDescFlagName, shell) + if flag := compCmd.Flags().Lookup(zulu.CompCmdNoDescFlagName); flag != nil { + t.Errorf("Unexpected --%s flag for %s shell", zulu.CompCmdNoDescFlagName, shell) } } // Re-enable for next test @@ -2327,11 +2328,11 @@ func TestDefaultCompletionCmd(t *testing.T) { rootCmd.CompletionOptions.DisableDescriptions = true assertNoErr(t, rootCmd.Execute()) for _, shell := range []string{"fish", "zsh", "bash", "powershell"} { - if compCmd, _, err = rootCmd.Find([]string{compCmdName, shell}); err != nil { + if compCmd, _, err = rootCmd.Find([]string{zulu.CompCmdName, shell}); err != nil { t.Errorf("Unexpected error: %v", err) } - if flag := compCmd.Flags().Lookup(compCmdNoDescFlagName); flag != nil { - t.Errorf("Unexpected --%s flag for %s shell", compCmdNoDescFlagName, shell) + if flag := compCmd.Flags().Lookup(zulu.CompCmdNoDescFlagName); flag != nil { + t.Errorf("Unexpected --%s flag for %s shell", zulu.CompCmdNoDescFlagName, shell) } } // Re-enable for next test @@ -2342,7 +2343,7 @@ func TestDefaultCompletionCmd(t *testing.T) { // Test that the 'completion' command can be hidden rootCmd.CompletionOptions.HiddenDefaultCmd = true assertNoErr(t, rootCmd.Execute()) - compCmd, _, err = rootCmd.Find([]string{compCmdName}) + compCmd, _, err = rootCmd.Find([]string{zulu.CompCmdName}) if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2356,15 +2357,15 @@ func TestDefaultCompletionCmd(t *testing.T) { } func TestCompleteCompletion(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - subCmd := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + subCmd := &zulu.Command{ Use: "sub", RunE: emptyRun, } rootCmd.AddCommand(subCmd) // Test sub-commands of the completion command - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "completion", "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "completion", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2382,16 +2383,16 @@ func TestCompleteCompletion(t *testing.T) { } // Test there are no completions for the sub-commands of the completion command - var compCmd *Command + var compCmd *zulu.Command for _, cmd := range rootCmd.Commands() { - if cmd.Name() == compCmdName { + if cmd.Name() == zulu.CompCmdName { compCmd = cmd break } } for _, shell := range compCmd.Commands() { - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, compCmdName, shell.Name(), "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, zulu.CompCmdName, shell.Name(), "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2407,7 +2408,7 @@ func TestCompleteCompletion(t *testing.T) { } func TestMultipleShorthandFlagCompletion(t *testing.T) { - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", ValidArgs: []string{"foo", "bar"}, RunE: emptyRun, @@ -2416,13 +2417,13 @@ func TestMultipleShorthandFlagCompletion(t *testing.T) { f.Bool("short", false, "short flag 1", zflag.OptShorthand('s')) f.Bool("short2", false, "short flag 2", zflag.OptShorthand('d')) f.String("short3", "", "short flag 3", zflag.OptShorthand('f'), - FlagOptCompletionFunc(func(*Command, []string, string) ([]string, ShellCompDirective) { - return []string{"works"}, ShellCompDirectiveNoFileComp + zulu.FlagOptCompletionFunc(func(*zulu.Command, []string, string) ([]string, zulu.ShellCompDirective) { + return []string{"works"}, zulu.ShellCompDirectiveNoFileComp }), ) // Test that a single shorthand flag works - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-s", "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-s", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2438,7 +2439,7 @@ func TestMultipleShorthandFlagCompletion(t *testing.T) { } // Test that multiple boolean shorthand flags work - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sd", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-sd", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2454,7 +2455,7 @@ func TestMultipleShorthandFlagCompletion(t *testing.T) { } // Test that multiple boolean + string shorthand flags work - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-sdf", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2469,7 +2470,7 @@ func TestMultipleShorthandFlagCompletion(t *testing.T) { } // Test that multiple boolean + string with equal sign shorthand flags work - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf=") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-sdf=") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2484,7 +2485,7 @@ func TestMultipleShorthandFlagCompletion(t *testing.T) { } // Test that multiple boolean + string with equal sign with value shorthand flags work - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-sdf=abc", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "-sdf=abc", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2502,12 +2503,12 @@ func TestMultipleShorthandFlagCompletion(t *testing.T) { func TestCompleteWithDisableFlagParsing(t *testing.T) { - flagValidArgs := func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { - return []string{"--flag", "-f"}, ShellCompDirectiveNoFileComp + flagValidArgs := func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { + return []string{"--flag", "-f"}, zulu.ShellCompDirectiveNoFileComp } - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - childCmd := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + childCmd := &zulu.Command{ Use: "child", RunE: emptyRun, DisableFlagParsing: true, @@ -2521,7 +2522,7 @@ func TestCompleteWithDisableFlagParsing(t *testing.T) { // Test that when DisableFlagParsing==true, ValidArgsFunction is called to complete flag names, // after Zulu tried to complete the flags it knows about. childCmd.DisableFlagParsing = true - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child", "-") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "child", "-") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2542,7 +2543,7 @@ func TestCompleteWithDisableFlagParsing(t *testing.T) { // Test that when DisableFlagParsing==false, Zulu completes the flags itself and ValidArgsFunction is not called childCmd.DisableFlagParsing = false - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child", "-") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "child", "-") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2564,17 +2565,17 @@ func TestCompleteWithDisableFlagParsing(t *testing.T) { func TestCompleteWithRootAndLegacyArgs(t *testing.T) { // Test a lonely root command which uses legacyArgs(). In such a case, the root // command should accept any number of arguments and completion should behave accordingly. - rootCmd := &Command{ + rootCmd := &zulu.Command{ Use: "root", Args: nil, // Args must be nil to trigger the legacyArgs() function RunE: emptyRun, - ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { - return []string{"arg1", "arg2"}, ShellCompDirectiveNoFileComp + ValidArgsFunction: func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) { + return []string{"arg1", "arg2"}, zulu.ShellCompDirectiveNoFileComp }, } // Make sure the first arg is completed - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2590,7 +2591,7 @@ func TestCompleteWithRootAndLegacyArgs(t *testing.T) { } // Make sure the completion of arguments continues - output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "arg1", "") + output, err = executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "arg1", "") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -2607,16 +2608,16 @@ func TestCompleteWithRootAndLegacyArgs(t *testing.T) { } func TestFixedCompletions(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} choices := []string{"apple", "banana", "orange"} - childCmd := &Command{ + childCmd := &zulu.Command{ Use: "child", - ValidArgsFunction: FixedCompletions(choices, ShellCompDirectiveNoFileComp), + ValidArgsFunction: zulu.FixedCompletions(choices, zulu.ShellCompDirectiveNoFileComp), RunE: emptyRun, } rootCmd.AddCommand(childCmd) - output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child", "a") + output, err := executeCommand(rootCmd, zulu.ShellCompNoDescRequestCmd, "child", "a") if err != nil { t.Errorf("Unexpected error: %v", err) } diff --git a/doc/adoc_docs_test.go b/doc/adoc_docs_test.go index 39d3880..478754f 100644 --- a/doc/adoc_docs_test.go +++ b/doc/adoc_docs_test.go @@ -1,4 +1,4 @@ -package doc +package doc_test import ( "bytes" @@ -8,12 +8,13 @@ import ( "testing" "github.com/gowarden/zulu" + "github.com/gowarden/zulu/doc" ) func TestGenAsciidoc(t *testing.T) { // We generate on subcommand so we have both subcommands and parents. buf := new(bytes.Buffer) - if err := GenAsciidoc(echoCmd, buf); err != nil { + if err := doc.GenAsciidoc(echoCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -31,7 +32,7 @@ func TestGenAsciidoc(t *testing.T) { func TestGenAsciidocWithNoLongOrSynopsis(t *testing.T) { // We generate on subcommand so we have both subcommands and parents. buf := new(bytes.Buffer) - if err := GenAsciidoc(dummyCmd, buf); err != nil { + if err := doc.GenAsciidoc(dummyCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -50,7 +51,7 @@ func TestGenAsciidocNoHiddenParents(t *testing.T) { defer func() { f.Hidden = false }() } buf := new(bytes.Buffer) - if err := GenAsciidoc(echoCmd, buf); err != nil { + if err := doc.GenAsciidoc(echoCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -70,7 +71,7 @@ func TestGenAsciidocNoTag(t *testing.T) { defer func() { rootCmd.DisableAutoGenTag = false }() buf := new(bytes.Buffer) - if err := GenAsciidoc(rootCmd, buf); err != nil { + if err := doc.GenAsciidoc(rootCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -86,7 +87,7 @@ func TestGenAsciidocTree(t *testing.T) { } defer os.RemoveAll(tmpdir) - if err := GenAsciidocTree(c, tmpdir); err != nil { + if err := doc.GenAsciidocTree(c, tmpdir); err != nil { t.Fatalf("GenAsciidocTree failed: %v", err) } @@ -105,7 +106,7 @@ func BenchmarkGenAsciidocToFile(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - if err := GenAsciidoc(rootCmd, file); err != nil { + if err := doc.GenAsciidoc(rootCmd, file); err != nil { b.Fatal(err) } } diff --git a/doc/cmd_test.go b/doc/cmd_test.go index d20e2c2..a011793 100644 --- a/doc/cmd_test.go +++ b/doc/cmd_test.go @@ -1,4 +1,4 @@ -package doc +package doc_test import ( "regexp" diff --git a/doc/exports_test.go b/doc/exports_test.go new file mode 100644 index 0000000..615a497 --- /dev/null +++ b/doc/exports_test.go @@ -0,0 +1,3 @@ +package doc + +var ManPrintFlags = manPrintFlags diff --git a/doc/man_docs_test.go b/doc/man_docs_test.go index df9b1c1..7442abc 100644 --- a/doc/man_docs_test.go +++ b/doc/man_docs_test.go @@ -1,4 +1,4 @@ -package doc +package doc_test import ( "bufio" @@ -13,6 +13,7 @@ import ( "github.com/gowarden/zflag" "github.com/gowarden/zulu" + "github.com/gowarden/zulu/doc" ) func assertNoErr(t *testing.T, e error) { @@ -26,14 +27,14 @@ func translate(in string) string { } func TestGenManDoc(t *testing.T) { - header := &GenManHeader{ + header := &doc.GenManHeader{ Title: "Project", Section: "2", } // We generate on a subcommand so we have both subcommands and parents buf := new(bytes.Buffer) - if err := GenMan(echoCmd, header, buf); err != nil { + if err := doc.GenMan(echoCmd, header, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -56,7 +57,7 @@ func TestGenManDoc(t *testing.T) { } func TestGenManNoHiddenParents(t *testing.T) { - header := &GenManHeader{ + header := &doc.GenManHeader{ Title: "Project", Section: "2", } @@ -68,7 +69,7 @@ func TestGenManNoHiddenParents(t *testing.T) { defer func() { f.Hidden = false }() } buf := new(bytes.Buffer) - if err := GenMan(echoCmd, header, buf); err != nil { + if err := doc.GenMan(echoCmd, header, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -95,14 +96,14 @@ func TestGenManNoGenTag(t *testing.T) { echoCmd.DisableAutoGenTag = true defer func() { echoCmd.DisableAutoGenTag = false }() - header := &GenManHeader{ + header := &doc.GenManHeader{ Title: "Project", Section: "2", } // We generate on a subcommand so we have both subcommands and parents buf := new(bytes.Buffer) - if err := GenMan(echoCmd, header, buf); err != nil { + if err := doc.GenMan(echoCmd, header, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -121,14 +122,14 @@ func TestGenManNoGenTagWithDisabledParent(t *testing.T) { rootCmd.DisableAutoGenTag = false }() - header := &GenManHeader{ + header := &doc.GenManHeader{ Title: "Project", Section: "2", } // We generate on a subcommand so we have both subcommands and parents buf := new(bytes.Buffer) - if err := GenMan(echoCmd, header, buf); err != nil { + if err := doc.GenMan(echoCmd, header, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -147,8 +148,8 @@ func TestGenManSeeAlso(t *testing.T) { rootCmd.AddCommand(aCmd, bCmd, cCmd) buf := new(bytes.Buffer) - header := &GenManHeader{} - if err := GenMan(rootCmd, header, buf); err != nil { + header := &doc.GenManHeader{} + if err := doc.GenMan(rootCmd, header, buf); err != nil { t.Fatal(err) } scanner := bufio.NewScanner(buf) @@ -164,12 +165,12 @@ func TestGenManSeeAlso(t *testing.T) { } } -func TestManPrintFlagsHidesShortDeperecated(t *testing.T) { +func TestManPrintFlagsHidesShortDeprecated(t *testing.T) { c := &zulu.Command{} c.Flags().String("foo", "default", "Foo flag", zflag.OptShorthand('f'), zflag.OptShorthandDeprecated("don't use it no more")) buf := new(bytes.Buffer) - manPrintFlags(buf, c.Flags()) + doc.ManPrintFlags(buf, c.Flags()) got := buf.String() expected := "**--foo**=\"default\"\n\tFoo flag\n\n" @@ -179,14 +180,14 @@ func TestManPrintFlagsHidesShortDeperecated(t *testing.T) { } func TestGenManCommands(t *testing.T) { - header := &GenManHeader{ + header := &doc.GenManHeader{ Title: "Project", Section: "2", } // Root command buf := new(bytes.Buffer) - if err := GenMan(rootCmd, header, buf); err != nil { + if err := doc.GenMan(rootCmd, header, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -197,7 +198,7 @@ func TestGenManCommands(t *testing.T) { // Echo command buf = new(bytes.Buffer) - if err := GenMan(echoCmd, header, buf); err != nil { + if err := doc.GenMan(echoCmd, header, buf); err != nil { t.Fatal(err) } output = buf.String() @@ -209,7 +210,7 @@ func TestGenManCommands(t *testing.T) { // Time command as echo's subcommand buf = new(bytes.Buffer) - if err := GenMan(timesCmd, header, buf); err != nil { + if err := doc.GenMan(timesCmd, header, buf); err != nil { t.Fatal(err) } output = buf.String() @@ -219,14 +220,14 @@ func TestGenManCommands(t *testing.T) { func TestGenManTree(t *testing.T) { c := &zulu.Command{Use: "do [OPTIONS] arg1 arg2"} - header := &GenManHeader{Section: "2"} + header := &doc.GenManHeader{Section: "2"} tmpdir, err := ioutil.TempDir("", "test-gen-man-tree") if err != nil { t.Fatalf("Failed to create tmpdir: %s", err.Error()) } defer os.RemoveAll(tmpdir) - if err := GenManTree(c, header, tmpdir); err != nil { + if err := doc.GenManTree(c, header, tmpdir); err != nil { t.Fatalf("GenManTree failed: %s", err.Error()) } @@ -280,7 +281,7 @@ func BenchmarkGenManToFile(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - if err := GenMan(rootCmd, nil, file); err != nil { + if err := doc.GenMan(rootCmd, nil, file); err != nil { b.Fatal(err) } } diff --git a/doc/md_docs_test.go b/doc/md_docs_test.go index 7b77388..09cb5e8 100644 --- a/doc/md_docs_test.go +++ b/doc/md_docs_test.go @@ -1,4 +1,4 @@ -package doc +package doc_test import ( "bytes" @@ -8,12 +8,13 @@ import ( "testing" "github.com/gowarden/zulu" + "github.com/gowarden/zulu/doc" ) func TestGenMdDoc(t *testing.T) { // We generate on subcommand so we have both subcommands and parents. buf := new(bytes.Buffer) - if err := GenMarkdown(echoCmd, buf); err != nil { + if err := doc.GenMarkdown(echoCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -31,7 +32,7 @@ func TestGenMdDoc(t *testing.T) { func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) { // We generate on subcommand so we have both subcommands and parents. buf := new(bytes.Buffer) - if err := GenMarkdown(dummyCmd, buf); err != nil { + if err := doc.GenMarkdown(dummyCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -50,7 +51,7 @@ func TestGenMdNoHiddenParents(t *testing.T) { defer func() { f.Hidden = false }() } buf := new(bytes.Buffer) - if err := GenMarkdown(echoCmd, buf); err != nil { + if err := doc.GenMarkdown(echoCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -70,7 +71,7 @@ func TestGenMdNoTag(t *testing.T) { defer func() { rootCmd.DisableAutoGenTag = false }() buf := new(bytes.Buffer) - if err := GenMarkdown(rootCmd, buf); err != nil { + if err := doc.GenMarkdown(rootCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -86,7 +87,7 @@ func TestGenMdTree(t *testing.T) { } defer os.RemoveAll(tmpdir) - if err := GenMarkdownTree(c, tmpdir); err != nil { + if err := doc.GenMarkdownTree(c, tmpdir); err != nil { t.Fatalf("GenMarkdownTree failed: %v", err) } @@ -105,7 +106,7 @@ func BenchmarkGenMarkdownToFile(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - if err := GenMarkdown(rootCmd, file); err != nil { + if err := doc.GenMarkdown(rootCmd, file); err != nil { b.Fatal(err) } } diff --git a/doc/rest_docs_test.go b/doc/rest_docs_test.go index def7ec4..a44f8c6 100644 --- a/doc/rest_docs_test.go +++ b/doc/rest_docs_test.go @@ -1,4 +1,4 @@ -package doc +package doc_test import ( "bytes" @@ -8,12 +8,13 @@ import ( "testing" "github.com/gowarden/zulu" + "github.com/gowarden/zulu/doc" ) func TestGenRSTDoc(t *testing.T) { // We generate on a subcommand so we have both subcommands and parents buf := new(bytes.Buffer) - if err := GenReST(echoCmd, buf); err != nil { + if err := doc.GenReST(echoCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -35,7 +36,7 @@ func TestGenRSTNoHiddenParents(t *testing.T) { defer func() { f.Hidden = false }() } buf := new(bytes.Buffer) - if err := GenReST(echoCmd, buf); err != nil { + if err := doc.GenReST(echoCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -55,7 +56,7 @@ func TestGenRSTNoTag(t *testing.T) { defer func() { rootCmd.DisableAutoGenTag = false }() buf := new(bytes.Buffer) - if err := GenReST(rootCmd, buf); err != nil { + if err := doc.GenReST(rootCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -73,7 +74,7 @@ func TestGenRSTTree(t *testing.T) { } defer os.RemoveAll(tmpdir) - if err := GenReSTTree(c, tmpdir); err != nil { + if err := doc.GenReSTTree(c, tmpdir); err != nil { t.Fatalf("GenReSTTree failed: %s", err.Error()) } @@ -92,7 +93,7 @@ func BenchmarkGenReSTToFile(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - if err := GenReST(rootCmd, file); err != nil { + if err := doc.GenReST(rootCmd, file); err != nil { b.Fatal(err) } } diff --git a/doc/yaml_docs_test.go b/doc/yaml_docs_test.go index e6b076f..b14eefe 100644 --- a/doc/yaml_docs_test.go +++ b/doc/yaml_docs_test.go @@ -1,18 +1,20 @@ -package doc +package doc_test import ( "bytes" - "github.com/gowarden/zulu" "io/ioutil" "os" "path/filepath" "testing" + + "github.com/gowarden/zulu" + "github.com/gowarden/zulu/doc" ) func TestGenYamlDoc(t *testing.T) { // We generate on s subcommand so we have both subcommands and parents buf := new(bytes.Buffer) - if err := GenYaml(echoCmd, buf); err != nil { + if err := doc.GenYaml(echoCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -30,7 +32,7 @@ func TestGenYamlNoTag(t *testing.T) { defer func() { rootCmd.DisableAutoGenTag = false }() buf := new(bytes.Buffer) - if err := GenYaml(rootCmd, buf); err != nil { + if err := doc.GenYaml(rootCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -47,7 +49,7 @@ func TestGenYamlTree(t *testing.T) { } defer os.RemoveAll(tmpdir) - if err := GenYamlTree(c, tmpdir); err != nil { + if err := doc.GenYamlTree(c, tmpdir); err != nil { t.Fatalf("GenYamlTree failed: %s", err.Error()) } @@ -59,7 +61,7 @@ func TestGenYamlTree(t *testing.T) { func TestGenYamlDocRunnable(t *testing.T) { // Testing a runnable command: should contain the "usage" field buf := new(bytes.Buffer) - if err := GenYaml(rootCmd, buf); err != nil { + if err := doc.GenYaml(rootCmd, buf); err != nil { t.Fatal(err) } output := buf.String() @@ -77,7 +79,7 @@ func BenchmarkGenYamlToFile(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - if err := GenYaml(rootCmd, file); err != nil { + if err := doc.GenYaml(rootCmd, file); err != nil { b.Fatal(err) } } diff --git a/exports_test.go b/exports_test.go new file mode 100644 index 0000000..b29d0ba --- /dev/null +++ b/exports_test.go @@ -0,0 +1,9 @@ +package zulu + +const ( + CompCmdName = compCmdName + CompCmdNoDescFlagName = compCmdNoDescFlagName +) + +var StripFlags = stripFlags +var StringInSlice = stringInSlice diff --git a/fish_completions_test.go b/fish_completions_test.go index d768e1f..3084372 100644 --- a/fish_completions_test.go +++ b/fish_completions_test.go @@ -1,15 +1,17 @@ -package zulu +package zulu_test import ( "bytes" "log" "os" "testing" + + "github.com/gowarden/zulu" ) func TestCompleteNoDesCmdInFishScript(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child := &zulu.Command{ Use: "child", ValidArgsFunction: validArgsFunc, RunE: emptyRun, @@ -20,12 +22,12 @@ func TestCompleteNoDesCmdInFishScript(t *testing.T) { assertNoErr(t, rootCmd.GenFishCompletion(buf, false)) output := buf.String() - check(t, output, ShellCompNoDescRequestCmd) + check(t, output, zulu.ShellCompNoDescRequestCmd) } func TestCompleteCmdInFishScript(t *testing.T) { - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child := &zulu.Command{ Use: "child", ValidArgsFunction: validArgsFunc, RunE: emptyRun, @@ -36,12 +38,12 @@ func TestCompleteCmdInFishScript(t *testing.T) { assertNoErr(t, rootCmd.GenFishCompletion(buf, true)) output := buf.String() - check(t, output, ShellCompRequestCmd) - checkOmit(t, output, ShellCompNoDescRequestCmd) + check(t, output, zulu.ShellCompRequestCmd) + checkOmit(t, output, zulu.ShellCompNoDescRequestCmd) } func TestProgWithDash(t *testing.T) { - rootCmd := &Command{Use: "root-dash", Args: NoArgs, RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root-dash", Args: zulu.NoArgs, RunE: emptyRun} buf := new(bytes.Buffer) assertNoErr(t, rootCmd.GenFishCompletion(buf, false)) output := buf.String() @@ -56,7 +58,7 @@ func TestProgWithDash(t *testing.T) { } func TestProgWithColon(t *testing.T) { - rootCmd := &Command{Use: "root:colon", Args: NoArgs, RunE: emptyRun} + rootCmd := &zulu.Command{Use: "root:colon", Args: zulu.NoArgs, RunE: emptyRun} buf := new(bytes.Buffer) assertNoErr(t, rootCmd.GenFishCompletion(buf, false)) output := buf.String() @@ -78,8 +80,8 @@ func TestGenFishCompletionFile(t *testing.T) { defer os.RemoveAll("./tmp") - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child := &zulu.Command{ Use: "child", ValidArgsFunction: validArgsFunc, RunE: emptyRun, @@ -100,8 +102,8 @@ func TestFailGenFishCompletionFile(t *testing.T) { f, _ := os.OpenFile("./tmp/test", os.O_CREATE, 0400) defer f.Close() - rootCmd := &Command{Use: "root", Args: NoArgs, RunE: emptyRun} - child := &Command{ + rootCmd := &zulu.Command{Use: "root", Args: zulu.NoArgs, RunE: emptyRun} + child := &zulu.Command{ Use: "child", ValidArgsFunction: validArgsFunc, RunE: emptyRun, diff --git a/internal/util/util_test.go b/internal/util/util_test.go index 04f0eca..5bff2b0 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -1,8 +1,10 @@ -package util +package util_test import ( "errors" "testing" + + "github.com/gowarden/zulu/internal/util" ) func TestCheckErr(t *testing.T) { @@ -50,7 +52,7 @@ func TestCheckErr(t *testing.T) { t.Errorf("Didn't panic to be %t", tt.panic) } }() - CheckErr(tt.msg) + util.CheckErr(tt.msg) }) } } diff --git a/zulu_test.go b/zulu_test.go index d7a0c84..11c8d28 100644 --- a/zulu_test.go +++ b/zulu_test.go @@ -1,8 +1,10 @@ -package zulu +package zulu_test import ( "testing" "text/template" + + "github.com/gowarden/zulu" ) func assertNoErr(t *testing.T, e error) { @@ -12,13 +14,13 @@ func assertNoErr(t *testing.T, e error) { } func TestAddTemplateFunctions(t *testing.T) { - AddTemplateFunc("t", func() bool { return true }) - AddTemplateFuncs(template.FuncMap{ + zulu.AddTemplateFunc("t", func() bool { return true }) + zulu.AddTemplateFuncs(template.FuncMap{ "f": func() bool { return false }, "h": func() string { return "Hello," }, "w": func() string { return "world." }}) - c := &Command{} + c := &zulu.Command{} c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`) const expected = "Hello, world."