1
1
package main_test
2
2
3
3
import (
4
+ "bytes"
4
5
"fmt"
5
6
"io"
6
7
"io/ioutil"
@@ -10,12 +11,12 @@ import (
10
11
"testing"
11
12
"time"
12
13
14
+ "github.com/stretchr/testify/assert"
15
+ "github.com/stretchr/testify/require"
16
+
13
17
"github.com/github/git-sizer/counts"
14
18
"github.com/github/git-sizer/git"
15
19
"github.com/github/git-sizer/sizes"
16
-
17
- "github.com/stretchr/testify/assert"
18
- "github.com/stretchr/testify/require"
19
20
)
20
21
21
22
// Smoke test that the program runs.
@@ -46,6 +47,45 @@ func updateRef(t *testing.T, repoPath string, refname string, oid git.OID) error
46
47
return cmd .Run ()
47
48
}
48
49
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
+
49
89
func addFile (t * testing.T , repoPath string , repo * git.Repository , relativePath , contents string ) {
50
90
dirPath := filepath .Dir (relativePath )
51
91
if dirPath != "." {
@@ -88,19 +128,18 @@ func newGitBomb(
88
128
repo , err = git .NewRepository (path )
89
129
require .NoError (t , err )
90
130
91
- oid , err := repo .CreateObject ( "blob" , func (w io.Writer ) error {
131
+ oid := createObject ( t , repo .Path (), "blob" , func (w io.Writer ) error {
92
132
_ , err := io .WriteString (w , body )
93
133
return err
94
134
})
95
- require .NoError (t , err )
96
135
97
136
digits := len (fmt .Sprintf ("%d" , breadth - 1 ))
98
137
99
138
mode := "100644"
100
139
prefix := "f"
101
140
102
141
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 {
104
143
for i := 0 ; i < breadth ; i ++ {
105
144
_ , err = fmt .Fprintf (
106
145
w , "%s %s%0*d\x00 %s" ,
@@ -112,13 +151,12 @@ func newGitBomb(
112
151
}
113
152
return nil
114
153
})
115
- require .NoError (t , err )
116
154
117
155
mode = "40000"
118
156
prefix = "d"
119
157
}
120
158
121
- oid , err = repo .CreateObject ( "commit" , func (w io.Writer ) error {
159
+ oid = createObject ( t , repo .Path (), "commit" , func (w io.Writer ) error {
122
160
_ , err := fmt .Fprintf (
123
161
w ,
124
162
"tree %s\n " +
@@ -130,7 +168,6 @@ func newGitBomb(
130
168
)
131
169
return err
132
170
})
133
- require .NoError (t , err )
134
171
135
172
err = updateRef (t , repo .Path (), "refs/heads/master" , oid )
136
173
require .NoError (t , err )
0 commit comments