Skip to content

Commit

Permalink
Better.
Browse files Browse the repository at this point in the history
  • Loading branch information
excitoon committed Sep 4, 2022
1 parent a39aaf0 commit d522bcd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 286 deletions.
25 changes: 13 additions & 12 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,19 @@ def mock_isfile(path):

# 9! == 362880 combinations.
for permutation in itertools.islice(itertools.permutations(data.items()), 0, None, 6 * 8):
statistics = {"open": 0, "isdir": 0, "isfile": 0}

with unittest.mock.patch("builtins.open", mock_open):
with unittest.mock.patch("os.path.isdir", mock_isdir):
with unittest.mock.patch("os.path.isfile", mock_isfile):
matches = gitignorefile.Cache(ignore_names=[ignore_file_name])
for path, expected in permutation:
self.assertEqual(matches(path), expected)

self.assertEqual(statistics["open"], 2)
self.assertEqual(statistics["isdir"], len(data) - 1)
self.assertEqual(statistics["isfile"], 7) # Unique path fragments.
with self.subTest(i=permutation):
statistics = {"open": 0, "isdir": 0, "isfile": 0}

with unittest.mock.patch("builtins.open", mock_open):
with unittest.mock.patch("os.path.isdir", mock_isdir):
with unittest.mock.patch("os.path.isfile", mock_isfile):
matches = gitignorefile.Cache(ignore_names=[ignore_file_name])
for path, expected in permutation:
self.assertEqual(matches(path), expected)

self.assertEqual(statistics["open"], 2)
self.assertEqual(statistics["isdir"], len(data) - 1)
self.assertEqual(statistics["isfile"], 7) # Unique path fragments.

def test_wrong_symlink(self):
with tempfile.TemporaryDirectory() as d:
Expand Down
323 changes: 49 additions & 274 deletions tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,284 +526,59 @@ def test_caleb_01_absolute_dir_paths_2(self):

def test_caleb_01_current_dir_paths(self):
matches = self.__parse_gitignore_string(["*.txt", "!test1/"], mock_base_path="/home/caleb")
results = set(
spec.match_files(
[
"./src/test1/a.txt",
"./src/test1/b.txt",
"./src/test1/c/c.txt",
"./src/test2/a.txt",
"./src/test2/b.txt",
"./src/test2/c/c.txt",
]
)
)
self.assertEqual(
results,
{
"./src/test2/a.txt",
"./src/test2/b.txt",
"./src/test2/c/c.txt",
},
)

def test_caleb_01_match_files(self):
matches = self.__parse_gitignore_string(["*.txt", "!test1/"], mock_base_path="/home/caleb")
test_files = [
"src/test1/a.txt",
"src/test1/b.txt",
"src/test1/c/c.txt",
"src/test2/a.txt",
"src/test2/b.txt",
"src/test2/c/c.txt",
]
single_results = set(filter(spec.match_file, test_files))
multi_results = set(spec.match_files(test_files))
self.assertEqual(single_results, multi_results)

def test_caleb_01_windows_current_dir_paths(self):
matches = self.__parse_gitignore_string(["*.txt", "!test1/"], mock_base_path="/home/caleb")
results = set(
spec.match_files(
[
".\\src\\test1\\a.txt",
".\\src\\test1\\b.txt",
".\\src\\test1\\c\\c.txt",
".\\src\\test2\\a.txt",
".\\src\\test2\\b.txt",
".\\src\\test2\\c\\c.txt",
],
separators=("\\",),
)
)
self.assertEqual(
results,
{
".\\src\\test2\\a.txt",
".\\src\\test2\\b.txt",
".\\src\\test2\\c\\c.txt",
},
)

def test_caleb_01_windows_paths(self):
matches = self.__parse_gitignore_string(["*.txt", "!test1/"], mock_base_path="/home/caleb")
results = set(
spec.match_files(
[
"src\\test1\\a.txt",
"src\\test1\\b.txt",
"src\\test1\\c\\c.txt",
"src\\test2\\a.txt",
"src\\test2\\b.txt",
"src\\test2\\c\\c.txt",
],
separators=("\\",),
)
)
self.assertEqual(
results,
{
"src\\test2\\a.txt",
"src\\test2\\b.txt",
"src\\test2\\c\\c.txt",
},
)
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertFalse(matches("/home/caleb/src/test1/a.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/src/test1/b.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/src/test1/c/c.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/src/test2/a.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/src/test2/b.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/src/test2/c/c.txt", is_dir=is_dir))

def test_caleb_05_match_entries(self):
"""
Test matching files collectively.
"""
spec = PathSpec.from_lines(
"gitwildmatch",
[
"*.txt",
"!b.txt",
],
)
self.make_dirs(
[
"X",
"X/Z",
"Y",
"Y/Z",
]
)
self.make_files(
[
"X/a.txt",
"X/b.txt",
"X/Z/c.txt",
"Y/a.txt",
"Y/b.txt",
"Y/Z/c.txt",
]
)
entries = iter_tree_entries(self.temp_dir)
results = {__entry.path for __entry in spec.match_entries(entries)}
self.assertEqual(
results,
{
"X/a.txt",
"X/Z/c.txt",
"Y/a.txt",
"Y/Z/c.txt",
},
)

def test_caleb_05_match_file(self):
"""
Test matching files individually.
"""
spec = PathSpec.from_lines(
"gitwildmatch",
[
"*.txt",
"!b.txt",
],
)
results = set(
filter(
spec.match_file,
[
"X/a.txt",
"X/b.txt",
"X/Z/c.txt",
"Y/a.txt",
"Y/b.txt",
"Y/Z/c.txt",
],
)
)
self.assertEqual(
results,
{
"X/a.txt",
"X/Z/c.txt",
"Y/a.txt",
"Y/Z/c.txt",
},
)

def test_caleb_05_match_files(self):
"""
Test matching files collectively.
"""
spec = PathSpec.from_lines(
"gitwildmatch",
[
"*.txt",
"!b.txt",
],
)
results = set(
spec.match_files(
[
"X/a.txt",
"X/b.txt",
"X/Z/c.txt",
"Y/a.txt",
"Y/b.txt",
"Y/Z/c.txt",
]
)
)
self.assertEqual(
results,
{
"X/a.txt",
"X/Z/c.txt",
"Y/a.txt",
"Y/Z/c.txt",
},
)

def test_caleb_05_match_tree_entries(self):
"""
Test matching a file tree.
"""
spec = PathSpec.from_lines(
"gitwildmatch",
[
"*.txt",
"!b.txt",
],
)
self.make_dirs(
[
"X",
"X/Z",
"Y",
"Y/Z",
]
)
self.make_files(
[
"X/a.txt",
"X/b.txt",
"X/Z/c.txt",
"Y/a.txt",
"Y/b.txt",
"Y/Z/c.txt",
]
)
results = {__entry.path for __entry in spec.match_tree_entries(self.temp_dir)}
self.assertEqual(
results,
{
"X/a.txt",
"X/Z/c.txt",
"Y/a.txt",
"Y/Z/c.txt",
},
)
matches = self.__parse_gitignore_string(["*.txt", "!b.txt"], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertFalse(matches("/home/caleb/X", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/a.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/b.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/Z", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/X/Z/c.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/Z", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/a.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/b.txt", is_dir=is_dir))
self.assertTrue(matches("/home/caleb/Y/Z/c.txt", is_dir=is_dir))

def test_caleb_05_match_tree_files(self):
"""
Test matching a file tree.
"""
spec = PathSpec.from_lines(
"gitwildmatch",
[
"*.txt",
"!b.txt",
],
)
self.make_dirs(
[
"X",
"X/Z",
"Y",
"Y/Z",
]
)
self.make_files(
[
"X/a.txt",
"X/b.txt",
"X/Z/c.txt",
"Y/a.txt",
"Y/b.txt",
"Y/Z/c.txt",
]
)
results = set(spec.match_tree_files(self.temp_dir))
self.assertEqual(
results,
{
"X/a.txt",
"X/Z/c.txt",
"Y/a.txt",
"Y/Z/c.txt",
},
)
def test_caleb_05_match_entries_empty(self):
matches = self.__parse_gitignore_string([], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertFalse(matches("/home/caleb/X", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/a.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/b.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/Z", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/Z/c.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/Z", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/a.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/b.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/Z/c.txt", is_dir=is_dir))

def test_caleb_00_empty(self):
"""
Tests an empty pattern.
"""
regex, include = GitWildMatchPattern.pattern_to_regex("")
self.assertIsNone(include)
self.assertIsNone(regex)
def test_caleb_05_match_entries_empty_rule(self):
matches = self.__parse_gitignore_string(["# Hey"], mock_base_path="/home/caleb")
for is_dir in (False, True):
with self.subTest(i=is_dir):
self.assertFalse(matches("/home/caleb/X", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/a.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/b.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/Z", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/X/Z/c.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/Z", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/a.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/b.txt", is_dir=is_dir))
self.assertFalse(matches("/home/caleb/Y/Z/c.txt", is_dir=is_dir))

def test_caleb_01_absolute(self):
"""
Expand Down

0 comments on commit d522bcd

Please sign in to comment.