diff --git a/tests/test_cache.py b/tests/test_cache.py index 2f45688..8d55edc 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -19,6 +19,12 @@ def __init__(self, is_file=False): self.st_dev = 0 self.st_mode = stat.S_IFREG if is_file else stat.S_IFDIR + def isdir(self): + return self.st_mode == stat.S_IFDIR + + def isfile(self): + return self.st_mode == stat.S_IFREG + class Stat: def __init__(self, directories, files): self.__filesystem = {} @@ -71,9 +77,19 @@ def mock_open(path): except KeyError: raise FileNotFoundError() - def mock_stat(path): - statistics["stat"] += 1 - return my_stat(path) + def mock_isdir(path): + statistics["isdir"] += 1 + try: + return my_stat(path).isdir() + except FileNotFoundError: + return False + + def mock_isfile(path): + statistics["isfile"] += 1 + try: + return my_stat(path).isfile() + except FileNotFoundError: + return False data = { "/home/vladimir/project/directory/subdirectory/file.txt": True, @@ -84,20 +100,22 @@ def mock_stat(path): "/home/vladimir/project/directory/file.txt": True, "/home/vladimir/project/directory/file2.txt": True, "/home/vladimir/project/file.txt": False, - "/home/vladimir/file.txt": False, # No rules and no `stat` calls for this file. + "/home/vladimir/file.txt": False, # No rules and no `isdir` calls for this file. } for permutation in itertools.islice(itertools.permutations(data.items()), 0, None, 100): - statistics = {"open": 0, "stat": 0} + statistics = {"open": 0, "isdir": 0, "isfile": 0} with unittest.mock.patch("builtins.open", mock_open): - with unittest.mock.patch("os.stat", mock_stat): - matches = gitignorefile.Cache() - for path, expected in permutation: - self.assertEqual(matches(path), expected) + with unittest.mock.patch("os.path.isdir", mock_isdir): + with unittest.mock.patch("os.path.isfile", mock_isfile): + matches = gitignorefile.Cache() + for path, expected in permutation: + self.assertEqual(matches(path), expected) self.assertEqual(statistics["open"], 2) - self.assertEqual(statistics["stat"], 7 + 9 - 1) + 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: