11package main_test
22
33import (
4+ "bytes"
45 "fmt"
56 "io"
67 "io/ioutil"
@@ -10,12 +11,12 @@ import (
1011 "testing"
1112 "time"
1213
14+ "github.com/stretchr/testify/assert"
15+ "github.com/stretchr/testify/require"
16+
1317 "github.com/github/git-sizer/counts"
1418 "github.com/github/git-sizer/git"
1519 "github.com/github/git-sizer/sizes"
16-
17- "github.com/stretchr/testify/assert"
18- "github.com/stretchr/testify/require"
1920)
2021
2122// Smoke test that the program runs.
@@ -46,6 +47,45 @@ func updateRef(t *testing.T, repoPath string, refname string, oid git.OID) error
4647 return cmd .Run ()
4748}
4849
50+ // CreateObject creates a new Git object, of the specified type, in
51+ // `Repository`. `writer` is a function that writes the object in `git
52+ // hash-object` input format. This is used for testing only.
53+ func createObject (
54+ t * testing.T , repoPath string , otype git.ObjectType , writer func (io.Writer ) error ,
55+ ) git.OID {
56+ t .Helper ()
57+
58+ cmd := gitCommand (t , repoPath , "hash-object" , "-w" , "-t" , string (otype ), "--stdin" )
59+ in , err := cmd .StdinPipe ()
60+ require .NoError (t , err )
61+
62+ out , err := cmd .StdoutPipe ()
63+ cmd .Stderr = os .Stderr
64+
65+ err = cmd .Start ()
66+ require .NoError (t , err )
67+
68+ err = writer (in )
69+ err2 := in .Close ()
70+ if err != nil {
71+ cmd .Wait ()
72+ require .NoError (t , err )
73+ }
74+ if err2 != nil {
75+ cmd .Wait ()
76+ require .NoError (t , err2 )
77+ }
78+
79+ output , err := ioutil .ReadAll (out )
80+ err2 = cmd .Wait ()
81+ require .NoError (t , err )
82+ require .NoError (t , err2 )
83+
84+ oid , err := git .NewOID (string (bytes .TrimSpace (output )))
85+ require .NoError (t , err )
86+ return oid
87+ }
88+
4989func addFile (t * testing.T , repoPath string , repo * git.Repository , relativePath , contents string ) {
5090 dirPath := filepath .Dir (relativePath )
5191 if dirPath != "." {
@@ -88,19 +128,18 @@ func newGitBomb(
88128 repo , err = git .NewRepository (path )
89129 require .NoError (t , err )
90130
91- oid , err := repo .CreateObject ( "blob" , func (w io.Writer ) error {
131+ oid := createObject ( t , repo .Path (), "blob" , func (w io.Writer ) error {
92132 _ , err := io .WriteString (w , body )
93133 return err
94134 })
95- require .NoError (t , err )
96135
97136 digits := len (fmt .Sprintf ("%d" , breadth - 1 ))
98137
99138 mode := "100644"
100139 prefix := "f"
101140
102141 for ; depth > 0 ; depth -- {
103- oid , err = repo .CreateObject ( "tree" , func (w io.Writer ) error {
142+ oid = createObject ( t , repo .Path (), "tree" , func (w io.Writer ) error {
104143 for i := 0 ; i < breadth ; i ++ {
105144 _ , err = fmt .Fprintf (
106145 w , "%s %s%0*d\x00 %s" ,
@@ -112,13 +151,12 @@ func newGitBomb(
112151 }
113152 return nil
114153 })
115- require .NoError (t , err )
116154
117155 mode = "40000"
118156 prefix = "d"
119157 }
120158
121- oid , err = repo .CreateObject ( "commit" , func (w io.Writer ) error {
159+ oid = createObject ( t , repo .Path (), "commit" , func (w io.Writer ) error {
122160 _ , err := fmt .Fprintf (
123161 w ,
124162 "tree %s\n " +
@@ -130,7 +168,6 @@ func newGitBomb(
130168 )
131169 return err
132170 })
133- require .NoError (t , err )
134171
135172 err = updateRef (t , repo .Path (), "refs/heads/master" , oid )
136173 require .NoError (t , err )
0 commit comments