summaryrefslogtreecommitdiff
path: root/tests/format/include_composition.py
blob: ec48d82a2817b5312b551c374d98d6bb31ad7fab (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
import os

from contextlib import contextmanager

from buildstream._project import Project
from buildstream._includes import Includes
from buildstream import _yaml

from tests.testutils import dummy_context


@contextmanager
def make_includes(basedir):
    _yaml.roundtrip_dump({'name': 'test'}, os.path.join(basedir, 'project.conf'))
    with dummy_context() as context:
        project = Project(basedir, context)
        loader = project.loader
        yield Includes(loader)


def test_main_has_priority(tmpdir):
    with make_includes(str(tmpdir)) as includes:

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

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

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

        includes.process(main)

        assert main.get_sequence('test').as_str_list() == ['main']


def test_include_cannot_append(tmpdir):
    with make_includes(str(tmpdir)) as includes:

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

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

        includes.process(main)

        assert main.get_sequence('test').as_str_list() == ['main']


def test_main_can_append(tmpdir):
    with make_includes(str(tmpdir)) as includes:

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

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

        includes.process(main)

        assert main.get_sequence('test').as_str_list() == ['a', 'main']


def test_sibling_cannot_append_backward(tmpdir):
    with make_includes(str(tmpdir)) as includes:

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

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

        includes.process(main)

        assert main.get_sequence('test').as_str_list() == ['b']


def test_sibling_can_append_forward(tmpdir):
    with make_includes(str(tmpdir)) as includes:

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

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

        includes.process(main)

        assert main.get_sequence('test').as_str_list() == ['a', 'b']


def test_lastest_sibling_has_priority(tmpdir):
    with make_includes(str(tmpdir)) as includes:

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

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

        includes.process(main)

        assert main.get_sequence('test').as_str_list() == ['b']


def test_main_keeps_keys(tmpdir):
    with make_includes(str(tmpdir)) as includes:

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

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

        includes.process(main)

        assert main.get_sequence('test').as_str_list() == ['a']
        assert main.get_str('something') == 'else'


def test_overwrite_directive_on_later_composite(tmpdir):
    with make_includes(str(tmpdir)) as includes:

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

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

        # a.yml
        _yaml.roundtrip_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.roundtrip_dump({'foo': 'should be present'},
                             str(tmpdir.join('b.yml')))

        includes.process(main)

        assert main.get_sequence('test').as_str_list() == ['Overwritten']
        assert main.get_str('foo') == 'should be present'