Skip to content

Commit 9657e59

Browse files
authored
Merge pull request #91 from ImageMarkup/namespace-partial-files-by-pid
Namespace isic-partial files by pid
2 parents b25aa2f + 5ac19bc commit 9657e59

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

isic_cli/cli/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
def cleanup_partially_downloaded_files(directory: Path) -> None:
30-
for p in directory.glob("**/.isic-partial.*"):
30+
for p in directory.glob(f"**/.isic-partial.{os.getpid()}.*"):
3131
# missing_ok=True because it's possible that another thread moved the temporary file to
3232
# its final destination after listing it but before unlinking.
3333
p.unlink(missing_ok=True)

isic_cli/io/http.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import logging
4+
import os
45
import shutil
56
from tempfile import NamedTemporaryFile
67
from typing import TYPE_CHECKING
@@ -140,7 +141,9 @@ def download_image(image: dict, to: Path, progress, task) -> None:
140141
r.raise_for_status()
141142

142143
temp_file_name = None
143-
with NamedTemporaryFile(dir=to, prefix=".isic-partial.", delete=False) as outfile:
144+
with NamedTemporaryFile(
145+
dir=to, prefix=f".isic-partial.{os.getpid()}.", delete=False
146+
) as outfile:
144147
temp_file_name = outfile.name
145148
for chunk in r.iter_content(1024 * 1024 * 5):
146149
outfile.write(chunk)

tests/test_cli_image.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from __future__ import annotations
22

3+
import os
34
from pathlib import Path
4-
from unittest.mock import MagicMock
55

66
import pytest
77

8+
from isic_cli.cli.image import cleanup_partially_downloaded_files
9+
810

911
@pytest.fixture()
1012
def outdir():
@@ -58,22 +60,16 @@ def test_image_download_metadata_newlines(cli_run, outdir):
5860

5961

6062
@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"
6665
partial_file.parent.mkdir(parents=True)
66+
partial_file.touch()
6767

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
7870

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))
7975
assert not partial_file.exists()

0 commit comments

Comments
 (0)