-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_test.go
More file actions
67 lines (58 loc) · 1.55 KB
/
list_test.go
File metadata and controls
67 lines (58 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"bytes"
"errors"
"strings"
"testing"
)
func TestList(t *testing.T) {
// Save original function and restore after test
origListWorktrees := listWorktreesFn
defer func() {
listWorktreesFn = origListWorktrees
}()
t.Run("success with worktrees", func(t *testing.T) {
listWorktreesFn = func() ([]string, error) {
return []string{"feature-a", "feature-b", "bugfix-c"}, nil
}
var buf bytes.Buffer
err := list(&buf)
if err != nil {
t.Errorf("list() unexpected error: %v", err)
}
output := buf.String()
lines := strings.Split(strings.TrimSpace(output), "\n")
if len(lines) != 3 {
t.Errorf("list() output %d lines, want 3", len(lines))
}
expected := map[string]bool{"feature-a": true, "feature-b": true, "bugfix-c": true}
for _, line := range lines {
if !expected[line] {
t.Errorf("list() unexpected line: %q", line)
}
}
})
t.Run("success with no worktrees", func(t *testing.T) {
listWorktreesFn = func() ([]string, error) {
return []string{}, nil
}
var buf bytes.Buffer
err := list(&buf)
if err != nil {
t.Errorf("list() unexpected error: %v", err)
}
if buf.Len() != 0 {
t.Errorf("list() wrote output for empty list: %q", buf.String())
}
})
t.Run("error from listWorktrees", func(t *testing.T) {
listWorktreesFn = func() ([]string, error) {
return nil, errors.New("not in a git repository")
}
var buf bytes.Buffer
err := list(&buf)
if err == nil || err.Error() != "not in a git repository" {
t.Errorf("list() error = %v, want 'not in a git repository'", err)
}
})
}