Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cpburnz committed Dec 6, 2023
1 parent 14fc3d2 commit b988d1c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 67 deletions.
10 changes: 10 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
Change History
==============

0.11.3 (TDB)
------------

Bug fixes:

- `Pull #83`_: Fix ReadTheDocs builds.


.. _`Pull #83`: https://github.com/cpburnz/python-pathspec/pull/83


0.11.2 (2023-07-28)
-------------------
Expand Down
1 change: 1 addition & 0 deletions pathspec/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"axesider <https://github.com/axesider>",
"tomruk <https://github.com/tomruk>",
"oprypin <https://github.com/oprypin>",
"kurtmckee <https://github.com/kurtmckee>",
]
__license__ = "MPL 2.0"
__version__ = "0.11.3.dev1"
12 changes: 1 addition & 11 deletions pathspec/gitignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
Pattern)
from .patterns.gitwildmatch import (
GitWildMatchPattern,
GitWildMatchPatternError,
_DIR_MARK)
from .util import (
_is_iterable)
Expand Down Expand Up @@ -110,16 +109,7 @@ def _match_file(
# Pattern matched.

# Check for directory marker.
try:
dir_mark = match.match.group(_DIR_MARK)
except IndexError as e:
# NOTICE: The exact content of this error message is subject
# to change.
raise GitWildMatchPatternError((
f"Invalid git pattern: directory marker regex group is missing. "
f"Debug: file={file!r} regex={pattern.regex!r} "
f"group={_DIR_MARK!r} match={match.match!r}."
)) from e
dir_mark = match.match.groupdict().get(_DIR_MARK)

if dir_mark:
# Pattern matched by a directory pattern.
Expand Down
44 changes: 20 additions & 24 deletions tests/test_02_gitwildmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_01_absolute_ignore(self):
Tests an ignore absolute path pattern.
"""
regex, include = GitWildMatchPattern.pattern_to_regex('!/foo/build')
self.assertFalse(include)
self.assertIs(include, False)
self.assertEqual(regex, f'^foo/build{RE_SUB}$')

# NOTE: The pattern match is backwards because the pattern itself
Expand Down Expand Up @@ -180,8 +180,7 @@ def test_02_ignore(self):
temp/foo
"""
regex, include = GitWildMatchPattern.pattern_to_regex('!temp')
self.assertIsNotNone(include)
self.assertFalse(include)
self.assertIs(include, False)
self.assertEqual(regex, f'^(?:.+/)?temp{RE_SUB}$')

# NOTE: The pattern match is backwards because the pattern itself
Expand Down Expand Up @@ -259,6 +258,7 @@ def test_03_only_double_asterisk(self):
regex, include = GitWildMatchPattern.pattern_to_regex('**')
self.assertTrue(include)
self.assertEqual(regex, f'^[^/]+{RE_SUB}$')

pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'x',
Expand Down Expand Up @@ -757,38 +757,28 @@ def test_12_asterisk_3_child(self):
"""
Test a relative asterisk path pattern matching a direct child path.
"""
pattern = GitWildMatchPattern('*')
results = set(filter(pattern.match_file, [
'file.txt',
]))
self.assertEqual(results, {
'file.txt',
})
pattern = GitWildMatchPattern("*")
self.assertTrue(pattern.match_file("file.txt"))

def test_12_asterisk_4_descendant(self):
"""
Test a relative asterisk path pattern matching a descendant path.
"""
pattern = GitWildMatchPattern('*')
results = set(filter(pattern.match_file, [
'anydir/file.txt',
]))
self.assertEqual(results, {
'anydir/file.txt',
})
pattern = GitWildMatchPattern("*")
self.assertTrue(pattern.match_file("anydir/file.txt"))

def test_12_issue_62(self):
"""
Test including all files, scenario A.
"""
pattern = GitWildMatchPattern('*')
pattern = GitWildMatchPattern("*")
results = set(filter(pattern.match_file, [
'file.txt',
'anydir/file.txt',
"file.txt",
"anydir/file.txt",
]))
self.assertEqual(results, {
'file.txt',
'anydir/file.txt',
"file.txt",
"anydir/file.txt",
})

def test_13_issue_77_1_negate_with_caret(self):
Expand All @@ -802,7 +792,10 @@ def test_13_issue_77_1_negate_with_caret(self):
"abc",
"adc",
]))
self.assertEqual(results, {"abc", "adc"})
self.assertEqual(results, {
"abc",
"adc",
})

def test_13_issue_77_1_negate_with_exclamation_mark(self):
"""
Expand All @@ -815,7 +808,10 @@ def test_13_issue_77_1_negate_with_exclamation_mark(self):
"abc",
"adc",
]))
self.assertEqual(results, {"abc", "adc"})
self.assertEqual(results, {
"abc",
"adc",
})

def test_13_issue_77_2_regex(self):
"""
Expand Down
78 changes: 46 additions & 32 deletions tests/test_04_gitignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,19 @@ def test_02_issue_41_a(self):
Test including a file and excluding a directory with the same name
pattern, scenario A.
"""
# Confirmed results with git (v2.42.0).
spec = GitIgnoreSpec.from_lines([
'*.yaml',
'!*.yaml/',
])
files = {
'dir.yaml/file.sql',
'dir.yaml/file.yaml',
'dir.yaml/index.txt',
'dir/file.sql',
'dir/file.yaml',
'dir/index.txt',
'file.yaml',
'dir.yaml/file.sql', # -
'dir.yaml/file.yaml', # 1:*.yaml
'dir.yaml/index.txt', # -
'dir/file.sql', # -
'dir/file.yaml', # 1:*.yaml
'dir/index.txt', # -
'file.yaml', # 1:*.yaml
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
Expand All @@ -119,18 +120,19 @@ def test_02_issue_41_b(self):
Test including a file and excluding a directory with the same name
pattern, scenario B.
"""
# Confirmed results with git (v2.42.0).
spec = GitIgnoreSpec.from_lines([
'!*.yaml/',
'*.yaml',
])
files = {
'dir.yaml/file.sql',
'dir.yaml/file.yaml',
'dir.yaml/index.txt',
'dir/file.sql',
'dir/file.yaml',
'dir/index.txt',
'file.yaml',
'dir.yaml/file.sql', # 2:*.yaml
'dir.yaml/file.yaml', # 2:*.yaml
'dir.yaml/index.txt', # 2:*.yaml
'dir/file.sql', # -
'dir/file.yaml', # 2:*.yaml
'dir/index.txt', # -
'file.yaml', # 2:*.yaml
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
Expand All @@ -150,18 +152,19 @@ def test_02_issue_41_c(self):
Test including a file and excluding a directory with the same name
pattern, scenario C.
"""
# Confirmed results with git (v2.42.0).
spec = GitIgnoreSpec.from_lines([
'*.yaml',
'!dir.yaml',
])
files = {
'dir.yaml/file.sql',
'dir.yaml/file.yaml',
'dir.yaml/index.txt',
'dir/file.sql',
'dir/file.yaml',
'dir/index.txt',
'file.yaml',
'dir.yaml/file.sql', # -
'dir.yaml/file.yaml', # 1:*.yaml
'dir.yaml/index.txt', # -
'dir/file.sql', # -
'dir/file.yaml', # 1:*.yaml
'dir/index.txt', # -
'file.yaml', # 1:*.yaml
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
Expand Down Expand Up @@ -391,54 +394,65 @@ def test_07_issue_74(self):

def test_08_issue_81_a(self):
"""
Test issue 81.
Test issue 81, scenario A.
"""
# Confirmed results with git (v2.42.0).
spec = GitIgnoreSpec.from_lines([
"*",
"!libfoo",
"!libfoo/**",
])
files = {
"./libfoo/__init__.py",
"ignore.txt", # 1:*
"libfoo/__init__.py", # 3:!libfoo/**
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, set())
self.assertEqual(ignores, {
"ignore.txt",
})
self.assertEqual(files - ignores, {
"./libfoo/__init__.py",
"libfoo/__init__.py",
})

def test_08_issue_81_b(self):
"""
Test issue 81.
Test issue 81, scenario B.
"""
# Confirmed results with git (v2.42.0).
spec = GitIgnoreSpec.from_lines([
"*",
"!libfoo",
"!libfoo/*",
])
files = {
"./libfoo/__init__.py",
"ignore.txt", # 1:*
"libfoo/__init__.py", # 3:!libfoo/*
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, set())
self.assertEqual(ignores, {
"ignore.txt",
})
self.assertEqual(files - ignores, {
"./libfoo/__init__.py",
"libfoo/__init__.py",
})

def test_08_issue_81_c(self):
"""
Test issue 81.
Test issue 81, scenario C.
"""
# Confirmed results with git (v2.42.0).
spec = GitIgnoreSpec.from_lines([
"*",
"!libfoo",
"!libfoo/",
])
files = {
"./libfoo/__init__.py",
"ignore.txt", # 1:*
"libfoo/__init__.py", # 1:*
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
"./libfoo/__init__.py",
"ignore.txt",
"libfoo/__init__.py",
})
self.assertEqual(files - ignores, set())

0 comments on commit b988d1c

Please sign in to comment.