Repository navigation
Conversation
The --from-json branch of validate_input_path never ran its extension
and format checks:
- options.get("--from-json") looked up the CLI flag string, but Click
keys ctx.params by the Python name from_json, so it was always False.
- --from-json was not eager, so it had not been parsed yet when the
input argument callback ran; it is now is_eager so the value is set.
- The extension and header checks sat after an unconditional raise and
under 'not is_file()', so they were unreachable and, once reachable,
guarded by the wrong condition. They now run when the input is a file.
- The header check read the file in text mode, raising UnicodeDecodeError
on binary content; it now reads bytes.
Add tests covering non-.json extensions, malformed JSON, and binary
content.
Signed-off-by: Deepak Bhagat <deepak988088@gmail.com>
This branch has not been deployed
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.
Fixes #5351
Description
validate_input_pathwas meant to add extra validation for--from-jsoninputs, but the whole JSON branch never did anything useful. This fixes the four reasons it was dead or inert:options.get("--from-json")looked up the CLI flag string, but Click keysctx.paramsby the Python parameter namefrom_json, so the lookup always returnedFalseand the entire block was skipped. Now it readsoptions.get("from_json", False).--from-jsonwas not eager, so it had not been parsed yet when theinputargument callback ran. Even with the key fixed, the value would still readFalse. The option is nowis_eager=True.raise(so they were unreachable) and were nested undernot is_file(...)(so once reachable they would only run for non-files, the opposite of the intent). They now run when the input is a file.UnicodeDecodeErroron binary content (for example a.json-named PNG) instead of a clean message. It now reads bytes and compares againstb"{".Tests
Added three tests in
tests/scancode/test_cli.pycovering a non-.jsonextension, malformed JSON content, and binary content (which previously crashed). The existing--from-jsonround-trip test still passes, confirming theis_eagerand key changes do not affect valid JSON loading.Credit to @0Cymantek0, who independently identified the wrong-key and wrong-nesting causes and the binary-read issue on the linked issue.
Tasks