Skip to content

Commit

Permalink
explicitly empty test command args
Browse files Browse the repository at this point in the history
See spf13/cobra#155

Errors can still be encountered when, for example, using precomiled
tests.  Explicitly setting constructed command args to the empty slice
ensures we avoid hitting any futher edge cases.
  • Loading branch information
lkingland committed Mar 28, 2022
1 parent dba1016 commit 6ca171f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions cmd/config_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func TestNewConfigLabelsCmd(t *testing.T) {
labels := &loaderSaver.f.Labels

cmd := NewConfigLabelsCmd(&loaderSaver)
cmd.SetArgs([]string{}) // Do not use test command args

run := createRunFunc(cmd, t)

Expand Down Expand Up @@ -143,6 +144,7 @@ func TestListLabels(t *testing.T) {
*labels = append(*labels, p("a", "b"), p("c", "d"))

cmd := NewConfigLabelsCmd(&loaderSaver)
cmd.SetArgs([]string{}) // Do not use test command args

ctx := context.Background()
c, _, err := vt10x.NewVT10XConsole()
Expand All @@ -151,8 +153,6 @@ func TestListLabels(t *testing.T) {
}
defer c.Close()

cmd.SetArgs(make([]string, 0))

errChan := make(chan error, 1)
func() {
var err error
Expand Down
2 changes: 2 additions & 0 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestCreate_NoRuntime(t *testing.T) {
defer Fromtemp(t)()

cmd := NewCreateCmd(NewClient)
cmd.SetArgs([]string{}) // Do not use test command args

err := cmd.Execute()
var e ErrNoRuntime
Expand Down Expand Up @@ -96,6 +97,7 @@ func TestCreateConfig_RepositoriesPath(t *testing.T) {
expected := filepath.Join(xdgConfigHome, "func", "repositories")

cmd := NewCreateCmd(NewClient)
cmd.SetArgs([]string{}) // Do not use test command args
cfg, err := newCreateConfig(cmd, []string{}, NewClient)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ created: 2021-01-01T00:00:00+00:00
cmd := NewDeleteCmd(NewClientFactory(func() *fn.Client {
return fn.New(fn.WithRemover(remover))
}))
cmd.SetArgs([]string{}) // Do not use test command args

// Execute the command simulating no arguments.
cmd.SetArgs([]string{})
err := cmd.Execute()
if err != nil {
t.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions cmd/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ created: 2009-11-10 23:00:00`,
fn.WithPipelinesProvider(pipeline),
fn.WithDeployer(deployer))
}))
cmd.SetArgs([]string{}) // Do not use test command args

// TODO: the below viper.SetDefault calls appear to be altering
// the default values of flags as a way set various values of flags.
Expand Down
12 changes: 12 additions & 0 deletions cmd/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func TestRepository_List(t *testing.T) {
defer WithEnvVar(t, "XDG_CONFIG_HOME", t.TempDir())()
cmd := NewRepositoryListCmd(NewClient)
cmd.SetArgs([]string{}) // Do not use test command args

// Execute the command, capturing the output sent to stdout
stdout := piped(t)
Expand All @@ -40,6 +41,9 @@ func TestRepository_Add(t *testing.T) {
list = NewRepositoryListCmd(NewClient)
stdout = piped(t)
)
// Do not use test command args
add.SetArgs([]string{})
list.SetArgs([]string{})

// add [flags] <old> <new>
add.SetArgs([]string{
Expand Down Expand Up @@ -76,6 +80,10 @@ func TestRepository_Rename(t *testing.T) {
list = NewRepositoryListCmd(NewClient)
stdout = piped(t)
)
// Do not use test command args
add.SetArgs([]string{})
rename.SetArgs([]string{})
list.SetArgs([]string{})

// add a repo which will be renamed
add.SetArgs([]string{"newrepo", TestRepoURI("repository", t)})
Expand Down Expand Up @@ -118,6 +126,10 @@ func TestRepository_Remove(t *testing.T) {
list = NewRepositoryListCmd(NewClient)
stdout = piped(t)
)
// Do not use test command args
add.SetArgs([]string{})
remove.SetArgs([]string{})
list.SetArgs([]string{})

// add a repo which will be removed
add.SetArgs([]string{"newrepo", TestRepoURI("repository", t)})
Expand Down
3 changes: 2 additions & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestRoot_PersistentFlags(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer Fromtemp(t)() // from a temp dir, deferred cleanup
cmd := NewCreateCmd(NewClient) // Create a new Function
cmd := NewCreateCmd(NewClient) // Create a new Function
cmd.SetArgs([]string{"--language", "go"}) // providing language (the only required param)
if err := cmd.Execute(); err != nil { // fail on any errors
t.Fatal(err)
Expand Down Expand Up @@ -188,6 +188,7 @@ func TestRoot_CommandNameParameterized(t *testing.T) {
cmd = NewRootCmd(RootCommandConfig{Name: testName})
out = strings.Builder{}
)
cmd.SetArgs([]string{}) // Do not use test command args
cmd.SetOut(&out)
if err := cmd.Help(); err != nil {
t.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ created: 2009-11-10 23:00:00`,
fn.WithRegistry("ghcr.com/reg"),
)
}))
cmd.SetArgs([]string{}) // Do not use test command args

// set test case's build
viper.SetDefault("build", tt.buildFlag)
Expand Down

0 comments on commit 6ca171f

Please sign in to comment.