diff options
Diffstat (limited to 'commit_test.go')
-rw-r--r-- | commit_test.go | 91 |
1 files changed, 87 insertions, 4 deletions
diff --git a/commit_test.go b/commit_test.go index 67b9e77..65f6400 100644 --- a/commit_test.go +++ b/commit_test.go @@ -1,6 +1,7 @@ package git import ( + "io" "os" "gopkg.in/src-d/go-git.v2/core" @@ -40,10 +41,73 @@ func (s *SuiteCommit) SetUpSuite(c *C) { } } -func (s *SuiteCommit) TestIterClose(c *C) { - i := &iter{ch: make(chan core.Object, 1)} - i.Close() - i.Close() +var iterTests = []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 +} + +func (s *SuiteCommit) TestIterSlice(c *C) { + for i, t := range iterTests { + r := s.repos[t.repo] + iter := NewCommitIter(r, core.NewObjectSliceIter(makeObjectSlice(t.commits, r.Storage))) + s.checkIter(c, r, i, iter, t.commits) + } +} + +func (s *SuiteCommit) TestIterLookup(c *C) { + for i, t := range iterTests { + r := s.repos[t.repo] + iter := NewCommitIter(r, core.NewObjectLookupIter(r.Storage, makeHashSlice(t.commits))) + s.checkIter(c, r, i, iter, t.commits) + } +} + +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) TestIterSliceClose(c *C) { + for i, t := range iterTests { + r := s.repos[t.repo] + iter := NewCommitIter(r, core.NewObjectSliceIter(makeObjectSlice(t.commits, r.Storage))) + s.checkIterClose(c, i, iter) + } +} + +func (s *SuiteCommit) TestIterLookupClose(c *C) { + for i, t := range iterTests { + r := s.repos[t.repo] + iter := NewCommitIter(r, core.NewObjectLookupIter(r.Storage, makeHashSlice(t.commits))) + s.checkIterClose(c, i, iter) + } +} + +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)) + + iter.Close() + _, err = iter.Next() + c.Assert(err, Equals, io.EOF, Commentf("subtest %d, close 2, err=%v", subtest, err)) } var fileTests = []struct { @@ -102,3 +166,22 @@ func (s *SuiteCommit) TestFile(c *C) { } } } + +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 makeHashSlice(hashes []string) []core.Hash { + series := make([]core.Hash, 0, len(hashes)) + for _, member := range hashes { + series = append(series, core.NewHash(member)) + } + return series +} |