diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-12-02 00:44:06 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-12-02 00:44:06 +0100 |
commit | 6996381ae43936e7365395e90c8f93372e49be9f (patch) | |
tree | ed0d9441157a62230b0b9ca57ef600fd2ba99f76 /common_test.go | |
parent | c15bf1dff332873644290db0e186b8f5ad9b8fb2 (diff) | |
download | go-git-6996381ae43936e7365395e90c8f93372e49be9f.tar.gz |
BaseSuite improvements, usage of file:// proto
Diffstat (limited to 'common_test.go')
-rw-r--r-- | common_test.go | 190 |
1 files changed, 36 insertions, 154 deletions
diff --git a/common_test.go b/common_test.go index 3aa3693..2dabf6b 100644 --- a/common_test.go +++ b/common_test.go @@ -2,18 +2,11 @@ package git import ( "fmt" - "io" - "os" "testing" "gopkg.in/src-d/go-git.v4/fixtures" - "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/format/packfile" - "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp" - "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability" - "gopkg.in/src-d/go-git.v4/plumbing/transport" "gopkg.in/src-d/go-git.v4/plumbing/transport/client" - "gopkg.in/src-d/go-git.v4/storage/filesystem" . "gopkg.in/check.v1" ) @@ -22,159 +15,71 @@ func Test(t *testing.T) { TestingT(t) } type BaseSuite struct { fixtures.Suite + Repository *Repository - Repository *Repository - Repositories map[string]*Repository + cache map[string]*Repository } func (s *BaseSuite) SetUpSuite(c *C) { s.Suite.SetUpSuite(c) s.installMockProtocol(c) - s.buildRepository(c) + s.buildBasicRepository(c) - s.Repositories = make(map[string]*Repository, 0) - s.buildRepositories(c, fixtures.Basic().ByTag("packfile")) + s.cache = make(map[string]*Repository, 0) } -func (s *BaseSuite) installMockProtocol(c *C) { - client.InstallProtocol("https", &MockClient{}) -} - -func (s *BaseSuite) buildRepository(c *C) { +func (s *BaseSuite) buildBasicRepository(c *C) { f := fixtures.Basic().One() - - var err error - s.Repository, err = NewFilesystemRepository(f.DotGit().Base()) - c.Assert(err, IsNil) + s.Repository = s.NewRepository(f) } -func (s *BaseSuite) buildRepositories(c *C, f fixtures.Fixtures) { - for _, fixture := range f { - r := NewMemoryRepository() - - f := fixture.Packfile() - defer f.Close() - - n := packfile.NewScanner(f) - d, err := packfile.NewDecoder(n, r.s) - c.Assert(err, IsNil) - _, err = d.Decode() - c.Assert(err, IsNil) - - s.Repositories[fixture.URL] = r +func (s *BaseSuite) NewRepository(f *fixtures.Fixture) *Repository { + r, err := NewFilesystemRepository(f.DotGit().Base()) + if err != nil { + panic(err) } -} - -const RepositoryFixture = "https://github.com/git-fixtures/basic.git" - -type MockClient struct{} - -type MockFetchPackSession struct { - endpoint transport.Endpoint - auth transport.AuthMethod -} - -func (c *MockClient) NewFetchPackSession(ep transport.Endpoint) ( - transport.FetchPackSession, error) { - return &MockFetchPackSession{ - endpoint: ep, - auth: nil, - }, nil -} - -func (c *MockClient) NewSendPackSession(ep transport.Endpoint) ( - transport.SendPackSession, error) { - - return nil, fmt.Errorf("not supported") -} - -func (c *MockFetchPackSession) SetAuth(auth transport.AuthMethod) error { - c.auth = auth - return nil + return r } -func (c *MockFetchPackSession) AdvertisedReferences() (*packp.AdvRefs, error) { - - h := fixtures.ByURL(c.endpoint.String()).One().Head - - cap := capability.NewList() - cap.Set(capability.Agent, "go-git/tests") - - ref := plumbing.ReferenceName("refs/heads/master") - branch := plumbing.ReferenceName("refs/heads/branch") - tag := plumbing.ReferenceName("refs/tags/v1.0.0") - - a := packp.NewAdvRefs() - a.Capabilities = cap - a.Head = &h - a.AddReference(plumbing.NewSymbolicReference(plumbing.HEAD, ref)) - a.AddReference(plumbing.NewHashReference(ref, h)) - a.AddReference(plumbing.NewHashReference(tag, h)) - a.AddReference(plumbing.NewHashReference(branch, plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881"))) +func (s *BaseSuite) NewRepositoryFromPackfile(f *fixtures.Fixture) *Repository { + h := f.PackfileHash.String() + if r, ok := s.cache[h]; ok { + return r + } - return a, nil -} + r := NewMemoryRepository() -func (c *MockFetchPackSession) FetchPack( - r *packp.UploadPackRequest) (io.ReadCloser, error) { + p := f.Packfile() + defer p.Close() - if !r.Capabilities.Supports(capability.Agent) { - return nil, fmt.Errorf("" + - "invalid test rquest, missing Agent capability, the request" + - "should be created using NewUploadPackRequestFromCapabilities", - ) + n := packfile.NewScanner(p) + d, err := packfile.NewDecoder(n, r.s) + if err != nil { + panic(err) } - f := fixtures.ByURL(c.endpoint.String()) - - if len(r.Wants) == 1 { - return f.Exclude("single-branch").One().Packfile(), nil + _, err = d.Decode() + if err != nil { + panic(err) } - return f.One().Packfile(), nil -} - -func (c *MockFetchPackSession) Close() error { - return nil + s.cache[h] = r + return r } -type packedFixture struct { - url string - packfile string +func (s *BaseSuite) installMockProtocol(c *C) { + client.InstallProtocol("https", nil) } -var fixtureRepos = []packedFixture{ - {"https://github.com/tyba/git-fixture.git", "formats/packfile/fixtures/git-fixture.ofs-delta"}, - {"https://github.com/jamesob/desk.git", "formats/packfile/fixtures/jamesob-desk.pack"}, - {"https://github.com/spinnaker/spinnaker.git", "formats/packfile/fixtures/spinnaker-spinnaker.pack"}, +func (s *BaseSuite) GetBasicLocalRepositoryURL() string { + fixture := fixtures.Basic().One() + return s.GetLocalRepositoryURL(fixture) } -func unpackFixtures(c *C, fixtures ...[]packedFixture) map[string]*Repository { - repos := make(map[string]*Repository, 0) - for _, group := range fixtures { - for _, fixture := range group { - if _, existing := repos[fixture.url]; existing { - continue - } - - comment := Commentf("fixture packfile: %q", fixture.packfile) - - repos[fixture.url] = NewMemoryRepository() - - f, err := os.Open(fixture.packfile) - c.Assert(err, IsNil, comment) - - r := packfile.NewScanner(f) - d, err := packfile.NewDecoder(r, repos[fixture.url].s) - c.Assert(err, IsNil, comment) - _, err = d.Decode() - c.Assert(err, IsNil, comment) - c.Assert(f.Close(), IsNil, comment) - } - } - - return repos +func (s *BaseSuite) GetLocalRepositoryURL(f *fixtures.Fixture) string { + path := f.DotGit().Base() + return fmt.Sprintf("file://%s", path) } type SuiteCommon struct{} @@ -202,26 +107,3 @@ func (s *SuiteCommon) TestCountLines(c *C) { c.Assert(o, Equals, t.e, Commentf("subtest %d, input=%q", i, t.i)) } } - -func (s *BaseSuite) Clone(url string) *Repository { - r := NewMemoryRepository() - if err := r.Clone(&CloneOptions{URL: url}); err != nil { - panic(err) - } - - return r -} - -func (s *BaseSuite) NewRepository(f *fixtures.Fixture) *Repository { - storage, err := filesystem.NewStorage(f.DotGit()) - if err != nil { - panic(err) - } - - r, err := NewRepository(storage) - if err != nil { - panic(err) - } - - return r -} |