From 9b23abed3cf2ce830aa65db089d5c3eaa1752ab6 Mon Sep 17 00:00:00 2001 From: Frank Harrison Date: Fri, 3 Apr 2020 08:21:45 +0100 Subject: [PATCH 1/3] profile| Runs pylint against external code, generating profile-heatmaps * Use https gitub uri so we do not need creditials * Shallow clone so we don't pull more data than we need * Ensure we skip external profiling unless explicitly wanted --- .../profile/test_profile_against_externals.py | 78 +++++++++++++++++++ tox.ini | 17 ++++ 2 files changed, 95 insertions(+) create mode 100644 tests/profile/test_profile_against_externals.py diff --git a/tests/profile/test_profile_against_externals.py b/tests/profile/test_profile_against_externals.py new file mode 100644 index 0000000000..0fe2cab61b --- /dev/null +++ b/tests/profile/test_profile_against_externals.py @@ -0,0 +1,78 @@ +""" Profiles basic -jX functionality """ +# Copyright (c) 2020 Frank Harrison + +# 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) + + # one day: assert runner.linter.msg_status == 0, ( + # one day: "Expected no errors to be thrown: %s" + # one day: % pprint.pformat(runner.linter.reporter.messages) + # one day: ) diff --git a/tox.ini b/tox.ini index 9ea7949837..bb8cf99943 100644 --- a/tox.ini +++ b/tox.ini @@ -157,4 +157,21 @@ commands = --benchmark-group-by="group" \ {posargs:} +[testenv:profile_against_external] +deps = + https://github.com/PyCQA/astroid/tarball/master#egg=astroid-master-2.0 + gprof2dot + mccabe + pytest + pytest-profiling + pytest-xdist + +setenv = + PYTEST_PROFILE_EXTERNAL = 1 + +commands = + python -Wi -m pytest --exitfirst \ + --profile-svg \ + {toxinidir}/tests/profile/test_profile_against_externals.py + changedir = {toxworkdir} From 187f133591644cb358991d3aafead9c8aee5ce6f Mon Sep 17 00:00:00 2001 From: Frank Harrison Date: Tue, 1 Sep 2020 08:13:45 +0100 Subject: [PATCH 2/3] Remove 0xdeadcode --- tests/profile/test_profile_against_externals.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/profile/test_profile_against_externals.py b/tests/profile/test_profile_against_externals.py index 0fe2cab61b..8de36c673c 100644 --- a/tests/profile/test_profile_against_externals.py +++ b/tests/profile/test_profile_against_externals.py @@ -71,8 +71,3 @@ def test_run(name, git_repo): % (len(filepaths), len(runner.linter.reporter.messages)) ) pprint.pprint(runner.linter.reporter.messages) - - # one day: assert runner.linter.msg_status == 0, ( - # one day: "Expected no errors to be thrown: %s" - # one day: % pprint.pformat(runner.linter.reporter.messages) - # one day: ) From 899156a069f723262aca4c66cc3702834d75f842 Mon Sep 17 00:00:00 2001 From: Frank Harrison Date: Tue, 20 Oct 2020 07:44:41 +0100 Subject: [PATCH 3/3] review fixes| Uses pytest's tmp_path fixture instead of custom context man --- .../profile/test_profile_against_externals.py | 42 +++++++------------ 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/tests/profile/test_profile_against_externals.py b/tests/profile/test_profile_against_externals.py index 8de36c673c..e7159264be 100644 --- a/tests/profile/test_profile_against_externals.py +++ b/tests/profile/test_profile_against_externals.py @@ -17,19 +17,6 @@ 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 @@ -53,21 +40,22 @@ def _get_py_files(scanpath): @pytest.mark.parametrize( "name,git_repo", [("numpy", "https://github.com/numpy/numpy.git")] ) -def test_run(name, git_repo): +def test_run(tmp_path, 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 - ) + checkoutdir = tmp_path / name + checkoutdir.mkdir() + os.system( + "git clone --depth=1 {git_repo} {checkoutdir}".format( + git_repo=git_repo, checkoutdir=str(checkoutdir) ) - filepaths = _get_py_files(scanpath=checkoutdir.path) - print("Have %d files" % len(filepaths)) + ) + filepaths = _get_py_files(scanpath=str(checkoutdir)) + print("Have %d files" % len(filepaths)) - runner = Run(filepaths, reporter=Reporter(), do_exit=False) + 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) + print( + "Had %d files with %d messages" + % (len(filepaths), len(runner.linter.reporter.messages)) + ) + pprint.pprint(runner.linter.reporter.messages)