summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/manifestparser/tests/test_filters.py
blob: 158741205ef50d7055aa806155134e76bee352ee (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
#!/usr/bin/env python

import os
from copy import deepcopy
from pprint import pprint

import mozpack.path as mozpath
import mozunit
import pytest
from manifestparser.filters import (
    enabled,
    fail_if,
    failures,
    filterlist,
    pathprefix,
    run_if,
    skip_if,
    subsuite,
    tags,
)

here = os.path.dirname(os.path.abspath(__file__))


def test_data_model():
    def foo(x, y):
        return x

    def bar(x, y):
        return x

    def baz(x, y):
        return x

    fl = filterlist()

    fl.extend([foo, bar])
    assert len(fl) == 2
    assert foo in fl

    fl.append(baz)
    assert fl[2] == baz

    fl.remove(baz)
    assert baz not in fl

    item = fl.pop()
    assert item == bar

    assert fl.index(foo) == 0

    del fl[0]
    assert foo not in fl
    with pytest.raises(IndexError):
        fl[0]


def test_add_non_callable_to_list():
    fl = filterlist()
    with pytest.raises(TypeError):
        fl.append("foo")


def test_add_duplicates_to_list():
    def foo(x, y):
        return x

    def bar(x, y):
        return x

    sub = subsuite("foo")
    fl = filterlist([foo, bar, sub])
    assert len(fl) == 3
    assert fl[0] == foo

    with pytest.raises(ValueError):
        fl.append(foo)

    with pytest.raises(ValueError):
        fl.append(subsuite("bar"))


def test_add_two_tags_filters():
    tag1 = tags("foo")
    tag2 = tags("bar")
    fl = filterlist([tag1])

    with pytest.raises(ValueError):
        fl.append(tag1)

    fl.append(tag2)
    assert len(fl) == 2


def test_filters_run_in_order():
    def a(x, y):
        return x

    def b(x, y):
        return x

    def c(x, y):
        return x

    def d(x, y):
        return x

    def e(x, y):
        return x

    def f(x, y):
        return x

    fl = filterlist([a, b])
    fl.append(c)
    fl.extend([d, e])
    fl += [f]
    assert [i for i in fl] == [a, b, c, d, e, f]


@pytest.fixture(scope="module")
def create_tests():
    def inner(*paths, **defaults):
        tests = []
        for p in paths:
            path = p
            if isinstance(path, tuple):
                path, kwargs = path
            else:
                kwargs = {}

            path = mozpath.normpath(path)
            manifest = kwargs.pop(
                "manifest",
                defaults.pop(
                    "manifest", mozpath.join(mozpath.dirname(path), "manifest.ini")
                ),
            )
            test = {
                "name": mozpath.basename(path),
                "path": "/root/" + path,
                "relpath": path,
                "manifest": "/root/" + manifest,
                "manifest_relpath": manifest,
            }
            test.update(**defaults)
            test.update(**kwargs)
            tests.append(test)

        # dump tests to stdout for easier debugging on failure
        print("The 'create_tests' fixture returned:")
        pprint(tests, indent=2)
        return tests

    return inner


@pytest.fixture
def tests(create_tests):
    return create_tests(
        "test0",
        ("test1", {"skip-if": "foo == 'bar'\nintermittent&&!debug"}),
        ("test2", {"run-if": "foo == 'bar'"}),
        ("test3", {"fail-if": "foo == 'bar'"}),
        ("test4", {"disabled": "some reason"}),
        ("test5", {"subsuite": "baz"}),
        ("test6", {"subsuite": "baz,foo == 'bar'"}),
        ("test7", {"tags": "foo bar"}),
        (
            "test8",
            {"skip-if": "\nbaz\nfoo == 'bar'\nfoo == 'baz'\nintermittent && debug"},
        ),
    )


def test_skip_if(tests):
    ref = deepcopy(tests)
    tests = list(skip_if(tests, {}))
    assert len(tests) == len(ref)

    tests = deepcopy(ref)
    tests = list(skip_if(tests, {"foo": "bar"}))
    assert "disabled" in tests[1]
    assert "disabled" in tests[8]


def test_run_if(tests):
    ref = deepcopy(tests)
    tests = list(run_if(tests, {}))
    assert "disabled" in tests[2]

    tests = deepcopy(ref)
    tests = list(run_if(tests, {"foo": "bar"}))
    assert "disabled" not in tests[2]


def test_fail_if(tests):
    ref = deepcopy(tests)
    tests = list(fail_if(tests, {}))
    assert "expected" not in tests[3]

    tests = deepcopy(ref)
    tests = list(fail_if(tests, {"foo": "bar"}))
    assert tests[3]["expected"] == "fail"


def test_enabled(tests):
    ref = deepcopy(tests)
    tests = list(enabled(tests, {}))
    assert ref[4] not in tests


def test_subsuite(tests):
    sub1 = subsuite()
    sub2 = subsuite("baz")

    ref = deepcopy(tests)
    tests = list(sub1(tests, {}))
    assert ref[5] not in tests
    assert len(tests) == len(ref) - 1

    tests = deepcopy(ref)
    tests = list(sub2(tests, {}))
    assert len(tests) == 1
    assert ref[5] in tests


def test_subsuite_condition(tests):
    sub1 = subsuite()
    sub2 = subsuite("baz")

    ref = deepcopy(tests)

    tests = list(sub1(tests, {"foo": "bar"}))
    assert ref[5] not in tests
    assert ref[6] not in tests

    tests = deepcopy(ref)
    tests = list(sub2(tests, {"foo": "bar"}))
    assert len(tests) == 2
    assert tests[0]["name"] == "test5"
    assert tests[1]["name"] == "test6"


def test_tags(tests):
    ftags1 = tags([])
    ftags2 = tags(["bar", "baz"])

    ref = deepcopy(tests)
    tests = list(ftags1(tests, {}))
    assert len(tests) == 0

    tests = deepcopy(ref)
    tests = list(ftags2(tests, {}))
    assert len(tests) == 1
    assert ref[7] in tests


def test_failures(tests):
    ref = deepcopy(tests)
    fail1 = failures("intermittent")
    tests = list(fail1(tests, {"intermittent": True, "debug": True}))
    assert len(tests) == 1

    tests = deepcopy(ref)
    tests = list(fail1(tests, {"intermittent": True}))
    assert len(tests) == 1

    tests = deepcopy(ref)
    tests = list(fail1(tests, {}))
    assert len(tests) == 0

    tests = deepcopy(ref)
    tests = list(fail1(tests, {"intermittent": False, "debug": True}))
    assert len(tests) == 0


def test_pathprefix(create_tests):
    tests = create_tests(
        "test0",
        "subdir/test1",
        "subdir/test2",
        ("subdir/test3", {"manifest": "manifest.ini"}),
        (
            "other/test4",
            {
                "manifest": "manifest-common.toml",
                "ancestor_manifest": "other/manifest.ini",
            },
        ),
    )

    def names(items):
        return sorted(i["name"] for i in items)

    # relative directory
    prefix = pathprefix("subdir")
    filtered = prefix(tests, {})
    assert names(filtered) == ["test1", "test2", "test3"]

    # absolute directory
    prefix = pathprefix(["/root/subdir"])
    filtered = prefix(tests, {})
    assert names(filtered) == ["test1", "test2", "test3"]

    # relative manifest
    prefix = pathprefix(["subdir/manifest.ini"])
    filtered = prefix(tests, {})
    assert names(filtered) == ["test1", "test2"]

    # absolute manifest
    prefix = pathprefix(["/root/subdir/manifest.ini"])
    filtered = prefix(tests, {})
    assert names(filtered) == ["test1", "test2"]

    # mixed test and manifest
    prefix = pathprefix(["subdir/test2", "manifest.ini"])
    filtered = prefix(tests, {})
    assert names(filtered) == ["test0", "test2", "test3"]

    # relative ancestor manifest
    prefix = pathprefix(["other/manifest.ini"])
    filtered = prefix(tests, {})
    assert names(filtered) == ["test4"]

    # absolute ancestor manifest
    prefix = pathprefix(["/root/other/manifest.ini"])
    filtered = prefix(tests, {})
    assert names(filtered) == ["test4"]


if __name__ == "__main__":
    mozunit.main()