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

Address #44: raise informative exception for invalid git patterns #50

Merged
merged 1 commit into from
Jul 18, 2021
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
15 changes: 15 additions & 0 deletions pathspec/patterns/gitwildmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
_BYTES_ENCODING = 'latin1'


class GitWildMatchPatternError(ValueError):
"""
The :class:`GitWildMatchPatternError` indicates an invalid git wild match
pattern.
"""
pass


class GitWildMatchPattern(RegexPattern):
"""
The :class:`GitWildMatchPattern` class represents a compiled Git
Expand Down Expand Up @@ -56,6 +64,7 @@ def pattern_to_regex(cls, pattern):
else:
raise TypeError("pattern:{!r} is not a unicode or byte string.".format(pattern))

original_pattern = pattern
pattern = pattern.strip()

if pattern.startswith('#'):
Expand Down Expand Up @@ -137,6 +146,12 @@ def pattern_to_regex(cls, pattern):
# according to `git check-ignore` (v2.4.1).
pass

if not pattern_segs:
# After resolving the edge cases, we end up with no
# pattern at all. This must be because the pattern is
# invalid.
raise GitWildMatchPatternError("Invalid git pattern: %r" % (original_pattern,))

if not pattern_segs[-1] and len(pattern_segs) > 1:
# A pattern ending with a slash ('/') will match all
# descendant paths if it is a directory but not if it is a
Expand Down
20 changes: 19 additions & 1 deletion pathspec/tests/test_gitwildmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pathspec.patterns.gitwildmatch
import pathspec.util
from pathspec.patterns.gitwildmatch import GitWildMatchPattern
from pathspec.patterns.gitwildmatch import GitWildMatchPattern, GitWildMatchPatternError

if sys.version_info[0] >= 3:
unichr = chr
Expand Down Expand Up @@ -528,3 +528,21 @@ def test_08_escape(self):
escaped = r"file\!with\*weird\#naming_\[1\].t\?t"
result = GitWildMatchPattern.escape(fname)
self.assertEqual(result, escaped)

def test_09_single_escape_fail(self):
"""
Test an escape on a line by itself.
"""
self._check_invalid_pattern("\\")

def test_09_single_exclamation_mark_fail(self):
"""
Test an escape on a line by itself.
"""
self._check_invalid_pattern("!")

def _check_invalid_pattern(self, git_ignore_pattern):
expected_message_pattern = re.escape(repr(git_ignore_pattern))
with self.assertRaisesRegexp(GitWildMatchPatternError, expected_message_pattern):
GitWildMatchPattern(git_ignore_pattern)