Skip to content

Commit 22e7612

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 27fafbc commit 22e7612

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
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: 33 additions & 34 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,44 @@
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+
for filename in filenames:
34+
with open(filename, "r") as fdesc:
35+
old_content = fdesc.read()
36+
records = json.loads(old_content)
37+
new_content = (
38+
json.dumps(
39+
records,
40+
indent=2,
41+
sort_keys=True,
42+
ensure_ascii=False,
43+
separators=(",", ": "),
44+
)
45+
+ "\n"
4546
)
46-
+ "\n"
47-
)
48-
49-
if old_content != new_content:
50-
if check:
51-
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)
47+
48+
if old_content != new_content:
49+
if check:
50+
print(f"[ERROR] File {filename} is badly formatted.")
51+
sys.exit(1)
52+
else:
53+
with open(filename, "w") as fdesc:
54+
fdesc.write(new_content)
5655

5756

5857
if __name__ == "__main__":

0 commit comments

Comments
 (0)