Skip to content

Commit

Permalink
Add constraints on number of args
Browse files Browse the repository at this point in the history
  • Loading branch information
kislaykishore committed Jun 25, 2024
1 parent fb59c66 commit 41afbae
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewRootCmd(mountFn func(config cfg.Config) error) (*cobra.Command, error) {
and access Cloud Storage buckets as local file systems. For a technical overview
of Cloud Storage FUSE, see https://cloud.google.com/storage/docs/gcs-fuse.`,
Version: getVersion(),
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
if cfgErr != nil {
return fmt.Errorf("error while parsing config: %w", cfgErr)
Expand Down
47 changes: 47 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,50 @@ func TestValidConfig(t *testing.T) {

assert.Nil(t, cmd.Execute())
}

func TestTooManyCobraArgs(t *testing.T) {
tests := []struct {
name string
args []string
expectError bool
}{
{
name: "Too many args",
args: []string{"abc", "pqr", "xyz"},
expectError: true,
},
{
name: "Too few args",
args: []string{},
expectError: true,
},
{
name: "One arg is okay",
args: []string{"abc"},
expectError: false,
},
{
name: "Two args is okay",
args: []string{"abc", "pqr"},
expectError: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cmd, err := NewRootCmd(func(config cfg.Config) error { return nil })
if err != nil {
t.Fatalf("Error while creating the root command: %v", err)
}
cmd.SetArgs(tc.args)

err = cmd.Execute()

if tc.expectError {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
})
}
}

0 comments on commit 41afbae

Please sign in to comment.