Skip to content

Commit eea0dba

Browse files
committed
✅ Update tests to use track_tmp_path for clean up
1 parent 6e1e585 commit eea0dba

File tree

3 files changed

+122
-114
lines changed

3 files changed

+122
-114
lines changed

tests/engines/test_engine_abc.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,26 @@ def test_ioconfig() -> NoReturn:
171171

172172

173173
def test_prepare_engines_save_dir(
174-
tmp_path: pytest.TempPathFactory,
174+
track_tmp_path: pytest.TempPathFactory,
175175
caplog: pytest.LogCaptureFixture,
176176
) -> NoReturn:
177177
"""Test prepare save directory for engines."""
178178
out_dir = prepare_engines_save_dir(
179-
save_dir=tmp_path / "patch_output",
179+
save_dir=track_tmp_path / "patch_output",
180180
patch_mode=True,
181181
overwrite=False,
182182
)
183183

184-
assert out_dir == tmp_path / "patch_output"
184+
assert out_dir == track_tmp_path / "patch_output"
185185
assert out_dir.exists()
186186

187187
out_dir = prepare_engines_save_dir(
188-
save_dir=tmp_path / "patch_output",
188+
save_dir=track_tmp_path / "patch_output",
189189
patch_mode=True,
190190
overwrite=True,
191191
)
192192

193-
assert out_dir == tmp_path / "patch_output"
193+
assert out_dir == track_tmp_path / "patch_output"
194194
assert out_dir.exists()
195195

196196
out_dir = prepare_engines_save_dir(
@@ -211,43 +211,43 @@ def test_prepare_engines_save_dir(
211211
)
212212

213213
out_dir = prepare_engines_save_dir(
214-
save_dir=tmp_path / "wsi_single_output",
214+
save_dir=track_tmp_path / "wsi_single_output",
215215
patch_mode=False,
216216
overwrite=False,
217217
)
218218

219-
assert out_dir == tmp_path / "wsi_single_output"
219+
assert out_dir == track_tmp_path / "wsi_single_output"
220220
assert out_dir.exists()
221221
assert r"When providing multiple whole-slide images / tiles" not in caplog.text
222222

223223
out_dir = prepare_engines_save_dir(
224-
save_dir=tmp_path / "wsi_multiple_output",
224+
save_dir=track_tmp_path / "wsi_multiple_output",
225225
patch_mode=False,
226226
overwrite=False,
227227
)
228228

229-
assert out_dir == tmp_path / "wsi_multiple_output"
229+
assert out_dir == track_tmp_path / "wsi_multiple_output"
230230
assert out_dir.exists()
231231
assert r"When providing multiple whole slide images" in caplog.text
232232

233233
# test for file overwrite with Path.mkdirs() method
234234
out_path = prepare_engines_save_dir(
235-
save_dir=tmp_path / "patch_output" / "output.zarr",
235+
save_dir=track_tmp_path / "patch_output" / "output.zarr",
236236
patch_mode=True,
237237
overwrite=True,
238238
)
239239
assert out_path.exists()
240240

241241
out_path = prepare_engines_save_dir(
242-
save_dir=tmp_path / "patch_output" / "output.zarr",
242+
save_dir=track_tmp_path / "patch_output" / "output.zarr",
243243
patch_mode=True,
244244
overwrite=True,
245245
)
246246
assert out_path.exists()
247247

248248
with pytest.raises(FileExistsError):
249249
out_path = prepare_engines_save_dir(
250-
save_dir=tmp_path / "patch_output" / "output.zarr",
250+
save_dir=track_tmp_path / "patch_output" / "output.zarr",
251251
patch_mode=True,
252252
overwrite=False,
253253
)
@@ -371,9 +371,9 @@ def test_engine_run_with_verbose() -> NoReturn:
371371
assert "labels" in out
372372

373373

374-
def test_patch_pred_zarr_store(tmp_path: pytest.TempPathFactory) -> NoReturn:
374+
def test_patch_pred_zarr_store(track_tmp_path: pytest.TempPathFactory) -> NoReturn:
375375
"""Test the engine run and patch pred store."""
376-
save_dir = tmp_path / "patch_output"
376+
save_dir = track_tmp_path / "patch_output"
377377

378378
eng = TestEngineABC(model="alexnet-kather100k")
379379
out = eng.run(
@@ -485,7 +485,9 @@ def test_get_dataloader(sample_svs: Path) -> None:
485485
assert isinstance(dataloader.dataset, WSIPatchDataset)
486486

487487

488-
def test_io_config_delegation(tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None:
488+
def test_io_config_delegation(
489+
track_tmp_path: Path, caplog: pytest.LogCaptureFixture
490+
) -> None:
489491
"""Test for delegating args to io config."""
490492
# test not providing config / full input info for not pretrained models
491493
model = CNNModel("resnet50")
@@ -499,12 +501,12 @@ def test_io_config_delegation(tmp_path: Path, caplog: pytest.LogCaptureFixture)
499501
eng.run(
500502
np.zeros((10, 224, 224, 3)),
501503
patch_mode=True,
502-
save_dir=tmp_path / "dump",
504+
save_dir=track_tmp_path / "dump",
503505
patch_input_shape=kwargs["patch_input_shape"],
504506
input_resolutions=kwargs["input_resolutions"],
505507
)
506508
assert "provide a valid ModelIOConfigABC" in caplog.text
507-
shutil.rmtree(tmp_path / "dump", ignore_errors=True)
509+
shutil.rmtree(track_tmp_path / "dump", ignore_errors=True)
508510

509511
# test providing config / full input info for non pretrained models
510512
ioconfig = ModelIOConfigABC(
@@ -515,24 +517,24 @@ def test_io_config_delegation(tmp_path: Path, caplog: pytest.LogCaptureFixture)
515517
eng.run(
516518
images=np.zeros((10, 224, 224, 3), dtype=np.uint8),
517519
patch_mode=True,
518-
save_dir=f"{tmp_path}/dump",
520+
save_dir=f"{track_tmp_path}/dump",
519521
ioconfig=ioconfig,
520522
)
521523
assert eng._ioconfig.patch_input_shape == (224, 224)
522524
assert eng._ioconfig.stride_shape == (256, 256)
523525
assert eng._ioconfig.input_resolutions == [{"resolution": 1.35, "units": "mpp"}]
524-
shutil.rmtree(tmp_path / "dump", ignore_errors=True)
526+
shutil.rmtree(track_tmp_path / "dump", ignore_errors=True)
525527

526528
eng.run(
527529
images=np.zeros((10, 224, 224, 3), dtype=np.uint8),
528530
patch_mode=True,
529-
save_dir=f"{tmp_path}/dump",
531+
save_dir=f"{track_tmp_path}/dump",
530532
**kwargs,
531533
)
532534
assert eng._ioconfig.patch_input_shape == [224, 224]
533535
assert eng._ioconfig.stride_shape == [224, 224]
534536
assert eng._ioconfig.input_resolutions == [{"resolution": 1.75, "units": "mpp"}]
535-
shutil.rmtree(tmp_path / "dump", ignore_errors=True)
537+
shutil.rmtree(track_tmp_path / "dump", ignore_errors=True)
536538

537539
# test overwriting pretrained ioconfig
538540
eng = TestEngineABC(model="alexnet-kather100k")
@@ -542,25 +544,25 @@ def test_io_config_delegation(tmp_path: Path, caplog: pytest.LogCaptureFixture)
542544
stride_shape=(300, 300),
543545
input_resolutions=[{"units": "baseline", "resolution": 1.99}],
544546
patch_mode=True,
545-
save_dir=f"{tmp_path}/dump",
547+
save_dir=f"{track_tmp_path}/dump",
546548
)
547549
assert eng._ioconfig.patch_input_shape == (300, 300)
548550
assert eng._ioconfig.stride_shape == (300, 300)
549551
assert eng._ioconfig.input_resolutions[0]["resolution"] == 1.99
550552
assert eng._ioconfig.input_resolutions[0]["units"] == "baseline"
551-
shutil.rmtree(tmp_path / "dump", ignore_errors=True)
553+
shutil.rmtree(track_tmp_path / "dump", ignore_errors=True)
552554

553555
eng.run(
554556
images=np.zeros((10, 300, 300, 3), dtype=np.uint8),
555557
patch_input_shape=(300, 300),
556558
stride_shape=(300, 300),
557559
input_resolutions=None,
558560
patch_mode=True,
559-
save_dir=f"{tmp_path}/dump",
561+
save_dir=f"{track_tmp_path}/dump",
560562
)
561563
assert eng._ioconfig.patch_input_shape == (300, 300)
562564
assert eng._ioconfig.stride_shape == (300, 300)
563-
shutil.rmtree(tmp_path / "dump", ignore_errors=True)
565+
shutil.rmtree(track_tmp_path / "dump", ignore_errors=True)
564566

565567
eng.ioconfig = None
566568
_ioconfig = eng._update_ioconfig(

0 commit comments

Comments
 (0)