Skip to content

Commit

Permalink
chore: Proper error handling, move files and add few unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nitish Gupta <imnitish.ng@gmail.com>
  • Loading branch information
imnitishng committed May 25, 2022
1 parent 073f3ea commit 11da95a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
3 changes: 2 additions & 1 deletion pkg/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/buildpacks/pack/pkg/image"
"github.com/buildpacks/pack/pkg/logging"
projectTypes "github.com/buildpacks/pack/pkg/project/types"
v02 "github.com/buildpacks/pack/pkg/project/v02"
)

const (
Expand Down Expand Up @@ -326,7 +327,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
Metadata: map[string]interface{}{"url": sourceURL},
}
} else {
projectMetadata.Source = GitMetadata(opts.AppPath)
projectMetadata.Source = v02.GitMetadata(opts.AppPath)
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/client/metadata.go → pkg/project/v02/metadata.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package v02

import (
"fmt"
Expand All @@ -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
62 changes: 57 additions & 5 deletions pkg/client/metadata_test.go → pkg/project/v02/metadata_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package v02

import (
"fmt"
Expand All @@ -10,20 +10,22 @@ 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) {
color.Disable(true)
defer color.Disable(false)
spec.Run(t, "Metadata", testMetadata, spec.Parallel(), spec.Report(report.Terminal{}))
spec.Run(t, "Metadata", testMetadata, spec.Sequential(), spec.Report(report.Terminal{}))
}

func testMetadata(t *testing.T, when spec.G, it spec.S) {
Expand All @@ -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 All @@ -564,12 +603,25 @@ func createCommits(t *testing.T, repo *git.Repository, repoPath string, numberOf
for i := 0; i < numberOfCommits; i++ {
file, err := ioutil.TempFile(repoPath, h.RandString(10))
h.AssertNil(t, err)
defer file.Close()

_, err = worktree.Add(filepath.Base(file.Name()))
h.AssertNil(t, err)

commitMsg := fmt.Sprintf("%s %d", "test commit number", i)
commitOpts := git.CommitOptions{}
commitOpts := git.CommitOptions{
All: true,
Author: &object.Signature{
Name: "Test Author",
Email: "testauthor@test.com",
When: time.Now(),
},
Committer: &object.Signature{
Name: "Test Committer",
Email: "testcommitter@test.com",
When: time.Now(),
},
}
commitHash, err := worktree.Commit(commitMsg, &commitOpts)
h.AssertNil(t, err)
commitHashes = append(commitHashes, commitHash)
Expand Down

0 comments on commit 11da95a

Please sign in to comment.