Skip to content

Commit

Permalink
gitignore
Browse files Browse the repository at this point in the history
cpburnz committed Aug 31, 2022
1 parent 458dbba commit cb9e0d8
Showing 2 changed files with 202 additions and 11 deletions.
2 changes: 2 additions & 0 deletions pathspec/__init__.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@
The following classes are imported and made available from the root of
the `pathspec` package:
- :class:`pathspec.gitignore.GitIgnoreSpec`
- :class:`pathspec.pathspec.PathSpec`
- :class:`pathspec.pattern.Pattern`
211 changes: 200 additions & 11 deletions tests/test_gitignore.py
Original file line number Diff line number Diff line change
@@ -18,9 +18,7 @@ def test_01_reversed_args(self):
"""
Test reversed args for `.from_lines()`.
"""
spec = GitIgnoreSpec.from_lines('gitwildmatch', [
'*.txt',
])
spec = GitIgnoreSpec.from_lines('gitwildmatch', ['*.txt'])
results = set(spec.match_files([
'a.txt',
'b.bin',
@@ -37,20 +35,25 @@ def test_02_dir_exclusions(self):
'*.txt',
'!test1/',
])
results = set(spec.match_files([
files = {
'test1/a.txt',
'test1/b.bin',
'test1/c/c.txt',
'test2/a.txt',
'test2/b.bin',
'test2/c/c.txt',
]))
self.assertEqual(results, {
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
'test1/a.txt',
'test1/c/c.txt',
'test2/a.txt',
'test2/c/c.txt',
})
self.assertEqual(files - ignores, {
'test1/b.bin',
'test2/b.bin',
})

def test_02_file_exclusions(self):
"""
@@ -60,25 +63,30 @@ def test_02_file_exclusions(self):
'*.txt',
'!b.txt',
])
results = set(spec.match_files([
files = {
'X/a.txt',
'X/b.txt',
'X/Z/c.txt',
'Y/a.txt',
'Y/b.txt',
'Y/Z/c.txt',
]))
self.assertEqual(results, {
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
'X/a.txt',
'X/Z/c.txt',
'Y/a.txt',
'Y/Z/c.txt',
})
self.assertEqual(files - ignores, {
'X/b.txt',
'Y/b.txt',
})

def test_02_issue_41(self):
def test_02_issue_41_a(self):
"""
Test including a file and excluding a directory with the same name
pattern.
pattern, scenario A.
"""
spec = GitIgnoreSpec.from_lines([
'*.yaml',
@@ -105,3 +113,184 @@ def test_02_issue_41(self):
'dir/file.sql',
'dir/index.txt',
})

def test_02_issue_41_b(self):
"""
Test including a file and excluding a directory with the same name
pattern, scenario B.
"""
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',
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
'dir.yaml/file.sql',
'dir.yaml/file.yaml',
'dir.yaml/index.txt',
'dir/file.yaml',
'file.yaml',
})
self.assertEqual(files - ignores, {
'dir/file.sql',
'dir/index.txt',
})

def test_02_issue_41_c(self):
"""
Test including a file and excluding a directory with the same name
pattern, scenario C.
"""
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',
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
'dir.yaml/file.yaml',
'dir/file.yaml',
'file.yaml',
})
self.assertEqual(files - ignores, {
'dir.yaml/file.sql',
'dir.yaml/index.txt',
'dir/file.sql',
'dir/index.txt',
})

def test_03_subdir(self):
"""
Test matching files in a subdirectory of an included directory.
"""
spec = GitIgnoreSpec.from_lines([
"dirG/",
])
files = {
'fileA',
'fileB',
'dirD/fileE',
'dirD/fileF',
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
})
self.assertEqual(files - ignores, {
'fileA',
'fileB',
'dirD/fileE',
'dirD/fileF',
})

def test_03_issue_19_a(self):
"""
Test matching files in a subdirectory of an included directory,
scenario A.
"""
spec = GitIgnoreSpec.from_lines([
"dirG/",
])
files = {
'fileA',
'fileB',
'dirD/fileE',
'dirD/fileF',
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
})
self.assertEqual(files - ignores, {
'fileA',
'fileB',
'dirD/fileE',
'dirD/fileF',
})

def test_03_issue_19_b(self):
"""
Test matching files in a subdirectory of an included directory,
scenario B.
"""
spec = GitIgnoreSpec.from_lines([
"dirG/*",
])
files = {
'fileA',
'fileB',
'dirD/fileE',
'dirD/fileF',
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
})
self.assertEqual(files - ignores, {
'fileA',
'fileB',
'dirD/fileE',
'dirD/fileF',
})

def test_03_issue_19_c(self):
"""
Test matching files in a subdirectory of an included directory,
scenario C.
"""
spec = GitIgnoreSpec.from_lines([
"dirG/**",
])
files = {
'fileA',
'fileB',
'dirD/fileE',
'dirD/fileF',
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
}
ignores = set(spec.match_files(files))
self.assertEqual(ignores, {
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
})
self.assertEqual(files - ignores, {
'fileA',
'fileB',
'dirD/fileE',
'dirD/fileF',
})

0 comments on commit cb9e0d8

Please sign in to comment.