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

Added test ignoring all subdirectories #33

Merged
Merged
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
10 changes: 5 additions & 5 deletions gitignore_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ def rule_from_pattern(pattern, base_path=None, source=None):
# A slash is a sign that we're tied to the base_path of our rule
# set.
anchored = '/' in pattern[:-1]
if pattern[0] == '/':
if pattern[:1] == '/':
pattern = pattern[1:]
if pattern[0] == '*' and len(pattern) >= 2 and pattern[1] == '*':
if pattern[:2] == '**':
pattern = pattern[2:]
anchored = False
if pattern[0] == '/':
if pattern[:1] == '/':
pattern = pattern[1:]
if pattern[-1] == '/':
if pattern[-1:] == '/':
pattern = pattern[:-1]
# patterns with leading hashes are escaped with a backslash in front, unescape it
if pattern[0] == '\\' and pattern[1] == '#':
if pattern[:2] == '\\#':
pattern = pattern[1:]
# trailing spaces are ignored unless they are escaped with a backslash
i = len(pattern)-1
Expand Down
7 changes: 7 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ def test_single_asterisk(self):
self.assertTrue(matches('/home/michael/directory'))
self.assertTrue(matches('/home/michael/directory-trailing/'))

def test_ignore_all_subdirectories(self):
matches = _parse_gitignore_string('**/', fake_base_dir='/home/michael')
self.assertFalse(matches('/home/michael/file.txt'))
self.assertTrue(matches('/home/michael/directory/'))
self.assertTrue(matches('/home/michael/directory/file.txt'))
self.assertTrue(matches('/home/michael/directory/subdirectory/'))


def _parse_gitignore_string(data: str, fake_base_dir: str = None):
with patch('builtins.open', mock_open(read_data=data)):
Expand Down