1- package client
1+ package v02
22
33import (
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
2325func 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
2931func 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
559582func 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