Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continues processing JSONs even if hook fails (fixes #1038) #1039

Merged
merged 1 commit into from
Apr 20, 2024
Merged
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
22 changes: 13 additions & 9 deletions pre_commit_hooks/pretty_format_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,20 @@ def main(argv: Sequence[str] | None = None) -> int:
f'Input File {json_file} is not a valid JSON, consider using '
f'check-json',
)
return 1

if contents != pretty_contents:
if args.autofix:
_autofix(json_file, pretty_contents)
else:
diff_output = get_diff(contents, pretty_contents, json_file)
sys.stdout.buffer.write(diff_output.encode())

status = 1
else:
if contents != pretty_contents:
if args.autofix:
_autofix(json_file, pretty_contents)
else:
diff_output = get_diff(
contents,
pretty_contents,
json_file,
)
sys.stdout.buffer.write(diff_output.encode())

status = 1

return status

Expand Down
18 changes: 18 additions & 0 deletions tests/pretty_format_json_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ def test_autofix_main(tmpdir):
assert ret == 0


def test_invalid_main(tmpdir):
amarvin marked this conversation as resolved.
Show resolved Hide resolved
srcfile1 = tmpdir.join('not_valid_json.json')
srcfile1.write(
'{\n'
' // not json\n'
' "a": "b"\n'
'}',
)
srcfile2 = tmpdir.join('to_be_json_formatted.json')
srcfile2.write('{ "a": "b" }')

# it should have skipped the first file and formatted the second one
assert main(['--autofix', str(srcfile1), str(srcfile2)]) == 1

# confirm second file was formatted (shouldn't trigger linter again)
assert main([str(srcfile2)]) == 0


def test_orderfile_get_pretty_format():
ret = main((
'--top-keys=alist', get_resource_path('pretty_formatted_json.json'),
Expand Down
Loading