summaryrefslogtreecommitdiff
path: root/tests/internals/yaml.py
blob: e25cf70b3e31810d3ba52af0a7a4b6d59fce722b (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
import os
from io import StringIO

import pytest

from buildstream import _yaml, Node, ProvenanceInformation, SequenceNode
from buildstream._exceptions import LoadError, LoadErrorReason


DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "yaml",)


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_load_yaml(datafiles):

    filename = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")

    loaded = _yaml.load(filename)
    assert loaded.get_str("kind") == "pony"


def assert_provenance(filename, line, col, node):
    provenance = node.get_provenance()

    assert isinstance(provenance, ProvenanceInformation)

    assert provenance._shortname == filename
    assert provenance._line == line
    assert provenance._col == col


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_basic_provenance(datafiles):

    filename = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")

    loaded = _yaml.load(filename)
    assert loaded.get_str("kind") == "pony"

    assert_provenance(filename, 1, 0, loaded)


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_member_provenance(datafiles):

    filename = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")

    loaded = _yaml.load(filename)
    assert loaded.get_str("kind") == "pony"
    assert_provenance(filename, 2, 13, loaded.get_scalar("description"))


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_element_provenance(datafiles):

    filename = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")

    loaded = _yaml.load(filename)
    assert loaded.get_str("kind") == "pony"
    assert_provenance(filename, 5, 2, loaded.get_sequence("moods").scalar_at(1))


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_mapping_validate_keys(datafiles):

    valid = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")
    invalid = os.path.join(datafiles.dirname, datafiles.basename, "invalid.yaml")

    base = _yaml.load(valid)

    base.validate_keys(["kind", "description", "moods", "children", "extra"])

    base = _yaml.load(invalid)

    with pytest.raises(LoadError) as exc:
        base.validate_keys(["kind", "description", "moods", "children", "extra"])

    assert exc.value.reason == LoadErrorReason.INVALID_DATA


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_node_get(datafiles):

    filename = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")

    base = _yaml.load(filename)
    assert base.get_str("kind") == "pony"

    children = base.get_sequence("children")
    assert isinstance(children, SequenceNode)
    assert len(children) == 7

    child = base.get_sequence("children").mapping_at(6)
    assert_provenance(filename, 20, 8, child.get_scalar("mood"))

    extra = base.get_mapping("extra")
    with pytest.raises(LoadError) as exc:
        extra.get_mapping("old")

    assert exc.value.reason == LoadErrorReason.INVALID_DATA


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_node_set(datafiles):

    filename = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")

    base = _yaml.load(filename)

    assert "mother" not in base
    base["mother"] = "snow white"
    assert base.get_str("mother") == "snow white"


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_node_set_overwrite(datafiles):

    filename = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")

    base = _yaml.load(filename)

    # Overwrite a string
    assert base.get_str("kind") == "pony"
    base["kind"] = "cow"
    assert base.get_str("kind") == "cow"

    # Overwrite a list as a string
    assert base.get_str_list("moods") == ["happy", "sad"]
    base["moods"] = "unemotional"
    assert base.get_str("moods") == "unemotional"


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_node_set_list_element(datafiles):

    filename = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")

    base = _yaml.load(filename)

    assert base.get_str_list("moods") == ["happy", "sad"]
    base.get_sequence("moods")[0] = "confused"

    assert base.get_str_list("moods") == ["confused", "sad"]


# Really this is testing _yaml.node_copy(), we want to
# be sure that compositing values still preserves the original
# values in the copied dict.
#
@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_composite_preserve_originals(datafiles):

    filename = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")
    overlayfile = os.path.join(datafiles.dirname, datafiles.basename, "composite.yaml")

    base = _yaml.load(filename)
    overlay = _yaml.load(overlayfile)
    base_copy = base.clone()
    overlay._composite(base_copy)

    copy_extra = base_copy.get_mapping("extra")
    orig_extra = base.get_mapping("extra")

    # Test that the node copy has the overridden value...
    assert copy_extra.get_str("old") == "override"

    # But the original node is not effected by the override.
    assert orig_extra.get_str("old") == "new"


# Tests for list composition
#
# Each test composits a filename on top of basics.yaml, and tests
# the toplevel children list at the specified index
#
# Parameters:
#    filename: The file to composite on top of basics.yaml
#    index: The index in the children list
#    length: The expected length of the children list
#    mood: The expected value of the mood attribute of the dictionary found at index in children
#    prov_file: The expected provenance filename of "mood"
#    prov_line: The expected provenance line of "mood"
#    prov_col: The expected provenance column of "mood"
#
@pytest.mark.datafiles(os.path.join(DATA_DIR))
@pytest.mark.parametrize(
    "filename,index,length,mood,prov_file,prov_line,prov_col",
    [
        # Test results of compositing with the (<) prepend directive
        ("listprepend.yaml", 0, 9, "prepended1", "listprepend.yaml", 5, 10),
        ("listprepend.yaml", 1, 9, "prepended2", "listprepend.yaml", 7, 10),
        ("listprepend.yaml", 2, 9, "silly", "basics.yaml", 8, 8),
        ("listprepend.yaml", 8, 9, "sleepy", "basics.yaml", 20, 8),
        # Test results of compositing with the (>) append directive
        ("listappend.yaml", 7, 9, "appended1", "listappend.yaml", 5, 10),
        ("listappend.yaml", 8, 9, "appended2", "listappend.yaml", 7, 10),
        ("listappend.yaml", 0, 9, "silly", "basics.yaml", 8, 8),
        ("listappend.yaml", 6, 9, "sleepy", "basics.yaml", 20, 8),
        # Test results of compositing with both (<) and (>) directives
        (
            "listappendprepend.yaml",
            0,
            11,
            "prepended1",
            "listappendprepend.yaml",
            5,
            10,
        ),
        (
            "listappendprepend.yaml",
            1,
            11,
            "prepended2",
            "listappendprepend.yaml",
            7,
            10,
        ),
        ("listappendprepend.yaml", 2, 11, "silly", "basics.yaml", 8, 8),
        ("listappendprepend.yaml", 8, 11, "sleepy", "basics.yaml", 20, 8),
        (
            "listappendprepend.yaml",
            9,
            11,
            "appended1",
            "listappendprepend.yaml",
            10,
            10,
        ),
        (
            "listappendprepend.yaml",
            10,
            11,
            "appended2",
            "listappendprepend.yaml",
            12,
            10,
        ),
        # Test results of compositing with the (=) overwrite directive
        ("listoverwrite.yaml", 0, 2, "overwrite1", "listoverwrite.yaml", 5, 10),
        ("listoverwrite.yaml", 1, 2, "overwrite2", "listoverwrite.yaml", 7, 10),
        # Test results of compositing without any directive, implicitly overwriting
        ("implicitoverwrite.yaml", 0, 2, "overwrite1", "implicitoverwrite.yaml", 4, 8),
        ("implicitoverwrite.yaml", 1, 2, "overwrite2", "implicitoverwrite.yaml", 6, 8),
    ],
)
def test_list_composition(
    datafiles, filename, tmpdir, index, length, mood, prov_file, prov_line, prov_col
):
    base_file = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")
    overlay_file = os.path.join(datafiles.dirname, datafiles.basename, filename)

    base = _yaml.load(base_file, "basics.yaml")
    overlay = _yaml.load(overlay_file, shortname=filename)

    overlay._composite(base)

    children = base.get_sequence("children")
    assert len(children) == length
    child = children.mapping_at(index)

    assert child.get_str("mood") == mood
    assert_provenance(prov_file, prov_line, prov_col, child.get_node("mood"))


# Test that overwriting a list with an empty list works as expected.
@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_list_deletion(datafiles):
    base = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")
    overlay = os.path.join(
        datafiles.dirname, datafiles.basename, "listoverwriteempty.yaml"
    )

    base = _yaml.load(base, shortname="basics.yaml")
    overlay = _yaml.load(overlay, shortname="listoverwriteempty.yaml")
    overlay._composite(base)

    children = base.get_sequence("children")
    assert not children


# Tests for deep list composition
#
# Same as test_list_composition(), but adds an additional file
# in between so that lists are composited twice.
#
# This test will to two iterations for each parameter
# specification, expecting the same results
#
#   First iteration:
#      composited = basics.yaml & filename1
#      composited = composited & filename2
#
#   Second iteration:
#      composited = filename1 & filename2
#      composited = basics.yaml & composited
#
# Parameters:
#    filename1: The file to composite on top of basics.yaml
#    filename2: The file to composite on top of filename1
#    index: The index in the children list
#    length: The expected length of the children list
#    mood: The expected value of the mood attribute of the dictionary found at index in children
#    prov_file: The expected provenance filename of "mood"
#    prov_line: The expected provenance line of "mood"
#    prov_col: The expected provenance column of "mood"
#
@pytest.mark.datafiles(os.path.join(DATA_DIR))
@pytest.mark.parametrize(
    "filename1,filename2,index,length,mood,prov_file,prov_line,prov_col",
    [
        # Test results of compositing literal list with (>) and then (<)
        (
            "listprepend.yaml",
            "listappend.yaml",
            0,
            11,
            "prepended1",
            "listprepend.yaml",
            5,
            10,
        ),
        (
            "listprepend.yaml",
            "listappend.yaml",
            1,
            11,
            "prepended2",
            "listprepend.yaml",
            7,
            10,
        ),
        ("listprepend.yaml", "listappend.yaml", 2, 11, "silly", "basics.yaml", 8, 8),
        ("listprepend.yaml", "listappend.yaml", 8, 11, "sleepy", "basics.yaml", 20, 8),
        (
            "listprepend.yaml",
            "listappend.yaml",
            9,
            11,
            "appended1",
            "listappend.yaml",
            5,
            10,
        ),
        (
            "listprepend.yaml",
            "listappend.yaml",
            10,
            11,
            "appended2",
            "listappend.yaml",
            7,
            10,
        ),
        # Test results of compositing literal list with (<) and then (>)
        (
            "listappend.yaml",
            "listprepend.yaml",
            0,
            11,
            "prepended1",
            "listprepend.yaml",
            5,
            10,
        ),
        (
            "listappend.yaml",
            "listprepend.yaml",
            1,
            11,
            "prepended2",
            "listprepend.yaml",
            7,
            10,
        ),
        ("listappend.yaml", "listprepend.yaml", 2, 11, "silly", "basics.yaml", 8, 8),
        ("listappend.yaml", "listprepend.yaml", 8, 11, "sleepy", "basics.yaml", 20, 8),
        (
            "listappend.yaml",
            "listprepend.yaml",
            9,
            11,
            "appended1",
            "listappend.yaml",
            5,
            10,
        ),
        (
            "listappend.yaml",
            "listprepend.yaml",
            10,
            11,
            "appended2",
            "listappend.yaml",
            7,
            10,
        ),
        # Test results of compositing literal list with (>) and then (>)
        ("listappend.yaml", "secondappend.yaml", 0, 11, "silly", "basics.yaml", 8, 8),
        ("listappend.yaml", "secondappend.yaml", 6, 11, "sleepy", "basics.yaml", 20, 8),
        (
            "listappend.yaml",
            "secondappend.yaml",
            7,
            11,
            "appended1",
            "listappend.yaml",
            5,
            10,
        ),
        (
            "listappend.yaml",
            "secondappend.yaml",
            8,
            11,
            "appended2",
            "listappend.yaml",
            7,
            10,
        ),
        (
            "listappend.yaml",
            "secondappend.yaml",
            9,
            11,
            "secondappend1",
            "secondappend.yaml",
            5,
            10,
        ),
        (
            "listappend.yaml",
            "secondappend.yaml",
            10,
            11,
            "secondappend2",
            "secondappend.yaml",
            7,
            10,
        ),
        # Test results of compositing literal list with (>) and then (>)
        (
            "listprepend.yaml",
            "secondprepend.yaml",
            0,
            11,
            "secondprepend1",
            "secondprepend.yaml",
            5,
            10,
        ),
        (
            "listprepend.yaml",
            "secondprepend.yaml",
            1,
            11,
            "secondprepend2",
            "secondprepend.yaml",
            7,
            10,
        ),
        (
            "listprepend.yaml",
            "secondprepend.yaml",
            2,
            11,
            "prepended1",
            "listprepend.yaml",
            5,
            10,
        ),
        (
            "listprepend.yaml",
            "secondprepend.yaml",
            3,
            11,
            "prepended2",
            "listprepend.yaml",
            7,
            10,
        ),
        ("listprepend.yaml", "secondprepend.yaml", 4, 11, "silly", "basics.yaml", 8, 8),
        (
            "listprepend.yaml",
            "secondprepend.yaml",
            10,
            11,
            "sleepy",
            "basics.yaml",
            20,
            8,
        ),
        # Test results of compositing literal list with (>) or (<) and then another literal list
        (
            "listappend.yaml",
            "implicitoverwrite.yaml",
            0,
            2,
            "overwrite1",
            "implicitoverwrite.yaml",
            4,
            8,
        ),
        (
            "listappend.yaml",
            "implicitoverwrite.yaml",
            1,
            2,
            "overwrite2",
            "implicitoverwrite.yaml",
            6,
            8,
        ),
        (
            "listprepend.yaml",
            "implicitoverwrite.yaml",
            0,
            2,
            "overwrite1",
            "implicitoverwrite.yaml",
            4,
            8,
        ),
        (
            "listprepend.yaml",
            "implicitoverwrite.yaml",
            1,
            2,
            "overwrite2",
            "implicitoverwrite.yaml",
            6,
            8,
        ),
        # Test results of compositing literal list with (>) or (<) and then an explicit (=) overwrite
        (
            "listappend.yaml",
            "listoverwrite.yaml",
            0,
            2,
            "overwrite1",
            "listoverwrite.yaml",
            5,
            10,
        ),
        (
            "listappend.yaml",
            "listoverwrite.yaml",
            1,
            2,
            "overwrite2",
            "listoverwrite.yaml",
            7,
            10,
        ),
        (
            "listprepend.yaml",
            "listoverwrite.yaml",
            0,
            2,
            "overwrite1",
            "listoverwrite.yaml",
            5,
            10,
        ),
        (
            "listprepend.yaml",
            "listoverwrite.yaml",
            1,
            2,
            "overwrite2",
            "listoverwrite.yaml",
            7,
            10,
        ),
        # Test results of compositing literal list an explicit overwrite (=) and then with (>) or (<)
        (
            "listoverwrite.yaml",
            "listappend.yaml",
            0,
            4,
            "overwrite1",
            "listoverwrite.yaml",
            5,
            10,
        ),
        (
            "listoverwrite.yaml",
            "listappend.yaml",
            1,
            4,
            "overwrite2",
            "listoverwrite.yaml",
            7,
            10,
        ),
        (
            "listoverwrite.yaml",
            "listappend.yaml",
            2,
            4,
            "appended1",
            "listappend.yaml",
            5,
            10,
        ),
        (
            "listoverwrite.yaml",
            "listappend.yaml",
            3,
            4,
            "appended2",
            "listappend.yaml",
            7,
            10,
        ),
        (
            "listoverwrite.yaml",
            "listprepend.yaml",
            0,
            4,
            "prepended1",
            "listprepend.yaml",
            5,
            10,
        ),
        (
            "listoverwrite.yaml",
            "listprepend.yaml",
            1,
            4,
            "prepended2",
            "listprepend.yaml",
            7,
            10,
        ),
        (
            "listoverwrite.yaml",
            "listprepend.yaml",
            2,
            4,
            "overwrite1",
            "listoverwrite.yaml",
            5,
            10,
        ),
        (
            "listoverwrite.yaml",
            "listprepend.yaml",
            3,
            4,
            "overwrite2",
            "listoverwrite.yaml",
            7,
            10,
        ),
    ],
)
def test_list_composition_twice(
    datafiles,
    tmpdir,
    filename1,
    filename2,
    index,
    length,
    mood,
    prov_file,
    prov_line,
    prov_col,
):
    file_base = os.path.join(datafiles.dirname, datafiles.basename, "basics.yaml")
    file1 = os.path.join(datafiles.dirname, datafiles.basename, filename1)
    file2 = os.path.join(datafiles.dirname, datafiles.basename, filename2)

    #####################
    # Round 1 - Fight !
    #####################
    base = _yaml.load(file_base, shortname="basics.yaml")
    overlay1 = _yaml.load(file1, shortname=filename1)
    overlay2 = _yaml.load(file2, shortname=filename2)

    overlay1._composite(base)
    overlay2._composite(base)

    children = base.get_sequence("children")
    assert len(children) == length
    child = children.mapping_at(index)

    assert child.get_str("mood") == mood
    assert_provenance(prov_file, prov_line, prov_col, child.get_node("mood"))

    #####################
    # Round 2 - Fight !
    #####################
    base = _yaml.load(file_base, shortname="basics.yaml")
    overlay1 = _yaml.load(file1, shortname=filename1)
    overlay2 = _yaml.load(file2, shortname=filename2)

    overlay2._composite(overlay1)
    overlay1._composite(base)

    children = base.get_sequence("children")
    assert len(children) == length
    child = children.mapping_at(index)

    assert child.get_str("mood") == mood
    assert_provenance(prov_file, prov_line, prov_col, child.get_node("mood"))


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_convert_value_to_string(datafiles):
    conf_file = os.path.join(
        datafiles.dirname, datafiles.basename, "convert_value_to_str.yaml"
    )

    # Run file through yaml to convert it
    test_dict = _yaml.load(conf_file)

    user_config = test_dict.get_str("Test1")
    assert isinstance(user_config, str)
    assert user_config == "1_23_4"

    user_config = test_dict.get_str("Test2")
    assert isinstance(user_config, str)
    assert user_config == "1.23.4"

    user_config = test_dict.get_str("Test3")
    assert isinstance(user_config, str)
    assert user_config == "1.20"

    user_config = test_dict.get_str("Test4")
    assert isinstance(user_config, str)
    assert user_config == "OneTwoThree"


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_value_doesnt_match_expected(datafiles):
    conf_file = os.path.join(
        datafiles.dirname, datafiles.basename, "convert_value_to_str.yaml"
    )

    # Run file through yaml to convert it
    test_dict = _yaml.load(conf_file)

    with pytest.raises(LoadError) as exc:
        test_dict.get_int("Test4")
    assert exc.value.reason == LoadErrorReason.INVALID_DATA


@pytest.mark.datafiles(os.path.join(DATA_DIR))
@pytest.mark.parametrize("fromdisk", [(True), (False)])
def test_roundtrip_dump(datafiles, fromdisk):
    filename = os.path.join(
        datafiles.dirname, datafiles.basename, "roundtrip-test.yaml"
    )
    with open(filename, "r") as fh:
        rt_raw = fh.read()
    if fromdisk:
        rt_loaded = _yaml.roundtrip_load(filename)
    else:
        rt_loaded = _yaml.roundtrip_load_data(rt_raw, filename=filename)

    # Now walk the loaded data structure, checking for ints etc.
    def walk_node(node):
        for v in node.values():
            if isinstance(v, list):
                walk_list(v)
            elif isinstance(v, dict):
                walk_node(v)
            else:
                assert isinstance(v, str)

    def walk_list(l):
        for v in l:
            if isinstance(v, list):
                walk_list(v)
            elif isinstance(v, dict):
                walk_node(v)
            else:
                assert isinstance(v, str)

    walk_node(rt_loaded)

    outfile = StringIO()
    _yaml.roundtrip_dump(rt_loaded, file=outfile)
    rt_back = outfile.getvalue()

    assert rt_raw == rt_back


@pytest.mark.datafiles(os.path.join(DATA_DIR))
@pytest.mark.parametrize(
    "case", [["a", "b", "c"], ["foo", 1], ["stuff", 0, "colour"], ["bird", 0, 1],]
)
def test_node_find_target(datafiles, case):
    filename = os.path.join(datafiles.dirname, datafiles.basename, "traversal.yaml")
    # We set copy_tree in order to ensure that the nodes in `loaded`
    # are not the same nodes as in `prov.toplevel`
    loaded = _yaml.load(filename, copy_tree=True)

    prov = loaded.get_provenance()

    toplevel = prov._toplevel

    assert toplevel is not loaded

    # Walk down the node tree, with insider knowledge of how nodes are
    # laid out.  Client code should never do this.
    def _walk(node, entry, rest):
        if rest:
            if isinstance(entry, int):
                new_node = node.node_at(entry)
            else:
                new_node = node.get_node(entry)

            return _walk(new_node, rest[0], rest[1:])
        else:
            if isinstance(entry, int):
                return node.node_at(entry)
            return node.get_node(entry)

    want = _walk(loaded, case[0], case[1:])
    found_path = toplevel._find(want)

    assert case == found_path


@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_node_find_target_fails(datafiles):
    filename = os.path.join(datafiles.dirname, datafiles.basename, "traversal.yaml")
    loaded = _yaml.load(filename, copy_tree=True)

    brand_new = Node.from_dict({})

    assert loaded._find(brand_new) is None