|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import logging |
3 | 4 | import os |
4 | 5 | from pathlib import Path |
5 | 6 |
|
@@ -90,6 +91,33 @@ def test_image_download_cleanup(cli_run, outdir): |
90 | 91 | assert not partial_file.exists() |
91 | 92 |
|
92 | 93 |
|
| 94 | +@pytest.mark.usefixtures("_isolated_filesystem", "_mock_images") |
| 95 | +def test_image_download_cleanup_permission_error(cli_run, outdir, mocker, caplog): |
| 96 | + partial_file = Path(outdir) / f".isic-partial.{os.getpid()}.ISIC_0000000.jpg" |
| 97 | + partial_file.parent.mkdir(parents=True) |
| 98 | + partial_file.touch() |
| 99 | + |
| 100 | + original_unlink = Path.unlink |
| 101 | + |
| 102 | + def mock_unlink(self, *, missing_ok=False): |
| 103 | + if str(partial_file) == str(self): |
| 104 | + raise PermissionError("Access is denied") |
| 105 | + return original_unlink(self, missing_ok=missing_ok) |
| 106 | + |
| 107 | + mocker.patch.object(Path, "unlink", mock_unlink) |
| 108 | + caplog.set_level(logging.WARNING) |
| 109 | + |
| 110 | + result = cli_run(["image", "download", outdir]) |
| 111 | + assert result.exit_code == 0 |
| 112 | + |
| 113 | + # run manually since atexit won't run in the test environment |
| 114 | + cleanup_partially_downloaded_files(Path(outdir)) |
| 115 | + |
| 116 | + assert ( |
| 117 | + "Permission error while cleaning up one or more partially downloaded files" in caplog.text |
| 118 | + ) |
| 119 | + |
| 120 | + |
93 | 121 | @pytest.mark.usefixtures("_isolated_filesystem", "_mock_images") |
94 | 122 | def test_image_download_legacy_diagnosis_unsupported(cli_run, outdir): |
95 | 123 | result = cli_run(["image", "download", outdir, "--search", "diagnosis:melanoma"]) |
|
0 commit comments