Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match filename rather than paths #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions cardboardlint/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"""Collection of classes and methods shared between different linters."""
from __future__ import print_function

import os
import re
from fnmatch import fnmatch
import subprocess

Expand Down Expand Up @@ -179,16 +181,30 @@ def matches_filefilter(filename, rules):
accepted: boolean
True if the file should be included.

Raises
------
ValueError
If first character of the pattern in `rules` is not '+' or '-'.
If there is a double backslash in a pattern.

"""
# Check format of the rules
re_dir = re.compile(r'(?<!\\)' + os.sep)
filename_split = re_dir.split(filename)
for rule in rules:
pattern_split = re_dir.split(rule[1:].strip())

# Check format of the rules
if rule[0] not in '+-':
raise ValueError('Unexpected first character in filename filter rule: {}'.format(
rule[0]))

for rule in rules:
pattern = rule[1:].strip()
if fnmatch(filename, pattern):
elif '\\' in rule[1:]:
raise ValueError('Cannot have double backslash in the pattern.')
elif len(pattern_split) > len(filename_split):
continue

# apply pattern
if all(fnmatch(name, pattern)
for name, pattern in zip(filename_split[::-1], pattern_split[::-1])):
return rule[0] == '+'


Expand Down
16 changes: 12 additions & 4 deletions cardboardlint/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,18 @@ def test_matches_filefilter():
assert not matches_filefilter('foo/test/test_a.py', ['- */test_*.py', '+ *.py'])
assert matches_filefilter('scripts/runfoo', ['+ scripts/*'])

with assert_raises(ValueError):
matches_filefilter('foo.py', ['b *.py'])
with assert_raises(ValueError):
matches_filefilter('foo.py', ['bork'])
assert matches_filefilter('foo/a.py', ['+ *.py'])
assert not matches_filefilter('foo/a.py', ['+ bar/*.py'])
assert not matches_filefilter('a.py', ['+ bar/*.py'])

assert matches_filefilter('foo\\/a.py', ['+ foo*.py'])
assert not matches_filefilter(r'foo\\/a.py', ['+ a.py'])
assert not matches_filefilter(r'foo/\\/a.py', ['+ foo*.py'])
assert matches_filefilter(r'foo/\\/a.py', ['+ foo/*.py'])

assert_raises(ValueError, matches_filefilter, 'foo.py', ['b *.py'])
assert_raises(ValueError, matches_filefilter, 'foo.py', ['bork'])
assert_raises(ValueError, matches_filefilter, 'foo.py', ['+ \\*py'])


def test_offset_step():
Expand Down