Skip to content

Commit e0c9259

Browse files
committed
Add a json schema for the test.json used in tests
This does a couple of nice things, one is that editors like vscode can be configured to use this schema to provide auto completion and error highlighting if invalid values are added or required values are missing. It also allows us test that the format of the test matrix work in a unit test, which I've added. It does require that the python jsonschema package is installed.
1 parent 91050e0 commit e0c9259

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed

.editorconfig

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ indent_size = 2
2323
[meson.build]
2424
indent_size = 2
2525

26+
[*.json]
27+
indent_size = 2

data/test.schema.json

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"env": {
5+
"type": "object",
6+
"additionalProperties": {
7+
"type": "string"
8+
}
9+
},
10+
"installed": {
11+
"type": "array",
12+
"items": {
13+
"type": "object",
14+
"properties": {
15+
"file": {
16+
"type": "string"
17+
},
18+
"type": {
19+
"type": "string",
20+
"enum": [
21+
"file",
22+
"exe",
23+
"shared_lib",
24+
"pdb",
25+
"implib",
26+
"implibempty",
27+
"expr"
28+
]
29+
},
30+
"platform": {
31+
"type": "string",
32+
"enum": [
33+
"msvc",
34+
"gcc",
35+
"cygwin",
36+
"!cygwin"
37+
]
38+
},
39+
"version": {
40+
"type": "string"
41+
},
42+
"language": {
43+
"type": "string"
44+
}
45+
},
46+
"required": [
47+
"file",
48+
"type"
49+
]
50+
}
51+
},
52+
"matrix": {
53+
"type": "object",
54+
"additionalProperties": {
55+
"properties": {
56+
"options": {
57+
"type": "array",
58+
"items": {
59+
"type": "object",
60+
"properties": {
61+
"val": {
62+
"type": "string"
63+
},
64+
"compilers": {
65+
"type": "object",
66+
"additionalProperties": {
67+
"type": "string"
68+
}
69+
},
70+
"skip_on_env": {
71+
"type": "array",
72+
"items": {
73+
"type": "string"
74+
}
75+
}
76+
},
77+
"required": [
78+
"val"
79+
]
80+
}
81+
},
82+
"exclude": {
83+
"type": "array",
84+
"items": {
85+
"type": "object",
86+
"additionalProperties": {
87+
"type": "string"
88+
}
89+
}
90+
}
91+
}
92+
}
93+
},
94+
"do_not_set_opts": {
95+
"type": "array",
96+
"items": {
97+
"type": "string",
98+
"enum": [
99+
"libdir",
100+
"prefix"
101+
]
102+
}
103+
}
104+
}
105+
}

docs/markdown/Contributing.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,17 @@ Additionally, the `skip_on_env` key can be used to specify a list of environment
294294
variables. If at least one environment variable in `skip_on_env` is present, all
295295
matrix entries containing this value are skipped.
296296

297-
Similarly, the `compilers` key can be used to define a set of compilers required
298-
for this value.
297+
Similarly, the `compilers` key can be used to define a mapping of compilers to languages that are required for this value.
299298

299+
```json
300+
{
301+
"compilers": {
302+
"c": "gcc",
303+
"cpp": "gcc",
304+
"d": "gdc"
305+
}
306+
}
307+
```
300308

301309
Specific option combinations can be excluded with the `exclude` section. It should
302310
be noted that `exclude` does not require exact matches. Instead, any matrix entry

run_unittests.py

+26
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,32 @@ def test_dependency_factory_order(self):
12271227
actual = [m() for m in f(env, MachineChoice.HOST, {'required': False})]
12281228
self.assertListEqual([m.type_name for m in actual], ['cmake', 'pkgconfig'])
12291229

1230+
def test_validate_json(self) -> None:
1231+
"""Validate the json schema for the test cases."""
1232+
try:
1233+
from jsonschema import validate, ValidationError
1234+
except ImportError:
1235+
if is_ci():
1236+
raise
1237+
raise unittest.SkipTest('Python jsonschema module not found.')
1238+
1239+
with Path('data/test.schema.json').open() as f:
1240+
schema = json.load(f)
1241+
1242+
errors = [] # type: T.Tuple[str, Exception]
1243+
for p in Path('test cases').glob('**/test.json'):
1244+
with p.open() as f:
1245+
try:
1246+
validate(json.load(f), schema=schema)
1247+
except ValidationError as e:
1248+
errors.append((p.resolve(), e))
1249+
1250+
for f, e in errors:
1251+
print('Failed to validate: "{}"'.format(f))
1252+
print(str(e))
1253+
1254+
self.assertFalse(errors)
1255+
12301256

12311257
@unittest.skipIf(is_tarball(), 'Skipping because this is a tarball release')
12321258
class DataTests(unittest.TestCase):

0 commit comments

Comments
 (0)