Skip to content

Commit b0c24a5

Browse files
committed
ci(run-tests): fix data JSON checker error reporting
Fixes a problem with the CI data JSON checker that was not reporting proper error code when checking multiple files.
1 parent 4d9d43e commit b0c24a5

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

run-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ data_dois() {
3636
}
3737

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

4242
data_licenses() {

scripts/clean_json_file.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/usr/bin/env python3
22

3-
"""Clean JSON file.
3+
"""
4+
Clean JSON files.
45
5-
Cleans JSON file so as to fix indentation, order fields, and remove trailing
6-
whitespace.
6+
Clean one or more JSON files so as to fix indentation, order fields, and remove
7+
trailing whitespace.
78
"""
89

910
import json
@@ -13,46 +14,49 @@
1314

1415

1516
@click.command()
16-
@click.argument("filename", type=click.Path(exists=True))
17+
@click.argument(
18+
"filenames", metavar="[FILENAME] ...", type=click.Path(exists=True), nargs=-1
19+
)
1720
@click.option(
1821
"--check",
1922
is_flag=True,
2023
default=False,
2124
help="Don't write the file back, only check whether the file is well formatted.",
2225
)
23-
def clean_json_file(filename, check): # noqa: D301
24-
"""Clean JSON file.
25-
26-
Clean JSON file so as to fix indentation, order fields, and remove trailing
27-
whitespace.
26+
def clean_json_file(filenames, check): # noqa: D301
27+
"""
28+
Clean JSON files.
2829
29-
\b
30-
:param filename: The file name to be reformatted.
31-
:param check: Do not modify file, only check whether it is well formatted.
32-
:type filename: str
33-
:type check: bool
30+
Clean one or more JSON files so as to fix indentation, order fields, and
31+
remove trailing whitespace.
3432
"""
35-
with open(filename, "r") as fdesc:
36-
old_content = fdesc.read()
37-
records = json.loads(old_content)
38-
new_content = (
39-
json.dumps(
40-
records,
41-
indent=2,
42-
sort_keys=True,
43-
ensure_ascii=False,
44-
separators=(",", ": "),
33+
problematic_files = []
34+
for filename in list(set(filenames)):
35+
with open(filename, "r") as fdesc:
36+
old_content = fdesc.read()
37+
records = json.loads(old_content)
38+
new_content = (
39+
json.dumps(
40+
records,
41+
indent=2,
42+
sort_keys=True,
43+
ensure_ascii=False,
44+
separators=(",", ": "),
45+
)
46+
+ "\n"
4547
)
46-
+ "\n"
47-
)
4848

49-
if old_content != new_content:
50-
if check:
49+
if old_content != new_content:
50+
if check:
51+
problematic_files.append(filename)
52+
else:
53+
with open(filename, "w") as fdesc:
54+
fdesc.write(new_content)
55+
56+
if check and problematic_files:
57+
for filename in problematic_files:
5158
print(f"[ERROR] File {filename} is badly formatted.")
52-
sys.exit(1)
53-
else:
54-
with open(filename, "w") as fdesc:
55-
fdesc.write(new_content)
59+
sys.exit(1)
5660

5761

5862
if __name__ == "__main__":

0 commit comments

Comments
 (0)