Skip to content

Commit

Permalink
Merge pull request #83 from ImageMarkup/improve-locale-support
Browse files Browse the repository at this point in the history
  • Loading branch information
danlamanna authored Jul 1, 2024
2 parents e1b2ef4 + d784a14 commit af06f39
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
10 changes: 5 additions & 5 deletions isic_cli/cli/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ def download(
headers, records = _extract_metadata(images, progress, task)

if records:
stream = (
sys.stdout
if outfile is None or os.fsdecode(outfile) == "-"
else Path(outfile).open("w", newline="", encoding="utf8") # noqa: SIM115
)
if outfile is None or os.fsdecode(outfile) == "-":
sys.stdout.reconfigure(encoding="utf8")
stream = sys.stdout
else:
stream = Path(outfile).open("w", newline="", encoding="utf8") # noqa: SIM115

writer = csv.DictWriter(stream, headers)
writer.writeheader()
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ def runner():
return CliRunner()


@pytest.fixture()
def runner_non_utf8():
return CliRunner(charset="cp1251")


@pytest.fixture()
def cli_run(runner):
return functools.partial(runner.invoke, cli)


@pytest.fixture()
def cli_run_non_utf8(runner_non_utf8):
return functools.partial(runner_non_utf8.invoke, cli)


@pytest.fixture()
def _isolated_filesystem(runner):
with runner.isolated_filesystem():
Expand Down
25 changes: 19 additions & 6 deletions tests/test_cli_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re

import pytest
from pytest_lazy_fixtures import lf


@pytest.fixture()
Expand Down Expand Up @@ -60,15 +61,23 @@ def test_metadata_validate_lesions_patients(runner, cli_run):


@pytest.mark.usefixtures("_mock_image_metadata")
def test_metadata_download_stdout(cli_run):
result = cli_run(["metadata", "download"])
@pytest.mark.parametrize(
"cli_runner",
[lf("cli_run"), lf("cli_run_non_utf8")],
)
def test_metadata_download_stdout(cli_runner):
result = cli_runner(["metadata", "download"])
assert result.exit_code == 0, result.exception
assert re.search(r"ISIC_0000000.*Foo.*CC-0.*melanoma.*male", result.output), result.output


@pytest.mark.usefixtures("_mock_image_metadata", "_isolated_filesystem")
def test_metadata_download_file(cli_run):
result = cli_run(["metadata", "download", "-o", "foo.csv"])
@pytest.mark.parametrize(
"cli_runner",
[lf("cli_run"), lf("cli_run_non_utf8")],
)
def test_metadata_download_file(cli_runner):
result = cli_runner(["metadata", "download", "-o", "foo.csv"])

assert result.exit_code == 0, result.exception

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


@pytest.mark.usefixtures("_mock_image_metadata")
def test_metadata_download_newlines(cli_run, mocker):
result = cli_run(["metadata", "download", "-o", "foo.csv"])
@pytest.mark.parametrize(
"cli_runner",
[lf("cli_run"), lf("cli_run_non_utf8")],
)
def test_metadata_download_newlines(cli_runner, mocker):
result = cli_runner(["metadata", "download", "-o", "foo.csv"])

assert result.exit_code == 0, result.exception

Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ commands =
[testenv:test]
deps =
pytest
pytest-lazy-fixtures
pytest-mock
commands =
pytest {posargs}
Expand Down

0 comments on commit af06f39

Please sign in to comment.