Skip to content

Commit

Permalink
Match filename rather than paths (Issue #72)
Browse files Browse the repository at this point in the history
1. Split the given path into its components and compare each component
   with the given pattern (re.split is used)
2. Add exception for double backslash (would cause problems with split)
3. Add tests and documentation
4. Little rearrangement
  • Loading branch information
kimt33 committed Nov 19, 2017
1 parent ed36bcb commit ab47b53
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
17 changes: 13 additions & 4 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 @@ -180,15 +182,22 @@ def matches_filefilter(filename, rules):
True if the file should be included.
"""
# 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]))
elif '\\' in rule[1:]:
raise ValueError('Cannot have double backslash in the pattern.')
elif len(pattern_split) > len(filename_split):
continue

for rule in rules:
pattern = rule[1:].strip()
if fnmatch(filename, 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('foo\/a.py', ['+ a.py'])
assert not matches_filefilter('foo/\/a.py', ['+ foo*.py'])
assert matches_filefilter('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

0 comments on commit ab47b53

Please sign in to comment.