Skip to content

Commit 81368ad

Browse files
committedDec 10, 2023
Fix issue #84
1 parent 37e2895 commit 81368ad

File tree

6 files changed

+253
-46
lines changed

6 files changed

+253
-46
lines changed
 

‎CHANGES.rst

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
Change History
33
==============
44

5+
6+
0.12.1 (TBD)
7+
-------------------
8+
9+
- Bug fixes:
10+
11+
- `Issue #84`_: PathSpec.match_file() returns None since 0.12.0.
12+
13+
14+
.. _`Issue #84`: https://github.com/cpburnz/python-pathspec/issues/84
15+
16+
517
0.12.0 (2023-12-09)
618
-------------------
719

@@ -12,7 +24,7 @@ Major changes:
1224

1325
API changes:
1426

15-
- 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]]`.
27+
- 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]]`.
1628

1729
New features:
1830

‎Makefile

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#
22
# This Makefile is used to manage development and distribution.
33
#
4-
# Created: 2022-08-11
5-
# Updated: 2022-09-01
6-
#
74

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

‎pathspec/_meta.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@
5555
"kurtmckee <https://github.com/kurtmckee>",
5656
]
5757
__license__ = "MPL 2.0"
58-
__version__ = "0.12.0"
58+
__version__ = "0.12.1.dev1"

‎pathspec/pathspec.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def match_file(
277277
"""
278278
norm_file = normalize_file(file, separators)
279279
include, _index = self._match_file(enumerate(self.patterns), norm_file)
280-
return include
280+
return bool(include)
281281

282282
def match_files(
283283
self,

‎tests/test_01_util.py

+126-7
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
from functools import (
1313
partial)
1414
from typing import (
15-
Iterable,
16-
Optional,
17-
Tuple)
15+
Iterable, # Replaced by `collections.abc.Iterable` in 3.9.
16+
Optional, # Replaced by `X | None` in 3.10.
17+
Tuple) # Replaced by `tuple` in 3.9.
1818

1919
from pathspec.patterns.gitwildmatch import (
2020
GitWildMatchPattern)
2121
from pathspec.util import (
2222
RecursionError,
23+
check_match_file,
2324
iter_tree_entries,
2425
iter_tree_files,
2526
match_file,
@@ -32,6 +33,82 @@
3233
ospath)
3334

3435

36+
class CheckMatchFileTest(unittest.TestCase):
37+
"""
38+
The :class:`CheckMatchFileTest` class tests the :meth:`.check_match_file`
39+
function.
40+
"""
41+
42+
def test_01_single_1_include(self):
43+
"""
44+
Test checking a single file that is included.
45+
"""
46+
patterns = list(enumerate(map(GitWildMatchPattern, [
47+
"*.txt",
48+
"!test/",
49+
])))
50+
51+
include_index = check_match_file(patterns, "include.txt")
52+
53+
self.assertEqual(include_index, (True, 0))
54+
55+
def test_01_single_2_exclude(self):
56+
"""
57+
Test checking a single file that is excluded.
58+
"""
59+
patterns = list(enumerate(map(GitWildMatchPattern, [
60+
"*.txt",
61+
"!test/",
62+
])))
63+
64+
include_index = check_match_file(patterns, "test/exclude.txt")
65+
66+
self.assertEqual(include_index, (False, 1))
67+
68+
def test_01_single_3_unmatch(self):
69+
"""
70+
Test checking a single file that is ignored.
71+
"""
72+
patterns = list(enumerate(map(GitWildMatchPattern, [
73+
"*.txt",
74+
"!test/",
75+
])))
76+
77+
include_index = check_match_file(patterns, "unmatch.bin")
78+
79+
self.assertEqual(include_index, (None, None))
80+
81+
def test_02_many(self):
82+
"""
83+
Test matching files individually.
84+
"""
85+
patterns = list(enumerate(map(GitWildMatchPattern, [
86+
'*.txt',
87+
'!b.txt',
88+
])))
89+
files = {
90+
'X/a.txt',
91+
'X/b.txt',
92+
'X/Z/c.txt',
93+
'Y/a.txt',
94+
'Y/b.txt',
95+
'Y/Z/c.txt',
96+
}
97+
98+
includes = {
99+
__file
100+
for __file in files
101+
if check_match_file(patterns, __file)[0]
102+
}
103+
104+
self.assertEqual(includes, {
105+
'X/a.txt',
106+
'X/Z/c.txt',
107+
'Y/a.txt',
108+
'Y/Z/c.txt',
109+
})
110+
111+
35112
class IterTreeTest(unittest.TestCase):
36113
"""
37114
The :class:`IterTreeTest` class tests :meth:`.iter_tree_entries` and
@@ -345,23 +422,65 @@ class MatchFileTest(unittest.TestCase):
345422
function.
346423
"""
347424

348-
def test_01_match_file(self):
425+
def test_01_single_1_include(self):
426+
"""
427+
Test checking a single file that is included.
428+
"""
429+
patterns = list(map(GitWildMatchPattern, [
430+
"*.txt",
431+
"!test/",
432+
]))
433+
434+
include = match_file(patterns, "include.txt")
435+
436+
self.assertIs(include, True)
437+
438+
def test_01_single_2_exclude(self):
439+
"""
440+
Test checking a single file that is excluded.
441+
"""
442+
patterns = list(map(GitWildMatchPattern, [
443+
"*.txt",
444+
"!test/",
445+
]))
446+
447+
include = match_file(patterns, "test/exclude.txt")
448+
449+
self.assertIs(include, False)
450+
451+
def test_01_single_3_unmatch(self):
452+
"""
453+
Test checking a single file that is ignored.
454+
"""
455+
patterns = list(map(GitWildMatchPattern, [
456+
"*.txt",
457+
"!test/",
458+
]))
459+
460+
include = match_file(patterns, "unmatch.bin")
461+
462+
self.assertIs(include, False)
463+
464+
def test_02_many(self):
349465
"""
350466
Test matching files individually.
351467
"""
352468
patterns = list(map(GitWildMatchPattern, [
353469
'*.txt',
354470
'!b.txt',
355471
]))
356-
results = set(filter(partial(match_file, patterns), [
472+
files = {
357473
'X/a.txt',
358474
'X/b.txt',
359475
'X/Z/c.txt',
360476
'Y/a.txt',
361477
'Y/b.txt',
362478
'Y/Z/c.txt',
363-
]))
364-
self.assertEqual(results, {
479+
}
480+
481+
includes = set(filter(partial(match_file, patterns), files))
482+
483+
self.assertEqual(includes, {
365484
'X/a.txt',
366485
'X/Z/c.txt',
367486
'Y/a.txt',

0 commit comments

Comments
 (0)
Please sign in to comment.