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

Commit

Permalink
Fix delete workspace (#816)
Browse files Browse the repository at this point in the history
* Fix delete workspace

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>

* Fix lint

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>
  • Loading branch information
kaduartur authored Jan 4, 2021
1 parent e299f0b commit 4605949
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 161 deletions.
14 changes: 14 additions & 0 deletions internal/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,20 @@ func (r *RepoManager) LatestTag(repo formula.Repo) string {
return args.String(0)
}

type DirManager struct {
mock.Mock
}

func (d *DirManager) Exists(path string) bool {
args := d.Called(path)
return args.Bool(0)
}

func (d *DirManager) IsDir(dir string) bool {
args := d.Called(dir)
return args.Bool(0)
}

type FileManager struct {
mock.Mock
}
Expand Down
45 changes: 17 additions & 28 deletions pkg/cmd/delete_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,43 @@ package cmd
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/formula/repo/repoutil"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/stream"
"github.com/spf13/cobra"
)

const (
msgWorkspaceIsNotValid = "the workspace informed is not valid"
msgEmptyWorkspaces = "there are no workspaces to delete"
)

var (
ErrWorkspaceIsNotValid = errors.New(msgWorkspaceIsNotValid)
ErrEmptyWorkspaces = errors.New(msgEmptyWorkspaces)
)
var ErrEmptyWorkspaces = errors.New("there are no workspaces to delete")

type deleteWorkspaceCmd struct {
userHomeDir string
workspace formula.WorkspaceListDeleter
directory stream.DirListChecker
repo formula.RepositoryDeleter
dir stream.DirChecker
inList prompt.InputList
inBool prompt.InputBool
}

func NewDeleteWorkspaceCmd(
userHomeDir string,
workspace formula.WorkspaceListDeleter,
directory stream.DirListChecker,
repo formula.RepositoryDeleter,
dir stream.DirChecker,
inList prompt.InputList,
inBool prompt.InputBool,
) *cobra.Command {
d := deleteWorkspaceCmd{
userHomeDir,
workspace,
directory,
inList,
inBool,
userHomeDir: userHomeDir,
workspace: workspace,
repo: repo,
dir: dir,
inList: inList,
inBool: inBool,
}

cmd := &cobra.Command{
Expand All @@ -80,7 +76,7 @@ func (d deleteWorkspaceCmd) runPrompt() CommandRunnerFunc {
}

defaultWorkspace := filepath.Join(d.userHomeDir, formula.DefaultWorkspaceDir)
if d.directory.Exists(defaultWorkspace) {
if d.dir.Exists(defaultWorkspace) {
workspaces[formula.DefaultWorkspaceName] = defaultWorkspace
}

Expand All @@ -102,7 +98,8 @@ func (d deleteWorkspaceCmd) runPrompt() CommandRunnerFunc {
return nil
}

if err := d.deleteWorkspace(wspace.Dir); err != nil {
repoLocalName := repoutil.LocalName(wspace.Name)
if err := d.repo.Delete(repoLocalName); err != nil {
return err
}

Expand All @@ -118,14 +115,6 @@ func (d deleteWorkspaceCmd) runPrompt() CommandRunnerFunc {
}
}

func (d deleteWorkspaceCmd) deleteWorkspace(workspace string) error {
if d.directory.Exists(workspace) {
return os.RemoveAll(workspace)
}

return ErrWorkspaceIsNotValid
}

func WorkspaceListInput(
workspaces formula.Workspaces,
inList prompt.InputList,
Expand Down
242 changes: 128 additions & 114 deletions pkg/cmd/delete_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,133 +18,147 @@ package cmd

import (
"errors"
"fmt"
"os"
"path/filepath"
"testing"

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

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

type listErrorMock struct{}

func (listErrorMock) List(name string, items []string, helper ...string) (string, error) {
workspace := filepath.Join(os.TempDir(), "formulas-ritchie")
return fmt.Sprintf("Formulas-Ritchie (%s)", workspace), nil
}

type listMock struct{}

func (listMock) List(name string, items []string, helper ...string) (string, error) {
workspace := filepath.Join(os.TempDir(), "ritchie-formulas-local")
return fmt.Sprintf("Default (%s)", workspace), nil
}

type workspaceMock struct{}

func (workspaceMock) List() (formula.Workspaces, error) {
m := formula.Workspaces{"Formulas-Ritchie": filepath.Join(os.TempDir(), "formulas-ritchie")}
return m, nil
}

func (workspaceMock) Delete(workspace formula.Workspace) error {
return errors.New("Some error")
}

func TestDeleteWorkspaceCmd(t *testing.T) {
fileManager := stream.NewFileManager()
dirManager := stream.NewDirManager(fileManager)

cmd := NewDeleteWorkspaceCmd(
os.TempDir(),
workspaceForm{},
dirManager,
listMock{},
inputTrueMock{},
)

workspace := filepath.Join(os.TempDir(), "formulas-ritchie")
if err := os.MkdirAll(filepath.Join(workspace, "mock", "test", "scr"), os.ModePerm); err != nil {
t.Errorf("TestNewDeleteWorkspaceCmd got error %v", err)
}

if cmd == nil {
t.Errorf("NewDeleteWorkspaceCmd got %v", cmd)
}

if err := cmd.Execute(); err != nil {
t.Errorf("%s = %v, want %v", cmd.Use, err, nil)
func TestDeleteWorkspaces(t *testing.T) {
type in struct {
wspaceList formula.Workspaces
wspaceListErr error
wspaceDeleteErr error
dirExist bool
inputList string
inputListErr error
inputBool bool
inputBoolErr error
repoDeleteErr error
}

cmd = NewDeleteWorkspaceCmd(
os.TempDir(),
workspaceForm{},
dirManager,
inputListCustomMock{
list: func(name string, items []string) (string, error) {
return "", errors.New("Some error")
tests := []struct {
name string
in in
want error
}{
{
name: "success",
in: in{
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
dirExist: true,
inputList: "local-commons",
inputBool: true,
},
},
inputTrueMock{},
)

workspace = filepath.Join(os.TempDir(), "ritchie-formulas-local")
if err := os.MkdirAll(filepath.Join(workspace, "mock", "test", "scr"), os.ModePerm); err != nil {
t.Errorf("TestNewDeleteWorkspaceCmd got error %v", err)
}

if cmd == nil {
t.Errorf("NewDeleteWorkspaceCmd got %v", cmd)
}

if err := cmd.Execute(); err == nil {
t.Errorf("%s = %v, want %v", cmd.Use, nil, err)
}

cmd = NewDeleteWorkspaceCmd(
os.TempDir(),
workspaceForm{},
DirManagerCustomMock{
exists: func(dir string) bool {
return false
{
name: "error to list workspace",
in: in{
wspaceListErr: errors.New("error to list workspace"),
},
want: errors.New("error to list workspace"),
},
{
name: "error empty workspace",
in: in{
wspaceList: formula.Workspaces{},
},
want: ErrEmptyWorkspaces,
},
{
name: "error to input list",
in: in{
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
dirExist: true,
inputListErr: errors.New("error to input list"),
inputBool: true,
},
want: errors.New("error to input list"),
},
{
name: "error to accept to delete selected workspace",
in: in{
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
dirExist: true,
inputList: "local-commons",
inputBoolErr: errors.New("error to accept"),
},
want: errors.New("error to accept"),
},
{
name: "not accept to delete selected workspace",
in: in{
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
dirExist: true,
inputList: "local-commons",
inputBool: false,
},
},
{
name: "error to delete repo",
in: in{
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
dirExist: true,
inputList: "local-commons",
inputBool: true,
repoDeleteErr: errors.New("error to delete repo"),
},
want: errors.New("error to delete repo"),
},
{
name: "error to delete workspace",
in: in{
wspaceList: formula.Workspaces{
"local-commons": "/home/user/commons",
},
dirExist: true,
inputList: "local-commons",
inputBool: true,
wspaceDeleteErr: errors.New("error to delete workspace"),
},
want: errors.New("error to delete workspace"),
},
listMock{},
inputTrueMock{},
)

workspace = filepath.Join(os.TempDir(), "ritchie-formulas-local")
if err := os.MkdirAll(filepath.Join(workspace, "mock", "test", "scr"), os.ModePerm); err != nil {
t.Errorf("TestNewDeleteWorkspaceCmd got error %v", err)
}

if cmd == nil {
t.Errorf("NewDeleteWorkspaceCmd got %v", cmd)
}

if err := cmd.Execute(); err == nil {
t.Errorf("%s = %v, want %v", cmd.Use, nil, err)
}

cmd = NewDeleteWorkspaceCmd(
os.TempDir(),
workspaceMock{},
dirManager,
listErrorMock{},
inputTrueMock{},
)

workspace = filepath.Join(os.TempDir(), "formulas-ritchie")
if err := os.MkdirAll(filepath.Join(workspace, "mock", "test", "scr"), os.ModePerm); err != nil {
t.Errorf("TestNewDeleteWorkspaceCmd got error %v", err)
}

if cmd == nil {
t.Errorf("NewDeleteWorkspaceCmd got %v", cmd)
}

if err := cmd.Execute(); err == nil {
t.Errorf("%s = %v, want %v", cmd.Use, nil, err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
workspaceMock := new(mocks.WorkspaceForm)
workspaceMock.On("List").Return(tt.in.wspaceList, tt.in.wspaceListErr)
workspaceMock.On("Delete", mock.Anything).Return(tt.in.wspaceDeleteErr)
dirMock := new(mocks.DirManager)
dirMock.On("Exists", mock.Anything).Return(tt.in.dirExist)
inListMock := new(mocks.InputListMock)
inListMock.On("List", mock.Anything, mock.Anything, mock.Anything).Return(tt.in.inputList, tt.in.inputListErr)
inBoolMock := new(mocks.InputBoolMock)
inBoolMock.On("Bool", mock.Anything, mock.Anything, mock.Anything).Return(tt.in.inputBool, tt.in.inputBoolErr)
repoManagerMock := new(mocks.RepoManager)
repoManagerMock.On("Delete", mock.Anything).Return(tt.in.repoDeleteErr)

cmd := NewDeleteWorkspaceCmd(
os.TempDir(),
workspaceMock,
repoManagerMock,
dirMock,
inListMock,
inBoolMock,
)

got := cmd.Execute()
assert.Equal(t, tt.want, got)
})
}
}
2 changes: 1 addition & 1 deletion pkg/commands/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func Build() *cobra.Command {
autocompleteBash := cmd.NewAutocompleteBash(autocompleteGen)
autocompleteFish := cmd.NewAutocompleteFish(autocompleteGen)
autocompletePowerShell := cmd.NewAutocompletePowerShell(autocompleteGen)
deleteWorkspaceCmd := cmd.NewDeleteWorkspaceCmd(userHomeDir, formulaWorkspace, dirManager, inputList, inputBool)
deleteWorkspaceCmd := cmd.NewDeleteWorkspaceCmd(userHomeDir, formulaWorkspace, repoDeleter, dirManager, inputList, inputBool)
deleteFormulaCmd := cmd.NewDeleteFormulaCmd(userHomeDir, ritchieHomeDir, formulaWorkspace, dirManager, inputBool, inputText, inputList, treeGen, fileManager)
addWorkspaceCmd := cmd.NewAddWorkspaceCmd(formulaWorkspace, inputText)

Expand Down
Loading

0 comments on commit 4605949

Please sign in to comment.