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

created rit add workspace command #809

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions internal/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,17 @@ func (tm *TemplateManagerMock) Validate() error {
args := tm.Called()
return args.Error(0)
}

type WorkspaceMock struct {
mock.Mock
}

func (w *WorkspaceMock) Add(workspace formula.Workspace) error {
args := w.Called(workspace)
return args.Error(0)
}

func (w *WorkspaceMock) List() (formula.Workspaces, error) {
args := w.Called()
return args.Get(0).(formula.Workspaces), args.Error(1)
}
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
CoreCmds = Commands{
"root_add": {Parent: "root", Usage: "add"},
"root_add_repo": {Parent: "root_add", Usage: "repo"},
"root_add_workspace": {Parent: "root_add", Usage: "workspace"},
"root_completion": {Parent: "root", Usage: "completion"},
"root_completion_bash": {Parent: "root_completion", Usage: "bash"},
"root_completion_zsh": {Parent: "root_completion", Usage: "zsh"},
Expand Down
103 changes: 103 additions & 0 deletions pkg/cmd/add_workspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package cmd

import (
"errors"
"strings"

"github.com/spf13/cobra"

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
)

const (
workspaceNameFlag = "name"
workspaceNameFlagDescription = "Workspace name"
workspacePathFlag = "path"
workspacePathFlagDescription = "Workspace path"
)

type addWorkspaceCmd struct {
workspace formula.WorkspaceAddLister
input prompt.InputText
}

func NewAddWorkspaceCmd(
workspace formula.WorkspaceAddLister,
input prompt.InputText,
) *cobra.Command {
a := addWorkspaceCmd{
workspace: workspace,
input: input,
}

cmd := &cobra.Command{
Use: "workspace",
Short: "Add new workspace",
Example: "rit add workspace",
RunE: a.runFormula(),
ValidArgs: []string{""},
Args: cobra.OnlyValidArgs,
}

flags := cmd.Flags()
flags.String(workspaceNameFlag, "", workspaceNameFlagDescription)
flags.String(workspacePathFlag, "", workspacePathFlagDescription)

return cmd
}

func (a addWorkspaceCmd) runFormula() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
if cmd.Flags().NFlag() == 0 {
wspace, err := addWorkspaceFromPrompt(a)
if err != nil {
return err
}

return a.workspace.Add(wspace)
}

wspace, err := addWorkspaceFromFlags(cmd)
if err != nil {
return err
}

return a.workspace.Add(wspace)
}
}

func addWorkspaceFromFlags(cmd *cobra.Command) (formula.Workspace, error) {
workspaceName, _ := cmd.Flags().GetString(workspaceNameFlag)
workspacePath, _ := cmd.Flags().GetString(workspacePathFlag)

if len(workspaceName) == 0 || len(workspacePath) == 0 {
return formula.Workspace{}, errors.New("all flags need to be filled")
}

wspace := formula.Workspace{
Name: strings.Title(workspaceName),
Dir: workspacePath,
}

return wspace, nil
}

func addWorkspaceFromPrompt(a addWorkspaceCmd) (formula.Workspace, error) {
workspaceName, err := a.input.Text("Enter the name of workspace", true)
if err != nil {
return formula.Workspace{}, err
}

workspacePath, err := a.input.Text("Enter the path of workspace (e.g.: /home/user/github)", true)
if err != nil {
return formula.Workspace{}, err
}

wspace := formula.Workspace{
Name: strings.Title(workspaceName),
Dir: workspacePath,
}

return wspace, nil
}
83 changes: 83 additions & 0 deletions pkg/cmd/add_workspace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cmd

import (
"errors"
"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"
)

func TestNewAddWorkspaceCmd(t *testing.T) {
var tests = []struct {
name string
addWorkspaceWithError error
workspaceName string
workspacePath string
argsName string
argsPath string
wantErr string
}{
{
name: "add new workspace by prompt",
Copy link
Contributor

Choose a reason for hiding this comment

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

One detail, on a success case, you want to check if the workspace entry was actually added to ~/.rit/formula_workspaces.json so maybe you do not want to mock the workspace added

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if I wanted to check that.
My unit it's until I called the method a.workspace.Add(wspace) after that the behavior ins't responsability of this method. Because of this I mocked my test.
What do you think?!

Copy link
Contributor

Choose a reason for hiding this comment

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

I do agree with you, lately we have been trying to adopt a more functional-like approach on our tests. But your argument is good as well, if you intend to continue with the mock, then maybe it is best to test that it has been called with the right arguments to ensure a thorough test. Check out assertCalled
https://godoc.org/github.com/stretchr/testify/mock#Mock.AssertCalled

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right, I can mock my test, but at least a need to guarantee I'm passing the right parameters.
Thanks for the observation! 😄

addWorkspaceWithError: nil,
workspaceName: "Teste",
workspacePath: "/home/user/dir",
wantErr: "",
},
{
name: "add new workspace by flags",
addWorkspaceWithError: nil,
workspaceName: "Teste",
workspacePath: "/home/user/dir",
argsName: "--name=Teste",
argsPath: "--path=/home/user/dir",
wantErr: "",
},
{
name: "error when one flags is no filled",
addWorkspaceWithError: nil,
workspaceName: "Teste",
workspacePath: "",
argsName: "--name=Teste",
argsPath: "",
wantErr: "all flags need to be filled",
},
{
name: "error when workspace does not exists",
addWorkspaceWithError: errors.New("workspace does not exists"),
workspaceName: "Teste",
workspacePath: "/home/user/dir",
wantErr: "workspace does not exists",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

textMock := &mocks.InputTextMock{}
textMock.On("Text", "Enter the name of workspace", true, mock.Anything).Return(tt.workspaceName, nil)
textMock.On("Text", "Enter the path of workspace (e.g.: /home/user/github)", true, mock.Anything).Return(tt.workspacePath, nil)

wspace := formula.Workspace{
Name: tt.workspaceName,
Dir: tt.workspacePath,
}

workspaceMock := &mocks.WorkspaceMock{}
workspaceMock.On("Add", wspace).Return(tt.addWorkspaceWithError)
Copy link
Contributor

Choose a reason for hiding this comment

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

nice, this is even better than what I suggested 😄 will be recommending this to the team


addNewWorkspace := NewAddWorkspaceCmd(workspaceMock, textMock)
addNewWorkspace.SetArgs([]string{tt.argsName, tt.argsPath})

err := addNewWorkspace.Execute()
if err != nil {
assert.Equal(t, err.Error(), tt.wantErr)
} else {
assert.Empty(t, tt.wantErr)
}
})
}
}
3 changes: 2 additions & 1 deletion pkg/commands/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,15 @@ func Build() *cobra.Command {
autocompletePowerShell := cmd.NewAutocompletePowerShell(autocompleteGen)
deleteWorkspaceCmd := cmd.NewDeleteWorkspaceCmd(userHomeDir, formulaWorkspace, dirManager, inputList, inputBool)
deleteFormulaCmd := cmd.NewDeleteFormulaCmd(userHomeDir, ritchieHomeDir, formulaWorkspace, dirManager, inputBool, inputText, inputList, treeGen, fileManager)
addWorkspaceCmd := cmd.NewAddWorkspaceCmd(formulaWorkspace, inputText)

createFormulaCmd := cmd.NewCreateFormulaCmd(userHomeDir, createBuilder, tplManager, formulaWorkspace, inputText, inputTextValidator, inputList, tutorialFinder, treeChecker)
buildFormulaCmd := cmd.NewBuildFormulaCmd()
showFormulaRunnerCmd := cmd.NewShowFormulaRunnerCmd(configManager)
setFormulaRunnerCmd := cmd.NewSetFormulaRunnerCmd(configManager, inputList)

autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash, autocompleteFish, autocompletePowerShell)
addCmd.AddCommand(addRepoCmd)
addCmd.AddCommand(addRepoCmd, addWorkspaceCmd)
updateCmd.AddCommand(updateRepoCmd)
createCmd.AddCommand(createFormulaCmd)
deleteCmd.AddCommand(deleteEnvCmd, deleteRepoCmd, deleteFormulaCmd, deleteWorkspaceCmd, deleteCredentialCmd)
Expand Down