|
5 | 5 | import time
|
6 | 6 | import typing as ty
|
7 | 7 | import pytest
|
8 |
| -from fileformats.core import FileSet, validated_property |
9 |
| -from fileformats.generic import File, BinaryFile, Directory, FsObject |
| 8 | +from fileformats.generic import File, BinaryFile, Directory, FsObject, SetOf |
| 9 | +from fileformats.core import ( |
| 10 | + FileSet, |
| 11 | + MockMixin, |
| 12 | + validated_property, |
| 13 | + mtime_cached_property, |
| 14 | +) |
| 15 | +from fileformats.text import TextFile |
10 | 16 | from fileformats.core.mixin import WithSeparateHeader
|
11 | 17 | from fileformats.core.exceptions import UnsatisfiableCopyModeError
|
12 | 18 | from conftest import write_test_file
|
@@ -54,6 +60,11 @@ def fsobject(luigi_file, bowser_dir, request):
|
54 | 60 | assert False
|
55 | 61 |
|
56 | 62 |
|
| 63 | +@pytest.fixture |
| 64 | +def mock_fileset(): |
| 65 | + return SetOf[TextFile].mock("/path/to/a/mock", "/path/to/another/mock") |
| 66 | + |
| 67 | + |
57 | 68 | @pytest.fixture
|
58 | 69 | def dest_dir(work_dir):
|
59 | 70 | dest_dir = work_dir / "new-dir"
|
@@ -523,3 +534,51 @@ def test_hash_files(fsobject: FsObject, work_dir: Path, dest_dir: Path):
|
523 | 534 | )
|
524 | 535 | cpy = fsobject.copy(dest_dir)
|
525 | 536 | assert cpy.hash_files() == fsobject.hash_files()
|
| 537 | + |
| 538 | + |
| 539 | +class MtimeTestFile(File): |
| 540 | + |
| 541 | + flag: int |
| 542 | + |
| 543 | + @mtime_cached_property |
| 544 | + def cached_prop(self): |
| 545 | + return self.flag |
| 546 | + |
| 547 | + |
| 548 | +def test_mtime_cached_property(tmp_path: Path): |
| 549 | + fspath = tmp_path / "file_1.txt" |
| 550 | + fspath.write_text("hello") |
| 551 | + |
| 552 | + file = MtimeTestFile(fspath) |
| 553 | + |
| 554 | + file.flag = 0 |
| 555 | + assert file.cached_prop == 0 |
| 556 | + # Need a long delay to ensure the mtime changes on Ubuntu and particularly on Windows |
| 557 | + # On MacOS, the mtime resolution is much higher so not usually an issue. Use |
| 558 | + # explicitly cache clearing if needed |
| 559 | + time.sleep(2) |
| 560 | + file.flag = 1 |
| 561 | + assert file.cached_prop == 0 |
| 562 | + time.sleep(2) |
| 563 | + fspath.write_text("world") |
| 564 | + assert file.cached_prop == 1 |
| 565 | + |
| 566 | + |
| 567 | +def test_mtime_cached_property_force_clear(tmp_path: Path): |
| 568 | + fspath = tmp_path / "file_1.txt" |
| 569 | + fspath.write_text("hello") |
| 570 | + |
| 571 | + file = MtimeTestFile(fspath) |
| 572 | + |
| 573 | + file.flag = 0 |
| 574 | + assert file.cached_prop == 0 |
| 575 | + file.flag = 1 |
| 576 | + MtimeTestFile.cached_prop.clear(file) |
| 577 | + assert file.cached_prop == 1 |
| 578 | + |
| 579 | + |
| 580 | +def test_hash_mock_files(mock_fileset: MockMixin, work_dir: Path, dest_dir: Path): |
| 581 | + file_hashes = mock_fileset.hash_files(relative_to="") |
| 582 | + assert sorted(Path(p) for p in file_hashes) == sorted( |
| 583 | + p for p in mock_fileset.fspaths |
| 584 | + ) |
0 commit comments