From b988d1c0206c5b3dfeedcfd73a896af744943ce9 Mon Sep 17 00:00:00 2001 From: cpburnz <2126043+cpburnz@users.noreply.github.com> Date: Tue, 5 Dec 2023 22:55:09 -0500 Subject: [PATCH] Improve tests --- CHANGES.rst | 10 +++++ pathspec/_meta.py | 1 + pathspec/gitignore.py | 12 +----- tests/test_02_gitwildmatch.py | 44 +++++++++----------- tests/test_04_gitignore.py | 78 +++++++++++++++++++++-------------- 5 files changed, 78 insertions(+), 67 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 6ab1ca1..16cf8d8 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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) ------------------- diff --git a/pathspec/_meta.py b/pathspec/_meta.py index 3aa1890..ab5405a 100644 --- a/pathspec/_meta.py +++ b/pathspec/_meta.py @@ -52,6 +52,7 @@ "axesider ", "tomruk ", "oprypin ", + "kurtmckee ", ] __license__ = "MPL 2.0" __version__ = "0.11.3.dev1" diff --git a/pathspec/gitignore.py b/pathspec/gitignore.py index a939225..81bf303 100644 --- a/pathspec/gitignore.py +++ b/pathspec/gitignore.py @@ -18,7 +18,6 @@ Pattern) from .patterns.gitwildmatch import ( GitWildMatchPattern, - GitWildMatchPatternError, _DIR_MARK) from .util import ( _is_iterable) @@ -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. diff --git a/tests/test_02_gitwildmatch.py b/tests/test_02_gitwildmatch.py index b933082..be915cf 100644 --- a/tests/test_02_gitwildmatch.py +++ b/tests/test_02_gitwildmatch.py @@ -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 @@ -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 @@ -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', @@ -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): @@ -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): """ @@ -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): """ diff --git a/tests/test_04_gitignore.py b/tests/test_04_gitignore.py index 9478800..aa3c231 100644 --- a/tests/test_04_gitignore.py +++ b/tests/test_04_gitignore.py @@ -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, { @@ -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, { @@ -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, { @@ -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())