Skip to content

Commit 72f7a67

Browse files
committed
createObject(): make into a test helper function
It's only needed for testing.
1 parent b4db7cd commit 72f7a67

File tree

2 files changed

+46
-55
lines changed

2 files changed

+46
-55
lines changed

git/git.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"fmt"
99
"io"
10-
"io/ioutil"
1110
"os"
1211
"os/exec"
1312
"path/filepath"
@@ -477,51 +476,6 @@ func (repo *Repository) NewObjectIter(args ...string) (
477476
}, in1, nil
478477
}
479478

480-
// CreateObject creates a new Git object, of the specified type, in
481-
// `Repository`. `writer` is a function that writes the object in `git
482-
// hash-object` input format. This is used for testing only.
483-
func (repo *Repository) CreateObject(t ObjectType, writer func(io.Writer) error) (OID, error) {
484-
cmd := repo.gitCommand("hash-object", "-w", "-t", string(t), "--stdin")
485-
in, err := cmd.StdinPipe()
486-
if err != nil {
487-
return OID{}, err
488-
}
489-
490-
out, err := cmd.StdoutPipe()
491-
if err != nil {
492-
return OID{}, err
493-
}
494-
495-
cmd.Stderr = os.Stderr
496-
497-
err = cmd.Start()
498-
if err != nil {
499-
return OID{}, err
500-
}
501-
502-
err = writer(in)
503-
err2 := in.Close()
504-
if err != nil {
505-
cmd.Wait()
506-
return OID{}, err
507-
}
508-
if err2 != nil {
509-
cmd.Wait()
510-
return OID{}, err2
511-
}
512-
513-
output, err := ioutil.ReadAll(out)
514-
err2 = cmd.Wait()
515-
if err != nil {
516-
return OID{}, err
517-
}
518-
if err2 != nil {
519-
return OID{}, err2
520-
}
521-
522-
return NewOID(string(bytes.TrimSpace(output)))
523-
}
524-
525479
// Next returns the next object, or EOF when done.
526480
func (l *ObjectIter) Next() (OID, ObjectType, counts.Count32, error) {
527481
line, err := l.f.ReadString('\n')

git_sizer_test.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main_test
22

33
import (
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+
4989
func 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

Comments
 (0)