| 1 |
package merkletrie_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/go-git/go-git/v5/utils/merkletrie" |
| 9 |
"github.com/go-git/go-git/v5/utils/merkletrie/internal/fsnoder" |
| 10 |
"github.com/go-git/go-git/v5/utils/merkletrie/noder" |
| 11 |
|
| 12 |
. "gopkg.in/check.v1" |
| 13 |
) |
| 14 |
|
| 15 |
type IterSuite struct{} |
| 16 |
|
| 17 |
var _ = Suite(&IterSuite{}) |
| 18 |
|
| 19 |
// A test is a list of operations we want to perform on an iterator and |
| 20 |
// their expected results. |
| 21 |
// |
| 22 |
// The operations are expressed as a sequence of `n` and `s`, |
| 23 |
// representing the amount of next and step operations we want to call |
| 24 |
// on the iterator and their order. For example, an operations value of |
| 25 |
// "nns" means: call a `n`ext, then another `n`ext and finish with a |
| 26 |
// `s`tep. |
| 27 |
// |
| 28 |
// The expected is the full path of the noders returned by the |
| 29 |
// operations, separated by spaces. |
| 30 |
// |
| 31 |
// For instance: |
| 32 |
// |
| 33 |
// t := test{ |
| 34 |
// operations: "ns", |
| 35 |
// expected: "a a/b" |
| 36 |
// } |
| 37 |
// |
| 38 |
// means: |
| 39 |
// |
| 40 |
// - the first iterator operation has to be Next, and it must return a |
| 41 |
// node called "a" with no ancestors. |
| 42 |
// |
| 43 |
// - the second operation has to be Step, and it must return a node |
| 44 |
// called "b" with a single ancestor called "a". |
| 45 |
type test struct { |
| 46 |
operations string |
| 47 |
expected string |
| 48 |
} |
| 49 |
|
| 50 |
// Runs a test on the provided iterator, checking that the names of the |
| 51 |
// returned values are correct. If not, the treeDescription value is |
| 52 |
// printed along with information about mismatch. |
| 53 |
func (t test) run(c *C, iter *merkletrie.Iter, |
| 54 |
treeDescription string, testNumber int) { |
| 55 |
|
| 56 |
expectedChunks := strings.Split(t.expected, " ") |
| 57 |
if t.expected == "" { |
| 58 |
expectedChunks = []string{} |
| 59 |
} |
| 60 |
|
| 61 |
if len(t.operations) < len(expectedChunks) { |
| 62 |
c.Fatalf("malformed test %d: not enough operations", testNumber) |
| 63 |
return |
| 64 |
} |
| 65 |
|
| 66 |
var obtained noder.Path |
| 67 |
var err error |
| 68 |
for i, b := range t.operations { |
| 69 |
comment := Commentf("\ntree: %q\ntest #%d (%q)\noperation #%d (%q)", |
| 70 |
treeDescription, testNumber, t.operations, i, t.operations[i]) |
| 71 |
|
| 72 |
switch t.operations[i] { |
| 73 |
case 'n': |
| 74 |
obtained, err = iter.Next() |
| 75 |
if err != io.EOF { |
| 76 |
c.Assert(err, IsNil) |
| 77 |
} |
| 78 |
case 's': |
| 79 |
obtained, err = iter.Step() |
| 80 |
if err != io.EOF { |
| 81 |
c.Assert(err, IsNil) |
| 82 |
} |
| 83 |
default: |
| 84 |
c.Fatalf("unknown operation at test %d, operation %d (%c)\n", |
| 85 |
testNumber, i, b) |
| 86 |
} |
| 87 |
if i >= len(expectedChunks) { |
| 88 |
c.Assert(err, Equals, io.EOF, comment) |
| 89 |
continue |
| 90 |
} |
| 91 |
|
| 92 |
c.Assert(err, IsNil, comment) |
| 93 |
c.Assert(obtained.String(), Equals, expectedChunks[i], comment) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// A testsCollection value represents a tree and a collection of tests |
| 98 |
// we want to perform on iterators of that tree. |
| 99 |
// |
| 100 |
// Example: |
| 101 |
// |
| 102 |
// . |
| 103 |
// | |
| 104 |
// --------- |
| 105 |
// | | | |
| 106 |
// a b c |
| 107 |
// | |
| 108 |
// z |
| 109 |
// |
| 110 |
// var foo testCollection = { |
| 111 |
// tree: "(a<> b(z<>) c<>)" |
| 112 |
// tests: []test{ |
| 113 |
// {operations: "nns", expected: "a b b/z"}, |
| 114 |
// {operations: "nnn", expected: "a b c"}, |
| 115 |
// }, |
| 116 |
// } |
| 117 |
// |
| 118 |
// A new iterator will be build for each test. |
| 119 |
type testsCollection struct { |
| 120 |
tree string // a fsnoder description of a tree. |
| 121 |
tests []test // the collection of tests we want to run |
| 122 |
} |
| 123 |
|
| 124 |
// Executes all the tests in a testsCollection. |
| 125 |
func (tc testsCollection) run(c *C) { |
| 126 |
root, err := fsnoder.New(tc.tree) |
| 127 |
c.Assert(err, IsNil) |
| 128 |
|
| 129 |
for i, t := range tc.tests { |
| 130 |
iter, err := merkletrie.NewIter(root) |
| 131 |
c.Assert(err, IsNil) |
| 132 |
t.run(c, iter, root.String(), i) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func (s *IterSuite) TestEmptyNamedDir(c *C) { |
| 137 |
tc := testsCollection{ |
| 138 |
tree: "A()", |
| 139 |
tests: []test{ |
| 140 |
{operations: "n", expected: ""}, |
| 141 |
{operations: "nn", expected: ""}, |
| 142 |
{operations: "nnn", expected: ""}, |
| 143 |
{operations: "nnns", expected: ""}, |
| 144 |
{operations: "nnnssnsnns", expected: ""}, |
| 145 |
{operations: "s", expected: ""}, |
| 146 |
{operations: "ss", expected: ""}, |
| 147 |
{operations: "sss", expected: ""}, |
| 148 |
{operations: "sssn", expected: ""}, |
| 149 |
{operations: "sssnnsnssn", expected: ""}, |
| 150 |
}, |
| 151 |
} |
| 152 |
tc.run(c) |
| 153 |
} |
| 154 |
|
| 155 |
func (s *IterSuite) TestEmptyUnnamedDir(c *C) { |
| 156 |
tc := testsCollection{ |
| 157 |
tree: "()", |
| 158 |
tests: []test{ |
| 159 |
{operations: "n", expected: ""}, |
| 160 |
{operations: "nn", expected: ""}, |
| 161 |
{operations: "nnn", expected: ""}, |
| 162 |
{operations: "nnns", expected: ""}, |
| 163 |
{operations: "nnnssnsnns", expected: ""}, |
| 164 |
{operations: "s", expected: ""}, |
| 165 |
{operations: "ss", expected: ""}, |
| 166 |
{operations: "sss", expected: ""}, |
| 167 |
{operations: "sssn", expected: ""}, |
| 168 |
{operations: "sssnnsnssn", expected: ""}, |
| 169 |
}, |
| 170 |
} |
| 171 |
tc.run(c) |
| 172 |
} |
| 173 |
|
| 174 |
func (s *IterSuite) TestOneFile(c *C) { |
| 175 |
tc := testsCollection{ |
| 176 |
tree: "(a<>)", |
| 177 |
tests: []test{ |
| 178 |
{operations: "n", expected: "a"}, |
| 179 |
{operations: "nn", expected: "a"}, |
| 180 |
{operations: "nnn", expected: "a"}, |
| 181 |
{operations: "nnns", expected: "a"}, |
| 182 |
{operations: "nnnssnsnns", expected: "a"}, |
| 183 |
{operations: "s", expected: "a"}, |
| 184 |
{operations: "ss", expected: "a"}, |
| 185 |
{operations: "sss", expected: "a"}, |
| 186 |
{operations: "sssn", expected: "a"}, |
| 187 |
{operations: "sssnnsnssn", expected: "a"}, |
| 188 |
}, |
| 189 |
} |
| 190 |
tc.run(c) |
| 191 |
} |
| 192 |
|
| 193 |
// root |
| 194 |
// / \ |
| 195 |
// a b |
| 196 |
func (s *IterSuite) TestTwoFiles(c *C) { |
| 197 |
tc := testsCollection{ |
| 198 |
tree: "(a<> b<>)", |
| 199 |
tests: []test{ |
| 200 |
{operations: "nnn", expected: "a b"}, |
| 201 |
{operations: "nns", expected: "a b"}, |
| 202 |
{operations: "nsn", expected: "a b"}, |
| 203 |
{operations: "nss", expected: "a b"}, |
| 204 |
{operations: "snn", expected: "a b"}, |
| 205 |
{operations: "sns", expected: "a b"}, |
| 206 |
{operations: "ssn", expected: "a b"}, |
| 207 |
{operations: "sss", expected: "a b"}, |
| 208 |
}, |
| 209 |
} |
| 210 |
tc.run(c) |
| 211 |
} |
| 212 |
|
| 213 |
// root |
| 214 |
// | |
| 215 |
// a |
| 216 |
// | |
| 217 |
// b |
| 218 |
func (s *IterSuite) TestDirWithFile(c *C) { |
| 219 |
tc := testsCollection{ |
| 220 |
tree: "(a(b<>))", |
| 221 |
tests: []test{ |
| 222 |
{operations: "nnn", expected: "a"}, |
| 223 |
{operations: "nns", expected: "a"}, |
| 224 |
{operations: "nsn", expected: "a a/b"}, |
| 225 |
{operations: "nss", expected: "a a/b"}, |
| 226 |
{operations: "snn", expected: "a"}, |
| 227 |
{operations: "sns", expected: "a"}, |
| 228 |
{operations: "ssn", expected: "a a/b"}, |
| 229 |
{operations: "sss", expected: "a a/b"}, |
| 230 |
}, |
| 231 |
} |
| 232 |
tc.run(c) |
| 233 |
} |
| 234 |
|
| 235 |
// root |
| 236 |
// /|\ |
| 237 |
// c a b |
| 238 |
func (s *IterSuite) TestThreeSiblings(c *C) { |
| 239 |
tc := testsCollection{ |
| 240 |
tree: "(c<> a<> b<>)", |
| 241 |
tests: []test{ |
| 242 |
{operations: "nnnn", expected: "a b c"}, |
| 243 |
{operations: "nnns", expected: "a b c"}, |
| 244 |
{operations: "nnsn", expected: "a b c"}, |
| 245 |
{operations: "nnss", expected: "a b c"}, |
| 246 |
{operations: "nsnn", expected: "a b c"}, |
| 247 |
{operations: "nsns", expected: "a b c"}, |
| 248 |
{operations: "nssn", expected: "a b c"}, |
| 249 |
{operations: "nsss", expected: "a b c"}, |
| 250 |
{operations: "snnn", expected: "a b c"}, |
| 251 |
{operations: "snns", expected: "a b c"}, |
| 252 |
{operations: "snsn", expected: "a b c"}, |
| 253 |
{operations: "snss", expected: "a b c"}, |
| 254 |
{operations: "ssnn", expected: "a b c"}, |
| 255 |
{operations: "ssns", expected: "a b c"}, |
| 256 |
{operations: "sssn", expected: "a b c"}, |
| 257 |
{operations: "ssss", expected: "a b c"}, |
| 258 |
}, |
| 259 |
} |
| 260 |
tc.run(c) |
| 261 |
} |
| 262 |
|
| 263 |
// root |
| 264 |
// | |
| 265 |
// b |
| 266 |
// | |
| 267 |
// c |
| 268 |
// | |
| 269 |
// a |
| 270 |
func (s *IterSuite) TestThreeVertical(c *C) { |
| 271 |
tc := testsCollection{ |
| 272 |
tree: "(b(c(a())))", |
| 273 |
tests: []test{ |
| 274 |
{operations: "nnnn", expected: "b"}, |
| 275 |
{operations: "nnns", expected: "b"}, |
| 276 |
{operations: "nnsn", expected: "b"}, |
| 277 |
{operations: "nnss", expected: "b"}, |
| 278 |
{operations: "nsnn", expected: "b b/c"}, |
| 279 |
{operations: "nsns", expected: "b b/c"}, |
| 280 |
{operations: "nssn", expected: "b b/c b/c/a"}, |
| 281 |
{operations: "nsss", expected: "b b/c b/c/a"}, |
| 282 |
{operations: "snnn", expected: "b"}, |
| 283 |
{operations: "snns", expected: "b"}, |
| 284 |
{operations: "snsn", expected: "b"}, |
| 285 |
{operations: "snss", expected: "b"}, |
| 286 |
{operations: "ssnn", expected: "b b/c"}, |
| 287 |
{operations: "ssns", expected: "b b/c"}, |
| 288 |
{operations: "sssn", expected: "b b/c b/c/a"}, |
| 289 |
{operations: "ssss", expected: "b b/c b/c/a"}, |
| 290 |
}, |
| 291 |
} |
| 292 |
tc.run(c) |
| 293 |
} |
| 294 |
|
| 295 |
// root |
| 296 |
// / \ |
| 297 |
// c a |
| 298 |
// | |
| 299 |
// b |
| 300 |
func (s *IterSuite) TestThreeMix1(c *C) { |
| 301 |
tc := testsCollection{ |
| 302 |
tree: "(c(b<>) a<>)", |
| 303 |
tests: []test{ |
| 304 |
{operations: "nnnn", expected: "a c"}, |
| 305 |
{operations: "nnns", expected: "a c"}, |
| 306 |
{operations: "nnsn", expected: "a c c/b"}, |
| 307 |
{operations: "nnss", expected: "a c c/b"}, |
| 308 |
{operations: "nsnn", expected: "a c"}, |
| 309 |
{operations: "nsns", expected: "a c"}, |
| 310 |
{operations: "nssn", expected: "a c c/b"}, |
| 311 |
{operations: "nsss", expected: "a c c/b"}, |
| 312 |
{operations: "snnn", expected: "a c"}, |
| 313 |
{operations: "snns", expected: "a c"}, |
| 314 |
{operations: "snsn", expected: "a c c/b"}, |
| 315 |
{operations: "snss", expected: "a c c/b"}, |
| 316 |
{operations: "ssnn", expected: "a c"}, |
| 317 |
{operations: "ssns", expected: "a c"}, |
| 318 |
{operations: "sssn", expected: "a c c/b"}, |
| 319 |
{operations: "ssss", expected: "a c c/b"}, |
| 320 |
}, |
| 321 |
} |
| 322 |
tc.run(c) |
| 323 |
} |
| 324 |
|
| 325 |
// root |
| 326 |
// / \ |
| 327 |
// b a |
| 328 |
// | |
| 329 |
// c |
| 330 |
func (s *IterSuite) TestThreeMix2(c *C) { |
| 331 |
tc := testsCollection{ |
| 332 |
tree: "(b() a(c<>))", |
| 333 |
tests: []test{ |
| 334 |
{operations: "nnnn", expected: "a b"}, |
| 335 |
{operations: "nnns", expected: "a b"}, |
| 336 |
{operations: "nnsn", expected: "a b"}, |
| 337 |
{operations: "nnss", expected: "a b"}, |
| 338 |
{operations: "nsnn", expected: "a a/c b"}, |
| 339 |
{operations: "nsns", expected: "a a/c b"}, |
| 340 |
{operations: "nssn", expected: "a a/c b"}, |
| 341 |
{operations: "nsss", expected: "a a/c b"}, |
| 342 |
{operations: "snnn", expected: "a b"}, |
| 343 |
{operations: "snns", expected: "a b"}, |
| 344 |
{operations: "snsn", expected: "a b"}, |
| 345 |
{operations: "snss", expected: "a b"}, |
| 346 |
{operations: "ssnn", expected: "a a/c b"}, |
| 347 |
{operations: "ssns", expected: "a a/c b"}, |
| 348 |
{operations: "sssn", expected: "a a/c b"}, |
| 349 |
{operations: "ssss", expected: "a a/c b"}, |
| 350 |
}, |
| 351 |
} |
| 352 |
tc.run(c) |
| 353 |
} |
| 354 |
|
| 355 |
// root |
| 356 |
// / | \ |
| 357 |
// / | ---- |
| 358 |
// f d h -------- |
| 359 |
// /\ / \ | |
| 360 |
// e a j b/ g |
| 361 |
// | / \ | |
| 362 |
// l n k icm |
| 363 |
// | |
| 364 |
// o |
| 365 |
// | |
| 366 |
// p/ |
| 367 |
func (s *IterSuite) TestCrazy(c *C) { |
| 368 |
tc := testsCollection{ |
| 369 |
tree: "(f(e(l<>) a(n(o(p())) k<>)) d<> h(j(i<> c<> m<>) b() g<>))", |
| 370 |
tests: []test{ |
| 371 |
{operations: "nnnnn", expected: "d f h"}, |
| 372 |
{operations: "nnnns", expected: "d f h"}, |
| 373 |
{operations: "nnnsn", expected: "d f h h/b h/g"}, |
| 374 |
{operations: "nnnss", expected: "d f h h/b h/g"}, |
| 375 |
{operations: "nnsnn", expected: "d f f/a f/e h"}, |
| 376 |
{operations: "nnsns", expected: "d f f/a f/e f/e/l"}, |
| 377 |
{operations: "nnssn", expected: "d f f/a f/a/k f/a/n"}, |
| 378 |
{operations: "nnsss", expected: "d f f/a f/a/k f/a/n"}, |
| 379 |
{operations: "nsnnn", expected: "d f h"}, |
| 380 |
{operations: "nsnns", expected: "d f h"}, |
| 381 |
{operations: "nsnsn", expected: "d f h h/b h/g"}, |
| 382 |
{operations: "nsnss", expected: "d f h h/b h/g"}, |
| 383 |
{operations: "nssnn", expected: "d f f/a f/e h"}, |
| 384 |
}, |
| 385 |
} |
| 386 |
tc.run(c) |
| 387 |
} |
| 388 |
|
| 389 |
// . |
| 390 |
// | |
| 391 |
// a |
| 392 |
// | |
| 393 |
// b |
| 394 |
// / \ |
| 395 |
// z h |
| 396 |
// / \ |
| 397 |
// d e |
| 398 |
// | |
| 399 |
// f |
| 400 |
func (s *IterSuite) TestNewIterFromPath(c *C) { |
| 401 |
tree, err := fsnoder.New("(a(b(z(d<> e(f<>)) h<>)))") |
| 402 |
c.Assert(err, IsNil) |
| 403 |
|
| 404 |
z := find(c, tree, "z") |
| 405 |
|
| 406 |
iter, err := merkletrie.NewIterFromPath(z) |
| 407 |
c.Assert(err, IsNil) |
| 408 |
|
| 409 |
n, err := iter.Next() |
| 410 |
c.Assert(err, IsNil) |
| 411 |
c.Assert(n.String(), Equals, "a/b/z/d") |
| 412 |
|
| 413 |
n, err = iter.Next() |
| 414 |
c.Assert(err, IsNil) |
| 415 |
c.Assert(n.String(), Equals, "a/b/z/e") |
| 416 |
|
| 417 |
n, err = iter.Step() |
| 418 |
c.Assert(err, IsNil) |
| 419 |
c.Assert(n.String(), Equals, "a/b/z/e/f") |
| 420 |
|
| 421 |
_, err = iter.Step() |
| 422 |
c.Assert(err, Equals, io.EOF) |
| 423 |
} |
| 424 |
|
| 425 |
func find(c *C, tree noder.Noder, name string) noder.Path { |
| 426 |
iter, err := merkletrie.NewIter(tree) |
| 427 |
c.Assert(err, IsNil) |
| 428 |
|
| 429 |
for { |
| 430 |
current, err := iter.Step() |
| 431 |
if err != io.EOF { |
| 432 |
c.Assert(err, IsNil) |
| 433 |
} else { |
| 434 |
c.Fatalf("node %s not found in tree %s", name, tree) |
| 435 |
} |
| 436 |
|
| 437 |
if current.Name() == name { |
| 438 |
return current |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
type errorNoder struct{ noder.Noder } |
| 444 |
|
| 445 |
func (e *errorNoder) Children() ([]noder.Noder, error) { |
| 446 |
return nil, fmt.Errorf("mock error") |
| 447 |
} |
| 448 |
|
| 449 |
func (s *IterSuite) TestNewIterNil(c *C) { |
| 450 |
i, err := merkletrie.NewIter(nil) |
| 451 |
c.Assert(err, IsNil) |
| 452 |
_, err = i.Next() |
| 453 |
c.Assert(err, Equals, io.EOF) |
| 454 |
} |
| 455 |
|
| 456 |
func (s *IterSuite) TestNewIterFailsOnChildrenErrors(c *C) { |
| 457 |
_, err := merkletrie.NewIter(&errorNoder{}) |
| 458 |
c.Assert(err, ErrorMatches, "mock error") |
| 459 |
} |