|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import os |
3 | 4 | from pathlib import Path |
4 | | -from unittest.mock import MagicMock |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
| 8 | +from isic_cli.cli.image import cleanup_partially_downloaded_files |
| 9 | + |
8 | 10 |
|
9 | 11 | @pytest.fixture() |
10 | 12 | def outdir(): |
@@ -58,22 +60,16 @@ def test_image_download_metadata_newlines(cli_run, outdir): |
58 | 60 |
|
59 | 61 |
|
60 | 62 | @pytest.mark.usefixtures("_isolated_filesystem", "_mock_images") |
61 | | -def test_image_download_cleanup_on_interrupt(mocker, outdir): |
62 | | - from click.testing import CliRunner # noqa: I001 |
63 | | - from isic_cli.cli import cli |
64 | | - |
65 | | - partial_file = Path(outdir) / ".isic-partial.ISIC_0000000.jpg" |
| 63 | +def test_image_download_cleanup(cli_run, outdir): |
| 64 | + partial_file = Path(outdir) / f".isic-partial.{os.getpid()}.ISIC_0000000.jpg" |
66 | 65 | partial_file.parent.mkdir(parents=True) |
| 66 | + partial_file.touch() |
67 | 67 |
|
68 | | - runner = CliRunner() |
69 | | - |
70 | | - cleanup = MagicMock(side_effect=KeyboardInterrupt) |
71 | | - mocker.patch("isic_cli.cli.image.get_num_images", cleanup) |
72 | | - |
73 | | - result = runner.invoke(cli, ["image", "download", outdir], standalone_mode=False) |
74 | | - |
75 | | - assert cleanup.called |
76 | | - assert result.exit_code == 1 |
77 | | - assert isinstance(result.exception.__cause__, KeyboardInterrupt) |
| 68 | + result = cli_run(["image", "download", outdir]) |
| 69 | + assert result.exit_code == 0 |
78 | 70 |
|
| 71 | + # this is run via atexit, but we want to test it here since we can't |
| 72 | + # easily test running the command in a subprocess. |
| 73 | + assert partial_file.exists() |
| 74 | + cleanup_partially_downloaded_files(Path(outdir)) |
79 | 75 | assert not partial_file.exists() |
0 commit comments