Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/check_jsonschema/cli/param_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ def convert(
file = BinaryFileInput(value)
if ctx is not None:
ctx.call_on_close(file.close)
file.check_can_open()
try:
file.check_can_open()
except OSError as e:
self.fail(f"'{value}' cannot be read: {e}", param, ctx)
# type checkers won't recognize a BinaryFileInput as a BinaryIO unless we do a
# lot of extra work, because they don't follow that it uses `__getattr__` to
# proxy everything to the underlying stream
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/cli/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ class BoxedContext:
ref = None


def test_missing_instance_file_is_a_usage_error(cli_runner, tmp_path):
schema = tmp_path / "schema.json"
schema.write_text("{}")
missing = tmp_path / "does-not-exist.yaml"

result = cli_runner.invoke(cli_main, ["--schemafile", str(schema), str(missing)])

assert result.exit_code == 2
assert "cannot be read" in result.output
assert "Traceback" not in result.output


def touch_files(dirpath, *filenames):
for fname in filenames:
(dirpath / fname).touch()
Expand Down