Skip to content

Commit 13b2e21

Browse files
committed
Fixed unit test
It wasn't passing because the reference name had spaces in the string (creating invalid references). The updated git library has enforced stricter checks on the ref name.
1 parent bdf4d2d commit 13b2e21

File tree

1 file changed

+28
-47
lines changed

1 file changed

+28
-47
lines changed

internal/libraries/gitutils/gitutils_test.go

+28-47
Original file line numberDiff line numberDiff line change
@@ -45,51 +45,32 @@ func TestResolveTag(t *testing.T) {
4545
repository, err := git.PlainInit(repositoryPath.String(), false)
4646
require.Nil(t, err)
4747

48-
testTables := []struct {
49-
objectTypeName string
50-
objectHash plumbing.Hash
51-
annotated bool
52-
errorAssertion assert.ErrorAssertionFunc
48+
commitHash := makeCommit(t, repository, repositoryPath)
49+
treeHash := getTreeHash(t, repository)
50+
blobHash := getBlobHash(t, repository)
51+
testTable := []struct {
52+
name string
53+
hash plumbing.Hash
54+
annotated bool
55+
assertion assert.ErrorAssertionFunc
5356
}{
54-
{
55-
objectTypeName: "Commit",
56-
objectHash: makeCommit(t, repository, repositoryPath),
57-
errorAssertion: assert.NoError,
58-
},
59-
{
60-
objectTypeName: "Tree",
61-
objectHash: getTreeHash(t, repository),
62-
errorAssertion: assert.Error,
63-
},
64-
{
65-
objectTypeName: "Blob",
66-
objectHash: getBlobHash(t, repository),
67-
errorAssertion: assert.Error,
68-
},
57+
{name: "Commit", hash: commitHash, assertion: assert.NoError},
58+
{name: "Tree", hash: treeHash, assertion: assert.Error},
59+
{name: "Blob", hash: blobHash, assertion: assert.Error},
60+
{name: "AnnotatedCommit", hash: commitHash, assertion: assert.NoError, annotated: true},
61+
{name: "AnnotatedTree", hash: treeHash, assertion: assert.Error, annotated: true},
62+
{name: "AnnotatedBlob", hash: blobHash, assertion: assert.Error, annotated: true},
6963
}
7064

71-
for _, testTable := range testTables {
72-
for _, annotationConfig := range []struct {
73-
annotated bool
74-
descriptor string
75-
}{
76-
{
77-
annotated: true,
78-
descriptor: "Annotated",
79-
},
80-
{
81-
annotated: false,
82-
descriptor: "Lightweight",
83-
},
84-
} {
85-
testName := fmt.Sprintf("%s, %s", testTable.objectTypeName, annotationConfig.descriptor)
86-
tag := makeTag(t, repository, testName, testTable.objectHash, annotationConfig.annotated)
65+
for _, test := range testTable {
66+
t.Run(test.name, func(t *testing.T) {
67+
tag := makeTag(t, repository, test.name, test.hash, test.annotated)
8768
resolvedTag, err := resolveTag(tag, repository)
88-
testTable.errorAssertion(t, err, fmt.Sprintf("%s tag resolution error", testName))
69+
test.assertion(t, err, "tag resolution error")
8970
if err == nil {
90-
assert.Equal(t, testTable.objectHash, *resolvedTag, fmt.Sprintf("%s tag resolution", testName))
71+
assert.Equal(t, test.hash, *resolvedTag, "tag resolution")
9172
}
92-
}
73+
})
9374
}
9475
}
9576

@@ -202,12 +183,12 @@ func makeCommit(t *testing.T, repository *git.Repository, repositoryPath *paths.
202183
// commitFile commits a file in the given repository and returns its path and the commit's plumbing.Hash object.
203184
func commitFile(t *testing.T, repository *git.Repository, repositoryPath *paths.Path) (*paths.Path, plumbing.Hash) {
204185
filePath, err := paths.WriteToTempFile([]byte{}, repositoryPath, "gitutils-makeCommit-tempfile")
205-
require.Nil(t, err)
186+
require.NoError(t, err)
206187

207188
worktree, err := repository.Worktree()
208-
require.Nil(t, err)
189+
require.NoError(t, err)
209190
_, err = worktree.Add(".")
210-
require.Nil(t, err)
191+
require.NoError(t, err)
211192

212193
signature := &object.Signature{
213194
Name: "Jane Developer",
@@ -221,26 +202,26 @@ func commitFile(t *testing.T, repository *git.Repository, repositoryPath *paths.
221202
Author: signature,
222203
},
223204
)
224-
require.Nil(t, err)
205+
require.NoError(t, err)
225206

226207
return filePath, commit
227208
}
228209

229210
// getTreeHash returns the plumbing.Hash object for an arbitrary Git tree object.
230211
func getTreeHash(t *testing.T, repository *git.Repository) plumbing.Hash {
231212
trees, err := repository.TreeObjects()
232-
require.Nil(t, err)
213+
require.NoError(t, err)
233214
tree, err := trees.Next()
234-
require.Nil(t, err)
215+
require.NoError(t, err)
235216
return tree.ID()
236217
}
237218

238219
// getTreeHash returns the plumbing.Hash object for an arbitrary Git blob object.
239220
func getBlobHash(t *testing.T, repository *git.Repository) plumbing.Hash {
240221
blobs, err := repository.BlobObjects()
241-
require.Nil(t, err)
222+
require.NoError(t, err)
242223
blob, err := blobs.Next()
243-
require.Nil(t, err)
224+
require.NoError(t, err)
244225
return blob.ID()
245226
}
246227

0 commit comments

Comments
 (0)