Skip to content

Commit

Permalink
ci(run-tests): fix data JSON checker error reporting
Browse files Browse the repository at this point in the history
Fixes a problem with the CI data JSON checker that was not reporting
proper error code when checking multiple files.
  • Loading branch information
tiborsimko committed Mar 5, 2025
1 parent 27fafbc commit 393e110
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ data_dois() {
}

data_json() {
find . -name "*.json" -exec ./scripts/clean_json_file.py --check {} \;
find . -name "*.json" -exec ./scripts/clean_json_file.py --check {} \+
}

data_licenses() {
Expand Down
68 changes: 36 additions & 32 deletions scripts/clean_json_file.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python3

"""Clean JSON file.
"""
Clean JSON files.
Cleans JSON file so as to fix indentation, order fields, and remove trailing
whitespace.
Clean one or more JSON files so as to fix indentation, order fields, and remove
trailing whitespace.
"""

import json
Expand All @@ -13,46 +14,49 @@


@click.command()
@click.argument("filename", type=click.Path(exists=True))
@click.argument(
"filenames", metavar="[FILENAME] ...", type=click.Path(exists=True), nargs=-1
)
@click.option(
"--check",
is_flag=True,
default=False,
help="Don't write the file back, only check whether the file is well formatted.",
)
def clean_json_file(filename, check): # noqa: D301
"""Clean JSON file.
Clean JSON file so as to fix indentation, order fields, and remove trailing
whitespace.
def clean_json_file(filenames, check): # noqa: D301
"""
Clean JSON files.
\b
:param filename: The file name to be reformatted.
:param check: Do not modify file, only check whether it is well formatted.
:type filename: str
:type check: bool
Clean one or more JSON files so as to fix indentation, order fields, and
remove trailing whitespace.
"""
with open(filename, "r") as fdesc:
old_content = fdesc.read()
records = json.loads(old_content)
new_content = (
json.dumps(
records,
indent=2,
sort_keys=True,
ensure_ascii=False,
separators=(",", ": "),
problematic_files = []
for filename in list(set(filenames)):
with open(filename, "r") as fdesc:
old_content = fdesc.read()
records = json.loads(old_content)
new_content = (
json.dumps(
records,
indent=2,
sort_keys=True,
ensure_ascii=False,
separators=(",", ": "),
)
+ "\n"
)
+ "\n"
)

if old_content != new_content:
if check:
if old_content != new_content:
if check:
problematic_files.append(filename)
else:
with open(filename, "w") as fdesc:
fdesc.write(new_content)

if check and problematic_files:
for filename in problematic_files:
print(f"[ERROR] File {filename} is badly formatted.")
sys.exit(1)
else:
with open(filename, "w") as fdesc:
fdesc.write(new_content)
sys.exit(1)


if __name__ == "__main__":
Expand Down

0 comments on commit 393e110

Please sign in to comment.