summaryrefslogtreecommitdiff
path: root/tests/format/dependencies.py
blob: f92b89afa2335f2bf037c4777b6060b08c26a9dc (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
# Pylint doesn't play well with fixtures and dependency injection from pytest
# pylint: disable=redefined-outer-name

import os
import pytest

from buildstream._exceptions import ErrorDomain, LoadErrorReason
from buildstream.testing import cli  # pylint: disable=unused-import

DATA_DIR = os.path.dirname(os.path.realpath(__file__))


#
# Exercising some different ways of loading the dependencies
#
@pytest.mark.datafiles(DATA_DIR)
def test_two_files(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')

    elements = cli.get_pipeline(project, ['target.bst'])
    assert elements == ['firstdep.bst', 'target.bst']


@pytest.mark.datafiles(DATA_DIR)
def test_shared_dependency(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')

    elements = cli.get_pipeline(project, ['shareddeptarget.bst'])
    assert elements == ['firstdep.bst', 'shareddep.bst', 'shareddeptarget.bst']


@pytest.mark.datafiles(DATA_DIR)
def test_dependency_dict(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')
    elements = cli.get_pipeline(project, ['target-depdict.bst'])
    assert elements == ['firstdep.bst', 'target-depdict.bst']


@pytest.mark.datafiles(DATA_DIR)
def test_invalid_dependency_declaration(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')
    result = cli.run(project=project, args=['show', 'invaliddep.bst'])
    result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.INVALID_DATA)


@pytest.mark.datafiles(DATA_DIR)
def test_invalid_dependency_type(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')
    result = cli.run(project=project, args=['show', 'invaliddeptype.bst'])
    result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.INVALID_DATA)


@pytest.mark.datafiles(DATA_DIR)
def test_invalid_strict_dependency(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')
    result = cli.run(project=project, args=['show', 'invalidstrict.bst'])
    result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.INVALID_DATA)


@pytest.mark.datafiles(DATA_DIR)
def test_invalid_non_strict_dependency(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')
    result = cli.run(project=project, args=['show', 'invalidnonstrict.bst'])
    result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.INVALID_DATA)


@pytest.mark.datafiles(DATA_DIR)
def test_circular_dependency(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')
    result = cli.run(project=project, args=['show', 'circulartarget.bst'])
    result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.CIRCULAR_DEPENDENCY)


@pytest.mark.datafiles(DATA_DIR)
def test_build_dependency(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')

    elements = cli.get_pipeline(project, ['builddep.bst'], scope='run')
    assert elements == ['builddep.bst']

    elements = cli.get_pipeline(project, ['builddep.bst'], scope='build')
    assert elements == ['firstdep.bst']


@pytest.mark.datafiles(DATA_DIR)
def test_runtime_dependency(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')
    elements = cli.get_pipeline(project, ['runtimedep.bst'], scope='build')

    # FIXME: The empty line should probably never happen here when there are no results.
    assert elements == ['']
    elements = cli.get_pipeline(project, ['runtimedep.bst'], scope='run')
    assert elements == ['firstdep.bst', 'runtimedep.bst']


@pytest.mark.datafiles(DATA_DIR)
def test_all_dependency(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')

    elements = cli.get_pipeline(project, ['alldep.bst'], scope='build')
    assert elements == ['firstdep.bst']

    elements = cli.get_pipeline(project, ['alldep.bst'], scope='run')
    assert elements == ['firstdep.bst', 'alldep.bst']


@pytest.mark.datafiles(DATA_DIR)
def test_list_build_dependency(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')

    # Check that the pipeline includes the build dependency
    deps = cli.get_pipeline(project, ['builddep-list.bst'], scope="build")
    assert "firstdep.bst" in deps


@pytest.mark.datafiles(DATA_DIR)
def test_list_runtime_dependency(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')

    # Check that the pipeline includes the runtime dependency
    deps = cli.get_pipeline(project, ['runtimedep-list.bst'], scope="run")
    assert "firstdep.bst" in deps


@pytest.mark.datafiles(DATA_DIR)
def test_list_dependencies_combined(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')

    # Check that runtime deps get combined
    rundeps = cli.get_pipeline(project, ['list-combine.bst'], scope="run")
    assert "firstdep.bst" not in rundeps
    assert "seconddep.bst" in rundeps
    assert "thirddep.bst" in rundeps

    # Check that build deps get combined
    builddeps = cli.get_pipeline(project, ['list-combine.bst'], scope="build")
    assert "firstdep.bst" in builddeps
    assert "seconddep.bst" not in builddeps
    assert "thirddep.bst" in builddeps


@pytest.mark.datafiles(DATA_DIR)
def test_list_overlap(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies1')

    # Check that dependencies get merged
    rundeps = cli.get_pipeline(project, ['list-overlap.bst'], scope="run")
    assert "firstdep.bst" in rundeps
    builddeps = cli.get_pipeline(project, ['list-overlap.bst'], scope="build")
    assert "firstdep.bst" in builddeps


#
# Testing the order of elements reported when iterating through
# Element.dependencies() with various scopes.
#
@pytest.mark.datafiles(DATA_DIR)
def test_scope_all(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies2')
    elements = ['target.bst']

    element_list = cli.get_pipeline(project, elements, scope='all')

    assert element_list == [
        "build-build.bst",
        "run-build.bst",
        "build.bst",
        "dep-one.bst",
        "run.bst",
        "dep-two.bst",
        "target.bst",
    ]


@pytest.mark.datafiles(DATA_DIR)
def test_scope_run(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies2')
    elements = ['target.bst']

    element_list = cli.get_pipeline(project, elements, scope='run')

    assert element_list == [
        "dep-one.bst",
        "run.bst",
        "dep-two.bst",
        "target.bst",
    ]


@pytest.mark.datafiles(DATA_DIR)
def test_scope_build(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies2')
    elements = ['target.bst']

    element_list = cli.get_pipeline(project, elements, scope='build')

    assert element_list == ["dep-one.bst", "run.bst", "dep-two.bst"]


@pytest.mark.datafiles(DATA_DIR)
def test_scope_build_of_child(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies2')
    elements = ['target.bst']

    element_list = cli.get_pipeline(project, elements, scope='build')

    # First pass, lets check dep-two
    element = element_list[2]

    # Pass two, let's look at these
    element_list = cli.get_pipeline(project, [element], scope='build')

    assert element_list == ["run-build.bst", "build.bst"]


@pytest.mark.datafiles(DATA_DIR)
def test_no_recurse(cli, datafiles):
    project = os.path.join(str(datafiles), 'dependencies2')
    elements = ['target.bst']

    # We abuse the 'plan' scope here to ensure that we call
    # element.dependencies() with recurse=False - currently, no `bst
    # show` option does this directly.
    element_list = cli.get_pipeline(project, elements, scope='plan')

    assert element_list == [
        'build-build.bst',
        'run-build.bst',
        'build.bst',
        'dep-one.bst',
        'run.bst',
        'dep-two.bst',
        'target.bst',
    ]


@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize(("element", "asserts"), [
    ('build-runtime', False),
    ('build-build', True),
    ('build-all', True),
    ('runtime-runtime', True),
    ('runtime-all', True),
    ('all-all', True),
])
def test_duplicate_deps(cli, datafiles, element, asserts):
    project = os.path.join(str(datafiles), 'dependencies3')

    result = cli.run(project=project, args=['show', '{}.bst'.format(element)])

    if asserts:
        result.assert_main_error(ErrorDomain.LOAD,
                                 LoadErrorReason.DUPLICATE_DEPENDENCY)
        assert '[line 10 column 2]' in result.stderr
        assert '[line 8 column 2]' in result.stderr
    else:
        result.assert_success()