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

Commit

Permalink
Add step to verify repo existence (#704)
Browse files Browse the repository at this point in the history
* Add step to verify repo existence

Signed-off-by: Bruna Tavares <[email protected]>

* Add tests

Signed-off-by: Bruna Tavares <[email protected]>

* Fix tests

Signed-off-by: Bruna Tavares <[email protected]>

* Add mock test

Signed-off-by: Bruna Tavares <[email protected]>

* Add bool mocks to tests

Signed-off-by: Bruna Tavares <[email protected]>

* Add list mock to tests

Signed-off-by: Bruna Tavares <[email protected]>

* Add int mock to tests

Signed-off-by: Bruna Tavares <[email protected]>

* Improvement url input

Signed-off-by: Bruna Tavares <[email protected]>

* Add password mock to tests

Signed-off-by: Bruna Tavares <[email protected]>

* Add text validator mock to tests

Signed-off-by: Bruna Tavares <[email protected]>

* Add tutorial find setter mock to tests

Signed-off-by: Bruna Tavares <[email protected]>

* Improvement RepoListerAdder

Signed-off-by: Bruna Tavares <[email protected]>

* Add Git Repository Mock mock to tests

Signed-off-by: Bruna Tavares <[email protected]>

* Refact

Signed-off-by: Bruna Tavares <[email protected]>

* Add more tests

Signed-off-by: Bruna Tavares <[email protected]>

* Add tests to repo existent

Signed-off-by: Bruna Tavares <[email protected]>

* Add tests to repo existent and user cancel operation

Signed-off-by: Bruna Tavares <[email protected]>

* Fix review

Signed-off-by: Bruna Tavares <[email protected]>

* Add function to default entries

Signed-off-by: Bruna Tavares <[email protected]>

* Fix use get fields

Signed-off-by: Bruna Tavares <[email protected]>

* Fix merge

Signed-off-by: Bruna Tavares <[email protected]>

* Fix name function

Signed-off-by: Bruna Tavares <[email protected]>
  • Loading branch information
brunasilvazup authored Dec 14, 2020
1 parent 36c6a39 commit 095d94a
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 249 deletions.
107 changes: 100 additions & 7 deletions internal/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/env"
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/git"
"github.com/ZupIT/ritchie-cli/pkg/rtutorial"
)

type EnvFinderMock struct {
Expand All @@ -46,24 +47,116 @@ func (d *DetailManagerMock) LatestTag(repo formula.Repo) string {
return args.String(0)
}

type RepositoriesMock struct {
type RepoListerAdderMock struct {
mock.Mock
}

func (m *RepositoriesMock) Zipball(info git.RepoInfo, version string) (io.ReadCloser, error) {
args := m.Called(info, version)
func (r *RepoListerAdderMock) List() (formula.Repos, error) {
args := r.Called()

return args.Get(0).(formula.Repos), args.Error(1)
}

func (r *RepoListerAdderMock) Add(repo formula.Repo) error {
args := r.Called(repo)

return args.Error(0)
}

type TutorialFindSetterMock struct {
mock.Mock
}

func (t *TutorialFindSetterMock) Find() (rtutorial.TutorialHolder, error) {
args := t.Called()

return args.Get(0).(rtutorial.TutorialHolder), args.Error(1)
}

func (t *TutorialFindSetterMock) Set(tutorial string) (rtutorial.TutorialHolder, error) {
args := t.Called(tutorial)

return args.Get(0).(rtutorial.TutorialHolder), args.Error(1)
}

type GitRepositoryMock struct {
mock.Mock
}

func (g *GitRepositoryMock) Zipball(info git.RepoInfo, version string) (io.ReadCloser, error) {
args := g.Called(info, version)

return args.Get(0).(io.ReadCloser), args.Error(1)
}

func (m *RepositoriesMock) Tags(info git.RepoInfo) (git.Tags, error) {
args := m.Called(info)
func (g *GitRepositoryMock) Tags(info git.RepoInfo) (git.Tags, error) {
args := g.Called(info)

return args.Get(0).(git.Tags), args.Error(1)
}

func (m *RepositoriesMock) LatestTag(info git.RepoInfo) (git.Tag, error) {
args := m.Called(info)
func (g *GitRepositoryMock) LatestTag(info git.RepoInfo) (git.Tag, error) {
args := g.Called(info)

return args.Get(0).(git.Tag), args.Error(1)
}

type InputURLMock struct {
mock.Mock
}

func (i *InputURLMock) URL(name, defaultValue string) (string, error) {
args := i.Called(name, defaultValue)

return args.String(0), args.Error(1)
}

type InputBoolMock struct {
mock.Mock
}

func (i *InputBoolMock) Bool(name string, items []string, helper ...string) (bool, error) {
args := i.Called(name, items, helper)

return args.Bool(0), args.Error(1)
}

type InputListMock struct {
mock.Mock
}

func (i *InputListMock) List(name string, items []string, helper ...string) (string, error) {
args := i.Called(name, items, helper)

return args.String(0), args.Error(1)
}

type InputIntMock struct {
mock.Mock
}

func (i *InputIntMock) Int(name string, helper ...string) (int64, error) {
args := i.Called(name, helper)

return args.Get(0).(int64), args.Error(1)
}

type InputPasswordMock struct {
mock.Mock
}

func (i *InputPasswordMock) Password(label string, helper ...string) (string, error) {
args := i.Called(label, helper)

return args.String(0), args.Error(1)
}

type InputTextValidatorMock struct {
mock.Mock
}

func (i *InputTextValidatorMock) Text(name string, validate func(interface{}) error, helper ...string) (string, error) {
args := i.Called(name, validate)

return args.String(0), args.Error(1)
}
25 changes: 24 additions & 1 deletion pkg/cmd/add_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/stdin"
)

const defaultRepoURL = "https://github.com/ZupIT/ritchie-formulas"
const (
defaultRepoURL = "https://github.com/ZupIT/ritchie-formulas"
messageExisting = "This formula repository already exists, check using \"rit list repo\""
)

var ErrRepoNameNotEmpty = errors.New("the field repository name must not be empty")

Expand Down Expand Up @@ -165,6 +168,11 @@ func (ad addRepoCmd) runPrompt() CommandRunnerFunc {
return err
}

if existsRepo(url, version, repos) {
prompt.Info(messageExisting)
return nil
}

priority, err := ad.Int("Set the priority:", "0 is higher priority, the lower higher the priority")
if err != nil {
return err
Expand Down Expand Up @@ -213,6 +221,12 @@ func (ad addRepoCmd) runStdin() CommandRunnerFunc {
r.Version = formula.RepoVersion(latestTag)
}

repos, _ := ad.repo.List()
if existsRepo(r.Url, r.Version.String(), repos) {
prompt.Info(messageExisting)
return nil
}

if err := ad.repo.Add(r); err != nil {
return err
}
Expand Down Expand Up @@ -241,6 +255,15 @@ func (ad addRepoCmd) repoNameValidator(text interface{}) error {
return nil
}

func existsRepo(urlToAdd, versionToAdd string, repos formula.Repos) bool {
for i := range repos {
if repos[i].Url == urlToAdd && repos[i].Version.String() == versionToAdd {
return true
}
}
return false
}

func tutorialAddRepo(tutorialStatus string) {
const tagTutorial = "\n[TUTORIAL]"
const MessageTitle = "To view your formula repositories:"
Expand Down
Loading

0 comments on commit 095d94a

Please sign in to comment.