From 9080c48656c637d1e653d6ecff13de7e109cd638 Mon Sep 17 00:00:00 2001 From: Shu Kutsuzawa Date: Sat, 3 Apr 2021 00:44:12 +0900 Subject: [PATCH] Fix an error handling for `config set` command (#70) --- cmd/config/set.go | 8 ++------ cmd/config/set_test.go | 25 ++++++++++--------------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/cmd/config/set.go b/cmd/config/set.go index f4d1a15..fdf5170 100644 --- a/cmd/config/set.go +++ b/cmd/config/set.go @@ -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() diff --git a/cmd/config/set_test.go b/cmd/config/set_test.go index 3e87bd4..e19824b 100644 --- a/cmd/config/set_test.go +++ b/cmd/config/set_test.go @@ -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, }, } @@ -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) }) } }