Skip to content

Commit

Permalink
Fix an error handling for config set command (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shu Kutsuzawa authored Apr 2, 2021
1 parent ad43ae5 commit 9080c48
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
8 changes: 2 additions & 6 deletions cmd/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ Can set the below settings:
err = entry.Set(key, value)
if err != nil {
if isInvalidKeyError(err) {
err := cmd.Help()
if err != nil {
return err
}
} else {
return err
_ = cmd.Help()
}
return err
}

err = config.Save()
Expand Down
25 changes: 10 additions & 15 deletions cmd/config/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,27 @@ func TestConfigSetCmd(t *testing.T) {
testCase := []struct {
name string
args []string
wantOut string
checkErr bool
existErr bool
}{
{
name: "success",
args: []string{"set", "api-url", "example.com"},
wantOut: "",
checkErr: false,
existErr: false,
},
{
name: "failure by too many args",
args: []string{"set", "api-url", "example.com", "many"},
wantOut: "",
checkErr: true,
existErr: true,
},
{
name: "failure by too little args",
args: []string{"set", "api-url"},
wantOut: "",
checkErr: true,
existErr: true,
},
{
name: "failure by setting an invalid key",
args: []string{"set", "invalid-key", "invalid-value"},
existErr: true,
},
}

Expand All @@ -57,13 +58,7 @@ func TestConfigSetCmd(t *testing.T) {
buf := bytes.NewBuffer(nil)
cmd.SetOut(buf)
err := cmd.Execute()
if tt.checkErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tt.wantOut, buf.String())
}

assert.Equal(t, tt.existErr, err != nil)
})
}
}

0 comments on commit 9080c48

Please sign in to comment.