Debian Codegraph @

utils/merkletrie/difftree_test.go

raw
1 package merkletrie_test
2
3 import (
4 "bytes"
5 ctx "context"
6 "fmt"
7 "reflect"
8 "sort"
9 "strings"
10 "testing"
11 "unicode"
12
13 "github.com/go-git/go-git/v5/utils/merkletrie"
14 "github.com/go-git/go-git/v5/utils/merkletrie/internal/fsnoder"
15
16 . "gopkg.in/check.v1"
17 )
18
19 func Test(t *testing.T) { TestingT(t) }
20
21 type DiffTreeSuite struct{}
22
23 var _ = Suite(&DiffTreeSuite{})
24
25 type diffTreeTest struct {
26 from string
27 to string
28 expected string
29 }
30
31 func (t diffTreeTest) innerRun(c *C, context string, reverse bool) {
32 comment := Commentf("\n%s", context)
33 if reverse {
34 comment = Commentf("%s [REVERSED]", comment.CheckCommentString())
35 }
36
37 a, err := fsnoder.New(t.from)
38 c.Assert(err, IsNil, comment)
39 comment = Commentf("%s\n\t from = %s", comment.CheckCommentString(), a)
40
41 b, err := fsnoder.New(t.to)
42 c.Assert(err, IsNil, comment)
43 comment = Commentf("%s\n\t to = %s", comment.CheckCommentString(), b)
44
45 expected, err := newChangesFromString(t.expected)
46 c.Assert(err, IsNil, comment)
47
48 if reverse {
49 a, b = b, a
50 expected = expected.reverse()
51 }
52 comment = Commentf("%s\n\texpected = %s", comment.CheckCommentString(), expected)
53
54 results, err := merkletrie.DiffTree(a, b, fsnoder.HashEqual)
55 c.Assert(err, IsNil, comment)
56
57 obtained, err := newChanges(results)
58 c.Assert(err, IsNil, comment)
59
60 comment = Commentf("%s\n\tobtained = %s", comment.CheckCommentString(), obtained)
61
62 c.Assert(obtained, changesEquals, expected, comment)
63 }
64
65 func (t diffTreeTest) innerRunCtx(c *C, context string, reverse bool) {
66 comment := Commentf("\n%s", context)
67 if reverse {
68 comment = Commentf("%s [REVERSED]", comment.CheckCommentString())
69 }
70
71 a, err := fsnoder.New(t.from)
72 c.Assert(err, IsNil, comment)
73 comment = Commentf("%s\n\t from = %s", comment.CheckCommentString(), a)
74
75 b, err := fsnoder.New(t.to)
76 c.Assert(err, IsNil, comment)
77 comment = Commentf("%s\n\t to = %s", comment.CheckCommentString(), b)
78
79 expected, err := newChangesFromString(t.expected)
80 c.Assert(err, IsNil, comment)
81
82 if reverse {
83 a, b = b, a
84 expected = expected.reverse()
85 }
86 comment = Commentf("%s\n\texpected = %s", comment.CheckCommentString(), expected)
87
88 results, err := merkletrie.DiffTreeContext(ctx.Background(), a, b, fsnoder.HashEqual)
89 c.Assert(err, IsNil, comment)
90
91 obtained, err := newChanges(results)
92 c.Assert(err, IsNil, comment)
93
94 comment = Commentf("%s\n\tobtained = %s", comment.CheckCommentString(), obtained)
95
96 c.Assert(obtained, changesEquals, expected, comment)
97 }
98
99 func (t diffTreeTest) run(c *C, context string) {
100 t.innerRun(c, context, false)
101 t.innerRun(c, context, true)
102 t.innerRunCtx(c, context, false)
103 t.innerRunCtx(c, context, true)
104 }
105
106 type change struct {
107 merkletrie.Action
108 path string
109 }
110
111 func (c change) String() string {
112 return fmt.Sprintf("<%s %s>", c.Action, c.path)
113 }
114
115 func (c change) reverse() change {
116 ret := change{
117 path: c.path,
118 }
119
120 switch c.Action {
121 case merkletrie.Insert:
122 ret.Action = merkletrie.Delete
123 case merkletrie.Delete:
124 ret.Action = merkletrie.Insert
125 case merkletrie.Modify:
126 ret.Action = merkletrie.Modify
127 default:
128 panic(fmt.Sprintf("unknown action type: %d", c.Action))
129 }
130
131 return ret
132 }
133
134 type changes []change
135
136 func newChanges(original merkletrie.Changes) (changes, error) {
137 ret := make(changes, len(original))
138 for i, c := range original {
139 action, err := c.Action()
140 if err != nil {
141 return nil, err
142 }
143 switch action {
144 case merkletrie.Insert:
145 ret[i] = change{
146 Action: merkletrie.Insert,
147 path: c.To.String(),
148 }
149 case merkletrie.Delete:
150 ret[i] = change{
151 Action: merkletrie.Delete,
152 path: c.From.String(),
153 }
154 case merkletrie.Modify:
155 ret[i] = change{
156 Action: merkletrie.Modify,
157 path: c.From.String(),
158 }
159 default:
160 panic(fmt.Sprintf("unsupported action %d", action))
161 }
162 }
163
164 return ret, nil
165 }
166
167 func newChangesFromString(s string) (changes, error) {
168 ret := make([]change, 0)
169
170 s = strings.TrimSpace(s)
171 s = removeDuplicatedSpace(s)
172 s = turnSpaceIntoLiteralSpace(s)
173
174 if s == "" {
175 return ret, nil
176 }
177
178 for _, chunk := range strings.Split(s, " ") {
179 change := change{
180 path: chunk[1:],
181 }
182
183 switch chunk[0] {
184 case '+':
185 change.Action = merkletrie.Insert
186 case '-':
187 change.Action = merkletrie.Delete
188 case '*':
189 change.Action = merkletrie.Modify
190 default:
191 panic(fmt.Sprintf("unsupported action descriptor %q", chunk[0]))
192 }
193
194 ret = append(ret, change)
195 }
196
197 return ret, nil
198 }
199
200 func removeDuplicatedSpace(s string) string {
201 var buf bytes.Buffer
202
203 var lastWasSpace, currentIsSpace bool
204 for _, r := range s {
205 currentIsSpace = unicode.IsSpace(r)
206
207 if lastWasSpace && currentIsSpace {
208 continue
209 }
210 lastWasSpace = currentIsSpace
211
212 buf.WriteRune(r)
213 }
214
215 return buf.String()
216 }
217
218 func turnSpaceIntoLiteralSpace(s string) string {
219 return strings.Map(
220 func(r rune) rune {
221 if unicode.IsSpace(r) {
222 return ' '
223 }
224 return r
225 }, s)
226 }
227
228 func (cc changes) Len() int { return len(cc) }
229 func (cc changes) Swap(i, j int) { cc[i], cc[j] = cc[j], cc[i] }
230 func (cc changes) Less(i, j int) bool { return strings.Compare(cc[i].String(), cc[j].String()) < 0 }
231
232 func (cc changes) equals(other changes) bool {
233 sort.Sort(cc)
234 sort.Sort(other)
235 return reflect.DeepEqual(cc, other)
236 }
237
238 func (cc changes) String() string {
239 var buf bytes.Buffer
240 fmt.Fprintf(&buf, "len(%d) [", len(cc))
241 sep := ""
242 for _, c := range cc {
243 fmt.Fprintf(&buf, "%s%s", sep, c)
244 sep = ", "
245 }
246 buf.WriteByte(']')
247 return buf.String()
248 }
249
250 func (cc changes) reverse() changes {
251 ret := make(changes, len(cc))
252 for i, c := range cc {
253 ret[i] = c.reverse()
254 }
255
256 return ret
257 }
258
259 type changesEqualsChecker struct {
260 *CheckerInfo
261 }
262
263 var changesEquals Checker = &changesEqualsChecker{
264 &CheckerInfo{Name: "changesEquals", Params: []string{"obtained", "expected"}},
265 }
266
267 func (checker *changesEqualsChecker) Check(params []interface{}, names []string) (result bool, error string) {
268 a, ok := params[0].(changes)
269 if !ok {
270 return false, "first parameter must be a changes"
271 }
272 b, ok := params[1].(changes)
273 if !ok {
274 return false, "second parameter must be a changes"
275 }
276
277 return a.equals(b), ""
278 }
279
280 func do(c *C, list []diffTreeTest) {
281 for i, t := range list {
282 t.run(c, fmt.Sprintf("test #%d:", i))
283 }
284 }
285
286 func (s *DiffTreeSuite) TestEmptyVsEmpty(c *C) {
287 do(c, []diffTreeTest{
288 {"()", "()", ""},
289 {"A()", "A()", ""},
290 {"A()", "()", ""},
291 {"A()", "B()", ""},
292 })
293 }
294
295 func (s *DiffTreeSuite) TestBasicCases(c *C) {
296 do(c, []diffTreeTest{
297 {"()", "()", ""},
298 {"()", "(a<>)", "+a"},
299 {"()", "(a<1>)", "+a"},
300 {"()", "(a())", ""},
301 {"()", "(a(b()))", ""},
302 {"()", "(a(b<>))", "+a/b"},
303 {"()", "(a(b<1>))", "+a/b"},
304 {"(a<>)", "(a<>)", ""},
305 {"(a<>)", "(a<1>)", "*a"},
306 {"(a<>)", "(a())", "-a"},
307 {"(a<>)", "(a(b()))", "-a"},
308 {"(a<>)", "(a(b<>))", "-a +a/b"},
309 {"(a<>)", "(a(b<1>))", "-a +a/b"},
310 {"(a<>)", "(c())", "-a"},
311 {"(a<>)", "(c(b()))", "-a"},
312 {"(a<>)", "(c(b<>))", "-a +c/b"},
313 {"(a<>)", "(c(b<1>))", "-a +c/b"},
314 {"(a<>)", "(c(a()))", "-a"},
315 {"(a<>)", "(c(a<>))", "-a +c/a"},
316 {"(a<>)", "(c(a<1>))", "-a +c/a"},
317 {"(a<1>)", "(a<1>)", ""},
318 {"(a<1>)", "(a<2>)", "*a"},
319 {"(a<1>)", "(b<1>)", "-a +b"},
320 {"(a<1>)", "(b<2>)", "-a +b"},
321 {"(a<1>)", "(a())", "-a"},
322 {"(a<1>)", "(a(b()))", "-a"},
323 {"(a<1>)", "(a(b<>))", "-a +a/b"},
324 {"(a<1>)", "(a(b<1>))", "-a +a/b"},
325 {"(a<1>)", "(a(b<2>))", "-a +a/b"},
326 {"(a<1>)", "(c())", "-a"},
327 {"(a<1>)", "(c(b()))", "-a"},
328 {"(a<1>)", "(c(b<>))", "-a +c/b"},
329 {"(a<1>)", "(c(b<1>))", "-a +c/b"},
330 {"(a<1>)", "(c(b<2>))", "-a +c/b"},
331 {"(a<1>)", "(c())", "-a"},
332 {"(a<1>)", "(c(a()))", "-a"},
333 {"(a<1>)", "(c(a<>))", "-a +c/a"},
334 {"(a<1>)", "(c(a<1>))", "-a +c/a"},
335 {"(a<1>)", "(c(a<2>))", "-a +c/a"},
336 {"(a())", "(a())", ""},
337 {"(a())", "(b())", ""},
338 {"(a())", "(a(b()))", ""},
339 {"(a())", "(b(a()))", ""},
340 {"(a())", "(a(b<>))", "+a/b"},
341 {"(a())", "(a(b<1>))", "+a/b"},
342 {"(a())", "(b(a<>))", "+b/a"},
343 {"(a())", "(b(a<1>))", "+b/a"},
344 })
345 }
346
347 func (s *DiffTreeSuite) TestHorizontals(c *C) {
348 do(c, []diffTreeTest{
349 {"()", "(a<> b<>)", "+a +b"},
350 {"()", "(a<> b<1>)", "+a +b"},
351 {"()", "(a<> b())", "+a"},
352 {"()", "(a() b<>)", "+b"},
353 {"()", "(a<1> b<>)", "+a +b"},
354 {"()", "(a<1> b<1>)", "+a +b"},
355 {"()", "(a<1> b<2>)", "+a +b"},
356 {"()", "(a<1> b())", "+a"},
357 {"()", "(a() b<1>)", "+b"},
358 {"()", "(a() b())", ""},
359 {"()", "(a<> b<> c<> d<>)", "+a +b +c +d"},
360 {"()", "(a<> b<1> c() d<> e<2> f())", "+a +b +d +e"},
361 })
362 }
363
364 func (s *DiffTreeSuite) TestVerticals(c *C) {
365 do(c, []diffTreeTest{
366 {"()", "(z<>)", "+z"},
367 {"()", "(a(z<>))", "+a/z"},
368 {"()", "(a(b(z<>)))", "+a/b/z"},
369 {"()", "(a(b(c(z<>))))", "+a/b/c/z"},
370 {"()", "(a(b(c(d(z<>)))))", "+a/b/c/d/z"},
371 {"()", "(a(b(c(d(z<1>)))))", "+a/b/c/d/z"},
372 })
373 }
374
375 func (s *DiffTreeSuite) TestSingleInserts(c *C) {
376 do(c, []diffTreeTest{
377 {"()", "(z<>)", "+z"},
378 {"(a())", "(a(z<>))", "+a/z"},
379 {"(a())", "(a(b(z<>)))", "+a/b/z"},
380 {"(a(b(c())))", "(a(b(c(z<>))))", "+a/b/c/z"},
381 {"(a<> b<> c<>)", "(a<> b<> c<> z<>)", "+z"},
382 {"(a(b<> c<> d<>))", "(a(b<> c<> d<> z<>))", "+a/z"},
383 {"(a(b(c<> d<> e<>)))", "(a(b(c<> d<> e<> z<>)))", "+a/b/z"},
384 {"(a(b<>) f<>)", "(a(b<>) f<> z<>)", "+z"},
385 {"(a(b<>) f<>)", "(a(b<> z<>) f<>)", "+a/z"},
386 })
387 }
388
389 func (s *DiffTreeSuite) TestDebug(c *C) {
390 do(c, []diffTreeTest{
391 {"(a(b<>) f<>)", "(a(b<> z<>) f<>)", "+a/z"},
392 })
393 }
394
395 // root
396 // / | \
397 // / | ----
398 // f d h --------
399 // /\ / \ |
400 // e a j b/ g
401 // | / \ |
402 // l n k icm
403 // |
404 // o
405 // |
406 // p/
407 func (s *DiffTreeSuite) TestCrazy(c *C) {
408 crazy := "(f(e(l<1>) a(n(o(p())) k<1>)) d<1> h(j(i<1> c<2> m<>) b() g<>))"
409 do(c, []diffTreeTest{
410 {
411 crazy,
412 "()",
413 "-d -f/e/l -f/a/k -h/j/i -h/j/c -h/j/m -h/g",
414 }, {
415 crazy,
416 crazy,
417 "",
418 }, {
419 crazy,
420 "(d<1>)",
421 "-f/e/l -f/a/k -h/j/i -h/j/c -h/j/m -h/g",
422 }, {
423 crazy,
424 "(d<1> h(b() g<>))",
425 "-f/e/l -f/a/k -h/j/i -h/j/c -h/j/m",
426 }, {
427 crazy,
428 "(d<1> f(e(l()) a()) h(b() g<>))",
429 "-f/e/l -f/a/k -h/j/i -h/j/c -h/j/m",
430 }, {
431 crazy,
432 "(d<1> f(e(l<1>) a()) h(b() g<>))",
433 "-f/a/k -h/j/i -h/j/c -h/j/m",
434 }, {
435 crazy,
436 "(d<2> f(e(l<2>) a(s(t<1>))) h(b() g<> r<> j(i<> c<3> m<>)))",
437 "+f/a/s/t +h/r -f/a/k *d *f/e/l *h/j/c *h/j/i",
438 }, {
439 crazy,
440 "(f(e(l<2>) a(n(o(p<1>)) k<>)) h(j(i<1> c<2> m<>) b() g<>))",
441 "*f/e/l +f/a/n/o/p *f/a/k -d",
442 }, {
443 crazy,
444 "(f(e(l<1>) a(n(o(p(r<1>))) k<1>)) d<1> h(j(i<1> c<2> b() m<>) g<1>))",
445 "+f/a/n/o/p/r *h/g",
446 },
447 })
448 }
449
450 func (s *DiffTreeSuite) TestSameNames(c *C) {
451 do(c, []diffTreeTest{
452 {
453 "(a(a(a<>)))",
454 "(a(a(a<1>)))",
455 "*a/a/a",
456 }, {
457 "(a(b(a<>)))",
458 "(a(b(a<>)) b(a<>))",
459 "+b/a",
460 }, {
461 "(a(b(a<>)))",
462 "(a(b()) b(a<>))",
463 "-a/b/a +b/a",
464 },
465 })
466 }
467
468 func (s *DiffTreeSuite) TestIssue275(c *C) {
469 do(c, []diffTreeTest{
470 {
471 "(a(b(c.go<1>) b.go<2>))",
472 "(a(b(c.go<1> d.go<3>) b.go<2>))",
473 "+a/b/d.go",
474 },
475 })
476 }
477
478 func (s *DiffTreeSuite) TestIssue1057(c *C) {
479 p1 := "TestAppWithUnicodéPath"
480 p2 := "TestAppWithUnicodéPath"
481 c.Assert(p1 == p2, Equals, false)
482 do(c, []diffTreeTest{
483 {
484 fmt.Sprintf("(%s(x.go<1>))", p1),
485 fmt.Sprintf("(%s(x.go<1>) %s(x.go<1>))", p1, p2),
486 fmt.Sprintf("+%s/x.go", p2),
487 },
488 })
489 // swap p1 with p2
490 do(c, []diffTreeTest{
491 {
492 fmt.Sprintf("(%s(x.go<1>))", p2),
493 fmt.Sprintf("(%s(x.go<1>) %s(x.go<1>))", p1, p2),
494 fmt.Sprintf("+%s/x.go", p1),
495 },
496 })
497 }
498
499 func (s *DiffTreeSuite) TestCancel(c *C) {
500 t := diffTreeTest{"()", "(a<> b<1> c() d<> e<2> f())", "+a +b +d +e"}
501 comment := Commentf("\n%s", "test cancel:")
502
503 a, err := fsnoder.New(t.from)
504 c.Assert(err, IsNil, comment)
505 comment = Commentf("%s\n\t from = %s", comment.CheckCommentString(), a)
506
507 b, err := fsnoder.New(t.to)
508 c.Assert(err, IsNil, comment)
509 comment = Commentf("%s\n\t to = %s", comment.CheckCommentString(), b)
510
511 expected, err := newChangesFromString(t.expected)
512 c.Assert(err, IsNil, comment)
513
514 comment = Commentf("%s\n\texpected = %s", comment.CheckCommentString(), expected)
515 context, cancel := ctx.WithCancel(ctx.Background())
516 cancel()
517 results, err := merkletrie.DiffTreeContext(context, a, b, fsnoder.HashEqual)
518 c.Assert(results, IsNil, comment)
519 c.Assert(err, ErrorMatches, "operation canceled")
520
521 }