Skip to content

Commit 6c8ae0d

Browse files
authored
Merge pull request #102 from ImageMarkup/refactor-permission-test-all-platforms
2 parents 0c17dbb + 3fd6ec9 commit 6c8ae0d

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

isic_cli/cli/types.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,9 @@ def convert(self, value, param, ctx):
115115
value.parent.mkdir(parents=True, exist_ok=True)
116116
with value.open("w", newline="", encoding="utf8"):
117117
pass
118-
except PermissionError:
119-
self.fail(f"Permission denied - cannot write to '{value}'.", param, ctx)
120-
except OSError as e:
121-
# this is a general catch-all for weirder issues like a read only filesystem,
118+
except (PermissionError, OSError):
119+
# a user can end up here from lacking permissions, a read only filesystem,
122120
# filenames that are too long or have invalid chars, etc.
123-
self.fail(f"Cannot write to '{value}'. {e!s}", param, ctx)
121+
self.fail(f"Permission denied - cannot write to '{value}'.", param, ctx)
124122

125123
return value

tests/test_cli_metadata.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,22 @@ def test_metadata_download_file(cli_runner):
8888
assert re.search(r"ISIC_0000000.*Foo.*CC-0.*melanoma.*male", output), output
8989

9090

91-
@pytest.mark.skipif(
92-
sys.platform in ["win32", "darwin"], reason="Windows and macOS don't support this test"
93-
)
9491
@pytest.mark.usefixtures("_mock_image_metadata", "_isolated_filesystem")
95-
def test_metadata_download_file_no_write(cli_run):
96-
result = cli_run(["metadata", "download", "-o", "/metadata.csv"])
92+
@pytest.mark.parametrize(
93+
"output_file", ["/metadata.csv", f"{'1' * 255}.csv"], ids=["no_permissions", "bad_filename"]
94+
)
95+
def test_metadata_download_permission_denied(cli_run, output_file):
96+
if sys.platform == "win32" and output_file == "/metadata.csv":
97+
pytest.skip("Windows doesn't support this test")
98+
99+
result = cli_run(["metadata", "download", "-o", output_file])
97100
# it's important that the exit code is 2 and not 1, because the key constraint of this
98101
# functionality is that the user gets the error message before spending their time
99102
# downloading the data. exit code 2 is for usage errors with click.
100103
assert result.exit_code == 2, result.exception
101104
assert re.search(r"Permission denied", result.output), result.output
102105

103106

104-
@pytest.mark.usefixtures("_mock_image_metadata", "_isolated_filesystem")
105-
def test_metadata_download_file_bad_filename(cli_run):
106-
result = cli_run(["metadata", "download", "-o", f"{'1' * 255}.csv"])
107-
# see comment in test_metadata_download_file_no_write for why exit code is 2
108-
assert result.exit_code == 2, result.exception
109-
assert re.search(r"Cannot write to", result.output), result.output
110-
111-
112107
@pytest.mark.usefixtures("_mock_image_metadata")
113108
@pytest.mark.parametrize(
114109
"cli_runner",

0 commit comments

Comments
 (0)