diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-22 03:29:05 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-22 03:29:05 +0200 |
commit | 2ed3474ab8e52c98a87e390d5128d45d693a115d (patch) | |
tree | 6f136c2508c22f6b5146ef08c49821a45a5f2357 /commit_test.go | |
parent | 5b13c1a2e55cb442484d9c7b45389f422b110eec (diff) | |
download | go-git-2ed3474ab8e52c98a87e390d5128d45d693a115d.tar.gz |
ForEach review and Commit.Tree err return
Diffstat (limited to 'commit_test.go')
-rw-r--r-- | commit_test.go | 191 |
1 files changed, 60 insertions, 131 deletions
diff --git a/commit_test.go b/commit_test.go index c9a17c0..82133da 100644 --- a/commit_test.go +++ b/commit_test.go @@ -9,160 +9,89 @@ import ( ) type SuiteCommit struct { - repos map[string]*Repository + BaseSuite + Commit *Commit } var _ = Suite(&SuiteCommit{}) -// create the repositories of the fixtures -func (s *SuiteCommit) SetUpSuite(c *C) { - commitFixtures := []packedFixture{ - {"https://github.com/tyba/git-fixture.git", "formats/packfile/fixtures/git-fixture.ofs-delta"}, - } - s.repos = unpackFixtures(c, commitFixtures) -} +func (s *SuiteCommit) SetUpTest(c *C) { + s.BaseSuite.SetUpTest(c) -var commitIterTests = []struct { - repo string // the repo name in the test suite's map of fixtures - commits []string // the commit hashes to iterate over in the test -}{ - {"https://github.com/tyba/git-fixture.git", []string{ - "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", - "918c48b83bd081e863dbe1b80f8998f058cd8294", - "af2d6a6954d532f8ffb47615169c8fdf9d383a1a", - "1669dce138d9b841a518c64b10914d88f5e488ea", - "35e85108805c84807bc66a02d91535e1e24b38b9", - "b029517f6300c2da0f4b651b8642506cd6aaf45d", - "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", - "b029517f6300c2da0f4b651b8642506cd6aaf45d", // Intentional duplicate - "b8e471f58bcbca63b07bda20e428190409c2db47", - "b029517f6300c2da0f4b651b8642506cd6aaf45d"}}, // Intentional duplicate -} + hash := core.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea") -func (s *SuiteCommit) TestIterSlice(c *C) { - for i, t := range commitIterTests { - r := s.repos[t.repo] - iter := NewCommitIter(r, core.NewObjectSliceIter(makeObjectSlice(t.commits, r.s.ObjectStorage()))) - s.checkIter(c, r, i, iter, t.commits) - } + var err error + s.Commit, err = s.Repository.Commit(hash) + c.Assert(err, IsNil) } -func (s *SuiteCommit) TestIterLookup(c *C) { - for i, t := range commitIterTests { - r := s.repos[t.repo] - iter := NewCommitIter(r, core.NewObjectLookupIter(r.s.ObjectStorage(), makeHashSlice(t.commits))) - s.checkIter(c, r, i, iter, t.commits) - } +func (s *SuiteCommit) TestDecodeNonCommit(c *C) { + hash := core.NewHash("9a48f23120e880dfbe41f7c9b7b708e9ee62a492") + blob, err := s.Repository.s.ObjectStorage().Get(hash) + c.Assert(err, IsNil) + + commit := &Commit{} + err = commit.Decode(blob) + c.Assert(err, Equals, ErrUnsupportedObject) } -func (s *SuiteCommit) checkIter(c *C, r *Repository, subtest int, iter *CommitIter, commits []string) { - for k := 0; k < len(commits); k++ { - commit, err := iter.Next() - c.Assert(err, IsNil, Commentf("subtest %d, iter %d, err=%v", subtest, k, err)) - c.Assert(commit.Hash.String(), Equals, commits[k], Commentf("subtest %d, iter %d, hash=%v, expected=%s", subtest, k, commit.Hash.String(), commits[k])) - } - _, err := iter.Next() - c.Assert(err, Equals, io.EOF) +func (s *SuiteCommit) TestType(c *C) { + c.Assert(s.Commit.Type(), Equals, core.CommitObject) } -func (s *SuiteCommit) TestIterSliceClose(c *C) { - for i, t := range commitIterTests { - r := s.repos[t.repo] - iter := NewCommitIter(r, core.NewObjectSliceIter(makeObjectSlice(t.commits, r.s.ObjectStorage()))) - s.checkIterClose(c, i, iter) - } +func (s *SuiteCommit) TestTree(c *C) { + tree, err := s.Commit.Tree() + c.Assert(err, IsNil) + c.Assert(tree.ID().String(), Equals, "eba74343e2f15d62adedfd8c883ee0262b5c8021") } -func (s *SuiteCommit) TestIterLookupClose(c *C) { - for i, t := range commitIterTests { - r := s.repos[t.repo] - iter := NewCommitIter(r, core.NewObjectLookupIter(r.s.ObjectStorage(), makeHashSlice(t.commits))) - s.checkIterClose(c, i, iter) +func (s *SuiteCommit) TestParents(c *C) { + expected := []string{ + "35e85108805c84807bc66a02d91535e1e24b38b9", + "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", } -} -func (s *SuiteCommit) checkIterClose(c *C, subtest int, iter *CommitIter) { - iter.Close() - _, err := iter.Next() - c.Assert(err, Equals, io.EOF, Commentf("subtest %d, close 1, err=%v", subtest, err)) + var output []string + i := s.Commit.Parents() + err := i.ForEach(func(commit *Commit) error { + output = append(output, commit.ID().String()) + return nil + }) - iter.Close() - _, err = iter.Next() - c.Assert(err, Equals, io.EOF, Commentf("subtest %d, close 2, err=%v", subtest, err)) + c.Assert(err, IsNil) + c.Assert(output, DeepEquals, expected) } -var fileTests = []struct { - repo string // the repo name as in localRepos - commit string // the commit to search for the file - path string // the path of the file to find - blobHash string // expected hash of the returned file - found bool // expected found value -}{ - // use git ls-tree commit to get the hash of the blobs - {"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "not-found", - "", false}, - {"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", ".gitignore", - "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", true}, - {"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "LICENSE", - "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", true}, - - {"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "not-found", - "", false}, - {"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", ".gitignore", - "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88", true}, - {"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "binary.jpg", - "d5c0f4ab811897cadf03aec358ae60d21f91c50d", true}, - {"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "LICENSE", - "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f", true}, - - {"https://github.com/tyba/git-fixture.git", "35e85108805c84807bc66a02d91535e1e24b38b9", "binary.jpg", - "d5c0f4ab811897cadf03aec358ae60d21f91c50d", true}, - {"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "binary.jpg", - "", false}, - - {"https://github.com/tyba/git-fixture.git", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5", "CHANGELOG", - "d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true}, - {"https://github.com/tyba/git-fixture.git", "1669dce138d9b841a518c64b10914d88f5e488ea", "CHANGELOG", - "d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true}, - {"https://github.com/tyba/git-fixture.git", "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", "CHANGELOG", - "d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true}, - {"https://github.com/tyba/git-fixture.git", "35e85108805c84807bc66a02d91535e1e24b38b9", "CHANGELOG", - "d3ff53e0564a9f87d8e84b6e28e5060e517008aa", false}, - {"https://github.com/tyba/git-fixture.git", "b8e471f58bcbca63b07bda20e428190409c2db47", "CHANGELOG", - "d3ff53e0564a9f87d8e84b6e28e5060e517008aa", true}, - {"https://github.com/tyba/git-fixture.git", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "CHANGELOG", - "d3ff53e0564a9f87d8e84b6e28e5060e517008aa", false}, +func (s *SuiteCommit) TestFile(c *C) { + file, err := s.Commit.File("CHANGELOG") + c.Assert(err, IsNil) + c.Assert(file.Name, Equals, "CHANGELOG") } -func (s *SuiteCommit) TestFile(c *C) { - for i, t := range fileTests { - commit, err := s.repos[t.repo].Commit(core.NewHash(t.commit)) - c.Assert(err, IsNil, Commentf("subtest %d: %v (%s)", i, err, t.commit)) - - file, err := commit.File(t.path) - found := err == nil - c.Assert(found, Equals, t.found, Commentf("subtest %d, path=%s, commit=%s", i, t.path, t.commit)) - if found { - c.Assert(file.Hash.String(), Equals, t.blobHash, Commentf("subtest %d, commit=%s, path=%s", i, t.commit, t.path)) - } - } +func (s *SuiteCommit) TestNumParents(c *C) { + c.Assert(s.Commit.NumParents(), Equals, 2) } -func makeObjectSlice(hashes []string, storage core.ObjectStorage) []core.Object { - series := make([]core.Object, 0, len(hashes)) - for _, member := range hashes { - obj, err := storage.Get(core.NewHash(member)) - if err == nil { - series = append(series, obj) - } - } - return series +func (s *SuiteCommit) TestString(c *C) { + c.Assert(s.Commit.String(), Equals, ""+ + "commit 1669dce138d9b841a518c64b10914d88f5e488ea\n"+ + "Author: Máximo Cuadros Ortiz <mcuadros@gmail.com>\n"+ + "Date: 2015-03-31 13:48:14 +0200 +0200\n", + ) } -func makeHashSlice(hashes []string) []core.Hash { - series := make([]core.Hash, 0, len(hashes)) - for _, member := range hashes { - series = append(series, core.NewHash(member)) - } - return series +func (s *SuiteCommit) TestCommitIterNext(c *C) { + i := s.Commit.Parents() + + commit, err := i.Next() + c.Assert(err, IsNil) + c.Assert(commit.ID().String(), Equals, "35e85108805c84807bc66a02d91535e1e24b38b9") + + commit, err = i.Next() + c.Assert(err, IsNil) + c.Assert(commit.ID().String(), Equals, "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69") + + commit, err = i.Next() + c.Assert(err, Equals, io.EOF) + c.Assert(commit, IsNil) } |