github.com/YousefHaggyHeroku/pack@v1.5.5/internal/registry/git_test.go (about)

     1  package registry_test
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/sclevine/spec"
    11  	"github.com/sclevine/spec/report"
    12  	"gopkg.in/src-d/go-git.v4"
    13  
    14  	ilogging "github.com/YousefHaggyHeroku/pack/internal/logging"
    15  	"github.com/YousefHaggyHeroku/pack/internal/registry"
    16  	"github.com/YousefHaggyHeroku/pack/logging"
    17  	h "github.com/YousefHaggyHeroku/pack/testhelpers"
    18  )
    19  
    20  func TestGit(t *testing.T) {
    21  	spec.Run(t, "Git", testGit, spec.Parallel(), spec.Report(report.Terminal{}))
    22  }
    23  
    24  func testGit(t *testing.T, when spec.G, it spec.S) {
    25  	var (
    26  		registryCache   registry.Cache
    27  		tmpDir          string
    28  		err             error
    29  		registryFixture string
    30  		outBuf          bytes.Buffer
    31  		logger          logging.Logger
    32  		username        string = "supra08"
    33  	)
    34  
    35  	it.Before(func() {
    36  		logger = ilogging.NewLogWithWriters(&outBuf, &outBuf)
    37  
    38  		tmpDir, err = ioutil.TempDir("", "registry")
    39  		h.AssertNil(t, err)
    40  
    41  		registryFixture = h.CreateRegistryFixture(t, tmpDir, filepath.Join("..", "..", "testdata", "registry"))
    42  		registryCache, err = registry.NewRegistryCache(logger, tmpDir, registryFixture)
    43  		h.AssertNil(t, err)
    44  	})
    45  
    46  	it.After(func() {
    47  		h.AssertNil(t, os.RemoveAll(tmpDir))
    48  	})
    49  
    50  	when("#GitCommit", func() {
    51  		when("ADD buildpack", func() {
    52  			it("commits addition", func() {
    53  				err := registry.GitCommit(registry.Buildpack{
    54  					Namespace: "example",
    55  					Name:      "python",
    56  					Version:   "1.0.0",
    57  					Yanked:    false,
    58  					Address:   "example.com",
    59  				}, username, registryCache)
    60  				h.AssertNil(t, err)
    61  
    62  				repo, err := git.PlainOpen(registryCache.Root)
    63  				h.AssertNil(t, err)
    64  
    65  				head, err := repo.Head()
    66  				h.AssertNil(t, err)
    67  
    68  				cIter, err := repo.Log(&git.LogOptions{From: head.Hash()})
    69  				h.AssertNil(t, err)
    70  
    71  				commit, err := cIter.Next()
    72  				h.AssertNil(t, err)
    73  
    74  				h.AssertEq(t, commit.Message, "ADD example/python@1.0.0")
    75  			})
    76  		})
    77  
    78  		when("YANK buildpack", func() {
    79  			it("commits yank", func() {
    80  				err := registry.GitCommit(registry.Buildpack{
    81  					Namespace: "example",
    82  					Name:      "python",
    83  					Version:   "1.0.0",
    84  					Yanked:    true,
    85  					Address:   "example.com",
    86  				}, username, registryCache)
    87  				h.AssertNil(t, err)
    88  
    89  				repo, err := git.PlainOpen(registryCache.Root)
    90  				h.AssertNil(t, err)
    91  
    92  				head, err := repo.Head()
    93  				h.AssertNil(t, err)
    94  
    95  				cIter, err := repo.Log(&git.LogOptions{From: head.Hash()})
    96  				h.AssertNil(t, err)
    97  
    98  				commit, err := cIter.Next()
    99  				h.AssertNil(t, err)
   100  
   101  				h.AssertEq(t, commit.Message, "YANK example/python@1.0.0")
   102  			})
   103  		})
   104  	})
   105  }