-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremove_force_test.go
More file actions
58 lines (47 loc) · 2 KB
/
remove_force_test.go
File metadata and controls
58 lines (47 loc) · 2 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
package main
import (
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestRemoveForceFlagRemovesDirtyWorktree(t *testing.T) {
if testing.Short() {
t.Skip("Skipping force remove test in short mode")
}
tmpDir := t.TempDir()
repoDir := filepath.Join(tmpDir, "test-repo")
worktreeRoot := filepath.Join(tmpDir, "worktrees")
setupTestRepo(t, repoDir)
wtBinary := buildWtBinary(t, tmpDir)
// Prepare a branch and checkout a worktree for it
runGitCommand(t, repoDir, "branch", "force-remove-branch")
checkoutCmd := exec.Command(wtBinary, "checkout", "force-remove-branch")
checkoutCmd.Dir = repoDir
checkoutCmd.Env = append(os.Environ(), "WORKTREE_ROOT="+worktreeRoot)
if output, err := checkoutCmd.CombinedOutput(); err != nil {
t.Fatalf("Failed to create worktree: %v\nOutput: %s", err, output)
}
worktreePath := filepath.Join(worktreeRoot, "test-repo", "force-remove-branch")
// Make the worktree dirty so a normal remove should fail
if err := os.WriteFile(filepath.Join(worktreePath, "dirty.txt"), []byte("dirty"), 0o644); err != nil {
t.Fatalf("Failed to create dirty file in worktree: %v", err)
}
removeCmd := exec.Command(wtBinary, "remove", "force-remove-branch")
removeCmd.Dir = repoDir
removeCmd.Env = append(os.Environ(), "WORKTREE_ROOT="+worktreeRoot)
if output, err := removeCmd.CombinedOutput(); err == nil {
t.Fatal("Expected remove without --force to fail for dirty worktree")
} else if _, err := os.Stat(worktreePath); os.IsNotExist(err) {
t.Fatalf("Worktree unexpectedly removed without --force: %v\nOutput: %s", err, output)
}
forceCmd := exec.Command(wtBinary, "remove", "--force", "force-remove-branch")
forceCmd.Dir = repoDir
forceCmd.Env = append(os.Environ(), "WORKTREE_ROOT="+worktreeRoot)
if output, err := forceCmd.CombinedOutput(); err != nil {
t.Fatalf("Failed to remove worktree with --force: %v\nOutput: %s", err, output)
}
if _, err := os.Stat(worktreePath); !os.IsNotExist(err) {
t.Fatalf("Expected worktree path to be removed with --force, got err: %v", err)
}
}