Skip to content

Commit

Permalink
chore: Proper error handling and add few unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imnitishng committed May 23, 2022
1 parent 7e015c8 commit 4d76ba3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pkg/client/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ type TagInfo struct {
func GitMetadata(appPath string) *platform.ProjectSource {
repo, err := git.PlainOpen(appPath)
if err != nil {
print("unable to open git repo")
return nil
}
headRef, err := repo.Head()
if err != nil {
print("unable to parse head")
return nil
}
commitTagMap := generateTagsMap(repo)

Expand Down Expand Up @@ -112,7 +112,7 @@ func parseGitDescribe(repo *git.Repository, headRef *plumbing.Reference, commitT
}
commits, err := repo.Log(logOpts)
if err != nil {
print("no commits found")
return ""
}

latestTag := headRef.Hash().String()
Expand Down
43 changes: 41 additions & 2 deletions pkg/client/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
"testing"
"time"

h "github.com/buildpacks/pack/testhelpers"
"github.com/buildpacks/lifecycle/platform"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

h "github.com/buildpacks/pack/testhelpers"
)

func TestMetadata(t *testing.T) {
Expand Down Expand Up @@ -49,6 +51,37 @@ func testMetadata(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, os.RemoveAll(repoPath))
})

when("#GitMetadata", func() {
it("returns proper metadata format", func() {
assert := h.NewAssertionManager(t)
remoteOpts := &config.RemoteConfig{
Name: "origin",
URLs: []string{"git@github.com:testorg/testproj.git", "git@github.com:testorg/testproj.git"},
}
repo.CreateRemote(remoteOpts)
createUnannotatedTag(t, repo, commits[len(commits)-1], "testTag")

output := GitMetadata(repoPath)
expectedOutput := &platform.ProjectSource{
Type: "git",
Version: map[string]interface{}{
"commit": commits[len(commits)-1].String(),
"describe": "testTag",
},
Metadata: map[string]interface{}{
"refs": []string{"master", "testTag"},
"url": "git@github.com:testorg/testproj.git",
},
}
assert.Equal(output, expectedOutput)
})

it("returns nil if error occurs while fetching metadata", func() {
output := GitMetadata("/git-path-not-found-ok")
h.AssertNil(t, output)
})
})

when("#generateTagsMap", func() {
when("repository has no tags", func() {
it("returns empty map", func() {
Expand Down Expand Up @@ -396,7 +429,6 @@ func testMetadata(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, output, expectedOutput)
})
})

})
})
})
Expand Down Expand Up @@ -554,6 +586,13 @@ func testMetadata(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, output, "git@fetch.com:testorg/testproj.git")
})
})

when("#getRefName", func() {
it("return proper ref for refs with `/`", func() {
output := getRefName("refs/tags/this/is/a/tag/with/slashes")
h.AssertEq(t, output, "this/is/a/tag/with/slashes")
})
})
}

func createCommits(t *testing.T, repo *git.Repository, repoPath string, numberOfCommits int) []plumbing.Hash {
Expand Down

0 comments on commit 4d76ba3

Please sign in to comment.