forked from boneskull/gh-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadopt_test.go
More file actions
150 lines (116 loc) · 3.36 KB
/
adopt_test.go
File metadata and controls
150 lines (116 loc) · 3.36 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
// cmd/adopt_test.go
package cmd_test
import (
"testing"
"github.com/boneskull/gh-stack/internal/config"
"github.com/boneskull/gh-stack/internal/git"
"github.com/boneskull/gh-stack/internal/tree"
)
func TestAdoptBranch(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Create an untracked branch
g.CreateBranch("untracked-feature")
// Adopt it
err := cfg.SetParent("untracked-feature", trunk)
if err != nil {
t.Fatalf("SetParent failed: %v", err)
}
// Verify
parent, err := cfg.GetParent("untracked-feature")
if err != nil {
t.Fatalf("GetParent failed: %v", err)
}
if parent != trunk {
t.Errorf("expected parent %q, got %q", trunk, parent)
}
}
func TestAdoptRejectsAlreadyTracked(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Create and track a branch
g.CreateBranch("tracked-feature")
cfg.SetParent("tracked-feature", trunk)
// Trying to get parent should succeed (it's tracked)
_, err := cfg.GetParent("tracked-feature")
if err != nil {
t.Error("expected branch to be tracked")
}
}
func TestAdoptRejectsUntrackedParent(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Create two untracked branches
g.CreateBranch("untracked-a")
g.CreateBranch("untracked-b")
// Trying to adopt with untracked parent should fail
// The parent must be trunk or tracked
_, err := cfg.GetParent("untracked-a")
if err == nil {
t.Error("untracked-a should not be tracked")
}
}
func TestAdoptDetectsCycle(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Create a chain: trunk -> feature-a -> feature-b
g.CreateBranch("feature-a")
cfg.SetParent("feature-a", trunk)
g.CreateBranch("feature-b")
cfg.SetParent("feature-b", "feature-a")
// Build tree and check for cycle detection
root, _ := tree.Build(cfg)
// If we tried to make feature-a's parent be feature-b, that would be a cycle
// Check that feature-a is an ancestor of feature-b
featureBNode := tree.FindNode(root, "feature-b")
ancestors := tree.GetAncestors(featureBNode)
foundFeatureA := false
for _, a := range ancestors {
if a.Name == "feature-a" {
foundFeatureA = true
break
}
}
if !foundFeatureA {
t.Error("expected feature-a to be ancestor of feature-b")
}
}
func TestAdoptStoresForkPoint(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Get trunk tip before creating branch
trunkTip, _ := g.GetTip(trunk)
// Create an untracked branch
g.CreateBranch("untracked-feature")
// Simulate adopt: set parent and fork point
cfg.SetParent("untracked-feature", trunk)
// Store fork point (what adopt command should now do)
forkPoint, fpErr := g.GetMergeBase("untracked-feature", trunk)
if fpErr != nil {
t.Fatalf("GetMergeBase failed: %v", fpErr)
}
cfg.SetForkPoint("untracked-feature", forkPoint)
// Verify fork point was stored
storedFP, err := cfg.GetForkPoint("untracked-feature")
if err != nil {
t.Fatalf("GetForkPoint failed: %v", err)
}
if storedFP != trunkTip {
t.Errorf("fork point = %s, want %s", storedFP, trunkTip)
}
}