From ab47b53e3ca0956c4178496355bd5aea08f55eef Mon Sep 17 00:00:00 2001 From: "Taewon D. Kim" Date: Sun, 19 Nov 2017 10:05:08 -0500 Subject: [PATCH] Match filename rather than paths (Issue #72) 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 --- cardboardlint/common.py | 17 +++++++++++++---- cardboardlint/tests/test_common.py | 16 ++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/cardboardlint/common.py b/cardboardlint/common.py index acc3044..a31c8df 100644 --- a/cardboardlint/common.py +++ b/cardboardlint/common.py @@ -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 @@ -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'(? 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] == '+' diff --git a/cardboardlint/tests/test_common.py b/cardboardlint/tests/test_common.py index 71c6758..93fa614 100644 --- a/cardboardlint/tests/test_common.py +++ b/cardboardlint/tests/test_common.py @@ -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():