Skip to content

Commit

Permalink
Handle adding existing branch in stack (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz authored Apr 10, 2024
1 parent 3656cd2 commit 90d0b55
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/stack/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func (sm StacksManager) AddBranch(branchName string, position int) error {
data := *sm.stacks
stack, _ := data.GetStackByName(data.CurrentStack)

if slices.Contains(stack.Branches, branchName) {
sm.printer.Println("Branch", color.Yellow(branchName), "already in", color.Green(data.CurrentStack))
return nil
}

if position == 0 || position > len(stack.Branches) {
stack.Branches = append(stack.Branches, branchName)
} else {
Expand Down
28 changes: 28 additions & 0 deletions internal/stack/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,34 @@ func TestStacksManager_AddBranch(t *testing.T) {
t.Errorf("got %s, want %s", data.Stacks[0].Branches[0], "my_branch")
}
})

t.Run("when the branch is already in the stack", func(t *testing.T) {
gitExecutor := gitExecutorStub{
stubExec: func(command ...string) (string, error) {
return "", nil
},
}

var messageReceived []string
stacksManager := StacksManagerForTest(gitExecutor, &messageReceived)

result := stacksManager.AddBranch("branch1", 0)

want := "Branch " + color.Yellow("branch1") + " already in " + color.Green("stack1") + "\n"
got := stacksManager.printerMessage()
if !strings.Contains(got, want) {
t.Errorf("got \"%s\", want \"%s\"", got, want)
}

if result != nil {
t.Errorf("show have no error, got %s", result)
}

data := *stacksManager.stacks
if len(data.Stacks[0].Branches) != 2 {
t.Errorf("got %d, want %d", len(data.Stacks[0].Branches), 2)
}
})
}

func TestStacksManager_List(t *testing.T) {
Expand Down

0 comments on commit 90d0b55

Please sign in to comment.