diff options
author | Santiago M. Mola <santi@mola.io> | 2016-12-14 23:12:44 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-12-14 23:12:44 +0100 |
commit | 0af572dd21c0aa79d13745b633ee24ba6c4d6cf1 (patch) | |
tree | 49e81e74e82d84fd88b2fc1e4b0dc7c7bfe9c40f /plumbing/object/blob_test.go | |
parent | df0f38af83f972f026d7e14150f3d37b95f13484 (diff) | |
download | go-git-0af572dd21c0aa79d13745b633ee24ba6c4d6cf1.tar.gz |
move plumbing from top level package to plumbing (#183)
* plumbing: rename Object -> EncodedObject.
* plumbing/storer: rename ObjectStorer -> EncodedObjectStorer.
* move difftree to plumbing/difftree.
* move diff -> utils/diff
* make Object/Tag/Blob/Tree/Commit/File depend on storer.
* Object and its implementations now depend only on
storer.EncodedObjectStorer, not git.Repository.
* Tests are decoupled accordingly.
* move Object/Commit/File/Tag/Tree to plumbing/object.
* move Object/Commit/File/Tag/Tree to plumbing/object.
* move checkClose to utils/ioutil.
* move RevListObjects to plumbing/revlist.Objects.
* move DiffTree to plumbing/difftree package.
* rename files with plural nouns to singular
* plumbing/object: add GetBlob/GetCommit/GetTag/GetTree.
Diffstat (limited to 'plumbing/object/blob_test.go')
-rw-r--r-- | plumbing/object/blob_test.go | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/plumbing/object/blob_test.go b/plumbing/object/blob_test.go new file mode 100644 index 0000000..5ed9de0 --- /dev/null +++ b/plumbing/object/blob_test.go @@ -0,0 +1,96 @@ +package object + +import ( + "io" + "io/ioutil" + + "gopkg.in/src-d/go-git.v4/plumbing" + + . "gopkg.in/check.v1" +) + +type BlobsSuite struct { + BaseObjectsSuite +} + +var _ = Suite(&BlobsSuite{}) + +func (s *BlobsSuite) TestBlobHash(c *C) { + o := &plumbing.MemoryObject{} + o.SetType(plumbing.BlobObject) + o.SetSize(3) + + writer, err := o.Writer() + c.Assert(err, IsNil) + defer func() { c.Assert(writer.Close(), IsNil) }() + + writer.Write([]byte{'F', 'O', 'O'}) + + blob := &Blob{} + c.Assert(blob.Decode(o), IsNil) + + c.Assert(blob.Size, Equals, int64(3)) + c.Assert(blob.Hash.String(), Equals, "d96c7efbfec2814ae0301ad054dc8d9fc416c9b5") + + reader, err := blob.Reader() + c.Assert(err, IsNil) + defer func() { c.Assert(reader.Close(), IsNil) }() + + data, err := ioutil.ReadAll(reader) + c.Assert(err, IsNil) + c.Assert(string(data), Equals, "FOO") +} + +func (s *BlobsSuite) TestBlobDecodeEncodeIdempotent(c *C) { + var objects []*plumbing.MemoryObject + for _, str := range []string{"foo", "foo\n"} { + obj := &plumbing.MemoryObject{} + obj.Write([]byte(str)) + obj.SetType(plumbing.BlobObject) + obj.Hash() + objects = append(objects, obj) + } + for _, object := range objects { + blob := &Blob{} + err := blob.Decode(object) + c.Assert(err, IsNil) + newObject := &plumbing.MemoryObject{} + err = blob.Encode(newObject) + c.Assert(err, IsNil) + newObject.Hash() // Ensure Hash is pre-computed before deep comparison + c.Assert(newObject, DeepEquals, object) + } +} + +func (s *BlobsSuite) TestBlobIter(c *C) { + encIter, err := s.Storer.IterEncodedObjects(plumbing.BlobObject) + c.Assert(err, IsNil) + iter := NewBlobIter(s.Storer, encIter) + + blobs := []*Blob{} + iter.ForEach(func(b *Blob) error { + blobs = append(blobs, b) + return nil + }) + + c.Assert(len(blobs) > 0, Equals, true) + iter.Close() + + encIter, err = s.Storer.IterEncodedObjects(plumbing.BlobObject) + c.Assert(err, IsNil) + iter = NewBlobIter(s.Storer, encIter) + + i := 0 + for { + b, err := iter.Next() + if err == io.EOF { + break + } + + c.Assert(err, IsNil) + c.Assert(b, DeepEquals, blobs[i]) + i += 1 + } + + iter.Close() +} |