tools/lint/test/conftest.py
author Andrew Halberstadt <ahalberstadt@mozilla.com>
Thu, 22 Mar 2018 17:24:15 -0400
changeset 410601 ad34ac3d45a62dff961bd247e36ab209af2cc0e5
child 412206 e9a4b0a9bc9b5b6cbf5e76f07540aaebde84e1a9
permissions -rw-r--r--
Bug 1436802 - [lint] Add some tests for the flake8 linter integration r=andi This essentially tests tools/lint/python/flake8.py. Though it also adds a basic framework for testing all the other linters as well. Getting this added now will allow others to collaborate on adding more tests without needing to get to 100% coverage for all linters right off the bat. All python tests under tools/lint/test will run as part of the 'ml' task on Linux, and the build task on Windows (OSX coverage is currently missing for python tests). The flake8 linter currently has a bug where the 'exclude' argument is ignored. This is why we need to also exclude 'tools/lint/test/files' in topsrcdir/.flake8, even though it is already listed in the 'mach_commands.py'. Other linters shouldn't need to do this, the exclusion in 'mach_commands.py' should be good enough. See bug 1277851 for more details. MozReview-Commit-ID: 9ho8C83eeuj

import os
import sys
from collections import defaultdict

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

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)


@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(scope='module')
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()
    # TODO Don't assume one linter per yaml file
    return parser.parse(config_path)[0]


@pytest.fixture(scope='module', 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(scope='module')
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']))

    def wrapper(paths, config=config, root=root, collapse_results=False, **lintargs):
        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