forked from github/git-sizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_sizer_test.go
344 lines (285 loc) · 11.2 KB
/
git_sizer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main_test
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/github/git-sizer/counts"
"github.com/github/git-sizer/git"
"github.com/github/git-sizer/sizes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Smoke test that the program runs.
func TestExec(t *testing.T) {
cmd := exec.Command("bin/git-sizer")
output, err := cmd.CombinedOutput()
assert.NoErrorf(t, err, "command failed; output: %#v", string(output))
}
func gitCommand(t *testing.T, repo *git.Repository, args ...string) *exec.Cmd {
cmd := exec.Command("git", args...)
cmd.Env = append(os.Environ(), "GIT_DIR="+repo.Path())
return cmd
}
func addFile(t *testing.T, repoPath string, repo *git.Repository, relativePath, contents string) {
dirPath := filepath.Dir(relativePath)
if dirPath != "." {
require.NoError(t, os.MkdirAll(filepath.Join(repoPath, dirPath), 0777), "creating subdir")
}
filename := filepath.Join(repoPath, relativePath)
f, err := os.Create(filename)
require.NoErrorf(t, err, "creating file %q", filename)
_, err = f.WriteString(contents)
require.NoErrorf(t, err, "writing to file %q", filename)
require.NoErrorf(t, f.Close(), "closing file %q", filename)
cmd := gitCommand(t, repo, "add", relativePath)
cmd.Dir = repoPath
require.NoErrorf(t, cmd.Run(), "adding file %q", relativePath)
}
func addAuthorInfo(cmd *exec.Cmd, timestamp *time.Time) {
cmd.Env = append(cmd.Env,
"GIT_AUTHOR_NAME=Arthur",
fmt.Sprintf("GIT_AUTHOR_DATE=%d -0700", timestamp.Unix()),
"GIT_COMMITTER_NAME=Constance",
fmt.Sprintf("GIT_COMMITTER_DATE=%d -0700", timestamp.Unix()),
)
*timestamp = timestamp.Add(60 * time.Second)
}
func newGitBomb(
repoName string, depth, breadth int, body string,
) (repo *git.Repository, err error) {
path, err := ioutil.TempDir("", repoName)
if err != nil {
return nil, err
}
defer func() {
if err != nil {
os.RemoveAll(path)
}
}()
cmd := exec.Command("git", "init", "--bare", path)
err = cmd.Run()
if err != nil {
return nil, err
}
repo, err = git.NewRepository(path)
if err != nil {
return nil, err
}
oid, err := repo.CreateObject("blob", func(w io.Writer) error {
_, err := io.WriteString(w, body)
return err
})
digits := len(fmt.Sprintf("%d", breadth-1))
mode := "100644"
prefix := "f"
for ; depth > 0; depth-- {
oid, err = repo.CreateObject("tree", func(w io.Writer) error {
for i := 0; i < breadth; i++ {
_, err = fmt.Fprintf(
w, "%s %s%0*d\x00%s",
mode, prefix, digits, i, oid.Bytes(),
)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, err
}
mode = "40000"
prefix = "d"
}
oid, err = repo.CreateObject("commit", func(w io.Writer) error {
_, err := fmt.Fprintf(
w,
"tree %s\n"+
"author Example <[email protected]> 1112911993 -0700\n"+
"committer Example <[email protected]> 1112911993 -0700\n"+
"\n"+
"Mwahahaha!\n",
oid,
)
return err
})
if err != nil {
return nil, err
}
err = repo.UpdateRef("refs/heads/master", oid)
if err != nil {
return nil, err
}
return repo, nil
}
func pow(x uint64, n int) uint64 {
p := uint64(1)
for ; n > 0; n-- {
p *= x
}
return p
}
func TestBomb(t *testing.T) {
t.Parallel()
assert := assert.New(t)
repo, err := newGitBomb("bomb", 10, 10, "boom!\n")
if err != nil {
t.Errorf("failed to create bomb: %s", err)
}
defer os.RemoveAll(repo.Path())
h, err := sizes.ScanRepositoryUsingGraph(
repo, git.AllReferencesFilter, sizes.NameStyleFull, false,
)
if !assert.NoError(err) {
return
}
assert.Equal(counts.Count32(1), h.UniqueCommitCount, "unique commit count")
assert.Equal(counts.Count64(169), h.UniqueCommitSize, "unique commit size")
assert.Equal(counts.Count32(169), h.MaxCommitSize, "max commit size")
assert.Equal("refs/heads/master", h.MaxCommitSizeCommit.Path(), "max commit size commit")
assert.Equal(counts.Count32(1), h.MaxHistoryDepth, "max history depth")
assert.Equal(counts.Count32(0), h.MaxParentCount, "max parent count")
assert.Equal("refs/heads/master", h.MaxParentCountCommit.Path(), "max parent count commit")
assert.Equal(counts.Count32(10), h.UniqueTreeCount, "unique tree count")
assert.Equal(counts.Count64(2910), h.UniqueTreeSize, "unique tree size")
assert.Equal(counts.Count64(100), h.UniqueTreeEntries, "unique tree entries")
assert.Equal(counts.Count32(10), h.MaxTreeEntries, "max tree entries")
assert.Equal("refs/heads/master:d0/d0/d0/d0/d0/d0/d0/d0/d0", h.MaxTreeEntriesTree.Path(), "max tree entries tree")
assert.Equal(counts.Count32(1), h.UniqueBlobCount, "unique blob count")
assert.Equal(counts.Count64(6), h.UniqueBlobSize, "unique blob size")
assert.Equal(counts.Count32(6), h.MaxBlobSize, "max blob size")
assert.Equal("refs/heads/master:d0/d0/d0/d0/d0/d0/d0/d0/d0/f0", h.MaxBlobSizeBlob.Path(), "max blob size blob")
assert.Equal(counts.Count32(0), h.UniqueTagCount, "unique tag count")
assert.Equal(counts.Count32(0), h.MaxTagDepth, "max tag depth")
assert.Equal(counts.Count32(1), h.ReferenceCount, "reference count")
assert.Equal(counts.Count32(10), h.MaxPathDepth, "max path depth")
assert.Equal("refs/heads/master^{tree}", h.MaxPathDepthTree.Path(), "max path depth tree")
assert.Equal(counts.Count32(29), h.MaxPathLength, "max path length")
assert.Equal("refs/heads/master^{tree}", h.MaxPathLengthTree.Path(), "max path length tree")
assert.Equal(counts.Count32((pow(10, 10)-1)/(10-1)), h.MaxExpandedTreeCount, "max expanded tree count")
assert.Equal("refs/heads/master^{tree}", h.MaxExpandedTreeCountTree.Path(), "max expanded tree count tree")
assert.Equal(counts.Count32(0xffffffff), h.MaxExpandedBlobCount, "max expanded blob count")
assert.Equal("refs/heads/master^{tree}", h.MaxExpandedBlobCountTree.Path(), "max expanded blob count tree")
assert.Equal(counts.Count64(6*pow(10, 10)), h.MaxExpandedBlobSize, "max expanded blob size")
assert.Equal("refs/heads/master^{tree}", h.MaxExpandedBlobSizeTree.Path(), "max expanded blob size tree")
assert.Equal(counts.Count32(0), h.MaxExpandedLinkCount, "max expanded link count")
assert.Nil(h.MaxExpandedLinkCountTree, "max expanded link count tree")
assert.Equal(counts.Count32(0), h.MaxExpandedSubmoduleCount, "max expanded submodule count")
assert.Nil(h.MaxExpandedSubmoduleCountTree, "max expanded submodule count tree")
}
func TestTaggedTags(t *testing.T) {
t.Parallel()
path, err := ioutil.TempDir("", "tagged-tags")
require.NoError(t, err, "creating temporary directory")
defer func() {
os.RemoveAll(path)
}()
cmd := exec.Command("git", "init", path)
require.NoError(t, cmd.Run(), "initializing repo")
repo, err := git.NewRepository(path)
require.NoError(t, err, "initializing Repository object")
timestamp := time.Unix(1112911993, 0)
cmd = gitCommand(t, repo, "commit", "-m", "initial", "--allow-empty")
addAuthorInfo(cmd, ×tamp)
require.NoError(t, cmd.Run(), "creating commit")
// The lexicographical order of these tags is important, hence
// their strange names.
cmd = gitCommand(t, repo, "tag", "-m", "tag 1", "tag", "master")
addAuthorInfo(cmd, ×tamp)
require.NoError(t, cmd.Run(), "creating tag 1")
cmd = gitCommand(t, repo, "tag", "-m", "tag 2", "bag", "tag")
addAuthorInfo(cmd, ×tamp)
require.NoError(t, cmd.Run(), "creating tag 2")
cmd = gitCommand(t, repo, "tag", "-m", "tag 3", "wag", "bag")
addAuthorInfo(cmd, ×tamp)
require.NoError(t, cmd.Run(), "creating tag 3")
h, err := sizes.ScanRepositoryUsingGraph(
repo, git.AllReferencesFilter, sizes.NameStyleNone, false,
)
require.NoError(t, err, "scanning repository")
assert.Equal(t, counts.Count32(3), h.MaxTagDepth, "tag depth")
}
func TestFromSubdir(t *testing.T) {
t.Parallel()
path, err := ioutil.TempDir("", "subdir")
require.NoError(t, err, "creating temporary directory")
defer func() {
os.RemoveAll(path)
}()
cmd := exec.Command("git", "init", path)
require.NoError(t, cmd.Run(), "initializing repo")
repo, err := git.NewRepository(path)
require.NoError(t, err, "initializing Repository object")
timestamp := time.Unix(1112911993, 0)
addFile(t, path, repo, "subdir/file.txt", "Hello, world!\n")
cmd = gitCommand(t, repo, "commit", "-m", "initial")
addAuthorInfo(cmd, ×tamp)
require.NoError(t, cmd.Run(), "creating commit")
repo2, err := git.NewRepository(filepath.Join(path, "subdir"))
require.NoError(t, err, "creating Repository object in subdirectory")
h, err := sizes.ScanRepositoryUsingGraph(
repo2, git.AllReferencesFilter, sizes.NameStyleNone, false,
)
require.NoError(t, err, "scanning repository")
assert.Equal(t, counts.Count32(2), h.MaxPathDepth, "max path depth")
}
func TestSubmodule(t *testing.T) {
t.Parallel()
path, err := ioutil.TempDir("", "submodule")
require.NoError(t, err, "creating temporary directory")
defer func() {
os.RemoveAll(path)
}()
timestamp := time.Unix(1112911993, 0)
submPath := filepath.Join(path, "subm")
cmd := exec.Command("git", "init", submPath)
require.NoError(t, cmd.Run(), "initializing subm repo")
submRepo, err := git.NewRepository(submPath)
require.NoError(t, err, "initializing subm Repository object")
addFile(t, submPath, submRepo, "submfile1.txt", "Hello, submodule!\n")
addFile(t, submPath, submRepo, "submfile2.txt", "Hello again, submodule!\n")
addFile(t, submPath, submRepo, "submfile3.txt", "Hello again, submodule!\n")
cmd = gitCommand(t, submRepo, "commit", "-m", "subm initial")
addAuthorInfo(cmd, ×tamp)
require.NoError(t, cmd.Run(), "creating subm commit")
mainPath := filepath.Join(path, "main")
cmd = exec.Command("git", "init", mainPath)
require.NoError(t, cmd.Run(), "initializing main repo")
mainRepo, err := git.NewRepository(mainPath)
require.NoError(t, err, "initializing main Repository object")
addFile(t, mainPath, mainRepo, "mainfile.txt", "Hello, main!\n")
cmd = gitCommand(t, mainRepo, "commit", "-m", "main initial")
addAuthorInfo(cmd, ×tamp)
require.NoError(t, cmd.Run(), "creating main commit")
// Make subm a submodule of main:
cmd = gitCommand(t, mainRepo, "submodule", "add", submPath, "sub")
cmd.Dir = mainPath
require.NoError(t, cmd.Run(), "adding submodule")
cmd = gitCommand(t, mainRepo, "commit", "-m", "add submodule")
addAuthorInfo(cmd, ×tamp)
require.NoError(t, cmd.Run(), "committing submodule to main")
// Analyze the main repo:
h, err := sizes.ScanRepositoryUsingGraph(
mainRepo, git.AllReferencesFilter, sizes.NameStyleNone, false,
)
require.NoError(t, err, "scanning repository")
assert.Equal(t, counts.Count32(2), h.UniqueBlobCount, "unique blob count")
assert.Equal(t, counts.Count32(2), h.MaxExpandedBlobCount, "max expanded blob count")
assert.Equal(t, counts.Count32(1), h.MaxExpandedSubmoduleCount, "max expanded submodule count")
// Analyze the submodule:
submRepo2, err := git.NewRepository(filepath.Join(mainPath, "sub"))
require.NoError(t, err, "creating Repository object in submodule")
h, err = sizes.ScanRepositoryUsingGraph(
submRepo2, git.AllReferencesFilter, sizes.NameStyleNone, false,
)
require.NoError(t, err, "scanning repository")
assert.Equal(t, counts.Count32(2), h.UniqueBlobCount, "unique blob count")
assert.Equal(t, counts.Count32(3), h.MaxExpandedBlobCount, "max expanded blob count")
}