aboutsummaryrefslogtreecommitdiffstats
path: root/formats/idxfile/encoder_test.go
blob: bfb9f915136c5258ff93fd619fdd24500a054c01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package idxfile

import (
	"bytes"
	"io"
	"os"

	. "gopkg.in/check.v1"
)

func (s *IdxfileSuite) TestEncode(c *C) {
	for i, path := range [...]string{
		"fixtures/git-fixture.idx",
		"../packfile/fixtures/spinnaker-spinnaker.idx",
	} {
		com := Commentf("subtest %d: path = %s", i, path)

		exp, idx, err := decode(path)
		c.Assert(err, IsNil, com)

		obt := new(bytes.Buffer)
		e := NewEncoder(obt)
		size, err := e.Encode(idx)
		c.Assert(err, IsNil, com)

		c.Assert(size, Equals, exp.Len(), com)
		c.Assert(obt, DeepEquals, exp, com)
	}
}

func decode(path string) (*bytes.Buffer, *Idxfile, error) {
	f, err := os.Open(path)
	if err != nil {
		return nil, nil, err
	}

	cont := new(bytes.Buffer)
	tee := io.TeeReader(f, cont)

	d := NewDecoder(tee)
	idx := &Idxfile{}
	if err = d.Decode(idx); err != nil {
		return nil, nil, err
	}

	return cont, idx, f.Close()
}