-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworktree_test.go
More file actions
212 lines (189 loc) · 6.53 KB
/
Copy pathworktree_test.go
File metadata and controls
212 lines (189 loc) · 6.53 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
207
208
209
210
211
212
package main
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// initGitRepo creates a temp git repository with one commit and returns its path.
func initGitRepo(t *testing.T) string {
t.Helper()
dir := t.TempDir()
// Resolve symlinks so the repo root matches what git reports (macOS /var →
// /private/var), keeping path comparisons in tests exact.
if resolved, err := filepath.EvalSymlinks(dir); err == nil {
dir = resolved
}
run := func(args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
// Deterministic identity + no signing so `git commit` works in CI.
cmd.Env = append(os.Environ(),
"GIT_AUTHOR_NAME=turf", "GIT_AUTHOR_EMAIL=turf@example.com",
"GIT_COMMITTER_NAME=turf", "GIT_COMMITTER_EMAIL=turf@example.com")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
}
}
run("init")
run("commit", "--allow-empty", "-m", "init")
return dir
}
// setWorktreeFlags sets the package-level worktree flag vars for a test and
// restores them afterward.
func setWorktreeFlags(t *testing.T, name, base string) {
t.Helper()
origName, origBase := flagWorktree, flagWorktreeBase
flagWorktree, flagWorktreeBase = name, base
t.Cleanup(func() { flagWorktree, flagWorktreeBase = origName, origBase })
}
// enterDir chdirs to dir and restores the original cwd when the test ends. Uses a
// defer-style restore registered before any t.TempDir cleanup so the process
// leaves the temp tree before it is removed.
func enterDir(t *testing.T, dir string) {
t.Helper()
orig, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
if err := os.Chdir(dir); err != nil {
t.Fatalf("chdir %s: %v", dir, err)
}
t.Cleanup(func() { _ = os.Chdir(orig) })
}
func TestSetupRunWorktreeNotRequested(t *testing.T) {
setWorktreeFlags(t, "", "")
wt, anchor, err := setupRunWorktree(context.Background())
if err != nil {
t.Fatalf("setupRunWorktree: %v", err)
}
if wt != nil {
t.Fatalf("expected nil worktree when --worktree unset, got %+v", wt)
}
// No worktree: the anchor is empty, so the session store falls back to cwd.
if anchor != "" {
t.Fatalf("expected empty anchor when --worktree unset, got %q", anchor)
}
}
func TestSetupRunWorktreeBaseRequiresWorktree(t *testing.T) {
setWorktreeFlags(t, "", "main")
_, _, err := setupRunWorktree(context.Background())
if err == nil || !strings.Contains(err.Error(), "--worktree-base requires --worktree") {
t.Fatalf("expected base-requires-worktree error, got %v", err)
}
}
func TestSetupRunWorktreeCreatesAndChdirs(t *testing.T) {
home := t.TempDir()
// Resolve symlinks (macOS /var → /private/var) so the expected dir matches the
// symlink-resolved cwd after setupRunWorktree chdirs.
if resolved, err := filepath.EvalSymlinks(home); err == nil {
home = resolved
}
t.Setenv("TURF_HOME", home)
repo := initGitRepo(t)
enterDir(t, repo)
setWorktreeFlags(t, "authoring", "")
wt, anchor, err := setupRunWorktree(context.Background())
if err != nil {
t.Fatalf("setupRunWorktree: %v", err)
}
if wt == nil {
t.Fatal("expected a worktree, got nil")
}
t.Cleanup(func() { _ = os.Chdir(repo); _ = wt.Remove(context.Background()) })
// The anchor is the launch dir (repo), captured before the chdir into the
// worktree, so session history is pinned to the real project, not the worktree.
if resolved, err := filepath.EvalSymlinks(anchor); err == nil {
anchor = resolved
}
if anchor != repo {
t.Errorf("anchor = %q, want launch dir %q", anchor, repo)
}
wantDir := filepath.Join(home, "worktrees", "authoring")
if wt.Dir != wantDir {
t.Errorf("worktree Dir = %q, want %q", wt.Dir, wantDir)
}
if wt.Branch != "worktree-authoring" {
t.Errorf("worktree Branch = %q, want worktree-authoring", wt.Branch)
}
// setupRunWorktree must chdir into the worktree so the server (which inherits
// cwd), the filesystem tool, and memory all follow.
cwd, _ := os.Getwd()
if resolved, err := filepath.EvalSymlinks(cwd); err == nil {
cwd = resolved
}
if cwd != wt.Dir {
t.Errorf("cwd = %q after setup, want worktree dir %q", cwd, wt.Dir)
}
}
func TestSetupRunWorktreeNonGitRepo(t *testing.T) {
t.Setenv("TURF_HOME", t.TempDir())
enterDir(t, t.TempDir())
setWorktreeFlags(t, "auto", "")
_, _, err := setupRunWorktree(context.Background())
if err == nil || !strings.Contains(err.Error(), "git repository") {
t.Fatalf("expected not-a-git-repository error, got %v", err)
}
}
func TestCleanupRunWorktreeNonInteractiveKeeps(t *testing.T) {
home := t.TempDir()
t.Setenv("TURF_HOME", home)
repo := initGitRepo(t)
enterDir(t, repo)
setWorktreeFlags(t, "keepme", "")
wt, _, err := setupRunWorktree(context.Background())
if err != nil {
t.Fatalf("setupRunWorktree: %v", err)
}
t.Cleanup(func() { _ = os.Chdir(repo); _ = wt.Remove(context.Background()) })
// Non-interactive runs never remove the worktree.
cleanupRunWorktree(context.Background(), wt, false)
if _, err := os.Stat(wt.Dir); err != nil {
t.Fatalf("worktree should survive a non-interactive run, stat err: %v", err)
}
}
func TestCleanupRunWorktreeCleanRemoves(t *testing.T) {
home := t.TempDir()
t.Setenv("TURF_HOME", home)
repo := initGitRepo(t)
enterDir(t, repo)
setWorktreeFlags(t, "throwaway", "")
wt, _, err := setupRunWorktree(context.Background())
if err != nil {
t.Fatalf("setupRunWorktree: %v", err)
}
// A clean, interactive worktree is auto-removed (no prompt path taken).
cleanupRunWorktree(context.Background(), wt, true)
if _, err := os.Stat(wt.Dir); !os.IsNotExist(err) {
t.Fatalf("clean worktree should be removed, stat err: %v", err)
}
}
func TestCleanupRunWorktreeDirtyKeepsOnDecline(t *testing.T) {
home := t.TempDir()
t.Setenv("TURF_HOME", home)
repo := initGitRepo(t)
enterDir(t, repo)
setWorktreeFlags(t, "dirty", "")
wt, _, err := setupRunWorktree(context.Background())
if err != nil {
t.Fatalf("setupRunWorktree: %v", err)
}
t.Cleanup(func() { _ = os.Chdir(repo); _ = wt.Remove(context.Background()) })
// Make the worktree dirty (untracked file) so cleanup takes the prompt path.
if err := os.WriteFile(filepath.Join(wt.Dir, "main.tf"), []byte("# hcl\n"), 0o644); err != nil {
t.Fatalf("write: %v", err)
}
// Feed EOF on stdin so the prompt reads no "yes" and defaults to keep.
origStdin := os.Stdin
r, w, _ := os.Pipe()
_ = w.Close() // immediate EOF
os.Stdin = r
t.Cleanup(func() { os.Stdin = origStdin })
cleanupRunWorktree(context.Background(), wt, true)
if _, err := os.Stat(wt.Dir); err != nil {
t.Fatalf("dirty worktree should be kept on a non-yes answer, stat err: %v", err)
}
}