From 0373247790c1dd490f6756697c9d89bed9f7c4bb Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Sat, 3 Aug 2019 15:50:31 -1000 Subject: [PATCH 1/4] Add assertion for unhandled error in TestLogFile Signed-off-by: knqyf263 --- repository_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/repository_test.go b/repository_test.go index b87eabbf6..d7450c4a7 100644 --- a/repository_test.go +++ b/repository_test.go @@ -13,6 +13,8 @@ import ( "testing" "time" + fixtures "gopkg.in/src-d/go-git-fixtures.v3" + "golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp/armor" openpgperr "golang.org/x/crypto/openpgp/errors" @@ -31,7 +33,6 @@ import ( "gopkg.in/src-d/go-billy.v4/memfs" "gopkg.in/src-d/go-billy.v4/osfs" "gopkg.in/src-d/go-billy.v4/util" - "gopkg.in/src-d/go-git-fixtures.v3" ) type RepositorySuite struct { @@ -1507,12 +1508,13 @@ func (s *RepositorySuite) TestLogFileForEach(c *C) { } expectedIndex := 0 - cIter.ForEach(func(commit *object.Commit) error { + err = cIter.ForEach(func(commit *object.Commit) error { expectedCommitHash := commitOrder[expectedIndex] c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String()) expectedIndex++ return nil }) + c.Assert(err, IsNil) c.Assert(expectedIndex, Equals, 1) } @@ -1551,12 +1553,13 @@ func (s *RepositorySuite) TestLogAllFileForEach(c *C) { } expectedIndex := 0 - cIter.ForEach(func(commit *object.Commit) error { + err = cIter.ForEach(func(commit *object.Commit) error { expectedCommitHash := commitOrder[expectedIndex] c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String()) expectedIndex++ return nil }) + c.Assert(err, IsNil) c.Assert(expectedIndex, Equals, 1) } @@ -1598,12 +1601,13 @@ func (s *RepositorySuite) TestLogFileInitialCommit(c *C) { } expectedIndex := 0 - cIter.ForEach(func(commit *object.Commit) error { + err = cIter.ForEach(func(commit *object.Commit) error { expectedCommitHash := commitOrder[expectedIndex] c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String()) expectedIndex++ return nil }) + c.Assert(err, IsNil) c.Assert(expectedIndex, Equals, 1) } From d7c00b034ff5ce038c168ab16a943cc5f55aac3c Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Sat, 3 Aug 2019 16:02:48 -1000 Subject: [PATCH 2/4] Handle io.EOF error in commitFileIter.ForEach Signed-off-by: knqyf263 --- plumbing/object/commit_walker_file.go | 5 ++++- repository_test.go | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/plumbing/object/commit_walker_file.go b/plumbing/object/commit_walker_file.go index 6f16e611f..76c158bcb 100644 --- a/plumbing/object/commit_walker_file.go +++ b/plumbing/object/commit_walker_file.go @@ -128,7 +128,9 @@ func isParentHash(hash plumbing.Hash, commit *Commit) bool { func (c *commitFileIter) ForEach(cb func(*Commit) error) error { for { commit, nextErr := c.Next() - if nextErr != nil { + if nextErr == io.EOF { + break + } else if nextErr != nil { return nextErr } err := cb(commit) @@ -138,6 +140,7 @@ func (c *commitFileIter) ForEach(cb func(*Commit) error) error { return err } } + return nil } func (c *commitFileIter) Close() { diff --git a/repository_test.go b/repository_test.go index d7450c4a7..0e46ded46 100644 --- a/repository_test.go +++ b/repository_test.go @@ -13,7 +13,6 @@ import ( "testing" "time" - fixtures "gopkg.in/src-d/go-git-fixtures.v3" "golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp/armor" @@ -33,6 +32,7 @@ import ( "gopkg.in/src-d/go-billy.v4/memfs" "gopkg.in/src-d/go-billy.v4/osfs" "gopkg.in/src-d/go-billy.v4/util" + "gopkg.in/src-d/go-git-fixtures.v3" ) type RepositorySuite struct { From d1f089794aff17a78d5487d5e681d63fba17aad1 Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Sat, 3 Aug 2019 23:08:52 -1000 Subject: [PATCH 3/4] Add TestLogFileWithError for coverage Signed-off-by: knqyf263 --- repository_test.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/repository_test.go b/repository_test.go index 0e46ded46..5fc6aeb9a 100644 --- a/repository_test.go +++ b/repository_test.go @@ -3,6 +3,7 @@ package git import ( "bytes" "context" + "errors" "fmt" "io" "io/ioutil" @@ -13,7 +14,6 @@ import ( "testing" "time" - "golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp/armor" openpgperr "golang.org/x/crypto/openpgp/errors" @@ -1653,6 +1653,28 @@ func (s *RepositorySuite) TestLogFileWithOtherParamsPass(c *C) { c.Assert(iterErr, Equals, io.EOF) } +type mockErrCommitIter struct{} + +func (m *mockErrCommitIter) Next() (*object.Commit, error) { + return nil, errors.New("mock next error") +} +func (m *mockErrCommitIter) ForEach(func(*object.Commit) error) error { + return errors.New("mock foreach error") +} + +func (m *mockErrCommitIter) Close() {} + +func (s *RepositorySuite) TestLogFileWithError(c *C) { + fileName := "README" + cIter := object.NewCommitFileIterFromIter(fileName, &mockErrCommitIter{}, false) + defer cIter.Close() + + err := cIter.ForEach(func(commit *object.Commit) error { + return nil + }) + c.Assert(err, NotNil) +} + func (s *RepositorySuite) TestCommit(c *C) { r, _ := Init(memory.NewStorage(), nil) err := r.clone(context.Background(), &CloneOptions{ From a2af865f9dbf292a9804c67abc2727551f7954dd Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Sun, 4 Aug 2019 01:00:33 -1000 Subject: [PATCH 4/4] Remove else Signed-off-by: knqyf263 --- plumbing/object/commit_walker_file.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plumbing/object/commit_walker_file.go b/plumbing/object/commit_walker_file.go index 76c158bcb..b73e4ce9d 100644 --- a/plumbing/object/commit_walker_file.go +++ b/plumbing/object/commit_walker_file.go @@ -130,7 +130,8 @@ func (c *commitFileIter) ForEach(cb func(*Commit) error) error { commit, nextErr := c.Next() if nextErr == io.EOF { break - } else if nextErr != nil { + } + if nextErr != nil { return nextErr } err := cb(commit)