aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/index/index_test.go
blob: ecf3c0d72d320101e515cfb2e9b18aa1cde5c7c8 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package index

import (
	"path/filepath"

	. "gopkg.in/check.v1"
)

func (s *IndexSuite) TestIndexAdd(c *C) {
	idx := &Index{}
	e := idx.Add("foo")
	e.Size = 42

	e, err := idx.Entry("foo")
	c.Assert(err, IsNil)
	c.Assert(e.Name, Equals, "foo")
	c.Assert(e.Size, Equals, uint32(42))
}

func (s *IndexSuite) TestIndexEntry(c *C) {
	idx := &Index{
		Entries: []*Entry{
			{Name: "foo", Size: 42},
			{Name: "bar", Size: 82},
		},
	}

	e, err := idx.Entry("foo")
	c.Assert(err, IsNil)
	c.Assert(e.Name, Equals, "foo")

	e, err = idx.Entry("missing")
	c.Assert(e, IsNil)
	c.Assert(err, Equals, ErrEntryNotFound)
}

func (s *IndexSuite) TestIndexRemove(c *C) {
	idx := &Index{
		Entries: []*Entry{
			{Name: "foo", Size: 42},
			{Name: "bar", Size: 82},
		},
	}

	e, err := idx.Remove("foo")
	c.Assert(err, IsNil)
	c.Assert(e.Name, Equals, "foo")

	e, err = idx.Remove("foo")
	c.Assert(e, IsNil)
	c.Assert(err, Equals, ErrEntryNotFound)
}

func (s *IndexSuite) TestIndexGlob(c *C) {
	idx := &Index{
		Entries: []*Entry{
			{Name: "foo/bar/bar", Size: 42},
			{Name: "foo/baz/qux", Size: 42},
			{Name: "fux", Size: 82},
		},
	}

	m, err := idx.Glob(filepath.Join("foo", "b*"))
	c.Assert(err, IsNil)
	c.Assert(m, HasLen, 2)
	c.Assert(m[0].Name, Equals, "foo/bar/bar")
	c.Assert(m[1].Name, Equals, "foo/baz/qux")

	m, err = idx.Glob("f*")
	c.Assert(err, IsNil)
	c.Assert(m, HasLen, 3)

	m, err = idx.Glob("f*/baz/q*")
	c.Assert(err, IsNil)
	c.Assert(m, HasLen, 1)
}