summaryrefslogtreecommitdiffstats
path: root/tools/lint/test/conftest.py
blob: f8cf8a30b63ea22ec1688fd24320de396b3cf464 (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
import logging
import os
import pathlib
import sys
from collections import defaultdict

import pytest
from mozbuild.base import MozbuildObject
from mozlint.parser import Parser
from mozlint.pathutils import findobject
from mozlint.result import ResultSummary
from mozlog.structuredlog import StructuredLogger
from mozpack import path

here = path.abspath(path.dirname(__file__))
build = MozbuildObject.from_environment(cwd=here, virtualenv_name="python-test")

lintdir = path.dirname(here)
sys.path.insert(0, lintdir)
logger = logging.getLogger("mozlint")


def pytest_generate_tests(metafunc):
    """Finds, loads and returns the config for the linter name specified by the
    LINTER global variable in the calling module.

    This implies that each test file (that uses this fixture) should only be
    used to test a single linter. If no LINTER variable is defined, the test
    will fail.
    """
    if "config" in metafunc.fixturenames:
        if not hasattr(metafunc.module, "LINTER"):
            pytest.fail(
                "'config' fixture used from a module that didn't set the LINTER variable"
            )

        name = metafunc.module.LINTER
        config_path = path.join(lintdir, "{}.yml".format(name))
        parser = Parser(build.topsrcdir)
        configs = parser.parse(config_path)
        config_names = {config["name"] for config in configs}

        marker = metafunc.definition.get_closest_marker("lint_config")
        if marker:
            config_name = marker.kwargs["name"]
            if config_name not in config_names:
                pytest.fail(f"lint config {config_name} not present in {name}.yml")
            configs = [
                config for config in configs if config["name"] == marker.kwargs["name"]
            ]

        ids = [config["name"] for config in configs]
        metafunc.parametrize("config", configs, ids=ids)


@pytest.fixture(scope="module")
def root(request):
    """Return the root directory for the files of the linter under test.

    For example, with LINTER=flake8 this would be tools/lint/test/files/flake8.
    """
    if not hasattr(request.module, "LINTER"):
        pytest.fail(
            "'root' fixture used from a module that didn't set the LINTER variable"
        )
    return path.join(here, "files", request.module.LINTER)


@pytest.fixture(scope="module")
def paths(root):
    """Return a function that can resolve file paths relative to the linter
    under test.

    Can be used like `paths('foo.py', 'bar/baz')`. This will return a list of
    absolute paths under the `root` files directory.
    """

    def _inner(*paths):
        if not paths:
            return [root]
        return [path.normpath(path.join(root, p)) for p in paths]

    return _inner


@pytest.fixture(autouse=True)
def run_setup(config):
    """Make sure that if the linter named in the LINTER global variable has a
    setup function, it gets called before running the tests.
    """
    if "setup" not in config:
        return

    if config["name"] == "clang-format":
        # Skip the setup for the clang-format linter, as it requires a Mach context
        # (which we may not have if pytest is invoked directly).
        return

    log = logging.LoggerAdapter(
        logger, {"lintname": config.get("name"), "pid": os.getpid()}
    )

    func = findobject(config["setup"])
    func(
        build.topsrcdir,
        virtualenv_manager=build.virtualenv_manager,
        virtualenv_bin_path=build.virtualenv_manager.bin_path,
        log=log,
    )


@pytest.fixture
def lint(config, root, request):
    """Find and return the 'lint' function for the external linter named in the
    LINTER global variable.

    This will automatically pass in the 'config' and 'root' arguments if not
    specified.
    """

    if hasattr(request.module, "fixed"):
        request.module.fixed = 0

    try:
        func = findobject(config["payload"])
    except (ImportError, ValueError):
        pytest.fail(
            "could not resolve a lint function from '{}'".format(config["payload"])
        )

    ResultSummary.root = root

    def wrapper(paths, config=config, root=root, collapse_results=False, **lintargs):
        logger.setLevel(logging.DEBUG)
        lintargs["log"] = logging.LoggerAdapter(
            logger, {"lintname": config.get("name"), "pid": os.getpid()}
        )

        results = func(paths, config, root=root, **lintargs)
        if hasattr(request.module, "fixed") and isinstance(results, dict):
            request.module.fixed += results["fixed"]

        if isinstance(results, dict):
            results = results["results"]

        if isinstance(results, (list, tuple)):
            results = sorted(results)

        if not collapse_results:
            return results

        ret = defaultdict(list)
        for r in results:
            ret[r.relpath].append(r)
        return ret

    return wrapper


@pytest.fixture
def structuredlog_lint(config, root, logger=None):
    """Find and return the 'lint' function for the external linter named in the
    LINTER global variable. This variant of the lint function is for linters that
    use the 'structuredlog' type.

    This will automatically pass in the 'config' and 'root' arguments if not
    specified.
    """
    try:
        func = findobject(config["payload"])
    except (ImportError, ValueError):
        pytest.fail(
            "could not resolve a lint function from '{}'".format(config["payload"])
        )

    ResultSummary.root = root

    if not logger:
        logger = structured_logger()

    def wrapper(
        paths,
        config=config,
        root=root,
        logger=logger,
        collapse_results=False,
        **lintargs,
    ):
        lintargs["log"] = logging.LoggerAdapter(
            logger, {"lintname": config.get("name"), "pid": os.getpid()}
        )
        results = func(paths, config, root=root, logger=logger, **lintargs)
        if not collapse_results:
            return results

        ret = defaultdict(list)
        for r in results:
            ret[r.path].append(r)
        return ret

    return wrapper


@pytest.fixture
def global_lint(config, root, request):
    try:
        func = findobject(config["payload"])
    except (ImportError, ValueError):
        pytest.fail(
            "could not resolve a lint function from '{}'".format(config["payload"])
        )

    ResultSummary.root = root

    def wrapper(config=config, root=root, collapse_results=False, **lintargs):
        logger.setLevel(logging.DEBUG)
        lintargs["log"] = logging.LoggerAdapter(
            logger, {"lintname": config.get("name"), "pid": os.getpid()}
        )
        results = func(config, root=root, **lintargs)
        if hasattr(request.module, "fixed") and isinstance(results, dict):
            request.module.fixed += results["fixed"]

        if isinstance(results, dict):
            results = results["results"]

        if isinstance(results, (list, tuple)):
            results = sorted(results)

        if not collapse_results:
            return results

        ret = defaultdict(list)
        for r in results:
            ret[r.relpath].append(r)
        return ret

    return wrapper


@pytest.fixture
def create_temp_file(tmpdir):
    def inner(contents, name=None):
        name = name or "temp.py"
        path = tmpdir.join(name)
        path.write(contents)
        return path.strpath

    return inner


@pytest.fixture
def structured_logger():
    return StructuredLogger("logger")


@pytest.fixture
def perfdocs_sample():
    from test_perfdocs import (
        DYNAMIC_SAMPLE_CONFIG,
        SAMPLE_CONFIG,
        SAMPLE_INI,
        SAMPLE_TEST,
        temp_dir,
        temp_file,
    )

    with temp_dir() as tmpdir:
        suite_dir = pathlib.Path(tmpdir, "suite")
        raptor_dir = pathlib.Path(tmpdir, "raptor")
        raptor_suitedir = pathlib.Path(tmpdir, "raptor", "suite")
        raptor_another_suitedir = pathlib.Path(tmpdir, "raptor", "another_suite")
        perfdocs_dir = pathlib.Path(tmpdir, "perfdocs")

        perfdocs_dir.mkdir(parents=True, exist_ok=True)
        suite_dir.mkdir(parents=True, exist_ok=True)
        raptor_dir.mkdir(parents=True, exist_ok=True)
        raptor_suitedir.mkdir(parents=True, exist_ok=True)
        raptor_another_suitedir.mkdir(parents=True, exist_ok=True)

        with temp_file(
            "perftest.ini", tempdir=suite_dir, content="[perftest_sample.js]"
        ) as tmpmanifest, temp_file(
            "raptor_example1.ini", tempdir=raptor_suitedir, content=SAMPLE_INI
        ) as tmpexample1manifest, temp_file(
            "raptor_example2.ini", tempdir=raptor_another_suitedir, content=SAMPLE_INI
        ) as tmpexample2manifest, temp_file(
            "perftest_sample.js", tempdir=suite_dir, content=SAMPLE_TEST
        ) as tmptest, temp_file(
            "config.yml", tempdir=perfdocs_dir, content=SAMPLE_CONFIG
        ) as tmpconfig, temp_file(
            "config_2.yml", tempdir=perfdocs_dir, content=DYNAMIC_SAMPLE_CONFIG
        ) as tmpconfig_2, temp_file(
            "index.rst", tempdir=perfdocs_dir, content="{documentation}"
        ) as tmpindex:
            yield {
                "top_dir": tmpdir,
                "manifest": {"path": tmpmanifest},
                "example1_manifest": tmpexample1manifest,
                "example2_manifest": tmpexample2manifest,
                "test": tmptest,
                "config": tmpconfig,
                "config_2": tmpconfig_2,
                "index": tmpindex,
            }