Skip to content

Commit

Permalink
Add globbing to codestyle_exclude (#43)
Browse files Browse the repository at this point in the history
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.

Close #42
  • Loading branch information
TLATER authored and henry0312 committed Nov 22, 2018
1 parent 3df08c4 commit 8383954
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
7 changes: 4 additions & 3 deletions pytest_codestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 5 additions & 4 deletions test_pytest_codestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit 8383954

Please sign in to comment.