Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constraints on number of args #2066

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(2, 3),
RunE: func(cmd *cobra.Command, args []string) error {
if cfgErr != nil {
return fmt.Errorf("error while parsing config: %w", cfgErr)
Expand Down
46 changes: 46 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/googlecloudplatform/gcsfuse/v2/cfg"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestInvalidConfig(t *testing.T) {
Expand All @@ -46,3 +47,48 @@ 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{"gcsfuse", "abc", "pqr", "xyz"},
expectError: true,
},
{
name: "Too few args",
args: []string{"gcsfuse"},
expectError: true,
},
{
name: "Two args is okay",
args: []string{"gcsfuse", "abc"},
expectError: false,
},
{
name: "Three args is okay",
args: []string{"gcsfuse", "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 })
require.Nil(t, err)
cmd.SetArgs(tc.args)

err = cmd.Execute()

if tc.expectError {
ashmeenkaur marked this conversation as resolved.
Show resolved Hide resolved
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
})
}
}
Loading