From 297c72d417bd136275df3a3d38e12771c5ba701e Mon Sep 17 00:00:00 2001 From: QianEric Date: Sun, 22 Nov 2026 21:36:33 +0800 Subject: [PATCH] Report an instance file that cannot be read as a usage error `BinaryFileInput.check_can_open()` stats the path to decide how to open it, and `BinaryFileInputParam.convert()` calls it without a guard, so a path that does not exist (or is a directory) escapes as an unhandled exception instead of the `click` usage error that other bad option values get: $ check-jsonschema --schemafile schema.json does-not-exist.yaml Traceback (most recent call last): File ".../click/core.py", line 2593, in invoke ... File ".../check_jsonschema/file_wrapper.py", line 38, in check_can_open if not stat.S_ISFIFO(os.stat(self.name).st_mode): FileNotFoundError: [Errno 2] No such file or directory: 'does-not-exist.yaml' Compare an invalid `--output-format`, which is reported and exits 2: $ check-jsonschema --schemafile schema.json -o bogus file.yaml Usage: python -m check_jsonschema [OPTIONS] INSTANCEFILES... Error: Invalid value for '-o' / '--output-format': 'bogus' is not one of ... ## Change `convert()` catches `OSError` from `check_can_open()` and reports it with the `self.fail()` helper that the rest of the module already uses: $ check-jsonschema --schemafile schema.json does-not-exist.yaml ; echo $? Usage: python -m check_jsonschema [OPTIONS] INSTANCEFILES... Error: Invalid value for 'INSTANCEFILES...': 'does-not-exist.yaml' cannot be read: [Errno 2] No such file or directory: 'does-not-exist.yaml' 2 | case | before | after | |---|---|---| | instance file missing | traceback, exit 1 | `Invalid value for 'INSTANCEFILES...'`, exit 2 | | instance file is a directory | `IsADirectoryError` traceback, exit 1 | same usage error, exit 2 | | instance file readable | validated | unchanged | | instance file is `-` (stdin) | reads stdin | unchanged (no stat performed) | | invalid `--output-format` | usage error | unchanged | ## Tests `test_missing_instance_file_is_a_usage_error` in `tests/unit/cli/test_parse.py`. `pytest tests/unit` here: 341 passed / 4 failed with the change, 340 passed / 5 failed on the current tree - the difference is the new test, the other 4 (`test_supports_common_option[cmd_args0]`, `test_color_cli_option[...]`) fail on the current tree too in this sandbox. --- src/check_jsonschema/cli/param_types.py | 5 ++++- tests/unit/cli/test_parse.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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()