Debian Codegraph @

utils/merkletrie/index/node_test.go

raw
1 package index
2
3 import (
4 "bytes"
5 "path"
6 "testing"
7
8 "github.com/go-git/go-git/v5/plumbing"
9 "github.com/go-git/go-git/v5/plumbing/format/index"
10 "github.com/go-git/go-git/v5/utils/merkletrie"
11 "github.com/go-git/go-git/v5/utils/merkletrie/noder"
12
13 . "gopkg.in/check.v1"
14 )
15
16 func Test(t *testing.T) { TestingT(t) }
17
18 type NoderSuite struct{}
19
20 var _ = Suite(&NoderSuite{})
21
22 func (s *NoderSuite) TestDiff(c *C) {
23 indexA := &index.Index{
24 Entries: []*index.Entry{
25 {Name: "foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
26 {Name: "bar/foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
27 {Name: "bar/qux", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
28 {Name: "bar/baz/foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
29 },
30 }
31
32 indexB := &index.Index{
33 Entries: []*index.Entry{
34 {Name: "foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
35 {Name: "bar/foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
36 {Name: "bar/qux", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
37 {Name: "bar/baz/foo", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
38 },
39 }
40
41 ch, err := merkletrie.DiffTree(NewRootNode(indexA), NewRootNode(indexB), isEquals)
42 c.Assert(err, IsNil)
43 c.Assert(ch, HasLen, 0)
44 }
45
46 func (s *NoderSuite) TestDiffChange(c *C) {
47 indexA := &index.Index{
48 Entries: []*index.Entry{{
49 Name: path.Join("bar", "baz", "bar"),
50 Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
51 }},
52 }
53
54 indexB := &index.Index{
55 Entries: []*index.Entry{{
56 Name: path.Join("bar", "baz", "foo"),
57 Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
58 }},
59 }
60
61 ch, err := merkletrie.DiffTree(NewRootNode(indexA), NewRootNode(indexB), isEquals)
62 c.Assert(err, IsNil)
63 c.Assert(ch, HasLen, 2)
64 }
65
66 func (s *NoderSuite) TestDiffSkipIssue1455(c *C) {
67 indexA := &index.Index{
68 Entries: []*index.Entry{
69 {
70 Name: path.Join("bar", "baz", "bar"),
71 Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
72 SkipWorktree: true,
73 },
74 {
75 Name: path.Join("bar", "biz", "bat"),
76 Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
77 SkipWorktree: false,
78 },
79 },
80 }
81
82 indexB := &index.Index{}
83
84 ch, err := merkletrie.DiffTree(NewRootNode(indexB), NewRootNode(indexA), isEquals)
85 c.Assert(err, IsNil)
86 c.Assert(ch, HasLen, 1)
87 a, err := ch[0].Action()
88 c.Assert(err, IsNil)
89 c.Assert(a, Equals, merkletrie.Insert)
90 }
91
92 func (s *NoderSuite) TestDiffDir(c *C) {
93 indexA := &index.Index{
94 Entries: []*index.Entry{{
95 Name: "foo",
96 Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
97 }},
98 }
99
100 indexB := &index.Index{
101 Entries: []*index.Entry{{
102 Name: path.Join("foo", "bar"),
103 Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
104 }},
105 }
106
107 ch, err := merkletrie.DiffTree(NewRootNode(indexA), NewRootNode(indexB), isEquals)
108 c.Assert(err, IsNil)
109 c.Assert(ch, HasLen, 2)
110 }
111
112 func (s *NoderSuite) TestDiffSameRoot(c *C) {
113 indexA := &index.Index{
114 Entries: []*index.Entry{
115 {Name: "foo.go", Hash: plumbing.NewHash("aab686eafeb1f44702738c8b0f24f2567c36da6d")},
116 {Name: "foo/bar", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
117 },
118 }
119
120 indexB := &index.Index{
121 Entries: []*index.Entry{
122 {Name: "foo/bar", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
123 {Name: "foo.go", Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d")},
124 },
125 }
126
127 ch, err := merkletrie.DiffTree(NewRootNode(indexA), NewRootNode(indexB), isEquals)
128 c.Assert(err, IsNil)
129 c.Assert(ch, HasLen, 1)
130 }
131
132 var empty = make([]byte, 24)
133
134 func isEquals(a, b noder.Hasher) bool {
135 if bytes.Equal(a.Hash(), empty) || bytes.Equal(b.Hash(), empty) {
136 return false
137 }
138
139 return bytes.Equal(a.Hash(), b.Hash())
140 }