forked from boneskull/gh-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorphan_test.go
More file actions
176 lines (139 loc) · 3.87 KB
/
orphan_test.go
File metadata and controls
176 lines (139 loc) · 3.87 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
// cmd/orphan_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 TestOrphanBranch(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("feature-a")
cfg.SetParent("feature-a", trunk)
// Verify it's tracked
_, err := cfg.GetParent("feature-a")
if err != nil {
t.Fatal("branch should be tracked")
}
// Orphan it
cfg.RemoveParent("feature-a")
// Verify it's no longer tracked
_, err = cfg.GetParent("feature-a")
if err == nil {
t.Error("branch should no longer be tracked after orphan")
}
}
func TestOrphanRejectsWithChildren(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Create 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
root, _ := tree.Build(cfg)
node := tree.FindNode(root, "feature-a")
// feature-a has children, so orphan should require --force
if len(node.Children) == 0 {
t.Error("expected feature-a to have children")
}
}
func TestOrphanForceWithDescendants(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Create chain: trunk -> feature-a -> feature-b -> feature-c
g.CreateBranch("feature-a")
cfg.SetParent("feature-a", trunk)
g.CreateBranch("feature-b")
cfg.SetParent("feature-b", "feature-a")
g.CreateBranch("feature-c")
cfg.SetParent("feature-c", "feature-b")
// Build tree and get descendants
root, _ := tree.Build(cfg)
node := tree.FindNode(root, "feature-a")
descendants := tree.GetDescendants(node)
// Should have 2 descendants
if len(descendants) != 2 {
t.Errorf("expected 2 descendants, got %d", len(descendants))
}
// Simulate --force orphan
for _, desc := range descendants {
cfg.RemoveParent(desc.Name)
}
cfg.RemoveParent("feature-a")
// Verify all are orphaned
_, err := cfg.GetParent("feature-a")
if err == nil {
t.Error("feature-a should be orphaned")
}
_, err = cfg.GetParent("feature-b")
if err == nil {
t.Error("feature-b should be orphaned")
}
_, err = cfg.GetParent("feature-c")
if err == nil {
t.Error("feature-c should be orphaned")
}
}
func TestOrphanRemovesPR(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Create branch with PR
g.CreateBranch("feature-a")
cfg.SetParent("feature-a", trunk)
cfg.SetPR("feature-a", 123)
// Verify PR is set
pr, err := cfg.GetPR("feature-a")
if err != nil || pr != 123 {
t.Fatal("PR should be set")
}
// Orphan (which should also remove PR)
cfg.RemoveParent("feature-a")
cfg.RemovePR("feature-a")
// Verify PR is gone
_, err = cfg.GetPR("feature-a")
if err == nil {
t.Error("PR should be removed after orphan")
}
}
func TestOrphanRemovesForkPoint(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Create branch with fork point
g.CreateBranch("feature-a")
cfg.SetParent("feature-a", trunk)
trunkTip, _ := g.GetTip(trunk)
cfg.SetForkPoint("feature-a", trunkTip)
// Verify fork point is set
fp, err := cfg.GetForkPoint("feature-a")
if err != nil || fp != trunkTip {
t.Fatal("fork point should be set")
}
// Orphan (which should also remove fork point)
cfg.RemoveParent("feature-a")
cfg.RemovePR("feature-a")
cfg.RemoveForkPoint("feature-a")
// Verify fork point is gone
_, err = cfg.GetForkPoint("feature-a")
if err == nil {
t.Error("fork point should be removed after orphan")
}
}