Skip to content

Commit e6df537

Browse files
committed
Add a test with subdirectories and recursion
1 parent 74de35c commit e6df537

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/fscacher/tests/test_cache.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import os.path as op
3+
from pathlib import Path
34
import platform
45
import shutil
56
import subprocess
@@ -239,6 +240,79 @@ def check_new_memoread(arg, content, expect_new=False):
239240
check_new_memoread(1, 14)
240241

241242

243+
def test_memoize_path_recursive_dir(cache, tmp_path):
244+
calls = []
245+
246+
@cache.memoize_path
247+
def memoread(path: Path):
248+
calls.append(path)
249+
file_qty = 0
250+
for p in sorted(path.iterdir()):
251+
if p.is_dir():
252+
file_qty += memoread(p)
253+
else:
254+
file_qty += 1
255+
return file_qty
256+
257+
(tmp_path / "file1.txt").touch()
258+
(tmp_path / "file2.txt").touch()
259+
sub1 = tmp_path / "sub1"
260+
sub1.mkdir()
261+
(sub1 / "file3.txt").touch()
262+
(sub1 / "file4.txt").touch()
263+
(sub1 / "file5.txt").touch()
264+
subsub = sub1 / "subsub"
265+
subsub.mkdir()
266+
(subsub / "file6.txt").touch()
267+
sub2 = tmp_path / "sub2"
268+
sub2.mkdir()
269+
(sub2 / "file7.txt").touch()
270+
(sub2 / "file8.txt").touch()
271+
272+
time.sleep(cache._min_dtime * 1.1)
273+
274+
assert memoread(tmp_path) == 8
275+
assert calls == [tmp_path, sub1, subsub, sub2]
276+
assert memoread(tmp_path) == 8
277+
assert calls == [tmp_path, sub1, subsub, sub2]
278+
279+
(sub2 / "file8.txt").touch()
280+
time.sleep(cache._min_dtime * 1.1)
281+
282+
assert memoread(tmp_path) == 8
283+
assert calls == [tmp_path, sub1, subsub, sub2, tmp_path, sub2]
284+
assert memoread(tmp_path) == 8
285+
assert calls == [tmp_path, sub1, subsub, sub2, tmp_path, sub2]
286+
287+
(subsub / "file9.txt").touch()
288+
time.sleep(cache._min_dtime * 1.1)
289+
290+
assert memoread(tmp_path) == 9
291+
assert calls == [
292+
tmp_path,
293+
sub1,
294+
subsub,
295+
sub2,
296+
tmp_path,
297+
sub2,
298+
tmp_path,
299+
sub1,
300+
subsub,
301+
]
302+
assert memoread(tmp_path) == 9
303+
assert calls == [
304+
tmp_path,
305+
sub1,
306+
subsub,
307+
sub2,
308+
tmp_path,
309+
sub2,
310+
tmp_path,
311+
sub1,
312+
subsub,
313+
]
314+
315+
242316
def test_memoize_path_persist(tmp_path):
243317
from subprocess import PIPE, run
244318

0 commit comments

Comments
 (0)