DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5f0b392beadb)

VCS Links

Line Code
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
from __future__ import absolute_import, print_function

import logging
import os
import sys
from collections import defaultdict

from mozbuild.base import MozbuildObject
from mozlint.pathutils import findobject
from mozlint.parser import Parser
from mozlint.result import ResultSummary

import pytest

here = os.path.abspath(os.path.dirname(__file__))
build = MozbuildObject.from_environment(cwd=here)

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


@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 os.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 [os.path.normpath(os.path.join(root, p)) for p in paths]
    return _inner


@pytest.fixture
def config(request):
    """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 not hasattr(request.module, 'LINTER'):
        pytest.fail("'config' fixture used from a module that didn't set the LINTER variable")

    name = request.module.LINTER
    config_path = os.path.join(lintdir, '{}.yml'.format(name))
    parser = Parser(build.topsrcdir)
    # TODO Don't assume one linter per yaml file
    return parser.parse(config_path)[0]


@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

    func = findobject(config['setup'])
    func(build.topsrcdir)


@pytest.fixture
def lint(config, root):
    """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.
    """
    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):
        lintargs['log'] = logging.LoggerAdapter(logger, {
            "lintname": config.get("name"),
            "pid": os.getpid()
        })
        results = func(paths, config, root=root, **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 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