Skip to content

Commit

Permalink
Fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
excitoon committed Aug 28, 2022
1 parent 2729d27 commit 5e8ed94
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down

0 comments on commit 5e8ed94

Please sign in to comment.