|
1 | 1 | package object
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "io"
|
5 | 6 |
|
6 | 7 | "gopkg.in/src-d/go-git.v4/plumbing"
|
@@ -113,6 +114,42 @@ func (s *TreeSuite) TestFindEntry(c *C) {
|
113 | 114 | c.Assert(e.Name, Equals, "foo.go")
|
114 | 115 | }
|
115 | 116 |
|
| 117 | +// Overrides returned plumbing.EncodedObject for given hash. |
| 118 | +// Otherwise, delegates to actual storer to get real object |
| 119 | +type fakeStorer struct { |
| 120 | + storer.EncodedObjectStorer |
| 121 | + hash plumbing.Hash |
| 122 | + fake fakeEncodedObject |
| 123 | +} |
| 124 | + |
| 125 | +func (fs fakeStorer) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) { |
| 126 | + if fs.hash == h { |
| 127 | + return fs.fake, nil |
| 128 | + } |
| 129 | + return fs.EncodedObjectStorer.EncodedObject(t, h) |
| 130 | +} |
| 131 | + |
| 132 | +// Overrides reader of plumbing.EncodedObject to simulate read error |
| 133 | +type fakeEncodedObject struct{ plumbing.EncodedObject } |
| 134 | + |
| 135 | +func (fe fakeEncodedObject) Reader() (io.ReadCloser, error) { |
| 136 | + return nil, errors.New("Simulate encoded object can't be read") |
| 137 | +} |
| 138 | + |
| 139 | +func (s *TreeSuite) TestDir(c *C) { |
| 140 | + vendor, err := s.Tree.dir("vendor") |
| 141 | + c.Assert(err, IsNil) |
| 142 | + |
| 143 | + t, err := GetTree(s.Tree.s, s.Tree.ID()) |
| 144 | + c.Assert(err, IsNil) |
| 145 | + o, err := t.s.EncodedObject(plumbing.AnyObject, vendor.ID()) |
| 146 | + c.Assert(err, IsNil) |
| 147 | + |
| 148 | + t.s = fakeStorer{t.s, vendor.ID(), fakeEncodedObject{o}} |
| 149 | + _, err = t.dir("vendor") |
| 150 | + c.Assert(err, NotNil) |
| 151 | +} |
| 152 | + |
116 | 153 | // This plumbing.EncodedObject implementation has a reader that only returns 6
|
117 | 154 | // bytes at a time, this should simulate the conditions when a read
|
118 | 155 | // returns less bytes than asked, for example when reading a hash which
|
|
0 commit comments