-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcleanup_test.go
More file actions
206 lines (175 loc) · 5.43 KB
/
cleanup_test.go
File metadata and controls
206 lines (175 loc) · 5.43 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package cmd
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestGetMergedBranches(t *testing.T) {
// This test runs in the actual git repository
// We test against the default base branch
base := getDefaultBase()
if base == "" {
t.Skip("Could not determine default branch, skipping test")
}
branches, err := getMergedBranches(base)
if err != nil {
// If we're in detached HEAD or can't run the command, skip
t.Skipf("Could not get merged branches: %v", err)
}
// Verify base branch is not included
for _, branch := range branches {
if branch == base || branch == "main" || branch == "master" {
t.Errorf("getMergedBranches() included base branch %q in results", branch)
}
}
// Verify no empty branches
for _, branch := range branches {
if strings.TrimSpace(branch) == "" {
t.Error("getMergedBranches() returned empty branch name")
}
}
}
func TestGetMergedBranchesFiltersBaseBranches(t *testing.T) {
// Create a temporary git repo to test branch filtering
tmpDir, err := os.MkdirTemp("", "wt-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
// Initialize git repo
cmds := [][]string{
{"git", "init"},
{"git", "config", "user.email", "test@test.com"},
{"git", "config", "user.name", "Test"},
{"git", "commit", "--allow-empty", "-m", "Initial commit"},
{"git", "branch", "-M", "main"},
}
for _, args := range cmds {
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = tmpDir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("Failed to run %v: %v\n%s", args, err, out)
}
}
// Save current dir and change to temp repo
origDir, _ := os.Getwd()
defer os.Chdir(origDir)
os.Chdir(tmpDir)
// Test that main is filtered out
branches, err := getMergedBranches("main")
if err != nil {
t.Fatalf("getMergedBranches failed: %v", err)
}
for _, b := range branches {
if b == "main" || b == "master" {
t.Errorf("getMergedBranches should filter out %q", b)
}
}
}
func TestCleanupCommandFlags(t *testing.T) {
// Test that the cleanup command has the expected flags
cmd := cleanupCmd
dryRunFlag := cmd.Flags().Lookup("dry-run")
if dryRunFlag == nil {
t.Error("cleanup command missing --dry-run flag")
}
forceFlag := cmd.Flags().Lookup("force")
if forceFlag == nil {
t.Error("cleanup command missing --force flag")
}
// Check shorthand for force
if forceFlag != nil && forceFlag.Shorthand != "f" {
t.Errorf("cleanup --force flag shorthand = %q, want %q", forceFlag.Shorthand, "f")
}
}
func TestCleanupCommandRegistered(t *testing.T) {
// Verify the cleanup command is registered with the root command
found := false
for _, cmd := range rootCmd.Commands() {
if cmd.Name() == "cleanup" {
found = true
break
}
}
if !found {
t.Error("cleanup command not registered with root command")
}
}
func TestCleanupE2E(t *testing.T) {
// Skip if not in a git repo with worktree support
if _, err := exec.Command("git", "rev-parse", "--git-dir").Output(); err != nil {
t.Skip("Not in a git repository, skipping E2E test")
}
// Create a temporary directory for our test worktree root
tmpRoot, err := os.MkdirTemp("", "wt-cleanup-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpRoot)
// Create a temporary git repo for isolated testing
repoDir := filepath.Join(tmpRoot, "repo")
worktreeDir := filepath.Join(tmpRoot, "worktrees")
os.MkdirAll(repoDir, 0755)
os.MkdirAll(worktreeDir, 0755)
// Initialize a test git repo
cmds := [][]string{
{"git", "init"},
{"git", "config", "user.email", "test@test.com"},
{"git", "config", "user.name", "Test"},
{"git", "commit", "--allow-empty", "-m", "Initial commit"},
{"git", "branch", "-M", "main"},
// Create a branch that will be "merged"
{"git", "checkout", "-b", "feature-merged"},
{"git", "checkout", "main"},
}
for _, args := range cmds {
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = repoDir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("Failed to run %v: %v\n%s", args, err, out)
}
}
// Create a worktree for the merged branch
wtPath := filepath.Join(worktreeDir, "feature-merged")
cmd := exec.Command("git", "worktree", "add", wtPath, "feature-merged")
cmd.Dir = repoDir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("Failed to create test worktree: %v\n%s", err, out)
}
// Verify the worktree was created
if _, err := os.Stat(wtPath); os.IsNotExist(err) {
t.Fatal("Test worktree was not created")
}
// Save current dir and change to test repo
origDir, _ := os.Getwd()
defer os.Chdir(origDir)
os.Chdir(repoDir)
// Test dry-run mode (should not remove anything)
cleanupDryRun = true
cleanupForce = false
err = cleanupCmd.RunE(cleanupCmd, []string{})
cleanupDryRun = false
if err != nil {
t.Errorf("cleanup --dry-run failed: %v", err)
}
// Verify worktree still exists after dry-run
if _, err := os.Stat(wtPath); os.IsNotExist(err) {
t.Error("Worktree was removed during dry-run (should not happen)")
}
// Test force mode (should remove without prompting)
cleanupForce = true
err = cleanupCmd.RunE(cleanupCmd, []string{})
cleanupForce = false
if err != nil {
t.Errorf("cleanup --force failed: %v", err)
}
// Verify worktree was removed
cmd = exec.Command("git", "worktree", "list")
cmd.Dir = repoDir
output, _ := cmd.Output()
if strings.Contains(string(output), "feature-merged") {
t.Error("Worktree was not removed after cleanup --force")
}
}