|
2 | 2 | package cmd_test |
3 | 3 |
|
4 | 4 | import ( |
5 | | - "os" |
6 | | - "os/exec" |
7 | | - "path/filepath" |
8 | 5 | "testing" |
9 | 6 |
|
10 | 7 | "github.com/boneskull/gh-stack/internal/config" |
11 | | - "github.com/boneskull/gh-stack/internal/detect" |
12 | 8 | "github.com/boneskull/gh-stack/internal/git" |
13 | 9 | "github.com/boneskull/gh-stack/internal/tree" |
14 | 10 | ) |
15 | 11 |
|
16 | | -// addCommit creates a file with the given content and commits it. |
17 | | -func addCommit(t *testing.T, dir, filename, content string) { |
18 | | - t.Helper() |
19 | | - if err := os.WriteFile(filepath.Join(dir, filename), []byte(content), 0644); err != nil { |
20 | | - t.Fatalf("write %s: %v", filename, err) |
21 | | - } |
22 | | - cmd := exec.Command("git", "-C", dir, "add", ".") |
23 | | - if err := cmd.Run(); err != nil { |
24 | | - t.Fatalf("git add: %v", err) |
25 | | - } |
26 | | - cmd = exec.Command("git", "-C", dir, "commit", "-m", "add "+filename) |
27 | | - if err := cmd.Run(); err != nil { |
28 | | - t.Fatalf("git commit: %v", err) |
29 | | - } |
30 | | -} |
31 | | - |
32 | 12 | func TestAdoptBranch(t *testing.T) { |
33 | 13 | dir := setupTestRepo(t) |
34 | 14 |
|
@@ -168,114 +148,3 @@ func TestAdoptStoresForkPoint(t *testing.T) { |
168 | 148 | t.Errorf("fork point = %s, want %s", storedFP, trunkTip) |
169 | 149 | } |
170 | 150 | } |
171 | | - |
172 | | -// TestAdoptAutoDetect exercises the full detection-to-adoption pipeline: |
173 | | -// detect parent, validate, set parent, store fork point. |
174 | | -func TestAdoptAutoDetect(t *testing.T) { |
175 | | - dir := setupTestRepo(t) |
176 | | - g := git.New(dir) |
177 | | - |
178 | | - cfg, err := config.Load(dir) |
179 | | - if err != nil { |
180 | | - t.Fatalf("Load config: %v", err) |
181 | | - } |
182 | | - |
183 | | - trunk, err := g.CurrentBranch() |
184 | | - if err != nil { |
185 | | - t.Fatalf("CurrentBranch: %v", err) |
186 | | - } |
187 | | - |
188 | | - err = cfg.SetTrunk(trunk) |
189 | | - if err != nil { |
190 | | - t.Fatalf("SetTrunk: %v", err) |
191 | | - } |
192 | | - |
193 | | - // Create tracked branch A |
194 | | - err = g.CreateAndCheckout("feature-a") |
195 | | - if err != nil { |
196 | | - t.Fatalf("CreateAndCheckout feature-a: %v", err) |
197 | | - } |
198 | | - addCommit(t, dir, "a.txt", "a") |
199 | | - |
200 | | - err = cfg.SetParent("feature-a", trunk) |
201 | | - if err != nil { |
202 | | - t.Fatalf("SetParent feature-a: %v", err) |
203 | | - } |
204 | | - |
205 | | - // Create untracked branch B off A |
206 | | - err = g.CreateAndCheckout("feature-b") |
207 | | - if err != nil { |
208 | | - t.Fatalf("CreateAndCheckout feature-b: %v", err) |
209 | | - } |
210 | | - addCommit(t, dir, "b.txt", "b") |
211 | | - |
212 | | - // feature-b should not be tracked yet |
213 | | - _, getErr := cfg.GetParent("feature-b") |
214 | | - if getErr == nil { |
215 | | - t.Fatal("feature-b should not be tracked yet") |
216 | | - } |
217 | | - |
218 | | - // Simulate what runAdopt does when no parent arg is given: |
219 | | - // 1. Detect parent |
220 | | - tracked, _ := cfg.ListTrackedBranches() |
221 | | - result, detectErr := detect.DetectParent("feature-b", tracked, trunk, g, nil) |
222 | | - if detectErr != nil { |
223 | | - t.Fatalf("detection failed: %v", detectErr) |
224 | | - } |
225 | | - if result.Confidence == detect.Ambiguous { |
226 | | - t.Fatal("expected non-ambiguous detection") |
227 | | - } |
228 | | - if result.Parent != "feature-a" { |
229 | | - t.Errorf("expected detected parent 'feature-a', got %q", result.Parent) |
230 | | - } |
231 | | - |
232 | | - // 2. Validate parent is tracked (same check as runAdopt) |
233 | | - if result.Parent != trunk { |
234 | | - if _, parentErr := cfg.GetParent(result.Parent); parentErr != nil { |
235 | | - t.Fatalf("detected parent %q is not tracked: %v", result.Parent, parentErr) |
236 | | - } |
237 | | - } |
238 | | - |
239 | | - // 3. Set parent (same as runAdopt) |
240 | | - err = cfg.SetParent("feature-b", result.Parent) |
241 | | - if err != nil { |
242 | | - t.Fatalf("SetParent failed: %v", err) |
243 | | - } |
244 | | - |
245 | | - // 4. Store fork point (same as runAdopt) |
246 | | - forkPoint, fpErr := g.GetMergeBase("feature-b", result.Parent) |
247 | | - if fpErr != nil { |
248 | | - t.Fatalf("GetMergeBase failed: %v", fpErr) |
249 | | - } |
250 | | - _ = cfg.SetForkPoint("feature-b", forkPoint) |
251 | | - |
252 | | - // Verify the full adoption persisted correctly |
253 | | - parent, err := cfg.GetParent("feature-b") |
254 | | - if err != nil { |
255 | | - t.Fatalf("feature-b should be tracked now: %v", err) |
256 | | - } |
257 | | - if parent != "feature-a" { |
258 | | - t.Errorf("expected parent 'feature-a', got %q", parent) |
259 | | - } |
260 | | - |
261 | | - storedFP, fpGetErr := cfg.GetForkPoint("feature-b") |
262 | | - if fpGetErr != nil { |
263 | | - t.Fatalf("GetForkPoint failed: %v", fpGetErr) |
264 | | - } |
265 | | - if storedFP != forkPoint { |
266 | | - t.Errorf("fork point mismatch: stored=%s, expected=%s", storedFP, forkPoint) |
267 | | - } |
268 | | - |
269 | | - // Verify tree now includes feature-b |
270 | | - root, buildErr := tree.Build(cfg) |
271 | | - if buildErr != nil { |
272 | | - t.Fatalf("Build failed: %v", buildErr) |
273 | | - } |
274 | | - nodeB := tree.FindNode(root, "feature-b") |
275 | | - if nodeB == nil { |
276 | | - t.Fatal("feature-b should appear in tree after adoption") |
277 | | - } |
278 | | - if nodeB.Parent.Name != "feature-a" { |
279 | | - t.Errorf("expected parent node 'feature-a', got %q", nodeB.Parent.Name) |
280 | | - } |
281 | | -} |
0 commit comments