Skip to content

Commit d27e70b

Browse files
authored
Merge pull request #79 from ImageMarkup/windows-newlines-fix
2 parents 62ea842 + 203d719 commit d27e70b

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

isic_cli/cli/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def download(
122122
thread_pool.map(func, image_chunk)
123123

124124
headers, records = _extract_metadata(images)
125-
with (outdir / "metadata.csv").open("w", encoding="utf8") as outfile:
125+
with (outdir / "metadata.csv").open("w", newline="", encoding="utf8") as outfile:
126126
writer = csv.DictWriter(outfile, headers)
127127
writer.writeheader()
128128
writer.writerows(records)

isic_cli/cli/metadata.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections import defaultdict
44
import csv
55
import itertools
6+
import os
67
from pathlib import Path
78
import sys
89
from typing import TYPE_CHECKING
@@ -200,10 +201,11 @@ def download(
200201
headers, records = _extract_metadata(images, progress, task)
201202

202203
if records:
203-
if outfile:
204-
stream = click.open_file(outfile.name, "w", encoding="utf8")
205-
else:
206-
stream = click.get_text_stream("stdout", encoding="utf8")
204+
stream = (
205+
sys.stdout
206+
if outfile is None or os.fsdecode(outfile) == "-"
207+
else Path(outfile).open("w", newline="", encoding="utf8") # noqa: SIM115
208+
)
207209

208210
writer = csv.DictWriter(stream, headers)
209211
writer.writeheader()

tests/test_cli_image.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ def test_image_download(cli_run, outdir):
4545
assert Path(f"{outdir}/metadata.csv").exists()
4646
assert Path(f"{outdir}/attribution.txt").exists()
4747
assert Path(f"{outdir}/licenses/CC-0.txt").exists()
48+
49+
50+
@pytest.mark.usefixtures("_isolated_filesystem", "_mock_images")
51+
def test_image_download_metadata_newlines(cli_run, outdir):
52+
result = cli_run(["image", "download", outdir])
53+
54+
assert result.exit_code == 0, result.exception
55+
with Path(f"{outdir}/metadata.csv").open() as f:
56+
assert f.read().count("\n") == 2

tests/test_cli_metadata.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
@pytest.fixture()
1010
def _mock_image_metadata(mocker):
11-
mocker.patch("isic_cli.cli.metadata.get_num_images", return_value=1)
11+
mocker.patch("isic_cli.cli.metadata.get_num_images", return_value=2)
1212
mocker.patch(
1313
"isic_cli.cli.metadata.get_images",
1414
return_value=iter(
@@ -21,7 +21,16 @@ def _mock_image_metadata(mocker):
2121
"acquisition": {},
2222
"clinical": {"sex": "male", "diagnosis": "melanoma"},
2323
},
24-
}
24+
},
25+
{
26+
"isic_id": "ISIC_0000001",
27+
"attribution": "\U00001f600 Bar",
28+
"copyright_license": "CC-BY-NC",
29+
"metadata": {
30+
"acquisition": {},
31+
"clinical": {"sex": "female", "diagnosis": "nevus"},
32+
},
33+
},
2534
]
2635
),
2736
)
@@ -67,3 +76,15 @@ def test_metadata_download_file(cli_run):
6776
output = f.read()
6877

6978
assert re.search(r"ISIC_0000000.*Foo.*CC-0.*melanoma.*male", output), output
79+
80+
81+
@pytest.mark.usefixtures("_mock_image_metadata")
82+
def test_metadata_download_newlines(cli_run, mocker):
83+
result = cli_run(["metadata", "download", "-o", "foo.csv"])
84+
85+
assert result.exit_code == 0, result.exception
86+
87+
with Path("foo.csv").open() as f:
88+
output = f.read()
89+
90+
assert output.count("\n") == 3, output

0 commit comments

Comments
 (0)