@@ -18,6 +18,7 @@ import (
1818
1919 "github.com/github/git-sizer/counts"
2020 "github.com/github/git-sizer/git"
21+ "github.com/github/git-sizer/internal/testutils"
2122 "github.com/github/git-sizer/sizes"
2223)
2324
@@ -28,104 +29,6 @@ func TestExec(t *testing.T) {
2829 assert .NoErrorf (t , err , "command failed; output: %#v" , string (output ))
2930}
3031
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- "GIT_AUTHOR_EMAIL=arthur@example.com" ,
121- fmt .Sprintf ("GIT_AUTHOR_DATE=%d -0700" , timestamp .Unix ()),
122- "GIT_COMMITTER_NAME=Constance" ,
123- "GIT_COMMITTER_EMAIL=constance@example.com" ,
124- fmt .Sprintf ("GIT_COMMITTER_DATE=%d -0700" , timestamp .Unix ()),
125- )
126- * timestamp = timestamp .Add (60 * time .Second )
127- }
128-
12932func newGitBomb (
13033 t * testing.T , path string , depth , breadth int , body string ,
13134) {
@@ -135,7 +38,7 @@ func newGitBomb(
13538 err := cmd .Run ()
13639 require .NoError (t , err )
13740
138- oid := createObject (t , path , "blob" , func (w io.Writer ) error {
41+ oid := testutils . CreateObject (t , path , "blob" , func (w io.Writer ) error {
13942 _ , err := io .WriteString (w , body )
14043 return err
14144 })
@@ -146,7 +49,7 @@ func newGitBomb(
14649 prefix := "f"
14750
14851 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 {
15053 for i := 0 ; i < breadth ; i ++ {
15154 _ , err = fmt .Fprintf (
15255 w , "%s %s%0*d\x00 %s" ,
@@ -163,7 +66,7 @@ func newGitBomb(
16366 prefix = "d"
16467 }
16568
166- oid = createObject (t , path , "commit" , func (w io.Writer ) error {
69+ oid = testutils . CreateObject (t , path , "commit" , func (w io.Writer ) error {
16770 _ , err := fmt .Fprintf (
16871 w ,
16972 "tree %s\n " +
@@ -176,7 +79,7 @@ func newGitBomb(
17679 return err
17780 })
17881
179- updateRef (t , path , "refs/heads/master" , oid )
82+ testutils . UpdateRef (t , path , "refs/heads/master" , oid )
18083}
18184
18285// TestRefSelections tests various combinations of reference selection
@@ -237,17 +140,17 @@ func TestRefSelections(t *testing.T) {
237140 require .NoError (t , err )
238141
239142 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 {
241144 _ , err := fmt .Fprintf (w , "%s\n " , p .refname )
242145 return err
243146 })
244147
245- oid = createObject (t , path , "tree" , func (w io.Writer ) error {
148+ oid = testutils . CreateObject (t , path , "tree" , func (w io.Writer ) error {
246149 _ , err = fmt .Fprintf (w , "100644 a.txt\x00 %s" , oid .Bytes ())
247150 return err
248151 })
249152
250- oid = createObject (t , path , "commit" , func (w io.Writer ) error {
153+ oid = testutils . CreateObject (t , path , "commit" , func (w io.Writer ) error {
251154 _ , err := fmt .Fprintf (
252155 w ,
253156 "tree %s\n " +
@@ -260,7 +163,7 @@ func TestRefSelections(t *testing.T) {
260163 return err
261164 })
262165
263- updateRef (t , path , p .refname , oid )
166+ testutils . UpdateRef (t , path , p .refname , oid )
264167 }
265168
266169 executable , err := exec .LookPath ("bin/git-sizer" )
@@ -372,15 +275,15 @@ func TestRefSelections(t *testing.T) {
372275 func (t * testing.T ) {
373276 if len (p .config ) != 0 {
374277 for _ , c := range p .config {
375- cmd := gitCommand (
278+ cmd := testutils . GitCommand (
376279 t , path ,
377280 "config" , "--add" , fmt .Sprintf ("refgroup.mygroup.%s" , c [0 ]), c [1 ],
378281 )
379282 err := cmd .Run ()
380283 require .NoError (t , err )
381284 }
382285 defer func () {
383- cmd := gitCommand (
286+ cmd := testutils . GitCommand (
384287 t , path , "config" , "--remove-section" , "refgroup.mygroup" ,
385288 )
386289 err := cmd .Run ()
@@ -439,7 +342,8 @@ func TestBomb(t *testing.T) {
439342 newGitBomb (t , path , 10 , 10 , "boom!\n " )
440343
441344 h , err := sizes .ScanRepositoryUsingGraph (
442- newRepository (t , path ), git .AllReferencesFilter , sizes .NameStyleFull , false ,
345+ testutils .NewRepository (t , path ),
346+ git .AllReferencesFilter , sizes .NameStyleFull , false ,
443347 )
444348 require .NoError (t , err )
445349
@@ -498,26 +402,27 @@ func TestTaggedTags(t *testing.T) {
498402
499403 timestamp := time .Unix (1112911993 , 0 )
500404
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 )
503407 require .NoError (t , cmd .Run (), "creating commit" )
504408
505409 // The lexicographical order of these tags is important, hence
506410 // 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 )
509413 require .NoError (t , cmd .Run (), "creating tag 1" )
510414
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 )
513417 require .NoError (t , cmd .Run (), "creating tag 2" )
514418
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 )
517421 require .NoError (t , cmd .Run (), "creating tag 3" )
518422
519423 h , err := sizes .ScanRepositoryUsingGraph (
520- newRepository (t , path ), git .AllReferencesFilter , sizes .NameStyleNone , false ,
424+ testutils .NewRepository (t , path ),
425+ git .AllReferencesFilter , sizes .NameStyleNone , false ,
521426 )
522427 require .NoError (t , err , "scanning repository" )
523428 assert .Equal (t , counts .Count32 (3 ), h .MaxTagDepth , "tag depth" )
@@ -537,14 +442,14 @@ func TestFromSubdir(t *testing.T) {
537442
538443 timestamp := time .Unix (1112911993 , 0 )
539444
540- addFile (t , path , "subdir/file.txt" , "Hello, world!\n " )
445+ testutils . AddFile (t , path , "subdir/file.txt" , "Hello, world!\n " )
541446
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 )
544449 require .NoError (t , cmd .Run (), "creating commit" )
545450
546451 h , err := sizes .ScanRepositoryUsingGraph (
547- newRepository (t , filepath .Join (path , "subdir" )),
452+ testutils . NewRepository (t , filepath .Join (path , "subdir" )),
548453 git .AllReferencesFilter , sizes .NameStyleNone , false ,
549454 )
550455 require .NoError (t , err , "scanning repository" )
@@ -565,36 +470,37 @@ func TestSubmodule(t *testing.T) {
565470 submPath := filepath .Join (path , "subm" )
566471 cmd := exec .Command ("git" , "init" , submPath )
567472 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 " )
571476
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 )
574479 require .NoError (t , cmd .Run (), "creating subm commit" )
575480
576481 mainPath := filepath .Join (path , "main" )
577482 cmd = exec .Command ("git" , "init" , mainPath )
578483 require .NoError (t , cmd .Run (), "initializing main repo" )
579484
580- addFile (t , mainPath , "mainfile.txt" , "Hello, main!\n " )
485+ testutils . AddFile (t , mainPath , "mainfile.txt" , "Hello, main!\n " )
581486
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 )
584489 require .NoError (t , cmd .Run (), "creating main commit" )
585490
586491 // 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" )
588493 cmd .Dir = mainPath
589494 require .NoError (t , cmd .Run (), "adding submodule" )
590495
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 )
593498 require .NoError (t , cmd .Run (), "committing submodule to main" )
594499
595500 // Analyze the main repo:
596501 h , err := sizes .ScanRepositoryUsingGraph (
597- newRepository (t , mainPath ), git .AllReferencesFilter , sizes .NameStyleNone , false ,
502+ testutils .NewRepository (t , mainPath ),
503+ git .AllReferencesFilter , sizes .NameStyleNone , false ,
598504 )
599505 require .NoError (t , err , "scanning repository" )
600506 assert .Equal (t , counts .Count32 (2 ), h .UniqueBlobCount , "unique blob count" )
@@ -603,7 +509,7 @@ func TestSubmodule(t *testing.T) {
603509
604510 // Analyze the submodule:
605511 h , err = sizes .ScanRepositoryUsingGraph (
606- newRepository (t , filepath .Join (mainPath , "sub" )),
512+ testutils . NewRepository (t , filepath .Join (mainPath , "sub" )),
607513 git .AllReferencesFilter , sizes .NameStyleNone , false ,
608514 )
609515 require .NoError (t , err , "scanning repository" )
0 commit comments