aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/idxfile/writer_test.go
blob: 51273a365ee45bd24961f635e9b86f5dc1b1d548 (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
package idxfile_test

import (
	"bytes"
	"io/ioutil"

	"gopkg.in/src-d/go-git.v4/plumbing/format/idxfile"
	"gopkg.in/src-d/go-git.v4/plumbing/format/packfile"

	. "gopkg.in/check.v1"
	"gopkg.in/src-d/go-git-fixtures.v3"
)

type IndexSuite struct {
	fixtures.Suite
}

var _ = Suite(&IndexSuite{})

func (s *IndexSuite) TestIndexWriter(c *C) {
	f := fixtures.Basic().One()
	scanner := packfile.NewScanner(f.Packfile())

	obs := new(idxfile.Writer)
	parser := packfile.NewParser(scanner, obs)

	_, err := parser.Parse()
	c.Assert(err, IsNil)

	idx, err := obs.Index()
	c.Assert(err, IsNil)

	idxFile := f.Idx()
	expected, err := ioutil.ReadAll(idxFile)
	c.Assert(err, IsNil)
	idxFile.Close()

	buf := new(bytes.Buffer)
	encoder := idxfile.NewEncoder(buf)
	n, err := encoder.Encode(idx)
	c.Assert(err, IsNil)
	c.Assert(n, Equals, len(expected))

	c.Assert(buf.Bytes(), DeepEquals, expected)
}