Repository navigation
report an instance file that cannot be read as a usage error - #753
Closed
qianeric-backup wants to merge 1 commit into
Closed
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
`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.
Member
|
This PR violates our AI policy. Please read and take time to digest and understand the policy. Sending us LLM outputs without any investment of real effort is actively harmful to the project maintainers and therefore harmful to the project. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BinaryFileInput.check_can_open()stats the path to decide how to open it, andBinaryFileInputParam.convert()calls it without a guard, so a path that does not exist (or is a directory) escapes as an unhandled exception instead of theclickusage error that other bad option values get:Compare an invalid
--output-format, which is reported and exits 2:Change
convert()catchesOSErrorfromcheck_can_open()and reports it with theself.fail()helper that the rest of the module already uses:Invalid value for 'INSTANCEFILES...', exit 2IsADirectoryErrortraceback, exit 1-(stdin)statperformed)--output-formatTests
test_missing_instance_file_is_a_usage_errorintests/unit/cli/test_parse.py.pytest tests/unithere: 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[...]) also fail on the current tree in this sandbox.