Debian Codegraph @

plumbing/format/objfile/writer_test.go

raw
1 package objfile
2
3 import (
4 "bytes"
5 "encoding/base64"
6 "fmt"
7 "io"
8
9 "github.com/go-git/go-git/v5/plumbing"
10
11 . "gopkg.in/check.v1"
12 )
13
14 type SuiteWriter struct{}
15
16 var _ = Suite(&SuiteWriter{})
17
18 func (s *SuiteWriter) TestWriteObjfile(c *C) {
19 for k, fixture := range objfileFixtures {
20 buffer := bytes.NewBuffer(nil)
21
22 com := fmt.Sprintf("test %d: ", k)
23 hash := plumbing.NewHash(fixture.hash)
24 content, _ := base64.StdEncoding.DecodeString(fixture.content)
25
26 // Write the data out to the buffer
27 testWriter(c, buffer, hash, fixture.t, content)
28
29 // Read the data back in from the buffer to be sure it matches
30 testReader(c, buffer, hash, fixture.t, content, com)
31 }
32 }
33
34 func testWriter(c *C, dest io.Writer, hash plumbing.Hash, t plumbing.ObjectType, content []byte) {
35 size := int64(len(content))
36 w := NewWriter(dest)
37
38 err := w.WriteHeader(t, size)
39 c.Assert(err, IsNil)
40
41 written, err := io.Copy(w, bytes.NewReader(content))
42 c.Assert(err, IsNil)
43 c.Assert(written, Equals, size)
44
45 c.Assert(w.Hash(), Equals, hash)
46 c.Assert(w.Close(), IsNil)
47 }
48
49 func (s *SuiteWriter) TestWriteOverflow(c *C) {
50 buf := bytes.NewBuffer(nil)
51 w := NewWriter(buf)
52
53 err := w.WriteHeader(plumbing.BlobObject, 8)
54 c.Assert(err, IsNil)
55
56 n, err := w.Write([]byte("1234"))
57 c.Assert(err, IsNil)
58 c.Assert(n, Equals, 4)
59
60 n, err = w.Write([]byte("56789"))
61 c.Assert(err, Equals, ErrOverflow)
62 c.Assert(n, Equals, 4)
63 }
64
65 func (s *SuiteWriter) TestNewWriterInvalidType(c *C) {
66 buf := bytes.NewBuffer(nil)
67 w := NewWriter(buf)
68
69 err := w.WriteHeader(plumbing.InvalidObject, 8)
70 c.Assert(err, Equals, plumbing.ErrInvalidType)
71 }
72
73 func (s *SuiteWriter) TestNewWriterInvalidSize(c *C) {
74 buf := bytes.NewBuffer(nil)
75 w := NewWriter(buf)
76
77 err := w.WriteHeader(plumbing.BlobObject, -1)
78 c.Assert(err, Equals, ErrNegativeSize)
79 err = w.WriteHeader(plumbing.BlobObject, -1651860)
80 c.Assert(err, Equals, ErrNegativeSize)
81 }