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 tests from pathspec #30

Open
wants to merge 2 commits 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
12 changes: 11 additions & 1 deletion gitignorefile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,19 @@ def _rule_from_pattern(pattern):
anchored = False
if pattern.startswith("/"):
pattern = pattern[1:]

if pattern.endswith("/"):
directory_only = True
pattern = pattern[:-1]

elif pattern.endswith("/*"):
directory_only = True
pattern = pattern[:-2]

elif pattern.endswith("/**"):
directory_only = True
pattern = pattern[:-3]

# patterns with leading hashes are escaped with a backslash in front, unescape it
if pattern.startswith("\\#"):
pattern = pattern[1:]
Expand Down Expand Up @@ -349,7 +359,7 @@ def _fnmatch_pathname_to_regexp(pattern, anchored, directory_only):
i += 1
if i < n and pattern[i] == "/":
i += 1
res.append("(.+/)?") # `/**/` matches `/`.
res.append("(?:.+/)?") # `/**/` matches `/`.

else:
res.append(".*")
Expand Down
25 changes: 13 additions & 12 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,19 @@ def mock_isfile(path):

# 9! == 362880 combinations.
for permutation in itertools.islice(itertools.permutations(data.items()), 0, None, 6 * 8):
statistics = {"open": 0, "isdir": 0, "isfile": 0}

with unittest.mock.patch("builtins.open", mock_open):
with unittest.mock.patch("os.path.isdir", mock_isdir):
with unittest.mock.patch("os.path.isfile", mock_isfile):
matches = gitignorefile.Cache(ignore_names=[ignore_file_name])
for path, expected in permutation:
self.assertEqual(matches(path), expected)

self.assertEqual(statistics["open"], 2)
self.assertEqual(statistics["isdir"], len(data) - 1)
self.assertEqual(statistics["isfile"], 7) # Unique path fragments.
with self.subTest(i=permutation):
statistics = {"open": 0, "isdir": 0, "isfile": 0}

with unittest.mock.patch("builtins.open", mock_open):
with unittest.mock.patch("os.path.isdir", mock_isdir):
with unittest.mock.patch("os.path.isfile", mock_isfile):
matches = gitignorefile.Cache(ignore_names=[ignore_file_name])
for path, expected in permutation:
self.assertEqual(matches(path), expected)

self.assertEqual(statistics["open"], 2)
self.assertEqual(statistics["isdir"], len(data) - 1)
self.assertEqual(statistics["isfile"], 7) # Unique path fragments.

def test_wrong_symlink(self):
with tempfile.TemporaryDirectory() as d:
Expand Down
Loading