summaryrefslogtreecommitdiff
path: root/tests/profile/test_profile_against_externals.py
blob: 8de36c673ca697c3b60e9440a19a32600b803117 (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
""" Profiles basic -jX functionality """
# Copyright (c) 2020 Frank Harrison <doublethefish@gmail.com>

# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING

# pylint: disable=protected-access,missing-function-docstring,no-self-use

import os
import pprint
import shutil
import tempfile

import pytest

from pylint.lint import Run
from pylint.testutils import TestReporter as Reporter


class TempdirGuard:
    """ creates and deletes a tmp-dir compatible with python2 and 3 """

    def __init__(self, prefix):
        self.path = tempfile.mkdtemp(prefix=prefix + "_")

    def __enter__(self):
        return self

    def __exit__(self, x_type, x_value, x_traceback):
        shutil.rmtree(self.path)  # always clean up on exit


def _get_py_files(scanpath):
    assert os.path.exists(scanpath), "Dir not found %s" % scanpath

    filepaths = []
    for dirpath, dirnames, filenames in os.walk(scanpath):
        dirnames[:] = [dirname for dirname in dirnames if dirname != "__pycache__"]
        filepaths.extend(
            [
                os.path.join(dirpath, filename)
                for filename in filenames
                if filename.endswith(".py")
            ]
        )
    return filepaths


@pytest.mark.skipif(
    not os.environ.get("PYTEST_PROFILE_EXTERNAL", False),
    reason="PYTEST_PROFILE_EXTERNAL, not set, assuming not a profile run",
)
@pytest.mark.parametrize(
    "name,git_repo", [("numpy", "https://github.com/numpy/numpy.git")]
)
def test_run(name, git_repo):
    """ Runs pylint against external sources """
    with TempdirGuard(prefix=name) as checkoutdir:
        os.system(
            "git clone --depth=1 {git_repo} {checkoutdir}".format(
                git_repo=git_repo, checkoutdir=checkoutdir.path
            )
        )
        filepaths = _get_py_files(scanpath=checkoutdir.path)
        print("Have %d files" % len(filepaths))

        runner = Run(filepaths, reporter=Reporter(), do_exit=False)

        print(
            "Had %d files with %d messages"
            % (len(filepaths), len(runner.linter.reporter.messages))
        )
        pprint.pprint(runner.linter.reporter.messages)