-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #369 from pyroscope-io/fix-exec-flags
Fix argument processing
- Loading branch information
Showing
9 changed files
with
182 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package command | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/pyroscope-io/pyroscope/pkg/cli" | ||
"github.com/pyroscope-io/pyroscope/pkg/config" | ||
) | ||
|
||
type cmdArgsTestCase struct { | ||
description string | ||
inputArgs []string | ||
expectedArgs []string | ||
err error | ||
} | ||
|
||
var appArgs = []string{"app", "-f", "1", "-b", "arg"} | ||
|
||
func execArgs(args ...string) []string { return append(args, appArgs...) } | ||
|
||
func TestExecCommand(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
|
||
testCases := []cmdArgsTestCase{ | ||
{ | ||
description: "no_arguments", | ||
inputArgs: []string{}, | ||
}, | ||
{ | ||
description: "help_flag", | ||
inputArgs: []string{"--help"}, | ||
}, | ||
{ | ||
description: "delimiter", | ||
inputArgs: []string{optionsEnd}, | ||
expectedArgs: []string{}, | ||
}, | ||
{ | ||
description: "unknown_flag", | ||
inputArgs: []string{"--non-existing_flag"}, | ||
err: errors.New("unknown flag: --non-existing_flag"), | ||
}, | ||
{ | ||
description: "exec_no_arguments", | ||
inputArgs: appArgs, | ||
expectedArgs: appArgs, | ||
}, | ||
{ | ||
description: "exec_separated", | ||
inputArgs: execArgs("-spy-name", "debugspy", optionsEnd), | ||
expectedArgs: appArgs, | ||
}, | ||
{ | ||
description: "exec_flags_mixed", | ||
expectedArgs: appArgs, | ||
inputArgs: execArgs( | ||
"--spy-name=debugspy", | ||
"--application-name", "app", | ||
"--no-logging", | ||
"-server-address=http://localhost:4040", | ||
"-log-level", "debug", | ||
"-no-logging", | ||
), | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.description, func(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
cfg := new(config.Exec) | ||
vpr := newViper() | ||
var cmdArgs []string | ||
cmd := &cobra.Command{ | ||
SilenceErrors: true, | ||
DisableFlagParsing: true, | ||
RunE: createCmdRunFn(cfg, vpr, func(_ *cobra.Command, args []string) error { | ||
cmdArgs = args | ||
return nil | ||
}), | ||
} | ||
|
||
cmd.SetUsageFunc(printUsageMessage) | ||
cmd.SetHelpFunc(printHelpMessage) | ||
cmd.SetArgs(testCase.inputArgs) | ||
cmd.SetOut(io.Discard) | ||
cli.PopulateFlagSet(cfg, cmd.Flags(), vpr) | ||
|
||
err := cmd.Execute() | ||
if testCase.err == nil { | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(cmdArgs).To(Equal(testCase.expectedArgs)) | ||
return | ||
} | ||
|
||
g.Expect(err).To(Equal(testCase.err)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters