Bug 1754025 [wpt PR 32729] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / tools / tryselect / test / conftest.py
blob1dab2f32194a7f683fe17103cc50ba9e61bc3a01
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 from __future__ import absolute_import, print_function, unicode_literals
7 import os
9 import pytest
10 import yaml
11 from unittest.mock import MagicMock
12 from moztest.resolve import TestResolver
13 from taskgraph.graph import Graph
14 from taskgraph.task import Task
15 from taskgraph.taskgraph import TaskGraph
17 from tryselect import push
20 @pytest.fixture
21 def tg(request):
22 if not hasattr(request.module, "TASKS"):
23 pytest.fail(
24 "'tg' fixture used from a module that didn't define the TASKS variable"
27 tasks = request.module.TASKS
28 for task in tasks:
29 task.setdefault("task", {})
30 task["task"].setdefault("tags", {})
32 tasks = {t["label"]: Task(**t) for t in tasks}
33 return TaskGraph(tasks, Graph(tasks.keys(), set()))
36 @pytest.fixture
37 def patch_resolver(monkeypatch):
38 def inner(suites, tests):
39 def fake_test_metadata(*args, **kwargs):
40 return suites, tests
42 monkeypatch.setattr(TestResolver, "resolve_metadata", fake_test_metadata)
44 return inner
47 @pytest.fixture(autouse=True)
48 def patch_vcs(monkeypatch):
49 attrs = {
50 "path": push.vcs.path,
52 mock = MagicMock()
53 mock.configure_mock(**attrs)
54 monkeypatch.setattr(push, "vcs", mock)
57 @pytest.fixture(scope="session")
58 def run_mach():
59 import mach_initialize
60 from mach.config import ConfigSettings
61 from tryselect.tasks import build
63 mach = mach_initialize.initialize(build.topsrcdir)
65 def inner(args):
66 mach.settings = ConfigSettings()
67 return mach.run(args)
69 return inner
72 def pytest_generate_tests(metafunc):
73 if all(
74 fixture in metafunc.fixturenames
75 for fixture in ("task_config", "args", "expected")
78 def load_tests():
79 for task_config, tests in metafunc.module.TASK_CONFIG_TESTS.items():
80 for args, expected in tests:
81 yield (task_config, args, expected)
83 tests = list(load_tests())
84 ids = ["{} {}".format(t[0], " ".join(t[1])).strip() for t in tests]
85 metafunc.parametrize("task_config,args,expected", tests, ids=ids)
87 elif all(
88 fixture in metafunc.fixturenames for fixture in ("shared_name", "shared_preset")
90 preset_path = os.path.join(
91 push.build.topsrcdir, "tools", "tryselect", "try_presets.yml"
93 with open(preset_path, "r") as fh:
94 presets = list(yaml.safe_load(fh).items())
96 ids = [p[0] for p in presets]
98 # Mark fuzzy presets on Windows xfail due to fzf not being installed.
99 if os.name == "nt":
100 for i, preset in enumerate(presets):
101 if preset[1]["selector"] == "fuzzy":
102 presets[i] = pytest.param(*preset, marks=pytest.mark.xfail)
104 metafunc.parametrize("shared_name,shared_preset", presets, ids=ids)