Skip to content

Commit b30ab78

Browse files
committed
testutils.TestRepo: new type representing a test git repository
Use the new type in the main tests.
1 parent 0cddf11 commit b30ab78

File tree

2 files changed

+156
-108
lines changed

2 files changed

+156
-108
lines changed

git_sizer_test.go

Lines changed: 54 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,10 @@ func TestExec(t *testing.T) {
2929
assert.NoErrorf(t, err, "command failed; output: %#v", string(output))
3030
}
3131

32-
func newGitBomb(
33-
t *testing.T, path string, depth, breadth int, body string,
34-
) {
32+
func newGitBomb(t *testing.T, repo *testutils.TestRepo, depth, breadth int, body string) {
3533
t.Helper()
3634

37-
cmd := exec.Command("git", "init", "--bare", path)
38-
err := cmd.Run()
39-
require.NoError(t, err)
40-
41-
oid := testutils.CreateObject(t, path, "blob", func(w io.Writer) error {
35+
oid := repo.CreateObject(t, "blob", func(w io.Writer) error {
4236
_, err := io.WriteString(w, body)
4337
return err
4438
})
@@ -49,9 +43,9 @@ func newGitBomb(
4943
prefix := "f"
5044

5145
for ; depth > 0; depth-- {
52-
oid = testutils.CreateObject(t, path, "tree", func(w io.Writer) error {
46+
oid = repo.CreateObject(t, "tree", func(w io.Writer) error {
5347
for i := 0; i < breadth; i++ {
54-
_, err = fmt.Fprintf(
48+
_, err := fmt.Fprintf(
5549
w, "%s %s%0*d\x00%s",
5650
mode, prefix, digits, i, oid.Bytes(),
5751
)
@@ -66,7 +60,7 @@ func newGitBomb(
6660
prefix = "d"
6761
}
6862

69-
oid = testutils.CreateObject(t, path, "commit", func(w io.Writer) error {
63+
oid = repo.CreateObject(t, "commit", func(w io.Writer) error {
7064
_, err := fmt.Fprintf(
7165
w,
7266
"tree %s\n"+
@@ -79,7 +73,7 @@ func newGitBomb(
7973
return err
8074
})
8175

82-
testutils.UpdateRef(t, path, "refs/heads/master", oid)
76+
repo.UpdateRef(t, "refs/heads/master", oid)
8377
}
8478

8579
// TestRefSelections tests various combinations of reference selection
@@ -131,16 +125,12 @@ func TestRefSelections(t *testing.T) {
131125
}
132126

133127
// Create a test repo with one orphan commit per refname:
134-
path, err := ioutil.TempDir("", "ref-selection")
135-
require.NoError(t, err)
128+
repo := testutils.NewTestRepo(t, true, "ref-selection")
136129

137-
defer os.RemoveAll(path)
138-
139-
err = exec.Command("git", "init", "--bare", path).Run()
140-
require.NoError(t, err)
130+
defer repo.Remove(t)
141131

142132
for _, p := range references {
143-
testutils.CreateReferencedOrphan(t, path, p.refname)
133+
repo.CreateReferencedOrphan(t, p.refname)
144134
}
145135

146136
executable, err := exec.LookPath("bin/git-sizer")
@@ -250,26 +240,18 @@ func TestRefSelections(t *testing.T) {
250240
t.Run(
251241
p.name,
252242
func(t *testing.T) {
253-
clonePath, err := ioutil.TempDir("", "ref-selection")
254-
require.NoError(t, err)
255-
256-
defer os.RemoveAll(clonePath)
243+
repo := repo.Clone(t, "ref-selection")
257244

258-
err = exec.Command(
259-
"git", "clone", "--bare", "--mirror", path, clonePath,
260-
).Run()
261-
require.NoError(t, err)
262-
263-
path := clonePath
245+
defer repo.Remove(t)
264246

265247
for _, e := range p.config {
266-
testutils.ConfigAdd(t, path, e.Key, e.Value)
248+
repo.ConfigAdd(t, e.Key, e.Value)
267249
}
268250

269251
args := []string{"--show-refs", "--no-progress", "--json", "--json-version=2"}
270252
args = append(args, p.args...)
271253
cmd := exec.Command(executable, args...)
272-
cmd.Dir = path
254+
cmd.Dir = repo.Path
273255
var stdout bytes.Buffer
274256
cmd.Stdout = &stdout
275257
var stderr bytes.Buffer
@@ -308,17 +290,13 @@ func pow(x uint64, n int) uint64 {
308290
func TestBomb(t *testing.T) {
309291
t.Parallel()
310292

311-
path, err := ioutil.TempDir("", "bomb")
312-
require.NoError(t, err)
313-
314-
defer func() {
315-
os.RemoveAll(path)
316-
}()
293+
repo := testutils.NewTestRepo(t, true, "bomb")
294+
defer repo.Remove(t)
317295

318-
newGitBomb(t, path, 10, 10, "boom!\n")
296+
newGitBomb(t, repo, 10, 10, "boom!\n")
319297

320298
h, err := sizes.ScanRepositoryUsingGraph(
321-
testutils.NewRepository(t, path),
299+
repo.Repository(t),
322300
git.AllReferencesFilter, sizes.NameStyleFull, false,
323301
)
324302
require.NoError(t, err)
@@ -366,38 +344,32 @@ func TestBomb(t *testing.T) {
366344

367345
func TestTaggedTags(t *testing.T) {
368346
t.Parallel()
369-
path, err := ioutil.TempDir("", "tagged-tags")
370-
require.NoError(t, err, "creating temporary directory")
371347

372-
defer func() {
373-
os.RemoveAll(path)
374-
}()
375-
376-
cmd := exec.Command("git", "init", path)
377-
require.NoError(t, cmd.Run(), "initializing repo")
348+
repo := testutils.NewTestRepo(t, false, "tagged-tags")
349+
defer repo.Remove(t)
378350

379351
timestamp := time.Unix(1112911993, 0)
380352

381-
cmd = testutils.GitCommand(t, path, "commit", "-m", "initial", "--allow-empty")
353+
cmd := repo.GitCommand(t, "commit", "-m", "initial", "--allow-empty")
382354
testutils.AddAuthorInfo(cmd, &timestamp)
383355
require.NoError(t, cmd.Run(), "creating commit")
384356

385357
// The lexicographical order of these tags is important, hence
386358
// their strange names.
387-
cmd = testutils.GitCommand(t, path, "tag", "-m", "tag 1", "tag", "master")
359+
cmd = repo.GitCommand(t, "tag", "-m", "tag 1", "tag", "master")
388360
testutils.AddAuthorInfo(cmd, &timestamp)
389361
require.NoError(t, cmd.Run(), "creating tag 1")
390362

391-
cmd = testutils.GitCommand(t, path, "tag", "-m", "tag 2", "bag", "tag")
363+
cmd = repo.GitCommand(t, "tag", "-m", "tag 2", "bag", "tag")
392364
testutils.AddAuthorInfo(cmd, &timestamp)
393365
require.NoError(t, cmd.Run(), "creating tag 2")
394366

395-
cmd = testutils.GitCommand(t, path, "tag", "-m", "tag 3", "wag", "bag")
367+
cmd = repo.GitCommand(t, "tag", "-m", "tag 3", "wag", "bag")
396368
testutils.AddAuthorInfo(cmd, &timestamp)
397369
require.NoError(t, cmd.Run(), "creating tag 3")
398370

399371
h, err := sizes.ScanRepositoryUsingGraph(
400-
testutils.NewRepository(t, path),
372+
repo.Repository(t),
401373
git.AllReferencesFilter, sizes.NameStyleNone, false,
402374
)
403375
require.NoError(t, err, "scanning repository")
@@ -406,26 +378,20 @@ func TestTaggedTags(t *testing.T) {
406378

407379
func TestFromSubdir(t *testing.T) {
408380
t.Parallel()
409-
path, err := ioutil.TempDir("", "subdir")
410-
require.NoError(t, err, "creating temporary directory")
411381

412-
defer func() {
413-
os.RemoveAll(path)
414-
}()
415-
416-
cmd := exec.Command("git", "init", path)
417-
require.NoError(t, cmd.Run(), "initializing repo")
382+
repo := testutils.NewTestRepo(t, false, "subdir")
383+
defer repo.Remove(t)
418384

419385
timestamp := time.Unix(1112911993, 0)
420386

421-
testutils.AddFile(t, path, "subdir/file.txt", "Hello, world!\n")
387+
repo.AddFile(t, "subdir/file.txt", "Hello, world!\n")
422388

423-
cmd = testutils.GitCommand(t, path, "commit", "-m", "initial")
389+
cmd := repo.GitCommand(t, "commit", "-m", "initial")
424390
testutils.AddAuthorInfo(cmd, &timestamp)
425391
require.NoError(t, cmd.Run(), "creating commit")
426392

427393
h, err := sizes.ScanRepositoryUsingGraph(
428-
testutils.NewRepository(t, filepath.Join(path, "subdir")),
394+
repo.Repository(t),
429395
git.AllReferencesFilter, sizes.NameStyleNone, false,
430396
)
431397
require.NoError(t, err, "scanning repository")
@@ -434,48 +400,51 @@ func TestFromSubdir(t *testing.T) {
434400

435401
func TestSubmodule(t *testing.T) {
436402
t.Parallel()
437-
path, err := ioutil.TempDir("", "submodule")
403+
404+
tmp, err := ioutil.TempDir("", "submodule")
438405
require.NoError(t, err, "creating temporary directory")
439406

440407
defer func() {
441-
os.RemoveAll(path)
408+
os.RemoveAll(tmp)
442409
}()
443410

444411
timestamp := time.Unix(1112911993, 0)
445412

446-
submPath := filepath.Join(path, "subm")
447-
cmd := exec.Command("git", "init", submPath)
448-
require.NoError(t, cmd.Run(), "initializing subm repo")
449-
testutils.AddFile(t, submPath, "submfile1.txt", "Hello, submodule!\n")
450-
testutils.AddFile(t, submPath, "submfile2.txt", "Hello again, submodule!\n")
451-
testutils.AddFile(t, submPath, "submfile3.txt", "Hello again, submodule!\n")
413+
submRepo := testutils.TestRepo{
414+
Path: filepath.Join(tmp, "subm"),
415+
}
416+
submRepo.Init(t, false)
417+
submRepo.AddFile(t, "submfile1.txt", "Hello, submodule!\n")
418+
submRepo.AddFile(t, "submfile2.txt", "Hello again, submodule!\n")
419+
submRepo.AddFile(t, "submfile3.txt", "Hello again, submodule!\n")
452420

453-
cmd = testutils.GitCommand(t, submPath, "commit", "-m", "subm initial")
421+
cmd := submRepo.GitCommand(t, "commit", "-m", "subm initial")
454422
testutils.AddAuthorInfo(cmd, &timestamp)
455423
require.NoError(t, cmd.Run(), "creating subm commit")
456424

457-
mainPath := filepath.Join(path, "main")
458-
cmd = exec.Command("git", "init", mainPath)
459-
require.NoError(t, cmd.Run(), "initializing main repo")
425+
mainRepo := testutils.TestRepo{
426+
Path: filepath.Join(tmp, "main"),
427+
}
428+
mainRepo.Init(t, false)
460429

461-
testutils.AddFile(t, mainPath, "mainfile.txt", "Hello, main!\n")
430+
mainRepo.AddFile(t, "mainfile.txt", "Hello, main!\n")
462431

463-
cmd = testutils.GitCommand(t, mainPath, "commit", "-m", "main initial")
432+
cmd = mainRepo.GitCommand(t, "commit", "-m", "main initial")
464433
testutils.AddAuthorInfo(cmd, &timestamp)
465434
require.NoError(t, cmd.Run(), "creating main commit")
466435

467436
// Make subm a submodule of main:
468-
cmd = testutils.GitCommand(t, mainPath, "submodule", "add", submPath, "sub")
469-
cmd.Dir = mainPath
437+
cmd = mainRepo.GitCommand(t, "submodule", "add", submRepo.Path, "sub")
438+
cmd.Dir = mainRepo.Path
470439
require.NoError(t, cmd.Run(), "adding submodule")
471440

472-
cmd = testutils.GitCommand(t, mainPath, "commit", "-m", "add submodule")
441+
cmd = mainRepo.GitCommand(t, "commit", "-m", "add submodule")
473442
testutils.AddAuthorInfo(cmd, &timestamp)
474443
require.NoError(t, cmd.Run(), "committing submodule to main")
475444

476445
// Analyze the main repo:
477446
h, err := sizes.ScanRepositoryUsingGraph(
478-
testutils.NewRepository(t, mainPath),
447+
mainRepo.Repository(t),
479448
git.AllReferencesFilter, sizes.NameStyleNone, false,
480449
)
481450
require.NoError(t, err, "scanning repository")
@@ -484,8 +453,11 @@ func TestSubmodule(t *testing.T) {
484453
assert.Equal(t, counts.Count32(1), h.MaxExpandedSubmoduleCount, "max expanded submodule count")
485454

486455
// Analyze the submodule:
456+
submRepo2 := testutils.TestRepo{
457+
Path: filepath.Join(mainRepo.Path, "sub"),
458+
}
487459
h, err = sizes.ScanRepositoryUsingGraph(
488-
testutils.NewRepository(t, filepath.Join(mainPath, "sub")),
460+
submRepo2.Repository(t),
489461
git.AllReferencesFilter, sizes.NameStyleNone, false,
490462
)
491463
require.NoError(t, err, "scanning repository")

0 commit comments

Comments
 (0)