-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscenarios_test.go
More file actions
453 lines (358 loc) · 10.8 KB
/
scenarios_test.go
File metadata and controls
453 lines (358 loc) · 10.8 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package testutil
import (
"os"
"path/filepath"
"testing"
)
func TestNewScenario(t *testing.T) {
scenario := NewScenario(t)
if scenario == nil {
t.Fatal("Expected scenario to be created")
}
if scenario.baseDir == "" {
t.Fatal("Expected base directory to be set")
}
if scenario.repos == nil {
t.Fatal("Expected repos map to be initialized")
}
}
func TestCreateConflictScenario(t *testing.T) {
scenario, local, remote := CreateConflictScenario(t)
if scenario == nil {
t.Fatal("Expected scenario to be created")
}
if local == nil {
t.Fatal("Expected local repo to be created")
}
if remote == nil {
t.Fatal("Expected remote repo to be created")
}
// Verify local repo exists
if _, err := os.Stat(local.path); os.IsNotExist(err) {
t.Fatalf("Local repo does not exist: %s", local.path)
}
// Verify remote repo exists
if _, err := os.Stat(remote.path); os.IsNotExist(err) {
t.Fatalf("Remote repo does not exist: %s", remote.path)
}
// Verify local repo has origin remote
remotes := GetRemotes(t, local.path)
if _, ok := remotes["origin"]; !ok {
t.Fatal("Expected origin remote to be configured")
}
// Verify file.txt exists in local repo
filePath := filepath.Join(local.path, "file.txt")
if _, err := os.Stat(filePath); os.IsNotExist(err) {
t.Fatalf("file.txt does not exist in local repo: %s", filePath)
}
// Verify local and remote have diverged
// This is tested by checking commit history
localSHA := GetCurrentSHA(t, local.path)
if localSHA == "" {
t.Fatal("Expected local repo to have commits")
}
}
func TestCreateWorktreeScenario(t *testing.T) {
scenario, main, worktree1, worktree2 := CreateWorktreeScenario(t)
if scenario == nil {
t.Fatal("Expected scenario to be created")
}
// Verify main repo exists
if _, err := os.Stat(main.path); os.IsNotExist(err) {
t.Fatalf("Main repo does not exist: %s", main.path)
}
// Verify worktrees exist
if _, err := os.Stat(worktree1.path); os.IsNotExist(err) {
t.Fatalf("Worktree 1 does not exist: %s", worktree1.path)
}
if _, err := os.Stat(worktree2.path); os.IsNotExist(err) {
t.Fatalf("Worktree 2 does not exist: %s", worktree2.path)
}
// Verify branches exist
branches := GetBranches(t, main.path)
hasFeat := false
hasBug := false
for _, b := range branches {
if b == "feature" {
hasFeat = true
}
if b == "bugfix" {
hasBug = true
}
}
if !hasFeat {
t.Fatal("Expected feature branch to exist")
}
if !hasBug {
t.Fatal("Expected bugfix branch to exist")
}
}
func TestCreateMultiRemoteScenario(t *testing.T) {
scenario, local, origin, backup, upstream := CreateMultiRemoteScenario(t)
if scenario == nil {
t.Fatal("Expected scenario to be created")
}
// Verify all repos exist
repos := []*ScenarioRepo{local, origin, backup, upstream}
names := []string{"local", "origin", "backup", "upstream"}
for i, repo := range repos {
if repo == nil {
t.Fatalf("Expected %s repo to be created", names[i])
}
if _, err := os.Stat(repo.path); os.IsNotExist(err) {
t.Fatalf("%s repo does not exist: %s", names[i], repo.path)
}
}
// Verify local has all remotes configured
remotes := GetRemotes(t, local.path)
expectedRemotes := []string{"origin", "backup", "upstream"}
for _, remoteName := range expectedRemotes {
if _, ok := remotes[remoteName]; !ok {
t.Fatalf("Expected %s remote to be configured", remoteName)
}
}
}
func TestCreateDirtyRepoScenario(t *testing.T) {
tests := []struct {
name string
staged bool
unstaged bool
wantFile string
}{
{"staged only", true, false, "staged.txt"},
{"unstaged only", false, true, "unstaged.txt"},
{"both", true, true, "staged.txt"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
scenario, repo := CreateDirtyRepoScenario(t, tt.staged, tt.unstaged)
if scenario == nil {
t.Fatal("Expected scenario to be created")
}
if repo == nil {
t.Fatal("Expected repo to be created")
}
// Verify repo is dirty
isDirty := IsDirty(t, repo.path)
if !isDirty {
t.Fatal("Expected repo to be dirty")
}
// Verify expected file exists
filePath := filepath.Join(repo.path, tt.wantFile)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
t.Fatalf("Expected file %s to exist", tt.wantFile)
}
})
}
}
func TestCreateCleanRepoScenario(t *testing.T) {
scenario, repo := CreateCleanRepoScenario(t)
if scenario == nil {
t.Fatal("Expected scenario to be created")
}
if repo == nil {
t.Fatal("Expected repo to be created")
}
// Verify repo is clean
isDirty := IsDirty(t, repo.path)
if isDirty {
t.Fatal("Expected repo to be clean")
}
// Verify repo has remote
remotes := GetRemotes(t, repo.path)
if _, ok := remotes["origin"]; !ok {
t.Fatal("Expected origin remote to be configured")
}
}
func TestCreateMultiBranchScenario(t *testing.T) {
branchNames := []string{"feature-1", "feature-2", "bugfix"}
scenario, repo := CreateMultiBranchScenario(t, branchNames)
if scenario == nil {
t.Fatal("Expected scenario to be created")
}
if repo == nil {
t.Fatal("Expected repo to be created")
}
// Verify all branches exist
branches := GetBranches(t, repo.path)
branchMap := make(map[string]bool)
for _, b := range branches {
branchMap[b] = true
}
for _, expectedBranch := range branchNames {
if !branchMap[expectedBranch] {
t.Fatalf("Expected branch %s to exist", expectedBranch)
}
}
}
func TestCreateLargeRepoScenario(t *testing.T) {
scenario, repo := CreateLargeRepoScenario(t, 5, 3)
if scenario == nil {
t.Fatal("Expected scenario to be created")
}
if repo == nil {
t.Fatal("Expected repo to be created")
}
// Verify files were created
// 5 files per commit * 3 commits = 15 files
// (We're just checking repo exists, full validation would check each file)
// Verify repo has commits
sha := GetCurrentSHA(t, repo.path)
if sha == "" {
t.Fatal("Expected repo to have commits")
}
}
func TestCreateDetachedHeadScenario(t *testing.T) {
scenario, repo, sha := CreateDetachedHeadScenario(t)
if scenario == nil {
t.Fatal("Expected scenario to be created")
}
if repo == nil {
t.Fatal("Expected repo to be created")
}
if sha == "" {
t.Fatal("Expected SHA to be returned")
}
// Verify HEAD is detached
currentSHA := GetCurrentSHA(t, repo.path)
if currentSHA != sha {
t.Fatalf("Expected HEAD to be at %s, got %s", sha, currentSHA)
}
}
func TestScenarioRepoChaining(t *testing.T) {
scenario := NewScenario(t)
// Test method chaining
repo := scenario.CreateRepo("test").
AddFile("file1.txt", "content 1").
Commit("Commit 1").
AddFile("file2.txt", "content 2").
Commit("Commit 2").
WithBranch("feature").
AddFile("feature.txt", "feature content").
Commit("Feature commit")
if repo == nil {
t.Fatal("Expected repo to be created")
}
// Verify files exist
files := []string{"file1.txt", "file2.txt", "feature.txt"}
for _, file := range files {
filePath := filepath.Join(repo.path, file)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
t.Fatalf("Expected file %s to exist", file)
}
}
// Verify feature branch exists
branches := GetBranches(t, repo.path)
hasFeature := false
for _, b := range branches {
if b == "feature" {
hasFeature = true
break
}
}
if !hasFeature {
t.Fatal("Expected feature branch to exist")
}
}
func TestScenarioWithRemoteAndPush(t *testing.T) {
scenario := NewScenario(t)
remote := scenario.CreateBareRepo("remote")
local := scenario.CreateRepo("local").
WithRemote("origin", remote).
AddFile("main.go", "package main\n").
Commit("Initial commit")
// Get default branch and push
defaultBranch := local.GetDefaultBranch()
local.Push("origin", defaultBranch)
// Verify push succeeded by checking remote has the branch
// (This would require git ls-remote or similar, skipping for now)
// Verify local has origin remote
remotes := GetRemotes(t, local.path)
if _, ok := remotes["origin"]; !ok {
t.Fatal("Expected origin remote to be configured")
}
}
func TestSnapshotAndRestore(t *testing.T) {
// Create a scenario with some complexity
_, repo := CreateMultiBranchScenario(t, []string{"feature", "bugfix"})
// Create snapshot
snapshot := SnapshotRepo(t, repo.path)
if snapshot == nil {
t.Fatal("Expected snapshot to be created")
}
if snapshot.Size() == 0 {
t.Fatal("Expected snapshot to have non-zero size")
}
// Restore snapshot
restoredPath := RestoreSnapshot(t, snapshot)
if restoredPath == "" {
t.Fatal("Expected restored path to be returned")
}
// Verify restored repo exists
if _, err := os.Stat(restoredPath); os.IsNotExist(err) {
t.Fatalf("Restored repo does not exist: %s", restoredPath)
}
// Verify restored repo has same branches
restoredBranches := GetBranches(t, restoredPath)
originalBranches := GetBranches(t, repo.path)
if len(restoredBranches) != len(originalBranches) {
t.Fatalf("Expected %d branches, got %d", len(originalBranches), len(restoredBranches))
}
// Verify same commit SHA
originalSHA := GetCurrentSHA(t, repo.path)
restoredSHA := GetCurrentSHA(t, restoredPath)
if originalSHA != restoredSHA {
t.Fatalf("Expected restored SHA %s to match original %s", restoredSHA, originalSHA)
}
}
func TestSnapshotPerformance(t *testing.T) {
if testing.Short() {
t.Skip("Skipping performance test in short mode")
}
// Create a moderately complex repo
_, repo := CreateLargeRepoScenario(t, 10, 5)
// Create snapshot
snapshot := SnapshotRepo(t, repo.path)
// Measure restoration time
// First restoration
restoredPath1 := RestoreSnapshot(t, snapshot)
if _, err := os.Stat(restoredPath1); os.IsNotExist(err) {
t.Fatalf("First restoration failed")
}
// Second restoration (should be fast)
restoredPath2 := RestoreSnapshot(t, snapshot)
if _, err := os.Stat(restoredPath2); os.IsNotExist(err) {
t.Fatalf("Second restoration failed")
}
// Both should succeed
t.Logf("Snapshot size: %d bytes", snapshot.Size())
t.Logf("First restoration: %s", restoredPath1)
t.Logf("Second restoration: %s", restoredPath2)
}
func TestSnapshotSaveAndLoad(t *testing.T) {
// Create a simple repo
_, repo := CreateCleanRepoScenario(t)
// Create snapshot
snapshot := SnapshotRepo(t, repo.path)
// Save to disk
tmpFile := filepath.Join(t.TempDir(), "snapshot.tar.gz")
SaveSnapshotToDisk(t, snapshot, tmpFile)
// Verify file exists
if _, err := os.Stat(tmpFile); os.IsNotExist(err) {
t.Fatalf("Snapshot file does not exist: %s", tmpFile)
}
// Load from disk
loadedSnapshot := LoadSnapshotFromDisk(t, tmpFile)
if loadedSnapshot == nil {
t.Fatal("Expected loaded snapshot to not be nil")
}
if loadedSnapshot.Size() != snapshot.Size() {
t.Fatalf("Expected loaded snapshot size %d to match original %d",
loadedSnapshot.Size(), snapshot.Size())
}
// Restore loaded snapshot
restoredPath := RestoreSnapshot(t, loadedSnapshot)
if _, err := os.Stat(restoredPath); os.IsNotExist(err) {
t.Fatalf("Restored repo from loaded snapshot does not exist: %s", restoredPath)
}
}