Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Update tutorial on stdin and add tests #435

Merged
Show file tree
Hide file tree
Changes from 7 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
31 changes: 21 additions & 10 deletions pkg/cmd/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -321,8 +321,6 @@ func (cscm credSettingsCustomMock) CredentialsPath() string {
return ""
}



type runnerMock struct {
error error
}
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 9 additions & 8 deletions pkg/cmd/tutorial.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd

import (
"fmt"
"os"

"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/rtutorial"
Expand All @@ -29,7 +28,7 @@ import (
type tutorialCmd struct {
homePath string
prompt.InputList
rtutorial.FindSetter
tutorial rtutorial.FindSetter
}

const (
Expand All @@ -55,17 +54,19 @@ 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)
if err != nil {
if err := stdin.ReadJson(cmd.InOrStdin(), &obj); err != nil {
return err
}

if _, err := o.tutorial.Set(obj.Tutorial); err != nil {
return err
}

fmt.Println(obj)
prompt.Success("Set tutorial successful!")

return nil
}
Expand All @@ -76,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
}
Expand All @@ -89,7 +90,7 @@ func (o tutorialCmd) runPrompt() CommandRunnerFunc {
return err
}

_, err = o.Set(response)
_, err = o.tutorial.Set(response)
if err != nil {
return err
}
Expand Down
155 changes: 153 additions & 2 deletions pkg/cmd/tutorial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +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 {
Expand All @@ -33,3 +37,150 @@ func TestNewTutorialCmd(t *testing.T) {
t.Errorf("%s = %v, want %v", cmd.Use, err, nil)
}
}

func TestNewTutorialStdin(t *testing.T) {
cmd := NewTutorialCmd("path/any", 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)
}
}

func Test_tutorialCmd_runAnyEntry(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name of this test appears to be nonstandard. Is that correct?

Copy link
Contributor

@brunasilvazup brunasilvazup Aug 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[EDIT] No, I made a mistake and changed the standards.
Thanks I will change (:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the pattern I commented on is camelcase and you used underline

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix in 628f9a9

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)
}
}
}

func Test_initCmd_runOnlyPrompt(t *testing.T) {
type fields struct {
prompt.InputList
tutorial rtutorial.FindSetter
}

var tutorialHolderEnabled rtutorial.TutorialHolder
tutorialHolderEnabled.Current = "enabled"

tests := []struct {
name string
fields fields
}{
{
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")
},
},
},
},
{
name: "Return error when list return error",
fields: fields{
InputList: inputListErrorMock{},
tutorial: TutorialFindSetterMock{},
},
},
}
for _, tt := range tests {
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) != wantErr {
t.Errorf("init_runPrompt() error = %v, wantErr %v", err, 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)
}
})
}