Skip to content

Commit 032e9e7

Browse files
committed
chore: Proper error handling, move files and add few unit tests
Signed-off-by: Nitish Gupta <imnitish.ng@gmail.com>
1 parent 073f3ea commit 032e9e7

3 files changed

Lines changed: 63 additions & 26 deletions

File tree

pkg/client/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/buildpacks/pack/pkg/image"
3737
"github.com/buildpacks/pack/pkg/logging"
3838
projectTypes "github.com/buildpacks/pack/pkg/project/types"
39+
v02 "github.com/buildpacks/pack/pkg/project/v02"
3940
)
4041

4142
const (
@@ -326,7 +327,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
326327
Metadata: map[string]interface{}{"url": sourceURL},
327328
}
328329
} else {
329-
projectMetadata.Source = GitMetadata(opts.AppPath)
330+
projectMetadata.Source = v02.GitMetadata(opts.AppPath)
330331
}
331332
}
332333

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package v02
22

33
import (
44
"fmt"
@@ -22,11 +22,11 @@ type TagInfo struct {
2222
func GitMetadata(appPath string) *platform.ProjectSource {
2323
repo, err := git.PlainOpen(appPath)
2424
if err != nil {
25-
print("unable to open git repo")
25+
return nil
2626
}
2727
headRef, err := repo.Head()
2828
if err != nil {
29-
print("unable to parse head")
29+
return nil
3030
}
3131
commitTagMap := generateTagsMap(repo)
3232

@@ -112,7 +112,7 @@ func parseGitDescribe(repo *git.Repository, headRef *plumbing.Reference, commitT
112112
}
113113
commits, err := repo.Log(logOpts)
114114
if err != nil {
115-
print("no commits found")
115+
return ""
116116
}
117117

118118
latestTag := headRef.Hash().String()
Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package v02
22

33
import (
44
"fmt"
@@ -10,20 +10,22 @@ import (
1010
"testing"
1111
"time"
1212

13-
h "github.com/buildpacks/pack/testhelpers"
13+
"github.com/buildpacks/lifecycle/platform"
1414
"github.com/go-git/go-git/v5"
1515
"github.com/go-git/go-git/v5/config"
1616
"github.com/go-git/go-git/v5/plumbing"
1717
"github.com/go-git/go-git/v5/plumbing/object"
1818
"github.com/heroku/color"
1919
"github.com/sclevine/spec"
2020
"github.com/sclevine/spec/report"
21+
22+
h "github.com/buildpacks/pack/testhelpers"
2123
)
2224

2325
func TestMetadata(t *testing.T) {
2426
color.Disable(true)
2527
defer color.Disable(false)
26-
spec.Run(t, "Metadata", testMetadata, spec.Parallel(), spec.Report(report.Terminal{}))
28+
spec.Run(t, "Metadata", testMetadata, spec.Sequential(), spec.Report(report.Terminal{}))
2729
}
2830

2931
func testMetadata(t *testing.T, when spec.G, it spec.S) {
@@ -49,6 +51,37 @@ func testMetadata(t *testing.T, when spec.G, it spec.S) {
4951
h.AssertNil(t, os.RemoveAll(repoPath))
5052
})
5153

54+
when("#GitMetadata", func() {
55+
it("returns proper metadata format", func() {
56+
assert := h.NewAssertionManager(t)
57+
remoteOpts := &config.RemoteConfig{
58+
Name: "origin",
59+
URLs: []string{"git@github.com:testorg/testproj.git", "git@github.com:testorg/testproj.git"},
60+
}
61+
repo.CreateRemote(remoteOpts)
62+
createUnannotatedTag(t, repo, commits[len(commits)-1], "testTag")
63+
64+
output := GitMetadata(repoPath)
65+
expectedOutput := &platform.ProjectSource{
66+
Type: "git",
67+
Version: map[string]interface{}{
68+
"commit": commits[len(commits)-1].String(),
69+
"describe": "testTag",
70+
},
71+
Metadata: map[string]interface{}{
72+
"refs": []string{"master", "testTag"},
73+
"url": "git@github.com:testorg/testproj.git",
74+
},
75+
}
76+
assert.Equal(output, expectedOutput)
77+
})
78+
79+
it("returns nil if error occurs while fetching metadata", func() {
80+
output := GitMetadata("/git-path-not-found-ok")
81+
h.AssertNil(t, output)
82+
})
83+
})
84+
5285
when("#generateTagsMap", func() {
5386
when("repository has no tags", func() {
5487
it("returns empty map", func() {
@@ -396,7 +429,6 @@ func testMetadata(t *testing.T, when spec.G, it spec.S) {
396429
h.AssertEq(t, output, expectedOutput)
397430
})
398431
})
399-
400432
})
401433
})
402434
})
@@ -522,22 +554,6 @@ func testMetadata(t *testing.T, when spec.G, it spec.S) {
522554
h.AssertEq(t, output, "git@github.com:testorg/testproj.git")
523555
})
524556

525-
it("returns first remote's fetch url if remote `origin` does not exists", func() {
526-
remoteOpts1 := &config.RemoteConfig{
527-
Name: "not-origin",
528-
URLs: []string{"git@gitlab.com:testorg/testproj.git", "git@gitlab.com:testorg/testproj.git"},
529-
}
530-
repo.CreateRemote(remoteOpts1)
531-
remoteOpts2 := &config.RemoteConfig{
532-
Name: "not-at-all-origin",
533-
URLs: []string{"git@github.com:testorg/testproj.git", "git@github.com:testorg/testproj.git"},
534-
}
535-
repo.CreateRemote(remoteOpts2)
536-
537-
output := parseGitRemote(repo)
538-
h.AssertEq(t, output, "git@gitlab.com:testorg/testproj.git")
539-
})
540-
541557
it("returns empty string if no remote exists", func() {
542558
output := parseGitRemote(repo)
543559
h.AssertEq(t, output, "")
@@ -554,6 +570,13 @@ func testMetadata(t *testing.T, when spec.G, it spec.S) {
554570
h.AssertEq(t, output, "git@fetch.com:testorg/testproj.git")
555571
})
556572
})
573+
574+
when("#getRefName", func() {
575+
it("return proper ref for refs with `/`", func() {
576+
output := getRefName("refs/tags/this/is/a/tag/with/slashes")
577+
h.AssertEq(t, output, "this/is/a/tag/with/slashes")
578+
})
579+
})
557580
}
558581

559582
func createCommits(t *testing.T, repo *git.Repository, repoPath string, numberOfCommits int) []plumbing.Hash {
@@ -564,12 +587,25 @@ func createCommits(t *testing.T, repo *git.Repository, repoPath string, numberOf
564587
for i := 0; i < numberOfCommits; i++ {
565588
file, err := ioutil.TempFile(repoPath, h.RandString(10))
566589
h.AssertNil(t, err)
590+
defer file.Close()
567591

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

571595
commitMsg := fmt.Sprintf("%s %d", "test commit number", i)
572-
commitOpts := git.CommitOptions{}
596+
commitOpts := git.CommitOptions{
597+
All: true,
598+
Author: &object.Signature{
599+
Name: "Test Author",
600+
Email: "testauthor@test.com",
601+
When: time.Now(),
602+
},
603+
Committer: &object.Signature{
604+
Name: "Test Committer",
605+
Email: "testcommitter@test.com",
606+
When: time.Now(),
607+
},
608+
}
573609
commitHash, err := worktree.Commit(commitMsg, &commitOpts)
574610
h.AssertNil(t, err)
575611
commitHashes = append(commitHashes, commitHash)

0 commit comments

Comments
 (0)