@@ -18,6 +18,7 @@ import (
18
18
19
19
"github.com/github/git-sizer/counts"
20
20
"github.com/github/git-sizer/git"
21
+ "github.com/github/git-sizer/internal/testutils"
21
22
"github.com/github/git-sizer/sizes"
22
23
)
23
24
@@ -28,104 +29,6 @@ func TestExec(t *testing.T) {
28
29
assert .NoErrorf (t , err , "command failed; output: %#v" , string (output ))
29
30
}
30
31
31
- func newRepository (t * testing.T , repoPath string ) * git.Repository {
32
- t .Helper ()
33
-
34
- repo , err := git .NewRepository (repoPath )
35
- require .NoError (t , err )
36
- return repo
37
- }
38
-
39
- func gitCommand (t * testing.T , repoPath string , args ... string ) * exec.Cmd {
40
- t .Helper ()
41
-
42
- gitArgs := []string {"-C" , repoPath }
43
- gitArgs = append (gitArgs , args ... )
44
- return exec .Command ("git" , gitArgs ... )
45
- }
46
-
47
- func updateRef (t * testing.T , repoPath string , refname string , oid git.OID ) {
48
- t .Helper ()
49
-
50
- var cmd * exec.Cmd
51
-
52
- if oid == git .NullOID {
53
- cmd = gitCommand (t , repoPath , "update-ref" , "-d" , refname )
54
- } else {
55
- cmd = gitCommand (t , repoPath , "update-ref" , refname , oid .String ())
56
- }
57
- require .NoError (t , cmd .Run ())
58
- }
59
-
60
- // createObject creates a new Git object, of the specified type, in
61
- // the repository at `repoPath`. `writer` is a function that writes
62
- // the object in `git hash-object` input format. This is used for
63
- // testing only.
64
- func createObject (
65
- t * testing.T , repoPath string , otype git.ObjectType , writer func (io.Writer ) error ,
66
- ) git.OID {
67
- t .Helper ()
68
-
69
- cmd := gitCommand (t , repoPath , "hash-object" , "-w" , "-t" , string (otype ), "--stdin" )
70
- in , err := cmd .StdinPipe ()
71
- require .NoError (t , err )
72
-
73
- out , err := cmd .StdoutPipe ()
74
- cmd .Stderr = os .Stderr
75
-
76
- err = cmd .Start ()
77
- require .NoError (t , err )
78
-
79
- err = writer (in )
80
- err2 := in .Close ()
81
- if err != nil {
82
- cmd .Wait ()
83
- require .NoError (t , err )
84
- }
85
- if err2 != nil {
86
- cmd .Wait ()
87
- require .NoError (t , err2 )
88
- }
89
-
90
- output , err := ioutil .ReadAll (out )
91
- err2 = cmd .Wait ()
92
- require .NoError (t , err )
93
- require .NoError (t , err2 )
94
-
95
- oid , err := git .NewOID (string (bytes .TrimSpace (output )))
96
- require .NoError (t , err )
97
- return oid
98
- }
99
-
100
- func addFile (t * testing.T , repoPath string , relativePath , contents string ) {
101
- dirPath := filepath .Dir (relativePath )
102
- if dirPath != "." {
103
- require .NoError (t , os .MkdirAll (filepath .Join (repoPath , dirPath ), 0777 ), "creating subdir" )
104
- }
105
-
106
- filename := filepath .Join (repoPath , relativePath )
107
- f , err := os .Create (filename )
108
- require .NoErrorf (t , err , "creating file %q" , filename )
109
- _ , err = f .WriteString (contents )
110
- require .NoErrorf (t , err , "writing to file %q" , filename )
111
- require .NoErrorf (t , f .Close (), "closing file %q" , filename )
112
-
113
- cmd := gitCommand (t , repoPath , "add" , relativePath )
114
- require .NoErrorf (t , cmd .Run (), "adding file %q" , relativePath )
115
- }
116
-
117
- func addAuthorInfo (cmd * exec.Cmd , timestamp * time.Time ) {
118
- cmd .Env = append (cmd .Env ,
119
- "GIT_AUTHOR_NAME=Arthur" ,
120
-
121
- fmt .Sprintf ("GIT_AUTHOR_DATE=%d -0700" , timestamp .Unix ()),
122
- "GIT_COMMITTER_NAME=Constance" ,
123
-
124
- fmt .Sprintf ("GIT_COMMITTER_DATE=%d -0700" , timestamp .Unix ()),
125
- )
126
- * timestamp = timestamp .Add (60 * time .Second )
127
- }
128
-
129
32
func newGitBomb (
130
33
t * testing.T , path string , depth , breadth int , body string ,
131
34
) {
@@ -135,7 +38,7 @@ func newGitBomb(
135
38
err := cmd .Run ()
136
39
require .NoError (t , err )
137
40
138
- oid := createObject (t , path , "blob" , func (w io.Writer ) error {
41
+ oid := testutils . CreateObject (t , path , "blob" , func (w io.Writer ) error {
139
42
_ , err := io .WriteString (w , body )
140
43
return err
141
44
})
@@ -146,7 +49,7 @@ func newGitBomb(
146
49
prefix := "f"
147
50
148
51
for ; depth > 0 ; depth -- {
149
- oid = createObject (t , path , "tree" , func (w io.Writer ) error {
52
+ oid = testutils . CreateObject (t , path , "tree" , func (w io.Writer ) error {
150
53
for i := 0 ; i < breadth ; i ++ {
151
54
_ , err = fmt .Fprintf (
152
55
w , "%s %s%0*d\x00 %s" ,
@@ -163,7 +66,7 @@ func newGitBomb(
163
66
prefix = "d"
164
67
}
165
68
166
- oid = createObject (t , path , "commit" , func (w io.Writer ) error {
69
+ oid = testutils . CreateObject (t , path , "commit" , func (w io.Writer ) error {
167
70
_ , err := fmt .Fprintf (
168
71
w ,
169
72
"tree %s\n " +
@@ -176,7 +79,7 @@ func newGitBomb(
176
79
return err
177
80
})
178
81
179
- updateRef (t , path , "refs/heads/master" , oid )
82
+ testutils . UpdateRef (t , path , "refs/heads/master" , oid )
180
83
}
181
84
182
85
// TestRefSelections tests various combinations of reference selection
@@ -237,17 +140,17 @@ func TestRefSelections(t *testing.T) {
237
140
require .NoError (t , err )
238
141
239
142
for _ , p := range references {
240
- oid := createObject (t , path , "blob" , func (w io.Writer ) error {
143
+ oid := testutils . CreateObject (t , path , "blob" , func (w io.Writer ) error {
241
144
_ , err := fmt .Fprintf (w , "%s\n " , p .refname )
242
145
return err
243
146
})
244
147
245
- oid = createObject (t , path , "tree" , func (w io.Writer ) error {
148
+ oid = testutils . CreateObject (t , path , "tree" , func (w io.Writer ) error {
246
149
_ , err = fmt .Fprintf (w , "100644 a.txt\x00 %s" , oid .Bytes ())
247
150
return err
248
151
})
249
152
250
- oid = createObject (t , path , "commit" , func (w io.Writer ) error {
153
+ oid = testutils . CreateObject (t , path , "commit" , func (w io.Writer ) error {
251
154
_ , err := fmt .Fprintf (
252
155
w ,
253
156
"tree %s\n " +
@@ -260,7 +163,7 @@ func TestRefSelections(t *testing.T) {
260
163
return err
261
164
})
262
165
263
- updateRef (t , path , p .refname , oid )
166
+ testutils . UpdateRef (t , path , p .refname , oid )
264
167
}
265
168
266
169
executable , err := exec .LookPath ("bin/git-sizer" )
@@ -372,15 +275,15 @@ func TestRefSelections(t *testing.T) {
372
275
func (t * testing.T ) {
373
276
if len (p .config ) != 0 {
374
277
for _ , c := range p .config {
375
- cmd := gitCommand (
278
+ cmd := testutils . GitCommand (
376
279
t , path ,
377
280
"config" , "--add" , fmt .Sprintf ("refgroup.mygroup.%s" , c [0 ]), c [1 ],
378
281
)
379
282
err := cmd .Run ()
380
283
require .NoError (t , err )
381
284
}
382
285
defer func () {
383
- cmd := gitCommand (
286
+ cmd := testutils . GitCommand (
384
287
t , path , "config" , "--remove-section" , "refgroup.mygroup" ,
385
288
)
386
289
err := cmd .Run ()
@@ -439,7 +342,8 @@ func TestBomb(t *testing.T) {
439
342
newGitBomb (t , path , 10 , 10 , "boom!\n " )
440
343
441
344
h , err := sizes .ScanRepositoryUsingGraph (
442
- newRepository (t , path ), git .AllReferencesFilter , sizes .NameStyleFull , false ,
345
+ testutils .NewRepository (t , path ),
346
+ git .AllReferencesFilter , sizes .NameStyleFull , false ,
443
347
)
444
348
require .NoError (t , err )
445
349
@@ -498,26 +402,27 @@ func TestTaggedTags(t *testing.T) {
498
402
499
403
timestamp := time .Unix (1112911993 , 0 )
500
404
501
- cmd = gitCommand (t , path , "commit" , "-m" , "initial" , "--allow-empty" )
502
- addAuthorInfo (cmd , & timestamp )
405
+ cmd = testutils . GitCommand (t , path , "commit" , "-m" , "initial" , "--allow-empty" )
406
+ testutils . AddAuthorInfo (cmd , & timestamp )
503
407
require .NoError (t , cmd .Run (), "creating commit" )
504
408
505
409
// The lexicographical order of these tags is important, hence
506
410
// their strange names.
507
- cmd = gitCommand (t , path , "tag" , "-m" , "tag 1" , "tag" , "master" )
508
- addAuthorInfo (cmd , & timestamp )
411
+ cmd = testutils . GitCommand (t , path , "tag" , "-m" , "tag 1" , "tag" , "master" )
412
+ testutils . AddAuthorInfo (cmd , & timestamp )
509
413
require .NoError (t , cmd .Run (), "creating tag 1" )
510
414
511
- cmd = gitCommand (t , path , "tag" , "-m" , "tag 2" , "bag" , "tag" )
512
- addAuthorInfo (cmd , & timestamp )
415
+ cmd = testutils . GitCommand (t , path , "tag" , "-m" , "tag 2" , "bag" , "tag" )
416
+ testutils . AddAuthorInfo (cmd , & timestamp )
513
417
require .NoError (t , cmd .Run (), "creating tag 2" )
514
418
515
- cmd = gitCommand (t , path , "tag" , "-m" , "tag 3" , "wag" , "bag" )
516
- addAuthorInfo (cmd , & timestamp )
419
+ cmd = testutils . GitCommand (t , path , "tag" , "-m" , "tag 3" , "wag" , "bag" )
420
+ testutils . AddAuthorInfo (cmd , & timestamp )
517
421
require .NoError (t , cmd .Run (), "creating tag 3" )
518
422
519
423
h , err := sizes .ScanRepositoryUsingGraph (
520
- newRepository (t , path ), git .AllReferencesFilter , sizes .NameStyleNone , false ,
424
+ testutils .NewRepository (t , path ),
425
+ git .AllReferencesFilter , sizes .NameStyleNone , false ,
521
426
)
522
427
require .NoError (t , err , "scanning repository" )
523
428
assert .Equal (t , counts .Count32 (3 ), h .MaxTagDepth , "tag depth" )
@@ -537,14 +442,14 @@ func TestFromSubdir(t *testing.T) {
537
442
538
443
timestamp := time .Unix (1112911993 , 0 )
539
444
540
- addFile (t , path , "subdir/file.txt" , "Hello, world!\n " )
445
+ testutils . AddFile (t , path , "subdir/file.txt" , "Hello, world!\n " )
541
446
542
- cmd = gitCommand (t , path , "commit" , "-m" , "initial" )
543
- addAuthorInfo (cmd , & timestamp )
447
+ cmd = testutils . GitCommand (t , path , "commit" , "-m" , "initial" )
448
+ testutils . AddAuthorInfo (cmd , & timestamp )
544
449
require .NoError (t , cmd .Run (), "creating commit" )
545
450
546
451
h , err := sizes .ScanRepositoryUsingGraph (
547
- newRepository (t , filepath .Join (path , "subdir" )),
452
+ testutils . NewRepository (t , filepath .Join (path , "subdir" )),
548
453
git .AllReferencesFilter , sizes .NameStyleNone , false ,
549
454
)
550
455
require .NoError (t , err , "scanning repository" )
@@ -565,36 +470,37 @@ func TestSubmodule(t *testing.T) {
565
470
submPath := filepath .Join (path , "subm" )
566
471
cmd := exec .Command ("git" , "init" , submPath )
567
472
require .NoError (t , cmd .Run (), "initializing subm repo" )
568
- addFile (t , submPath , "submfile1.txt" , "Hello, submodule!\n " )
569
- addFile (t , submPath , "submfile2.txt" , "Hello again, submodule!\n " )
570
- addFile (t , submPath , "submfile3.txt" , "Hello again, submodule!\n " )
473
+ testutils . AddFile (t , submPath , "submfile1.txt" , "Hello, submodule!\n " )
474
+ testutils . AddFile (t , submPath , "submfile2.txt" , "Hello again, submodule!\n " )
475
+ testutils . AddFile (t , submPath , "submfile3.txt" , "Hello again, submodule!\n " )
571
476
572
- cmd = gitCommand (t , submPath , "commit" , "-m" , "subm initial" )
573
- addAuthorInfo (cmd , & timestamp )
477
+ cmd = testutils . GitCommand (t , submPath , "commit" , "-m" , "subm initial" )
478
+ testutils . AddAuthorInfo (cmd , & timestamp )
574
479
require .NoError (t , cmd .Run (), "creating subm commit" )
575
480
576
481
mainPath := filepath .Join (path , "main" )
577
482
cmd = exec .Command ("git" , "init" , mainPath )
578
483
require .NoError (t , cmd .Run (), "initializing main repo" )
579
484
580
- addFile (t , mainPath , "mainfile.txt" , "Hello, main!\n " )
485
+ testutils . AddFile (t , mainPath , "mainfile.txt" , "Hello, main!\n " )
581
486
582
- cmd = gitCommand (t , mainPath , "commit" , "-m" , "main initial" )
583
- addAuthorInfo (cmd , & timestamp )
487
+ cmd = testutils . GitCommand (t , mainPath , "commit" , "-m" , "main initial" )
488
+ testutils . AddAuthorInfo (cmd , & timestamp )
584
489
require .NoError (t , cmd .Run (), "creating main commit" )
585
490
586
491
// Make subm a submodule of main:
587
- cmd = gitCommand (t , mainPath , "submodule" , "add" , submPath , "sub" )
492
+ cmd = testutils . GitCommand (t , mainPath , "submodule" , "add" , submPath , "sub" )
588
493
cmd .Dir = mainPath
589
494
require .NoError (t , cmd .Run (), "adding submodule" )
590
495
591
- cmd = gitCommand (t , mainPath , "commit" , "-m" , "add submodule" )
592
- addAuthorInfo (cmd , & timestamp )
496
+ cmd = testutils . GitCommand (t , mainPath , "commit" , "-m" , "add submodule" )
497
+ testutils . AddAuthorInfo (cmd , & timestamp )
593
498
require .NoError (t , cmd .Run (), "committing submodule to main" )
594
499
595
500
// Analyze the main repo:
596
501
h , err := sizes .ScanRepositoryUsingGraph (
597
- newRepository (t , mainPath ), git .AllReferencesFilter , sizes .NameStyleNone , false ,
502
+ testutils .NewRepository (t , mainPath ),
503
+ git .AllReferencesFilter , sizes .NameStyleNone , false ,
598
504
)
599
505
require .NoError (t , err , "scanning repository" )
600
506
assert .Equal (t , counts .Count32 (2 ), h .UniqueBlobCount , "unique blob count" )
@@ -603,7 +509,7 @@ func TestSubmodule(t *testing.T) {
603
509
604
510
// Analyze the submodule:
605
511
h , err = sizes .ScanRepositoryUsingGraph (
606
- newRepository (t , filepath .Join (mainPath , "sub" )),
512
+ testutils . NewRepository (t , filepath .Join (mainPath , "sub" )),
607
513
git .AllReferencesFilter , sizes .NameStyleNone , false ,
608
514
)
609
515
require .NoError (t , err , "scanning repository" )
0 commit comments