|
1 | 1 | import os |
2 | 2 | import os.path as op |
| 3 | +from pathlib import Path |
3 | 4 | import platform |
4 | 5 | import shutil |
5 | 6 | import subprocess |
@@ -239,6 +240,79 @@ def check_new_memoread(arg, content, expect_new=False): |
239 | 240 | check_new_memoread(1, 14) |
240 | 241 |
|
241 | 242 |
|
| 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 | + |
242 | 316 | def test_memoize_path_persist(tmp_path): |
243 | 317 | from subprocess import PIPE, run |
244 | 318 |
|
|
0 commit comments