diff --git a/src/check_jsonschema/cli/param_types.py b/src/check_jsonschema/cli/param_types.py index 700af0c3d..316157094 100644 --- a/src/check_jsonschema/cli/param_types.py +++ b/src/check_jsonschema/cli/param_types.py @@ -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 diff --git a/tests/unit/cli/test_parse.py b/tests/unit/cli/test_parse.py index a566466fc..e4aa5c83d 100644 --- a/tests/unit/cli/test_parse.py +++ b/tests/unit/cli/test_parse.py @@ -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()