@@ -29,16 +29,10 @@ func TestExec(t *testing.T) {
29
29
assert .NoErrorf (t , err , "command failed; output: %#v" , string (output ))
30
30
}
31
31
32
- func newGitBomb (
33
- t * testing.T , path string , depth , breadth int , body string ,
34
- ) {
32
+ func newGitBomb (t * testing.T , repo * testutils.TestRepo , depth , breadth int , body string ) {
35
33
t .Helper ()
36
34
37
- cmd := exec .Command ("git" , "init" , "--bare" , path )
38
- err := cmd .Run ()
39
- require .NoError (t , err )
40
-
41
- oid := testutils .CreateObject (t , path , "blob" , func (w io.Writer ) error {
35
+ oid := repo .CreateObject (t , "blob" , func (w io.Writer ) error {
42
36
_ , err := io .WriteString (w , body )
43
37
return err
44
38
})
@@ -49,9 +43,9 @@ func newGitBomb(
49
43
prefix := "f"
50
44
51
45
for ; depth > 0 ; depth -- {
52
- oid = testutils .CreateObject (t , path , "tree" , func (w io.Writer ) error {
46
+ oid = repo .CreateObject (t , "tree" , func (w io.Writer ) error {
53
47
for i := 0 ; i < breadth ; i ++ {
54
- _ , err = fmt .Fprintf (
48
+ _ , err : = fmt .Fprintf (
55
49
w , "%s %s%0*d\x00 %s" ,
56
50
mode , prefix , digits , i , oid .Bytes (),
57
51
)
@@ -66,7 +60,7 @@ func newGitBomb(
66
60
prefix = "d"
67
61
}
68
62
69
- oid = testutils .CreateObject (t , path , "commit" , func (w io.Writer ) error {
63
+ oid = repo .CreateObject (t , "commit" , func (w io.Writer ) error {
70
64
_ , err := fmt .Fprintf (
71
65
w ,
72
66
"tree %s\n " +
@@ -79,7 +73,7 @@ func newGitBomb(
79
73
return err
80
74
})
81
75
82
- testutils .UpdateRef (t , path , "refs/heads/master" , oid )
76
+ repo .UpdateRef (t , "refs/heads/master" , oid )
83
77
}
84
78
85
79
// TestRefSelections tests various combinations of reference selection
@@ -131,16 +125,12 @@ func TestRefSelections(t *testing.T) {
131
125
}
132
126
133
127
// Create a test repo with one orphan commit per refname:
134
- path , err := ioutil .TempDir ("" , "ref-selection" )
135
- require .NoError (t , err )
128
+ repo := testutils .NewTestRepo (t , true , "ref-selection" )
136
129
137
- defer os .RemoveAll (path )
138
-
139
- err = exec .Command ("git" , "init" , "--bare" , path ).Run ()
140
- require .NoError (t , err )
130
+ defer repo .Remove (t )
141
131
142
132
for _ , p := range references {
143
- testutils .CreateReferencedOrphan (t , path , p .refname )
133
+ repo .CreateReferencedOrphan (t , p .refname )
144
134
}
145
135
146
136
executable , err := exec .LookPath ("bin/git-sizer" )
@@ -250,26 +240,18 @@ func TestRefSelections(t *testing.T) {
250
240
t .Run (
251
241
p .name ,
252
242
func (t * testing.T ) {
253
- clonePath , err := ioutil .TempDir ("" , "ref-selection" )
254
- require .NoError (t , err )
255
-
256
- defer os .RemoveAll (clonePath )
243
+ repo := repo .Clone (t , "ref-selection" )
257
244
258
- err = exec .Command (
259
- "git" , "clone" , "--bare" , "--mirror" , path , clonePath ,
260
- ).Run ()
261
- require .NoError (t , err )
262
-
263
- path := clonePath
245
+ defer repo .Remove (t )
264
246
265
247
for _ , e := range p .config {
266
- testutils .ConfigAdd (t , path , e .Key , e .Value )
248
+ repo .ConfigAdd (t , e .Key , e .Value )
267
249
}
268
250
269
251
args := []string {"--show-refs" , "--no-progress" , "--json" , "--json-version=2" }
270
252
args = append (args , p .args ... )
271
253
cmd := exec .Command (executable , args ... )
272
- cmd .Dir = path
254
+ cmd .Dir = repo . Path
273
255
var stdout bytes.Buffer
274
256
cmd .Stdout = & stdout
275
257
var stderr bytes.Buffer
@@ -308,17 +290,13 @@ func pow(x uint64, n int) uint64 {
308
290
func TestBomb (t * testing.T ) {
309
291
t .Parallel ()
310
292
311
- path , err := ioutil .TempDir ("" , "bomb" )
312
- require .NoError (t , err )
313
-
314
- defer func () {
315
- os .RemoveAll (path )
316
- }()
293
+ repo := testutils .NewTestRepo (t , true , "bomb" )
294
+ defer repo .Remove (t )
317
295
318
- newGitBomb (t , path , 10 , 10 , "boom!\n " )
296
+ newGitBomb (t , repo , 10 , 10 , "boom!\n " )
319
297
320
298
h , err := sizes .ScanRepositoryUsingGraph (
321
- testutils . NewRepository ( t , path ),
299
+ repo . Repository ( t ),
322
300
git .AllReferencesFilter , sizes .NameStyleFull , false ,
323
301
)
324
302
require .NoError (t , err )
@@ -366,38 +344,32 @@ func TestBomb(t *testing.T) {
366
344
367
345
func TestTaggedTags (t * testing.T ) {
368
346
t .Parallel ()
369
- path , err := ioutil .TempDir ("" , "tagged-tags" )
370
- require .NoError (t , err , "creating temporary directory" )
371
347
372
- defer func () {
373
- os .RemoveAll (path )
374
- }()
375
-
376
- cmd := exec .Command ("git" , "init" , path )
377
- require .NoError (t , cmd .Run (), "initializing repo" )
348
+ repo := testutils .NewTestRepo (t , false , "tagged-tags" )
349
+ defer repo .Remove (t )
378
350
379
351
timestamp := time .Unix (1112911993 , 0 )
380
352
381
- cmd = testutils .GitCommand (t , path , "commit" , "-m" , "initial" , "--allow-empty" )
353
+ cmd := repo .GitCommand (t , "commit" , "-m" , "initial" , "--allow-empty" )
382
354
testutils .AddAuthorInfo (cmd , & timestamp )
383
355
require .NoError (t , cmd .Run (), "creating commit" )
384
356
385
357
// The lexicographical order of these tags is important, hence
386
358
// their strange names.
387
- cmd = testutils .GitCommand (t , path , "tag" , "-m" , "tag 1" , "tag" , "master" )
359
+ cmd = repo .GitCommand (t , "tag" , "-m" , "tag 1" , "tag" , "master" )
388
360
testutils .AddAuthorInfo (cmd , & timestamp )
389
361
require .NoError (t , cmd .Run (), "creating tag 1" )
390
362
391
- cmd = testutils .GitCommand (t , path , "tag" , "-m" , "tag 2" , "bag" , "tag" )
363
+ cmd = repo .GitCommand (t , "tag" , "-m" , "tag 2" , "bag" , "tag" )
392
364
testutils .AddAuthorInfo (cmd , & timestamp )
393
365
require .NoError (t , cmd .Run (), "creating tag 2" )
394
366
395
- cmd = testutils .GitCommand (t , path , "tag" , "-m" , "tag 3" , "wag" , "bag" )
367
+ cmd = repo .GitCommand (t , "tag" , "-m" , "tag 3" , "wag" , "bag" )
396
368
testutils .AddAuthorInfo (cmd , & timestamp )
397
369
require .NoError (t , cmd .Run (), "creating tag 3" )
398
370
399
371
h , err := sizes .ScanRepositoryUsingGraph (
400
- testutils . NewRepository ( t , path ),
372
+ repo . Repository ( t ),
401
373
git .AllReferencesFilter , sizes .NameStyleNone , false ,
402
374
)
403
375
require .NoError (t , err , "scanning repository" )
@@ -406,26 +378,20 @@ func TestTaggedTags(t *testing.T) {
406
378
407
379
func TestFromSubdir (t * testing.T ) {
408
380
t .Parallel ()
409
- path , err := ioutil .TempDir ("" , "subdir" )
410
- require .NoError (t , err , "creating temporary directory" )
411
381
412
- defer func () {
413
- os .RemoveAll (path )
414
- }()
415
-
416
- cmd := exec .Command ("git" , "init" , path )
417
- require .NoError (t , cmd .Run (), "initializing repo" )
382
+ repo := testutils .NewTestRepo (t , false , "subdir" )
383
+ defer repo .Remove (t )
418
384
419
385
timestamp := time .Unix (1112911993 , 0 )
420
386
421
- testutils .AddFile (t , path , "subdir/file.txt" , "Hello, world!\n " )
387
+ repo .AddFile (t , "subdir/file.txt" , "Hello, world!\n " )
422
388
423
- cmd = testutils .GitCommand (t , path , "commit" , "-m" , "initial" )
389
+ cmd := repo .GitCommand (t , "commit" , "-m" , "initial" )
424
390
testutils .AddAuthorInfo (cmd , & timestamp )
425
391
require .NoError (t , cmd .Run (), "creating commit" )
426
392
427
393
h , err := sizes .ScanRepositoryUsingGraph (
428
- testutils . NewRepository ( t , filepath . Join ( path , "subdir" ) ),
394
+ repo . Repository ( t ),
429
395
git .AllReferencesFilter , sizes .NameStyleNone , false ,
430
396
)
431
397
require .NoError (t , err , "scanning repository" )
@@ -434,48 +400,51 @@ func TestFromSubdir(t *testing.T) {
434
400
435
401
func TestSubmodule (t * testing.T ) {
436
402
t .Parallel ()
437
- path , err := ioutil .TempDir ("" , "submodule" )
403
+
404
+ tmp , err := ioutil .TempDir ("" , "submodule" )
438
405
require .NoError (t , err , "creating temporary directory" )
439
406
440
407
defer func () {
441
- os .RemoveAll (path )
408
+ os .RemoveAll (tmp )
442
409
}()
443
410
444
411
timestamp := time .Unix (1112911993 , 0 )
445
412
446
- submPath := filepath .Join (path , "subm" )
447
- cmd := exec .Command ("git" , "init" , submPath )
448
- require .NoError (t , cmd .Run (), "initializing subm repo" )
449
- testutils .AddFile (t , submPath , "submfile1.txt" , "Hello, submodule!\n " )
450
- testutils .AddFile (t , submPath , "submfile2.txt" , "Hello again, submodule!\n " )
451
- testutils .AddFile (t , submPath , "submfile3.txt" , "Hello again, submodule!\n " )
413
+ submRepo := testutils.TestRepo {
414
+ Path : filepath .Join (tmp , "subm" ),
415
+ }
416
+ submRepo .Init (t , false )
417
+ submRepo .AddFile (t , "submfile1.txt" , "Hello, submodule!\n " )
418
+ submRepo .AddFile (t , "submfile2.txt" , "Hello again, submodule!\n " )
419
+ submRepo .AddFile (t , "submfile3.txt" , "Hello again, submodule!\n " )
452
420
453
- cmd = testutils .GitCommand (t , submPath , "commit" , "-m" , "subm initial" )
421
+ cmd := submRepo .GitCommand (t , "commit" , "-m" , "subm initial" )
454
422
testutils .AddAuthorInfo (cmd , & timestamp )
455
423
require .NoError (t , cmd .Run (), "creating subm commit" )
456
424
457
- mainPath := filepath .Join (path , "main" )
458
- cmd = exec .Command ("git" , "init" , mainPath )
459
- require .NoError (t , cmd .Run (), "initializing main repo" )
425
+ mainRepo := testutils.TestRepo {
426
+ Path : filepath .Join (tmp , "main" ),
427
+ }
428
+ mainRepo .Init (t , false )
460
429
461
- testutils .AddFile (t , mainPath , "mainfile.txt" , "Hello, main!\n " )
430
+ mainRepo .AddFile (t , "mainfile.txt" , "Hello, main!\n " )
462
431
463
- cmd = testutils .GitCommand (t , mainPath , "commit" , "-m" , "main initial" )
432
+ cmd = mainRepo .GitCommand (t , "commit" , "-m" , "main initial" )
464
433
testutils .AddAuthorInfo (cmd , & timestamp )
465
434
require .NoError (t , cmd .Run (), "creating main commit" )
466
435
467
436
// Make subm a submodule of main:
468
- cmd = testutils .GitCommand (t , mainPath , "submodule" , "add" , submPath , "sub" )
469
- cmd .Dir = mainPath
437
+ cmd = mainRepo .GitCommand (t , "submodule" , "add" , submRepo . Path , "sub" )
438
+ cmd .Dir = mainRepo . Path
470
439
require .NoError (t , cmd .Run (), "adding submodule" )
471
440
472
- cmd = testutils .GitCommand (t , mainPath , "commit" , "-m" , "add submodule" )
441
+ cmd = mainRepo .GitCommand (t , "commit" , "-m" , "add submodule" )
473
442
testutils .AddAuthorInfo (cmd , & timestamp )
474
443
require .NoError (t , cmd .Run (), "committing submodule to main" )
475
444
476
445
// Analyze the main repo:
477
446
h , err := sizes .ScanRepositoryUsingGraph (
478
- testutils . NewRepository ( t , mainPath ),
447
+ mainRepo . Repository ( t ),
479
448
git .AllReferencesFilter , sizes .NameStyleNone , false ,
480
449
)
481
450
require .NoError (t , err , "scanning repository" )
@@ -484,8 +453,11 @@ func TestSubmodule(t *testing.T) {
484
453
assert .Equal (t , counts .Count32 (1 ), h .MaxExpandedSubmoduleCount , "max expanded submodule count" )
485
454
486
455
// Analyze the submodule:
456
+ submRepo2 := testutils.TestRepo {
457
+ Path : filepath .Join (mainRepo .Path , "sub" ),
458
+ }
487
459
h , err = sizes .ScanRepositoryUsingGraph (
488
- testutils . NewRepository ( t , filepath . Join ( mainPath , "sub" ) ),
460
+ submRepo2 . Repository ( t ),
489
461
git .AllReferencesFilter , sizes .NameStyleNone , false ,
490
462
)
491
463
require .NoError (t , err , "scanning repository" )
0 commit comments