diff options
author | Alberto Cortés <alcortesm@gmail.com> | 2016-08-02 10:58:49 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-02 10:58:49 +0200 |
commit | 5413c7aeadb7cb18a6d51dae0bc313f2e129a337 (patch) | |
tree | 9c4f02ea046f23974075856126e439ec270a74ab /repository_test.go | |
parent | dc1e2bd485f8345c14cf7b22a5b71fd03028cfdf (diff) | |
download | go-git-5413c7aeadb7cb18a6d51dae0bc313f2e129a337.tar.gz |
Repository head (#61)v3.1.1
* add Repository.Head() tests
* add head support for remote repos and more tests
* add head support for local repos
* clean code
* remove dead code
Diffstat (limited to 'repository_test.go')
-rw-r--r-- | repository_test.go | 82 |
1 files changed, 71 insertions, 11 deletions
diff --git a/repository_test.go b/repository_test.go index 7d4abb5..80a765c 100644 --- a/repository_test.go +++ b/repository_test.go @@ -13,19 +13,26 @@ import ( . "gopkg.in/check.v1" ) -var dirFixtures = [...]struct { +var dirFixturesInit = [...]struct { name string tgz string + head string }{ { name: "binrels", tgz: "storage/seekable/internal/gitdir/fixtures/alcortesm-binary-relations.tgz", + head: "c44b5176e99085c8fe36fa27b045590a7b9d34c9", }, } +type dirFixture struct { + path string + head core.Hash +} + type SuiteRepository struct { - repos map[string]*Repository - dirFixturePaths map[string]string + repos map[string]*Repository + dirFixtures map[string]dirFixture } var _ = Suite(&SuiteRepository{}) @@ -33,22 +40,25 @@ var _ = Suite(&SuiteRepository{}) func (s *SuiteRepository) SetUpSuite(c *C) { s.repos = unpackFixtures(c, tagFixtures, treeWalkerFixtures) - s.dirFixturePaths = make(map[string]string, len(dirFixtures)) - for _, fix := range dirFixtures { + s.dirFixtures = make(map[string]dirFixture, len(dirFixturesInit)) + for _, fix := range dirFixturesInit { com := Commentf("fixture name = %s\n", fix.name) path, err := tgz.Extract(fix.tgz) c.Assert(err, IsNil, com) - s.dirFixturePaths[fix.name] = path + s.dirFixtures[fix.name] = dirFixture{ + path: path, + head: core.NewHash(fix.head), + } } } func (s *SuiteRepository) TearDownSuite(c *C) { - for name, path := range s.dirFixturePaths { - err := os.RemoveAll(path) + for name, fix := range s.dirFixtures { + err := os.RemoveAll(fix.path) c.Assert(err, IsNil, Commentf("cannot delete tmp dir for fixture %s: %s\n", - name, path)) + name, fix.path)) } } @@ -66,9 +76,9 @@ func (s *SuiteRepository) TestNewRepositoryWithAuth(c *C) { } func (s *SuiteRepository) TestNewRepositoryFromFS(c *C) { - for name, path := range s.dirFixturePaths { + for name, fix := range s.dirFixtures { fs := fs.NewOS() - gitPath := fs.Join(path, ".git/") + gitPath := fs.Join(fix.path, ".git/") com := Commentf("dir fixture %q → %q\n", name, gitPath) repo, err := NewRepositoryFromFS(fs, gitPath) c.Assert(err, IsNil, com) @@ -205,3 +215,53 @@ func (s *SuiteRepository) TestCommitIterClosePanic(c *C) { c.Assert(err, IsNil) commits.Close() } + +func (s *SuiteRepository) TestHeadFromFs(c *C) { + for name, fix := range s.dirFixtures { + fs := fs.NewOS() + gitPath := fs.Join(fix.path, ".git/") + com := Commentf("dir fixture %q → %q\n", name, gitPath) + repo, err := NewRepositoryFromFS(fs, gitPath) + c.Assert(err, IsNil, com) + + head, err := repo.Head("") + c.Assert(err, IsNil) + + c.Assert(head, Equals, fix.head) + } +} + +func (s *SuiteRepository) TestHeadFromRemote(c *C) { + r, err := NewRepository(RepositoryFixture, nil) + c.Assert(err, IsNil) + + upSrv := &MockGitUploadPackService{} + r.Remotes[DefaultRemoteName].upSrv = upSrv + err = r.Remotes[DefaultRemoteName].Connect() + c.Assert(err, IsNil) + + info, err := upSrv.Info() + c.Assert(err, IsNil) + expected := info.Head + + obtained, err := r.Head(DefaultRemoteName) + c.Assert(err, IsNil) + + c.Assert(obtained, Equals, expected) +} + +func (s *SuiteRepository) TestHeadErrors(c *C) { + r, err := NewRepository(RepositoryFixture, nil) + c.Assert(err, IsNil) + + upSrv := &MockGitUploadPackService{} + r.Remotes[DefaultRemoteName].upSrv = upSrv + + remote := "not found" + _, err = r.Head(remote) + c.Assert(err, ErrorMatches, fmt.Sprintf("unable to find remote %q", remote)) + + remote = "" + _, err = r.Head(remote) + c.Assert(err, ErrorMatches, "cannot retrieve local head: no local data found") +} |