summaryrefslogtreecommitdiff
path: root/tests/format/include_composition.py
blob: 2051f02d5972c83609921ac62ab71e47fb8229ba (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
import os
from buildstream._context import Context
from buildstream._project import Project
from buildstream._includes import Includes
from buildstream import _yaml


def make_includes(basedir):
    _yaml.dump({'name': 'test'},
               os.path.join(basedir, 'project.conf'))
    context = Context()
    project = Project(basedir, context)
    loader = project.loader
    return Includes(loader)


def test_main_has_prority(tmpdir):
    includes = make_includes(str(tmpdir))

    _yaml.dump({'(@)': ['a.yml'],
                'test': ['main']},
               str(tmpdir.join('main.yml')))

    main = _yaml.load(str(tmpdir.join('main.yml')))

    _yaml.dump({'test': ['a']},
               str(tmpdir.join('a.yml')))

    includes.process(main)

    assert _yaml.node_get(main, list, 'test') == ['main']


def test_include_cannot_append(tmpdir):
    includes = make_includes(str(tmpdir))

    _yaml.dump({'(@)': ['a.yml'],
                'test': ['main']},
               str(tmpdir.join('main.yml')))
    main = _yaml.load(str(tmpdir.join('main.yml')))

    _yaml.dump({'test': {'(>)': ['a']}},
               str(tmpdir.join('a.yml')))

    includes.process(main)

    assert _yaml.node_get(main, list, 'test') == ['main']


def test_main_can_append(tmpdir):
    includes = make_includes(str(tmpdir))

    _yaml.dump({'(@)': ['a.yml'],
                'test': {'(>)': ['main']}},
               str(tmpdir.join('main.yml')))
    main = _yaml.load(str(tmpdir.join('main.yml')))

    _yaml.dump({'test': ['a']},
               str(tmpdir.join('a.yml')))

    includes.process(main)

    assert _yaml.node_get(main, list, 'test') == ['a', 'main']


def test_sibling_cannot_append_backward(tmpdir):
    includes = make_includes(str(tmpdir))

    _yaml.dump({'(@)': ['a.yml', 'b.yml']},
               str(tmpdir.join('main.yml')))
    main = _yaml.load(str(tmpdir.join('main.yml')))

    _yaml.dump({'test': {'(>)': ['a']}},
               str(tmpdir.join('a.yml')))
    _yaml.dump({'test': ['b']},
               str(tmpdir.join('b.yml')))

    includes.process(main)

    assert _yaml.node_get(main, list, 'test') == ['b']


def test_sibling_can_append_forward(tmpdir):
    includes = make_includes(str(tmpdir))

    _yaml.dump({'(@)': ['a.yml', 'b.yml']},
               str(tmpdir.join('main.yml')))
    main = _yaml.load(str(tmpdir.join('main.yml')))

    _yaml.dump({'test': ['a']},
               str(tmpdir.join('a.yml')))
    _yaml.dump({'test': {'(>)': ['b']}},
               str(tmpdir.join('b.yml')))

    includes.process(main)

    assert _yaml.node_get(main, list, 'test') == ['a', 'b']


def test_lastest_sibling_has_priority(tmpdir):
    includes = make_includes(str(tmpdir))

    _yaml.dump({'(@)': ['a.yml', 'b.yml']},
               str(tmpdir.join('main.yml')))
    main = _yaml.load(str(tmpdir.join('main.yml')))

    _yaml.dump({'test': ['a']},
               str(tmpdir.join('a.yml')))
    _yaml.dump({'test': ['b']},
               str(tmpdir.join('b.yml')))

    includes.process(main)

    assert _yaml.node_get(main, list, 'test') == ['b']


def test_main_keeps_keys(tmpdir):
    includes = make_includes(str(tmpdir))

    _yaml.dump({'(@)': ['a.yml'],
                'something': 'else'},
               str(tmpdir.join('main.yml')))
    main = _yaml.load(str(tmpdir.join('main.yml')))

    _yaml.dump({'test': ['a']},
               str(tmpdir.join('a.yml')))

    includes.process(main)

    assert _yaml.node_get(main, list, 'test') == ['a']
    assert main.get_str('something') == 'else'


def test_overwrite_directive_on_later_composite(tmpdir):
    includes = make_includes(str(tmpdir))

    _yaml.dump({'(@)': ['a.yml', 'b.yml'],
                'test': {'(=)': ['Overwritten']}},
               str(tmpdir.join('main.yml')))

    main = _yaml.load(str(tmpdir.join('main.yml')))

    # a.yml
    _yaml.dump({'test': ['some useless', 'list', 'to be overwritten'],
                'foo': 'should not be present'},
               str(tmpdir.join('a.yml')))

    # b.yaml isn't going to have a 'test' node to overwrite
    _yaml.dump({'foo': 'should be present'},
               str(tmpdir.join('b.yml')))

    includes.process(main)

    assert _yaml.node_get(main, list, 'test') == ['Overwritten']
    assert main.get_str('foo') == 'should be present'