Skip to content

Commit

Permalink
Fix issue #84
Browse files Browse the repository at this point in the history
  • Loading branch information
cpburnz committed Dec 10, 2023
1 parent 37e2895 commit 81368ad
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 46 deletions.
14 changes: 13 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
Change History
==============


0.12.1 (TBD)
-------------------

- Bug fixes:

- `Issue #84`_: PathSpec.match_file() returns None since 0.12.0.


.. _`Issue #84`: https://github.com/cpburnz/python-pathspec/issues/84


0.12.0 (2023-12-09)
-------------------

Expand All @@ -12,7 +24,7 @@ Major changes:

API changes:

- Signature of protected method `pathspec.pathspec.PathSpec._match_file()` has been changed from `def _match_file(patterns: Iterable[Pattern], file: str) -> bool` to `def _match_file(patterns: Iterable[Tuple[int, Pattern]], file: str) -> Tuple[Optional[bool], Optional[int]]`.
- Signature of protected method `pathspec.pathspec.PathSpec._match_file()` (with a leading underscore) has been changed from `def _match_file(patterns: Iterable[Pattern], file: str) -> bool` to `def _match_file(patterns: Iterable[Tuple[int, Pattern]], file: str) -> Tuple[Optional[bool], Optional[int]]`.

New features:

Expand Down
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#
# This Makefile is used to manage development and distribution.
#
# Created: 2022-08-11
# Updated: 2022-09-01
#

.PHONY: build create-venv help prebuild publish test test-all test-docs update-venv

Expand Down
2 changes: 1 addition & 1 deletion pathspec/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@
"kurtmckee <https://github.com/kurtmckee>",
]
__license__ = "MPL 2.0"
__version__ = "0.12.0"
__version__ = "0.12.1.dev1"
2 changes: 1 addition & 1 deletion pathspec/pathspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def match_file(
"""
norm_file = normalize_file(file, separators)
include, _index = self._match_file(enumerate(self.patterns), norm_file)
return include
return bool(include)

def match_files(
self,
Expand Down
133 changes: 126 additions & 7 deletions tests/test_01_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
from functools import (
partial)
from typing import (
Iterable,
Optional,
Tuple)
Iterable, # Replaced by `collections.abc.Iterable` in 3.9.
Optional, # Replaced by `X | None` in 3.10.
Tuple) # Replaced by `tuple` in 3.9.

from pathspec.patterns.gitwildmatch import (
GitWildMatchPattern)
from pathspec.util import (
RecursionError,
check_match_file,
iter_tree_entries,
iter_tree_files,
match_file,
Expand All @@ -32,6 +33,82 @@
ospath)


class CheckMatchFileTest(unittest.TestCase):
"""
The :class:`CheckMatchFileTest` class tests the :meth:`.check_match_file`
function.
"""

def test_01_single_1_include(self):
"""
Test checking a single file that is included.
"""
patterns = list(enumerate(map(GitWildMatchPattern, [
"*.txt",
"!test/",
])))

include_index = check_match_file(patterns, "include.txt")

self.assertEqual(include_index, (True, 0))

def test_01_single_2_exclude(self):
"""
Test checking a single file that is excluded.
"""
patterns = list(enumerate(map(GitWildMatchPattern, [
"*.txt",
"!test/",
])))

include_index = check_match_file(patterns, "test/exclude.txt")

self.assertEqual(include_index, (False, 1))

def test_01_single_3_unmatch(self):
"""
Test checking a single file that is ignored.
"""
patterns = list(enumerate(map(GitWildMatchPattern, [
"*.txt",
"!test/",
])))

include_index = check_match_file(patterns, "unmatch.bin")

self.assertEqual(include_index, (None, None))

def test_02_many(self):
"""
Test matching files individually.
"""
patterns = list(enumerate(map(GitWildMatchPattern, [
'*.txt',
'!b.txt',
])))
files = {
'X/a.txt',
'X/b.txt',
'X/Z/c.txt',
'Y/a.txt',
'Y/b.txt',
'Y/Z/c.txt',
}

includes = {
__file
for __file in files
if check_match_file(patterns, __file)[0]
}

self.assertEqual(includes, {
'X/a.txt',
'X/Z/c.txt',
'Y/a.txt',
'Y/Z/c.txt',
})


class IterTreeTest(unittest.TestCase):
"""
The :class:`IterTreeTest` class tests :meth:`.iter_tree_entries` and
Expand Down Expand Up @@ -345,23 +422,65 @@ class MatchFileTest(unittest.TestCase):
function.
"""

def test_01_match_file(self):
def test_01_single_1_include(self):
"""
Test checking a single file that is included.
"""
patterns = list(map(GitWildMatchPattern, [
"*.txt",
"!test/",
]))

include = match_file(patterns, "include.txt")

self.assertIs(include, True)

def test_01_single_2_exclude(self):
"""
Test checking a single file that is excluded.
"""
patterns = list(map(GitWildMatchPattern, [
"*.txt",
"!test/",
]))

include = match_file(patterns, "test/exclude.txt")

self.assertIs(include, False)

def test_01_single_3_unmatch(self):
"""
Test checking a single file that is ignored.
"""
patterns = list(map(GitWildMatchPattern, [
"*.txt",
"!test/",
]))

include = match_file(patterns, "unmatch.bin")

self.assertIs(include, False)

def test_02_many(self):
"""
Test matching files individually.
"""
patterns = list(map(GitWildMatchPattern, [
'*.txt',
'!b.txt',
]))
results = set(filter(partial(match_file, patterns), [
files = {
'X/a.txt',
'X/b.txt',
'X/Z/c.txt',
'Y/a.txt',
'Y/b.txt',
'Y/Z/c.txt',
]))
self.assertEqual(results, {
}

includes = set(filter(partial(match_file, patterns), files))

self.assertEqual(includes, {
'X/a.txt',
'X/Z/c.txt',
'Y/a.txt',
Expand Down
Loading

0 comments on commit 81368ad

Please sign in to comment.