From f7a92db6300b8dbd54fc3f701dfaf90dcd0ba691 Mon Sep 17 00:00:00 2001 From: Tristan Maat Date: Wed, 31 Oct 2018 16:28:31 +0000 Subject: [PATCH] Add globbing to codestyle_exclude Sometimes a specific directory might include files that follow different style conventions (e.g., code generated using grpc) - before this change, any such files would need to be excluded individually. This commit makes specifying excluded files more flexible by allowing globbing in file paths. codestyle_exclude will now accept paths like `embedded_lib/**/*.py`, which will match any file in any subdirectory of `embedded_lib` that has a `.py` extension. --- pytest_codestyle.py | 7 ++++--- test_pytest_codestyle.py | 9 +++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pytest_codestyle.py b/pytest_codestyle.py index 7672b57..8bfe158 100644 --- a/pytest_codestyle.py +++ b/pytest_codestyle.py @@ -20,14 +20,15 @@ def pytest_addoption(parser): ignored=pycodestyle.DEFAULT_IGNORE.replace(',', ' '))) parser.addini('codestyle_show_source', type="bool", default=True, help='show source code for each error (default: True)') - parser.addini('codestyle_exclude', type="pathlist", + parser.addini('codestyle_exclude', type="args", help='source files to be excluded from codestyle') def pytest_collect_file(parent, path): config = parent.config - if config.getoption('codestyle') and path.ext == '.py' and path not in config.getini('codestyle_exclude'): - return Item(path, parent) + if config.getoption('codestyle') and path.ext == '.py': + if not any(path.fnmatch(pattern) for pattern in config.getini('codestyle_exclude')): + return Item(path, parent) class Item(pytest.Item, pytest.File): diff --git a/test_pytest_codestyle.py b/test_pytest_codestyle.py index 1426c87..fe44a88 100644 --- a/test_pytest_codestyle.py +++ b/test_pytest_codestyle.py @@ -46,9 +46,9 @@ def test_ini(request): ignore = ['d', 'e', 'f'] assert config.getini('codestyle_ignore') == ignore assert config.getini('codestyle_show_source') is True - exclude = ['{dirname}/{base}/exclude.py', '{dirname}/{base}/path/to/another/exclude.py'] + exclude = ['exclude.py', 'path/to/another/exclude.py'] assert config.getini('codestyle_exclude') == exclude - """.format(dirname=testdir.tmpdir.dirname, base=testdir.tmpdir.basename)) + """) p = p.write(p.read() + "\n") result = testdir.runpytest('--codestyle') result.assert_outcomes(passed=2) @@ -65,13 +65,14 @@ def test_pytest_collect_file(testdir): def test_pytest_collect_file_with_exclude(testdir): testdir.makeini(""" [pytest] - codestyle_exclude = a.py path/to/c.py + codestyle_exclude = a.py path/**/*.py """) testdir.tmpdir.ensure('a.py') testdir.tmpdir.ensure('b.py') testdir.tmpdir.ensure('path/to/c.py') + testdir.tmpdir.ensure('path/to/hoge/foo.py') result = testdir.runpytest('--codestyle') - result.assert_outcomes(passed=1) + result.assert_outcomes(passed=2) def test_cache(testdir):