| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import os |
| 4 |
from copy import deepcopy |
| 5 |
from pprint import pprint |
| 6 |
|
| 7 |
import mozpack.path as mozpath |
| 8 |
import mozunit |
| 9 |
import pytest |
| 10 |
from manifestparser.filters import ( |
| 11 |
enabled, |
| 12 |
fail_if, |
| 13 |
failures, |
| 14 |
filterlist, |
| 15 |
pathprefix, |
| 16 |
run_if, |
| 17 |
skip_if, |
| 18 |
subsuite, |
| 19 |
tags, |
| 20 |
) |
| 21 |
|
| 22 |
here = os.path.dirname(os.path.abspath(__file__)) |
| 23 |
|
| 24 |
|
| 25 |
def test_data_model(): |
| 26 |
def foo(x, y): |
| 27 |
return x |
| 28 |
|
| 29 |
def bar(x, y): |
| 30 |
return x |
| 31 |
|
| 32 |
def baz(x, y): |
| 33 |
return x |
| 34 |
|
| 35 |
fl = filterlist() |
| 36 |
|
| 37 |
fl.extend([foo, bar]) |
| 38 |
assert len(fl) == 2 |
| 39 |
assert foo in fl |
| 40 |
|
| 41 |
fl.append(baz) |
| 42 |
assert fl[2] == baz |
| 43 |
|
| 44 |
fl.remove(baz) |
| 45 |
assert baz not in fl |
| 46 |
|
| 47 |
item = fl.pop() |
| 48 |
assert item == bar |
| 49 |
|
| 50 |
assert fl.index(foo) == 0 |
| 51 |
|
| 52 |
del fl[0] |
| 53 |
assert foo not in fl |
| 54 |
with pytest.raises(IndexError): |
| 55 |
fl[0] |
| 56 |
|
| 57 |
|
| 58 |
def test_add_non_callable_to_list(): |
| 59 |
fl = filterlist() |
| 60 |
with pytest.raises(TypeError): |
| 61 |
fl.append("foo") |
| 62 |
|
| 63 |
|
| 64 |
def test_add_duplicates_to_list(): |
| 65 |
def foo(x, y): |
| 66 |
return x |
| 67 |
|
| 68 |
def bar(x, y): |
| 69 |
return x |
| 70 |
|
| 71 |
sub = subsuite("foo") |
| 72 |
fl = filterlist([foo, bar, sub]) |
| 73 |
assert len(fl) == 3 |
| 74 |
assert fl[0] == foo |
| 75 |
|
| 76 |
with pytest.raises(ValueError): |
| 77 |
fl.append(foo) |
| 78 |
|
| 79 |
with pytest.raises(ValueError): |
| 80 |
fl.append(subsuite("bar")) |
| 81 |
|
| 82 |
|
| 83 |
def test_add_two_tags_filters(): |
| 84 |
tag1 = tags("foo") |
| 85 |
tag2 = tags("bar") |
| 86 |
fl = filterlist([tag1]) |
| 87 |
|
| 88 |
with pytest.raises(ValueError): |
| 89 |
fl.append(tag1) |
| 90 |
|
| 91 |
fl.append(tag2) |
| 92 |
assert len(fl) == 2 |
| 93 |
|
| 94 |
|
| 95 |
def test_filters_run_in_order(): |
| 96 |
def a(x, y): |
| 97 |
return x |
| 98 |
|
| 99 |
def b(x, y): |
| 100 |
return x |
| 101 |
|
| 102 |
def c(x, y): |
| 103 |
return x |
| 104 |
|
| 105 |
def d(x, y): |
| 106 |
return x |
| 107 |
|
| 108 |
def e(x, y): |
| 109 |
return x |
| 110 |
|
| 111 |
def f(x, y): |
| 112 |
return x |
| 113 |
|
| 114 |
fl = filterlist([a, b]) |
| 115 |
fl.append(c) |
| 116 |
fl.extend([d, e]) |
| 117 |
fl += [f] |
| 118 |
assert [i for i in fl] == [a, b, c, d, e, f] |
| 119 |
|
| 120 |
|
| 121 |
@pytest.fixture(scope="module") |
| 122 |
def create_tests(): |
| 123 |
def inner(*paths, **defaults): |
| 124 |
tests = [] |
| 125 |
for p in paths: |
| 126 |
path = p |
| 127 |
if isinstance(path, tuple): |
| 128 |
path, kwargs = path |
| 129 |
else: |
| 130 |
kwargs = {} |
| 131 |
|
| 132 |
path = mozpath.normpath(path) |
| 133 |
manifest = kwargs.pop( |
| 134 |
"manifest", |
| 135 |
defaults.pop( |
| 136 |
"manifest", mozpath.join(mozpath.dirname(path), "manifest.ini") |
| 137 |
), |
| 138 |
) |
| 139 |
test = { |
| 140 |
"name": mozpath.basename(path), |
| 141 |
"path": "/root/" + path, |
| 142 |
"relpath": path, |
| 143 |
"manifest": "/root/" + manifest, |
| 144 |
"manifest_relpath": manifest, |
| 145 |
} |
| 146 |
test.update(**defaults) |
| 147 |
test.update(**kwargs) |
| 148 |
tests.append(test) |
| 149 |
|
| 150 |
# dump tests to stdout for easier debugging on failure |
| 151 |
print("The 'create_tests' fixture returned:") |
| 152 |
pprint(tests, indent=2) |
| 153 |
return tests |
| 154 |
|
| 155 |
return inner |
| 156 |
|
| 157 |
|
| 158 |
@pytest.fixture |
| 159 |
def tests(create_tests): |
| 160 |
return create_tests( |
| 161 |
"test0", |
| 162 |
("test1", {"skip-if": "foo == 'bar'\nintermittent&&!debug"}), |
| 163 |
("test2", {"run-if": "foo == 'bar'"}), |
| 164 |
("test3", {"fail-if": "foo == 'bar'"}), |
| 165 |
("test4", {"disabled": "some reason"}), |
| 166 |
("test5", {"subsuite": "baz"}), |
| 167 |
("test6", {"subsuite": "baz,foo == 'bar'"}), |
| 168 |
("test7", {"tags": "foo bar"}), |
| 169 |
( |
| 170 |
"test8", |
| 171 |
{"skip-if": "\nbaz\nfoo == 'bar'\nfoo == 'baz'\nintermittent && debug"}, |
| 172 |
), |
| 173 |
) |
| 174 |
|
| 175 |
|
| 176 |
def test_skip_if(tests): |
| 177 |
ref = deepcopy(tests) |
| 178 |
tests = list(skip_if(tests, {})) |
| 179 |
assert len(tests) == len(ref) |
| 180 |
|
| 181 |
tests = deepcopy(ref) |
| 182 |
tests = list(skip_if(tests, {"foo": "bar"})) |
| 183 |
assert "disabled" in tests[1] |
| 184 |
assert "disabled" in tests[8] |
| 185 |
|
| 186 |
|
| 187 |
def test_run_if(tests): |
| 188 |
ref = deepcopy(tests) |
| 189 |
tests = list(run_if(tests, {})) |
| 190 |
assert "disabled" in tests[2] |
| 191 |
|
| 192 |
tests = deepcopy(ref) |
| 193 |
tests = list(run_if(tests, {"foo": "bar"})) |
| 194 |
assert "disabled" not in tests[2] |
| 195 |
|
| 196 |
|
| 197 |
def test_fail_if(tests): |
| 198 |
ref = deepcopy(tests) |
| 199 |
tests = list(fail_if(tests, {})) |
| 200 |
assert "expected" not in tests[3] |
| 201 |
|
| 202 |
tests = deepcopy(ref) |
| 203 |
tests = list(fail_if(tests, {"foo": "bar"})) |
| 204 |
assert tests[3]["expected"] == "fail" |
| 205 |
|
| 206 |
|
| 207 |
def test_enabled(tests): |
| 208 |
ref = deepcopy(tests) |
| 209 |
tests = list(enabled(tests, {})) |
| 210 |
assert ref[4] not in tests |
| 211 |
|
| 212 |
|
| 213 |
def test_subsuite(tests): |
| 214 |
sub1 = subsuite() |
| 215 |
sub2 = subsuite("baz") |
| 216 |
|
| 217 |
ref = deepcopy(tests) |
| 218 |
tests = list(sub1(tests, {})) |
| 219 |
assert ref[5] not in tests |
| 220 |
assert len(tests) == len(ref) - 1 |
| 221 |
|
| 222 |
tests = deepcopy(ref) |
| 223 |
tests = list(sub2(tests, {})) |
| 224 |
assert len(tests) == 1 |
| 225 |
assert ref[5] in tests |
| 226 |
|
| 227 |
|
| 228 |
def test_subsuite_condition(tests): |
| 229 |
sub1 = subsuite() |
| 230 |
sub2 = subsuite("baz") |
| 231 |
|
| 232 |
ref = deepcopy(tests) |
| 233 |
|
| 234 |
tests = list(sub1(tests, {"foo": "bar"})) |
| 235 |
assert ref[5] not in tests |
| 236 |
assert ref[6] not in tests |
| 237 |
|
| 238 |
tests = deepcopy(ref) |
| 239 |
tests = list(sub2(tests, {"foo": "bar"})) |
| 240 |
assert len(tests) == 2 |
| 241 |
assert tests[0]["name"] == "test5" |
| 242 |
assert tests[1]["name"] == "test6" |
| 243 |
|
| 244 |
|
| 245 |
def test_tags(tests): |
| 246 |
ftags1 = tags([]) |
| 247 |
ftags2 = tags(["bar", "baz"]) |
| 248 |
|
| 249 |
ref = deepcopy(tests) |
| 250 |
tests = list(ftags1(tests, {})) |
| 251 |
assert len(tests) == 0 |
| 252 |
|
| 253 |
tests = deepcopy(ref) |
| 254 |
tests = list(ftags2(tests, {})) |
| 255 |
assert len(tests) == 1 |
| 256 |
assert ref[7] in tests |
| 257 |
|
| 258 |
|
| 259 |
def test_failures(tests): |
| 260 |
ref = deepcopy(tests) |
| 261 |
fail1 = failures("intermittent") |
| 262 |
tests = list(fail1(tests, {"intermittent": True, "debug": True})) |
| 263 |
assert len(tests) == 1 |
| 264 |
|
| 265 |
tests = deepcopy(ref) |
| 266 |
tests = list(fail1(tests, {"intermittent": True})) |
| 267 |
assert len(tests) == 1 |
| 268 |
|
| 269 |
tests = deepcopy(ref) |
| 270 |
tests = list(fail1(tests, {})) |
| 271 |
assert len(tests) == 0 |
| 272 |
|
| 273 |
tests = deepcopy(ref) |
| 274 |
tests = list(fail1(tests, {"intermittent": False, "debug": True})) |
| 275 |
assert len(tests) == 0 |
| 276 |
|
| 277 |
|
| 278 |
def test_pathprefix(create_tests): |
| 279 |
tests = create_tests( |
| 280 |
"test0", |
| 281 |
"subdir/test1", |
| 282 |
"subdir/test2", |
| 283 |
("subdir/test3", {"manifest": "manifest.ini"}), |
| 284 |
( |
| 285 |
"other/test4", |
| 286 |
{ |
| 287 |
"manifest": "manifest-common.toml", |
| 288 |
"ancestor_manifest": "other/manifest.ini", |
| 289 |
}, |
| 290 |
), |
| 291 |
) |
| 292 |
|
| 293 |
def names(items): |
| 294 |
return sorted(i["name"] for i in items) |
| 295 |
|
| 296 |
# relative directory |
| 297 |
prefix = pathprefix("subdir") |
| 298 |
filtered = prefix(tests, {}) |
| 299 |
assert names(filtered) == ["test1", "test2", "test3"] |
| 300 |
|
| 301 |
# absolute directory |
| 302 |
prefix = pathprefix(["/root/subdir"]) |
| 303 |
filtered = prefix(tests, {}) |
| 304 |
assert names(filtered) == ["test1", "test2", "test3"] |
| 305 |
|
| 306 |
# relative manifest |
| 307 |
prefix = pathprefix(["subdir/manifest.ini"]) |
| 308 |
filtered = prefix(tests, {}) |
| 309 |
assert names(filtered) == ["test1", "test2"] |
| 310 |
|
| 311 |
# absolute manifest |
| 312 |
prefix = pathprefix(["/root/subdir/manifest.ini"]) |
| 313 |
filtered = prefix(tests, {}) |
| 314 |
assert names(filtered) == ["test1", "test2"] |
| 315 |
|
| 316 |
# mixed test and manifest |
| 317 |
prefix = pathprefix(["subdir/test2", "manifest.ini"]) |
| 318 |
filtered = prefix(tests, {}) |
| 319 |
assert names(filtered) == ["test0", "test2", "test3"] |
| 320 |
|
| 321 |
# relative ancestor manifest |
| 322 |
prefix = pathprefix(["other/manifest.ini"]) |
| 323 |
filtered = prefix(tests, {}) |
| 324 |
assert names(filtered) == ["test4"] |
| 325 |
|
| 326 |
# absolute ancestor manifest |
| 327 |
prefix = pathprefix(["/root/other/manifest.ini"]) |
| 328 |
filtered = prefix(tests, {}) |
| 329 |
assert names(filtered) == ["test4"] |
| 330 |
|
| 331 |
|
| 332 |
if __name__ == "__main__": |
| 333 |
mozunit.main() |