Skip to content

Commit d784a14

Browse files
committed
Encode metadata to stdout as utf8
This fixes a regression from #79.
1 parent e1b2ef4 commit d784a14

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

isic_cli/cli/metadata.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ def download(
201201
headers, records = _extract_metadata(images, progress, task)
202202

203203
if records:
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-
)
204+
if outfile is None or os.fsdecode(outfile) == "-":
205+
sys.stdout.reconfigure(encoding="utf8")
206+
stream = sys.stdout
207+
else:
208+
stream = Path(outfile).open("w", newline="", encoding="utf8") # noqa: SIM115
209209

210210
writer = csv.DictWriter(stream, headers)
211211
writer.writeheader()

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@ def runner():
1818
return CliRunner()
1919

2020

21+
@pytest.fixture()
22+
def runner_non_utf8():
23+
return CliRunner(charset="cp1251")
24+
25+
2126
@pytest.fixture()
2227
def cli_run(runner):
2328
return functools.partial(runner.invoke, cli)
2429

2530

31+
@pytest.fixture()
32+
def cli_run_non_utf8(runner_non_utf8):
33+
return functools.partial(runner_non_utf8.invoke, cli)
34+
35+
2636
@pytest.fixture()
2737
def _isolated_filesystem(runner):
2838
with runner.isolated_filesystem():

tests/test_cli_metadata.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55

66
import pytest
7+
from pytest_lazy_fixtures import lf
78

89

910
@pytest.fixture()
@@ -60,15 +61,23 @@ def test_metadata_validate_lesions_patients(runner, cli_run):
6061

6162

6263
@pytest.mark.usefixtures("_mock_image_metadata")
63-
def test_metadata_download_stdout(cli_run):
64-
result = cli_run(["metadata", "download"])
64+
@pytest.mark.parametrize(
65+
"cli_runner",
66+
[lf("cli_run"), lf("cli_run_non_utf8")],
67+
)
68+
def test_metadata_download_stdout(cli_runner):
69+
result = cli_runner(["metadata", "download"])
6570
assert result.exit_code == 0, result.exception
6671
assert re.search(r"ISIC_0000000.*Foo.*CC-0.*melanoma.*male", result.output), result.output
6772

6873

6974
@pytest.mark.usefixtures("_mock_image_metadata", "_isolated_filesystem")
70-
def test_metadata_download_file(cli_run):
71-
result = cli_run(["metadata", "download", "-o", "foo.csv"])
75+
@pytest.mark.parametrize(
76+
"cli_runner",
77+
[lf("cli_run"), lf("cli_run_non_utf8")],
78+
)
79+
def test_metadata_download_file(cli_runner):
80+
result = cli_runner(["metadata", "download", "-o", "foo.csv"])
7281

7382
assert result.exit_code == 0, result.exception
7483

@@ -79,8 +88,12 @@ def test_metadata_download_file(cli_run):
7988

8089

8190
@pytest.mark.usefixtures("_mock_image_metadata")
82-
def test_metadata_download_newlines(cli_run, mocker):
83-
result = cli_run(["metadata", "download", "-o", "foo.csv"])
91+
@pytest.mark.parametrize(
92+
"cli_runner",
93+
[lf("cli_run"), lf("cli_run_non_utf8")],
94+
)
95+
def test_metadata_download_newlines(cli_runner, mocker):
96+
result = cli_runner(["metadata", "download", "-o", "foo.csv"])
8497

8598
assert result.exit_code == 0, result.exception
8699

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ commands =
1616
[testenv:test]
1717
deps =
1818
pytest
19+
pytest-lazy-fixtures
1920
pytest-mock
2021
commands =
2122
pytest {posargs}

0 commit comments

Comments
 (0)