From ef5ef3bf56fbce50cdd8b3663e0525ae8a49e592 Mon Sep 17 00:00:00 2001 From: Bruna Tavares Date: Fri, 14 Aug 2020 15:37:28 -0300 Subject: [PATCH 1/7] Update tutorial on stdin and add tests Signed-off-by: Bruna Tavares --- pkg/cmd/tutorial.go | 10 ++++++---- pkg/cmd/tutorial_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/tutorial.go b/pkg/cmd/tutorial.go index 5ae82361e..911df516d 100644 --- a/pkg/cmd/tutorial.go +++ b/pkg/cmd/tutorial.go @@ -18,7 +18,6 @@ package cmd import ( "fmt" - "os" "github.com/ZupIT/ritchie-cli/pkg/prompt" "github.com/ZupIT/ritchie-cli/pkg/rtutorial" @@ -55,17 +54,20 @@ func NewTutorialCmd(homePath string, il prompt.InputList, fs rtutorial.FindSette func (o tutorialCmd) runStdin() CommandRunnerFunc { return func(cmd *cobra.Command, args []string) error { - obj := struct { Tutorial string `json:"tutorial"` }{} - err := stdin.ReadJson(os.Stdin, &obj) + err := stdin.ReadJson(cmd.InOrStdin(), &obj) if err != nil { return err } - fmt.Println(obj) + _, err = o.Set(obj.Tutorial) + if err != nil { + return err + } + prompt.Success("Set tutorial successful!") return nil } diff --git a/pkg/cmd/tutorial_test.go b/pkg/cmd/tutorial_test.go index 5aa31bf68..9f934ede0 100644 --- a/pkg/cmd/tutorial_test.go +++ b/pkg/cmd/tutorial_test.go @@ -18,6 +18,7 @@ package cmd import ( "os" + "strings" "testing" ) @@ -33,3 +34,20 @@ func TestNewTutorialCmd(t *testing.T) { t.Errorf("%s = %v, want %v", cmd.Use, err, nil) } } + +func TestNewTutorialStdin(t *testing.T) { + cmd := NewTutorialCmd(os.TempDir(), inputListMock{}, TutorialFindSetterMock{}) + cmd.PersistentFlags().Bool("stdin", true, "input by stdin") + + input := "{\"tutorial\": \"enabled\"}\n" + newReader := strings.NewReader(input) + cmd.SetIn(newReader) + + if cmd == nil { + t.Errorf("NewTutorialCmd got %v", cmd) + } + + if err := cmd.Execute(); err != nil { + t.Errorf("%s = %v, want %v", cmd.Use, err, nil) + } +} From 872d38331e27eefca60cc1beddcefd6bac44d784 Mon Sep 17 00:00:00 2001 From: Bruna Tavares Date: Mon, 17 Aug 2020 14:07:37 -0300 Subject: [PATCH 2/7] Fix review Signed-off-by: Bruna Tavares --- pkg/cmd/tutorial.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/tutorial.go b/pkg/cmd/tutorial.go index 911df516d..5e3f2e87d 100644 --- a/pkg/cmd/tutorial.go +++ b/pkg/cmd/tutorial.go @@ -58,12 +58,11 @@ func (o tutorialCmd) runStdin() CommandRunnerFunc { Tutorial string `json:"tutorial"` }{} - err := stdin.ReadJson(cmd.InOrStdin(), &obj) - if err != nil { + if err := stdin.ReadJson(cmd.InOrStdin(), &obj); err != nil { return err } - _, err = o.Set(obj.Tutorial) + _, err := o.Set(obj.Tutorial) if err != nil { return err } From ffb595bc8a6b015bab831183b6102fe6cf4624ed Mon Sep 17 00:00:00 2001 From: Bruna Tavares Date: Tue, 18 Aug 2020 11:19:22 -0300 Subject: [PATCH 3/7] Fix review Signed-off-by: Bruna Tavares --- pkg/cmd/tutorial.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/tutorial.go b/pkg/cmd/tutorial.go index 5e3f2e87d..040feb065 100644 --- a/pkg/cmd/tutorial.go +++ b/pkg/cmd/tutorial.go @@ -28,7 +28,7 @@ import ( type tutorialCmd struct { homePath string prompt.InputList - rtutorial.FindSetter + tutorial rtutorial.FindSetter } const ( @@ -62,10 +62,10 @@ func (o tutorialCmd) runStdin() CommandRunnerFunc { return err } - _, err := o.Set(obj.Tutorial) - if err != nil { + if _, err := o.tutorial.Set(obj.Tutorial); err != nil { return err } + prompt.Success("Set tutorial successful!") return nil @@ -77,7 +77,7 @@ func (o tutorialCmd) runPrompt() CommandRunnerFunc { msg := "Status tutorial?" var statusTypes = []string{tutorialStatusEnabled, tutorialStatusDisabled} - tutorialHolder, err := o.Find() + tutorialHolder, err := o.tutorial.Find() if err != nil { return err } @@ -90,7 +90,7 @@ func (o tutorialCmd) runPrompt() CommandRunnerFunc { return err } - _, err = o.Set(response) + _, err = o.tutorial.Set(response) if err != nil { return err } From 444f9d13d29baded89a8f369bf5acd81e4116998 Mon Sep 17 00:00:00 2001 From: Bruna Tavares Date: Tue, 18 Aug 2020 12:09:16 -0300 Subject: [PATCH 4/7] Adds tests for error cases for both entries Signed-off-by: Bruna Tavares --- pkg/cmd/mocks_test.go | 31 +++++++++++----- pkg/cmd/tutorial_test.go | 79 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 13 deletions(-) diff --git a/pkg/cmd/mocks_test.go b/pkg/cmd/mocks_test.go index 8df018721..2e442e8fd 100644 --- a/pkg/cmd/mocks_test.go +++ b/pkg/cmd/mocks_test.go @@ -101,9 +101,9 @@ func (inputPasswordMock) Password(label string) (string, error) { return "s3cr3t", nil } -type inputPasswordErrorMock struct {} +type inputPasswordErrorMock struct{} -func (inputPasswordErrorMock) Password(label string) (string, error){ +func (inputPasswordErrorMock) Password(label string) (string, error) { return "", errors.New("password error") } @@ -289,19 +289,19 @@ func (s credSettingsMock) CredentialsPath() string { } type credSettingsCustomMock struct { - ReadCredentialsValueMock func(path string)([]credential.ListCredData, error) - ReadCredentialsFieldsMock func(path string) (credential.Fields, error) + ReadCredentialsValueMock func(path string) ([]credential.ListCredData, error) + ReadCredentialsFieldsMock func(path string) (credential.Fields, error) WriteDefaultCredentialsFieldsMock func(path string) error - WriteCredentialsFieldsMock func (fields credential.Fields, path string) error - ProviderPathMock func () string - CredentialsPathMock func () string + WriteCredentialsFieldsMock func(fields credential.Fields, path string) error + ProviderPathMock func() string + CredentialsPathMock func() string } func (cscm credSettingsCustomMock) ReadCredentialsFields(path string) (credential.Fields, error) { return cscm.ReadCredentialsFieldsMock(path) } -func (cscm credSettingsCustomMock) ReadCredentialsValue (path string)([]credential.ListCredData, error) { +func (cscm credSettingsCustomMock) ReadCredentialsValue(path string) ([]credential.ListCredData, error) { return cscm.ReadCredentialsValueMock(path) } @@ -321,8 +321,6 @@ func (cscm credSettingsCustomMock) CredentialsPath() string { return "" } - - type runnerMock struct { error error } @@ -386,6 +384,19 @@ func (TutorialFindSetterMock) Set(tutorial string) (rtutorial.TutorialHolder, er return s.Set(tutorial) } +type TutorialFindSetterCustomMock struct { + find func() (rtutorial.TutorialHolder, error) + set func(tutorial string) (rtutorial.TutorialHolder, error) +} + +func (t TutorialFindSetterCustomMock) Find() (rtutorial.TutorialHolder, error) { + return t.find() +} + +func (t TutorialFindSetterCustomMock) Set(tutorial string) (rtutorial.TutorialHolder, error) { + return t.set(tutorial) +} + var ( defaultRepoAdderMock = repoListerAdderCustomMock{ add: func(d formula.Repo) error { diff --git a/pkg/cmd/tutorial_test.go b/pkg/cmd/tutorial_test.go index 9f934ede0..36d4c646b 100644 --- a/pkg/cmd/tutorial_test.go +++ b/pkg/cmd/tutorial_test.go @@ -17,13 +17,16 @@ package cmd import ( - "os" + "errors" "strings" "testing" + + "github.com/ZupIT/ritchie-cli/pkg/prompt" + "github.com/ZupIT/ritchie-cli/pkg/rtutorial" ) func TestNewTutorialCmd(t *testing.T) { - cmd := NewTutorialCmd(os.TempDir(), inputListMock{}, TutorialFindSetterMock{}) + cmd := NewTutorialCmd("path/any", inputListMock{}, TutorialFindSetterMock{}) cmd.PersistentFlags().Bool("stdin", false, "input by stdin") if cmd == nil { @@ -36,7 +39,7 @@ func TestNewTutorialCmd(t *testing.T) { } func TestNewTutorialStdin(t *testing.T) { - cmd := NewTutorialCmd(os.TempDir(), inputListMock{}, TutorialFindSetterMock{}) + cmd := NewTutorialCmd("path/any", inputListMock{}, TutorialFindSetterMock{}) cmd.PersistentFlags().Bool("stdin", true, "input by stdin") input := "{\"tutorial\": \"enabled\"}\n" @@ -51,3 +54,73 @@ func TestNewTutorialStdin(t *testing.T) { t.Errorf("%s = %v, want %v", cmd.Use, err, nil) } } + +func Test_tutorialCmd_runAnyEntry(t *testing.T) { + var tutorialHolderEnabled, tutorialHolderDisabled rtutorial.TutorialHolder + type fields struct { + prompt.InputList + tutorial rtutorial.FindSetter + } + + tutorialHolderEnabled.Current = "enabled" + tutorialHolderDisabled.Current = "disabled" + + tests := []struct { + name string + fields fields + wantErr bool + inputStdin string + }{ + { + name: "Run With Success when set tutorial enabled", + fields: fields{ + InputList: inputListCustomMock{name: "enabled"}, + tutorial: TutorialFindSetterMock{}, + }, + wantErr: false, + inputStdin: "{\"tutorial\": \"enabled\"}\n", + }, + { + name: "Run With Success when set tutorial disabled", + fields: fields{ + InputList: inputListCustomMock{name: "disabled"}, + tutorial: TutorialFindSetterMock{}, + }, + wantErr: false, + inputStdin: "{\"tutorial\": \"disabled\"}\n", + }, + { + name: "Return error when set return error", + fields: fields{ + InputList: inputListCustomMock{name: "enabled"}, + tutorial: TutorialFindSetterCustomMock{ + set: func(tutorial string) (rtutorial.TutorialHolder, error) { + return tutorialHolderEnabled, errors.New("some error") + }, + find: func() (rtutorial.TutorialHolder, error) { + return tutorialHolderEnabled, nil + }, + }, + }, + wantErr: true, + inputStdin: "{\"tutorial\": \"enabled\"}\n", + }, + } + for _, tt := range tests { + initPrompt := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial) + initStdin := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial) + + initPrompt.PersistentFlags().Bool("stdin", false, "input by stdin") + initStdin.PersistentFlags().Bool("stdin", true, "input by stdin") + + newReader := strings.NewReader(tt.inputStdin) + initStdin.SetIn(newReader) + + if err := initPrompt.Execute(); (err != nil) != tt.wantErr { + t.Errorf("init_runPrompt() error = %v, wantErr %v", err, tt.wantErr) + } + if err := initStdin.Execute(); (err != nil) != tt.wantErr { + t.Errorf("init_runStdin() error = %v, wantErr %v", err, tt.wantErr) + } + } +} From 4542c0493819a06dfda9c20bed14189ad6f2a895 Mon Sep 17 00:00:00 2001 From: Bruna Tavares Date: Tue, 18 Aug 2020 13:43:53 -0300 Subject: [PATCH 5/7] Adds specific error tests per entry. Signed-off-by: Bruna Tavares --- pkg/cmd/tutorial_test.go | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/pkg/cmd/tutorial_test.go b/pkg/cmd/tutorial_test.go index 36d4c646b..c38e498db 100644 --- a/pkg/cmd/tutorial_test.go +++ b/pkg/cmd/tutorial_test.go @@ -124,3 +124,66 @@ func Test_tutorialCmd_runAnyEntry(t *testing.T) { } } } + +func Test_initCmd_runOnlyPrompt(t *testing.T) { + var tutorialHolderEnabled, tutorialHolderDisabled rtutorial.TutorialHolder + type fields struct { + prompt.InputList + tutorial rtutorial.FindSetter + } + + tutorialHolderEnabled.Current = "enabled" + tutorialHolderDisabled.Current = "disabled" + + tests := []struct { + name string + fields fields + wantErr bool + }{ + { + name: "Return error when find return error", + fields: fields{ + InputList: inputListCustomMock{name: "enabled"}, + tutorial: TutorialFindSetterCustomMock{ + find: func() (rtutorial.TutorialHolder, error) { + return tutorialHolderEnabled, errors.New("some error") + }, + }, + }, + wantErr: true, + }, + { + name: "Return error when list return error", + fields: fields{ + InputList: inputListErrorMock{}, + tutorial: TutorialFindSetterMock{}, + }, + wantErr: true, + }, + } + for _, tt := range tests { + initPrompt := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial) + + initPrompt.PersistentFlags().Bool("stdin", false, "input by stdin") + + if err := initPrompt.Execute(); (err != nil) != tt.wantErr { + t.Errorf("init_runPrompt() error = %v, wantErr %v", err, tt.wantErr) + } + } +} + +func Test_initCmd_runOnlyStdin(t *testing.T) { + t.Run("Error when readJson returns err", func(t *testing.T) { + wantErr := true + + initStdin := NewTutorialCmd("path/any", inputListMock{}, TutorialFindSetterMock{}) + + initStdin.PersistentFlags().Bool("stdin", true, "input by stdin") + newReader := strings.NewReader("{\"tutorial\": 1}\n") + initStdin.SetIn(newReader) + + if err := initStdin.Execute(); (err != nil) != wantErr { + t.Errorf("init_runStdin() error = %v, wantErr %v", err, wantErr) + } + }) +} From a25b32618639838eb13cb19c091a303e4e3a6392 Mon Sep 17 00:00:00 2001 From: Bruna Tavares Date: Tue, 18 Aug 2020 13:50:21 -0300 Subject: [PATCH 6/7] Code improvement Signed-off-by: Bruna Tavares --- pkg/cmd/tutorial_test.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/tutorial_test.go b/pkg/cmd/tutorial_test.go index c38e498db..202edd9c8 100644 --- a/pkg/cmd/tutorial_test.go +++ b/pkg/cmd/tutorial_test.go @@ -126,19 +126,17 @@ func Test_tutorialCmd_runAnyEntry(t *testing.T) { } func Test_initCmd_runOnlyPrompt(t *testing.T) { - var tutorialHolderEnabled, tutorialHolderDisabled rtutorial.TutorialHolder type fields struct { prompt.InputList tutorial rtutorial.FindSetter } + var tutorialHolderEnabled rtutorial.TutorialHolder tutorialHolderEnabled.Current = "enabled" - tutorialHolderDisabled.Current = "disabled" tests := []struct { - name string - fields fields - wantErr bool + name string + fields fields }{ { name: "Return error when find return error", @@ -150,7 +148,6 @@ func Test_initCmd_runOnlyPrompt(t *testing.T) { }, }, }, - wantErr: true, }, { name: "Return error when list return error", @@ -158,16 +155,16 @@ func Test_initCmd_runOnlyPrompt(t *testing.T) { InputList: inputListErrorMock{}, tutorial: TutorialFindSetterMock{}, }, - wantErr: true, }, } for _, tt := range tests { - initPrompt := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial) + wantErr := true + initPrompt := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial) initPrompt.PersistentFlags().Bool("stdin", false, "input by stdin") - if err := initPrompt.Execute(); (err != nil) != tt.wantErr { - t.Errorf("init_runPrompt() error = %v, wantErr %v", err, tt.wantErr) + if err := initPrompt.Execute(); (err != nil) != wantErr { + t.Errorf("init_runPrompt() error = %v, wantErr %v", err, wantErr) } } } @@ -177,8 +174,8 @@ func Test_initCmd_runOnlyStdin(t *testing.T) { wantErr := true initStdin := NewTutorialCmd("path/any", inputListMock{}, TutorialFindSetterMock{}) - initStdin.PersistentFlags().Bool("stdin", true, "input by stdin") + newReader := strings.NewReader("{\"tutorial\": 1}\n") initStdin.SetIn(newReader) From 628f9a9e3850d5280e009d7cd3f868970e3a1fbe Mon Sep 17 00:00:00 2001 From: Bruna Tavares Date: Tue, 18 Aug 2020 16:52:21 -0300 Subject: [PATCH 7/7] Fix name patterns Signed-off-by: Bruna Tavares --- pkg/cmd/tutorial_test.go | 42 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/pkg/cmd/tutorial_test.go b/pkg/cmd/tutorial_test.go index 202edd9c8..fd42f7ab9 100644 --- a/pkg/cmd/tutorial_test.go +++ b/pkg/cmd/tutorial_test.go @@ -55,7 +55,7 @@ func TestNewTutorialStdin(t *testing.T) { } } -func Test_tutorialCmd_runAnyEntry(t *testing.T) { +func TestTutorialRunAnyEntry(t *testing.T) { var tutorialHolderEnabled, tutorialHolderDisabled rtutorial.TutorialHolder type fields struct { prompt.InputList @@ -107,25 +107,25 @@ func Test_tutorialCmd_runAnyEntry(t *testing.T) { }, } for _, tt := range tests { - initPrompt := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial) - initStdin := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial) + cmdPrompt := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial) + cmdStdin := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial) - initPrompt.PersistentFlags().Bool("stdin", false, "input by stdin") - initStdin.PersistentFlags().Bool("stdin", true, "input by stdin") + cmdPrompt.PersistentFlags().Bool("stdin", false, "input by stdin") + cmdStdin.PersistentFlags().Bool("stdin", true, "input by stdin") newReader := strings.NewReader(tt.inputStdin) - initStdin.SetIn(newReader) + cmdStdin.SetIn(newReader) - if err := initPrompt.Execute(); (err != nil) != tt.wantErr { - t.Errorf("init_runPrompt() error = %v, wantErr %v", err, tt.wantErr) + if err := cmdPrompt.Execute(); (err != nil) != tt.wantErr { + t.Errorf("cmd_runPrompt() error = %v, wantErr %v", err, tt.wantErr) } - if err := initStdin.Execute(); (err != nil) != tt.wantErr { - t.Errorf("init_runStdin() error = %v, wantErr %v", err, tt.wantErr) + if err := cmdStdin.Execute(); (err != nil) != tt.wantErr { + t.Errorf("cmd_runStdin() error = %v, wantErr %v", err, tt.wantErr) } } } -func Test_initCmd_runOnlyPrompt(t *testing.T) { +func TestTutorialRunOnlyPrompt(t *testing.T) { type fields struct { prompt.InputList tutorial rtutorial.FindSetter @@ -160,27 +160,27 @@ func Test_initCmd_runOnlyPrompt(t *testing.T) { for _, tt := range tests { wantErr := true - initPrompt := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial) - initPrompt.PersistentFlags().Bool("stdin", false, "input by stdin") + cmd := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial) + cmd.PersistentFlags().Bool("stdin", false, "input by stdin") - if err := initPrompt.Execute(); (err != nil) != wantErr { - t.Errorf("init_runPrompt() error = %v, wantErr %v", err, wantErr) + if err := cmd.Execute(); (err != nil) != wantErr { + t.Errorf("cmd_runPrompt() error = %v, wantErr %v", err, wantErr) } } } -func Test_initCmd_runOnlyStdin(t *testing.T) { +func TestTutorialRunOnlyStdin(t *testing.T) { t.Run("Error when readJson returns err", func(t *testing.T) { wantErr := true - initStdin := NewTutorialCmd("path/any", inputListMock{}, TutorialFindSetterMock{}) - initStdin.PersistentFlags().Bool("stdin", true, "input by stdin") + cmdStdin := NewTutorialCmd("path/any", inputListMock{}, TutorialFindSetterMock{}) + cmdStdin.PersistentFlags().Bool("stdin", true, "input by stdin") newReader := strings.NewReader("{\"tutorial\": 1}\n") - initStdin.SetIn(newReader) + cmdStdin.SetIn(newReader) - if err := initStdin.Execute(); (err != nil) != wantErr { - t.Errorf("init_runStdin() error = %v, wantErr %v", err, wantErr) + if err := cmdStdin.Execute(); (err != nil) != wantErr { + t.Errorf("cmd_runStdin() error = %v, wantErr %v", err, wantErr) } }) }