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

Commit

Permalink
Merge pull request #435 from brunats/improvement/update-stdin-for-tut…
Browse files Browse the repository at this point in the history
…orial-command

Update tutorial on stdin and add tests
  • Loading branch information
brunasilvazup authored Aug 19, 2020
2 parents 5c9f28e + 628f9a9 commit 686b80d
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 10 deletions.
13 changes: 13 additions & 0 deletions pkg/cmd/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,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 TestTutorialRunAnyEntry(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 {
cmdPrompt := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial)
cmdStdin := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial)

cmdPrompt.PersistentFlags().Bool("stdin", false, "input by stdin")
cmdStdin.PersistentFlags().Bool("stdin", true, "input by stdin")

newReader := strings.NewReader(tt.inputStdin)
cmdStdin.SetIn(newReader)

if err := cmdPrompt.Execute(); (err != nil) != tt.wantErr {
t.Errorf("cmd_runPrompt() 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 TestTutorialRunOnlyPrompt(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

cmd := NewTutorialCmd("path/any", tt.fields.InputList, tt.fields.tutorial)
cmd.PersistentFlags().Bool("stdin", false, "input by stdin")

if err := cmd.Execute(); (err != nil) != wantErr {
t.Errorf("cmd_runPrompt() error = %v, wantErr %v", err, wantErr)
}
}
}

func TestTutorialRunOnlyStdin(t *testing.T) {
t.Run("Error when readJson returns err", func(t *testing.T) {
wantErr := true

cmdStdin := NewTutorialCmd("path/any", inputListMock{}, TutorialFindSetterMock{})
cmdStdin.PersistentFlags().Bool("stdin", true, "input by stdin")

newReader := strings.NewReader("{\"tutorial\": 1}\n")
cmdStdin.SetIn(newReader)

if err := cmdStdin.Execute(); (err != nil) != wantErr {
t.Errorf("cmd_runStdin() error = %v, wantErr %v", err, wantErr)
}
})
}

0 comments on commit 686b80d

Please sign in to comment.