|
| 1 | +import os |
| 2 | +import queue |
| 3 | +import threading |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from murfey.client.watchdir import DirWatcher |
| 9 | +from tests.conftest import ExampleVisit |
| 10 | + |
| 11 | + |
| 12 | +def test_dirwatcher_initialises(tmp_path: Path): |
| 13 | + # Check that the DirWatcher initialises with the default attributes |
| 14 | + watcher = DirWatcher(path=str(tmp_path)) |
| 15 | + assert watcher._basepath == os.fspath(str(tmp_path)) |
| 16 | + assert watcher._lastscan == {} |
| 17 | + assert watcher._file_candidates == {} |
| 18 | + assert watcher._statusbar is None |
| 19 | + assert watcher.settling_time == 60 |
| 20 | + assert watcher._appearance_time is None |
| 21 | + assert watcher._substrings_blacklist == {} |
| 22 | + assert watcher._transfer_all is True |
| 23 | + assert watcher._modification_overwrite is None |
| 24 | + assert isinstance(watcher._init_time, float) |
| 25 | + assert isinstance(watcher.queue, queue.Queue) |
| 26 | + assert isinstance(watcher.thread, threading.Thread) |
| 27 | + assert watcher._stopping is False |
| 28 | + assert watcher._halt_thread is False |
| 29 | + |
| 30 | + # Check that the string representation is as expected |
| 31 | + assert str(watcher) == f"<DirWatcher ({os.fspath(str(tmp_path))})>" |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def clem_visit_dir(tmp_path: Path): |
| 36 | + visit_name = f"{ExampleVisit.proposal_code}{ExampleVisit.proposal_number}-{ExampleVisit.visit_number}" |
| 37 | + visit_dir = tmp_path / "clem" / "data" / "2025" / visit_name |
| 38 | + visit_dir.mkdir(parents=True, exist_ok=True) |
| 39 | + return visit_dir |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def clem_test_files(clem_visit_dir: Path): |
| 44 | + # Create test files for the DirWatcher to scan |
| 45 | + file_list: list[Path] = [] |
| 46 | + project_dir = clem_visit_dir / "images" / "test_grid" |
| 47 | + |
| 48 | + # Example atlas collection |
| 49 | + for s in range(20): |
| 50 | + file_list.append( |
| 51 | + project_dir |
| 52 | + / "Overview 1" |
| 53 | + / "Image 1" |
| 54 | + / f"Image 1--Stage{str(s).zfill(2)}.tif" |
| 55 | + ) |
| 56 | + file_list.append( |
| 57 | + project_dir / "Overview 1" / "Image 1" / "Metadata" / "Image 1.xlif" |
| 58 | + ) |
| 59 | + |
| 60 | + # Example image stack collection |
| 61 | + for c in range(3): |
| 62 | + for z in range(10): |
| 63 | + file_list.append( |
| 64 | + project_dir |
| 65 | + / "TileScan 1" |
| 66 | + / "Position 1" |
| 67 | + / f"Position 1--C{str(c).zfill(2)}--Z{str(z).zfill(2)}.tif" |
| 68 | + ) |
| 69 | + file_list.append( |
| 70 | + project_dir / "TileScan 1" / "Position 1" / "Metadata" / "Position 1.xlif" |
| 71 | + ) |
| 72 | + |
| 73 | + # Create all files and directories specified |
| 74 | + for file in file_list: |
| 75 | + if not file.parent.exists(): |
| 76 | + file.parent.mkdir(parents=True) |
| 77 | + if not file.exists(): |
| 78 | + file.touch() |
| 79 | + return sorted(file_list) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.fixture |
| 83 | +def clem_junk_files(clem_visit_dir: Path): |
| 84 | + # Create junk files that are to be blacklisted from the CLEM workflow |
| 85 | + file_list: list[Path] = [] |
| 86 | + project_dir = clem_visit_dir / "images" / "test_grid" |
| 87 | + |
| 88 | + # Create junk atlas data |
| 89 | + for n in range(5): |
| 90 | + for s in range(20): |
| 91 | + file_list.append( |
| 92 | + project_dir |
| 93 | + / "Image 1" |
| 94 | + / f"Image 1_pmd_{n}" |
| 95 | + / f"Image 1_pmd_{n}--Stage{str(s).zfill(2)}.tif" |
| 96 | + ) |
| 97 | + file_list.append( |
| 98 | + project_dir / "Image 1" / f"Image 1_pmd_{n}" / "Metadata" / "Image 1.xlif" |
| 99 | + ) |
| 100 | + |
| 101 | + # Creat junk image data |
| 102 | + for n in range(5): |
| 103 | + for c in range(3): |
| 104 | + for z in range(10): |
| 105 | + file_list.append( |
| 106 | + project_dir |
| 107 | + / "Position 1" |
| 108 | + / f"Position 1_pmd_{n}" |
| 109 | + / f"Position 1_pmd_{n}--C{str(c).zfill(2)}--Z{str(z).zfill(2)}.tif" |
| 110 | + ) |
| 111 | + file_list.append( |
| 112 | + project_dir |
| 113 | + / "Position 1" |
| 114 | + / f"Position 1_pmd_{n}" |
| 115 | + / "Metadata" |
| 116 | + / "Position 1.xlif" |
| 117 | + ) |
| 118 | + |
| 119 | + # Create remaining junk files |
| 120 | + for file_path in ( |
| 121 | + "1.xlef", |
| 122 | + "Metadata/IOManagerConfiguation.xlif", |
| 123 | + "Metadata/Overview 1.xlcf", |
| 124 | + "Metadata/TileScan 1.xlcf", |
| 125 | + "Overview 1/Image 1/Image 1_histo.lof", |
| 126 | + "TileScan 1/Position 1/Position 1_histo.lof", |
| 127 | + "Overview 1/Image 1/Metadata/Image 1_histo.xlif", |
| 128 | + "TileScan 1/Position 1/Metadata/Position 1_histo.xlif", |
| 129 | + ): |
| 130 | + file_list.append(project_dir / file_path) |
| 131 | + |
| 132 | + # Create files and directoriees |
| 133 | + for file in file_list: |
| 134 | + if not file.parent.exists(): |
| 135 | + file.parent.mkdir(parents=True) |
| 136 | + if not file.exists(): |
| 137 | + file.touch() |
| 138 | + return sorted(file_list) |
| 139 | + |
| 140 | + |
| 141 | +scan_directory_params_matrix: tuple[tuple[str, dict[str, list[str]]], ...] = ( |
| 142 | + # Workflow type | Substrings blacklist |
| 143 | + ( |
| 144 | + "clem", |
| 145 | + { |
| 146 | + "directories": [ |
| 147 | + "_pmd_", |
| 148 | + ], |
| 149 | + "files": [ |
| 150 | + ".xlef", |
| 151 | + ".xlcf", |
| 152 | + "_histo.lof", |
| 153 | + "_histo.xlif", |
| 154 | + "IOManagerConfiguation.xlif", |
| 155 | + ], |
| 156 | + }, |
| 157 | + ), |
| 158 | +) |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.parametrize("test_params", scan_directory_params_matrix) |
| 162 | +def test_scan_directory( |
| 163 | + clem_visit_dir: Path, |
| 164 | + clem_test_files: list[Path], |
| 165 | + clem_junk_files: list[Path], |
| 166 | + test_params: tuple[str, dict[str, list[str]]], |
| 167 | +): |
| 168 | + # Unpack test params |
| 169 | + workflow_type, substrings_blacklist = test_params |
| 170 | + |
| 171 | + # Initialise different watchers based on the workflow to test and run the scan |
| 172 | + if workflow_type == "clem": |
| 173 | + watcher = DirWatcher( |
| 174 | + path=str(clem_visit_dir), |
| 175 | + substrings_blacklist=substrings_blacklist, |
| 176 | + ) |
| 177 | + result = watcher._scan_directory() |
| 178 | + |
| 179 | + # Check that the result does not contain the junk files |
| 180 | + assert [str(file) for file in clem_test_files] == sorted(result.keys()) |
0 commit comments