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

Add tests with testify for delete repo #906

Merged
merged 1 commit into from
Apr 15, 2021
Merged
Changes from all 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
118 changes: 118 additions & 0 deletions pkg/cmd/delete_repo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cmd

import (
"errors"
"testing"

"github.com/ZupIT/ritchie-cli/internal/mocks"
"github.com/ZupIT/ritchie-cli/pkg/formula"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestNewDeleteRepo(t *testing.T) {
type in struct {
args []string
repoList formula.Repos
repoListErr error
inputListString string
inputListErr error
repoDeleteErr error
}

tests := []struct {
name string
in in
want error
}{
{
name: "success",
in: in{
args: []string{},
repoList: formula.Repos{
{
Name: "repoName",
Priority: 0,
},
},
inputListString: "repoName",
},
},
{
name: "error to list repos",
in: in{
args: []string{},
repoListErr: errors.New("error to list repos"),
},
want: errors.New("error to list repos"),
},
{
name: "error to input list",
in: in{
args: []string{},
repoList: formula.Repos{
{
Name: "repoName",
Priority: 0,
},
},
inputListErr: errors.New("error to input list"),
},
want: errors.New("error to input list"),
},
{
name: "error to delete repo",
in: in{
args: []string{},
repoList: formula.Repos{
{
Name: "repoName",
Priority: 0,
},
},
inputListString: "repoName",
repoDeleteErr: errors.New("error to delete repo"),
},
want: errors.New("error to delete repo"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inputListMock := new(mocks.InputListMock)
inputListMock.On("List", mock.Anything, mock.Anything, mock.Anything).Return(tt.in.inputListString, tt.in.inputListErr)
repoManagerMock := new(mocks.RepoManager)
repoManagerMock.On("Delete", mock.Anything).Return(tt.in.repoDeleteErr)
repoManagerMock.On("List", mock.Anything, mock.Anything, mock.Anything).Return(tt.in.repoList, tt.in.repoListErr)

cmd := NewDeleteRepoCmd(
repoManagerMock,
inputListMock,
repoManagerMock,
)
// TODO: remove stdin flag after deprecation
cmd.PersistentFlags().Bool("stdin", false, "input by stdin")
cmd.SetArgs(tt.in.args)

got := cmd.Execute()
assert.Equal(t, tt.want, got)
})
}
}