From 458b90ec958fcfe4246be87ef85aad215c24256d Mon Sep 17 00:00:00 2001 From: ferhat elmas Date: Wed, 22 Nov 2017 09:06:13 +0100 Subject: plumbing/object: add test for finding tree entry --- plumbing/object/tree_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'plumbing/object/tree_test.go') diff --git a/plumbing/object/tree_test.go b/plumbing/object/tree_test.go index 796d979..6d3907e 100644 --- a/plumbing/object/tree_test.go +++ b/plumbing/object/tree_test.go @@ -1,6 +1,7 @@ package object import ( + "errors" "io" "gopkg.in/src-d/go-git.v4/plumbing" @@ -113,6 +114,42 @@ func (s *TreeSuite) TestFindEntry(c *C) { c.Assert(e.Name, Equals, "foo.go") } +// Overrides returned plumbing.EncodedObject for given hash. +// Otherwise, delegates to actual storer to get real object +type fakeStorer struct { + storer.EncodedObjectStorer + hash plumbing.Hash + fake fakeEncodedObject +} + +func (fs fakeStorer) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) { + if fs.hash == h { + return fs.fake, nil + } + return fs.EncodedObjectStorer.EncodedObject(t, h) +} + +// Overrides reader of plumbing.EncodedObject to simulate read error +type fakeEncodedObject struct{ plumbing.EncodedObject } + +func (fe fakeEncodedObject) Reader() (io.ReadCloser, error) { + return nil, errors.New("Simulate encoded object can't be read") +} + +func (s *TreeSuite) TestDir(c *C) { + vendor, err := s.Tree.dir("vendor") + c.Assert(err, IsNil) + + t, err := GetTree(s.Tree.s, s.Tree.ID()) + c.Assert(err, IsNil) + o, err := t.s.EncodedObject(plumbing.AnyObject, vendor.ID()) + c.Assert(err, IsNil) + + t.s = fakeStorer{t.s, vendor.ID(), fakeEncodedObject{o}} + _, err = t.dir("vendor") + c.Assert(err, NotNil) +} + // This plumbing.EncodedObject implementation has a reader that only returns 6 // bytes at a time, this should simulate the conditions when a read // returns less bytes than asked, for example when reading a hash which -- cgit