Skip to content

Commit 4f1acb0

Browse files
committed
test: add testme.json format validation in the pre-commit + improve the error messages of test.py + make it faster to start.
1 parent 21b5358 commit 4f1acb0

4 files changed

Lines changed: 79 additions & 6 deletions

File tree

.pre-commit-config.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,23 @@ repos:
5757
hooks:
5858
- id: cpplint
5959
args: [--counting=detailed, --exclude=test/*]
60+
61+
- repo: https://github.com/python-jsonschema/check-jsonschema
62+
rev: 0.38.0
63+
hooks:
64+
- id: check-jsonschema
65+
files: ^test/.*/testme.json$
66+
args: ["--schemafile", "pytools/testme.schema.json"]
67+
68+
- repo: local
69+
hooks:
70+
- id: pytest
71+
name: testme.json validation
72+
entry: ./test.py --validate-testme-jsons
73+
language: python
74+
files: ^test/.*/testme.json$
75+
pass_filenames: false
76+
additional_dependencies:
77+
- matplotlib >= 3.5.0
78+
- pytest >= 6.0
79+
- jsonschema >= 4.26.0

pytools/idfx_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ def __init__(self, current_test_file, name=""):
141141
action="store_true",
142142
)
143143

144+
parser.add_argument(
145+
"--validate-testme-jsons",
146+
help="Only validate the testme.json files.",
147+
action="store_true",
148+
)
149+
144150
parser.add_argument(
145151
"--help-pytest",
146152
help="Display the options you can transmit directly to pytest in addition to the specific to idefix tests.",

pytools/idfx_test_run.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,28 @@ def _loadTestmeSchema(self) -> dict:
5656

5757
def _validateNaming(self, namings: str, autoExtracted: str, file: str):
5858
names = namings.split(",")
59-
for n in autoExtracted.split(","):
60-
if not n in names:
61-
raise Exception(
62-
f"Naming parameter list not match the auto-detectection, some are missinge. You gave : '{names}', detected '{autoExtracted}' into {file}"
63-
)
59+
names_sorted = namings.split(",")
60+
autoNames = autoExtracted.split(",")
61+
autoNames_sorted = autoExtracted.split(",")
62+
63+
# quick compare
64+
names_sorted.sort()
65+
autoNames_sorted.sort()
66+
67+
# check and if error check in details to report
68+
if names_sorted != autoNames_sorted:
69+
for n in autoNames:
70+
if n not in names:
71+
raise Exception(
72+
f"Naming parameter list not match the auto-detectection, some are missing : '{n}'\n"
73+
f"You gave : '{names}', detected '{autoNames}' into {file}"
74+
)
75+
for n in names:
76+
if n not in autoNames:
77+
raise Exception(
78+
f"Naming parameter list not match the auto-detectection, some are missing : '{n}'.\n"
79+
f"You gave : '{names}', detected '{autoNames}' into {file}"
80+
)
6481

6582
def _makeVariableArgAsList(self, namings: str, variants) -> list:
6683
# make as a list
@@ -155,7 +172,7 @@ def genTests(self) -> list:
155172
)
156173
except Exception as e:
157174
raise Exception(
158-
f"Fail to generate tests from {testfileRelPath}"
175+
f"Fail to generate tests from {testfileRelPath}.\n{e}"
159176
) from e
160177

161178
# ok
@@ -448,7 +465,29 @@ def _runNonRegSingleRun(
448465
f"Dump file {file} was overwritten on restart"
449466
)
450467

468+
def validateTestmeJsons(self) -> bool:
469+
try:
470+
self.genTests()
471+
except Exception as e:
472+
print(f"{e}")
473+
return False
474+
return True
475+
451476
def main(self, all: bool = False):
477+
# make testme.json validation for pre-commit
478+
if "--validate-testme-jsons" in sys.argv:
479+
try:
480+
tests = self.genTests()
481+
for test in tests:
482+
testfile = test.values[0]["testfile"]
483+
id = test.id
484+
print(f"{testfile} => {id}")
485+
except Exception as e:
486+
print(f"{e}")
487+
sys.exit(1)
488+
return
489+
490+
# run
452491
if all:
453492
sys.argv.append("-all")
454493
idefixTest = tst.idfxTest(self.parentScritFile, name="main")

test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
gblIdefixPytestRunner = IdexPytestRunner(__file__)
1919

2020

21+
# if called directly as a script
22+
if __name__ == "__main__" and "--validate-testme-jsons" in sys.argv:
23+
if gblIdefixPytestRunner.validateTestmeJsons():
24+
sys.exit(0)
25+
else:
26+
sys.exit(1)
27+
28+
2129
# define the pytest test
2230
@pytest.mark.parametrize("config", gblIdefixPytestRunner.genTests())
2331
def test_idefix_build_run_check(config):

0 commit comments

Comments
 (0)