-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate_test.go
More file actions
227 lines (177 loc) · 5.27 KB
/
create_test.go
File metadata and controls
227 lines (177 loc) · 5.27 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// cmd/create_test.go
package cmd_test
import (
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/boneskull/gh-stack/internal/config"
"github.com/boneskull/gh-stack/internal/git"
)
func TestCreateFromTrunk(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
cfg.SetTrunk("main")
// Simulate what create command does
currentBranch, _ := g.CurrentBranch()
trunk, _ := cfg.GetTrunk()
if currentBranch != trunk && currentBranch != "master" {
t.Skipf("not on trunk branch, got %q", currentBranch)
}
// Create branch
err := g.CreateAndCheckout("feature-a")
if err != nil {
t.Fatalf("CreateAndCheckout failed: %v", err)
}
// Set parent
err = cfg.SetParent("feature-a", currentBranch)
if err != nil {
t.Fatalf("SetParent failed: %v", err)
}
// Verify
parent, err := cfg.GetParent("feature-a")
if err != nil {
t.Fatalf("GetParent failed: %v", err)
}
if parent != currentBranch {
t.Errorf("expected parent %q, got %q", currentBranch, parent)
}
newBranch, _ := g.CurrentBranch()
if newBranch != "feature-a" {
t.Errorf("expected to be on feature-a, got %q", newBranch)
}
}
func TestCreateFromTrackedBranch(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
currentBranch, _ := g.CurrentBranch()
cfg.SetTrunk(currentBranch)
// Create first feature branch
g.CreateAndCheckout("feature-a")
cfg.SetParent("feature-a", currentBranch)
// Create second feature branch stacked on first
g.CreateAndCheckout("feature-b")
cfg.SetParent("feature-b", "feature-a")
// Verify chain
parentB, _ := cfg.GetParent("feature-b")
if parentB != "feature-a" {
t.Errorf("expected parent 'feature-a', got %q", parentB)
}
parentA, _ := cfg.GetParent("feature-a")
if parentA != currentBranch {
t.Errorf("expected parent %q, got %q", currentBranch, parentA)
}
}
func TestCreateRejectsUntrackedBranch(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Create an untracked branch manually (not via stack)
g.CreateAndCheckout("untracked-branch")
// Now try to verify it's not tracked
_, err := cfg.GetParent("untracked-branch")
if err == nil {
t.Error("expected error for untracked branch")
}
// The create command would reject creating from here
// since untracked-branch is not trunk and not tracked
currentBranch, _ := g.CurrentBranch()
trunkName, _ := cfg.GetTrunk()
if currentBranch == trunkName {
t.Skip("unexpectedly on trunk")
}
_, err = cfg.GetParent(currentBranch)
if err == nil {
t.Error("expected untracked branch to have no parent")
}
}
func TestCreateWithStagedChanges(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Create and stage a file
f := filepath.Join(dir, "newfile.txt")
os.WriteFile(f, []byte("content"), 0644)
exec.Command("git", "-C", dir, "add", f).Run()
// Verify staged changes
hasStaged, _ := g.HasStagedChanges()
if !hasStaged {
t.Fatal("expected staged changes")
}
// Create branch and commit
g.CreateAndCheckout("feature-with-commit")
cfg.SetParent("feature-with-commit", trunk)
g.Commit("feat: add new file")
// Verify no more staged changes
hasStaged, _ = g.HasStagedChanges()
if hasStaged {
t.Error("expected no staged changes after commit")
}
}
func TestCreateEmptyWithStagedChanges(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Create and stage a file
f := filepath.Join(dir, "newfile.txt")
os.WriteFile(f, []byte("content"), 0644)
exec.Command("git", "-C", dir, "add", f).Run()
// Create branch WITHOUT committing (--empty behavior)
g.CreateAndCheckout("feature-empty")
cfg.SetParent("feature-empty", trunk)
// Don't commit - this simulates --empty flag
// Staged changes should still exist
hasStaged, _ := g.HasStagedChanges()
if !hasStaged {
t.Error("expected staged changes to remain with --empty")
}
}
func TestBranchAlreadyExists(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
// Create a branch
g.CreateBranch("existing-branch")
// Verify it exists
if !g.BranchExists("existing-branch") {
t.Fatal("branch should exist")
}
// The create command would reject this
// We just verify the check works
if !g.BranchExists("existing-branch") {
t.Error("BranchExists should return true")
}
}
func TestCreateStoresForkPoint(t *testing.T) {
dir := setupTestRepo(t)
g := git.New(dir)
cfg, _ := config.New(g)
trunk, _ := g.CurrentBranch()
cfg.SetTrunk(trunk)
// Get the tip of trunk before creating branch
trunkTip, _ := g.GetTip(trunk)
// Simulate create command: create branch and set parent + fork point
g.CreateAndCheckout("feature")
cfg.SetParent("feature", trunk)
// Store fork point (what create command should now do)
forkPoint, fpErr := g.GetMergeBase("feature", trunk)
if fpErr != nil {
t.Fatalf("GetMergeBase failed: %v", fpErr)
}
cfg.SetForkPoint("feature", forkPoint)
// Verify fork point was stored and equals trunk tip
storedFP, err := cfg.GetForkPoint("feature")
if err != nil {
t.Fatalf("GetForkPoint failed: %v", err)
}
if storedFP != trunkTip {
t.Errorf("fork point = %s, want %s (trunk tip at creation)", storedFP, trunkTip)
}
}