Skip to content

Commit b1ea388

Browse files
author
Steven Cartmell
committed
Fix test errors produced by JSON schema after rebase
- Modified the expected error code in some of the test cases. The error message is now issued by the JSON validator. - Stop validation code being called if no application configuration is given to validate. - Change test mock code to check for specific calls, instead of number of calls. - Derive absolute paths for JSON schema files before loading them, which seems to work better with the URI path resolution of the schema parser.
1 parent 7010010 commit b1ea388

File tree

7 files changed

+40
-28
lines changed

7 files changed

+40
-28
lines changed

tools/config/__init__.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
"""
1717

1818
from copy import deepcopy
19+
from six import moves
1920
import json
21+
import six
2022
import os
2123
from os.path import dirname, abspath, exists, join, isabs
2224
import sys
@@ -407,18 +409,23 @@ def __init__(self, tgt, top_level_dirs=None, app_config=None):
407409
ConfigException("Could not parse mbed app configuration from %s"
408410
% self.app_config_location))
409411

410-
# Validate the format of the JSON file based on the schema_app.json
411-
schema_root = os.path.dirname(__file__)
412-
schema_path = os.path.join(schema_root, "schema_app.json")
413-
schema_file = json_file_to_dict(schema_path)
414412

415-
resolver = RefResolver("file://{}/".format(schema_root), schema_file)
416-
validator = Draft4Validator(schema_file, resolver=resolver)
413+
if self.app_config_location is not None:
414+
# Validate the format of the JSON file based on schema_app.json
415+
schema_root = os.path.dirname(os.path.abspath(__file__))
416+
schema_path = os.path.join(schema_root, "schema_app.json")
417+
schema = json_file_to_dict(schema_path)
417418

418-
errors = sorted(validator.iter_errors(self.app_config_data))
419+
url = moves.urllib.request.pathname2url(schema_path)
420+
uri = moves.urllib_parse.urljoin("file://", url)
419421

420-
if errors:
421-
raise ConfigException(",".join(x.message for x in errors))
422+
resolver = RefResolver(uri, schema)
423+
validator = Draft4Validator(schema, resolver=resolver)
424+
425+
errors = sorted(validator.iter_errors(self.app_config_data))
426+
427+
if errors:
428+
raise ConfigException(",".join(x.message for x in errors))
422429

423430
# Update the list of targets with the ones defined in the application
424431
# config, if applicable
@@ -470,12 +477,14 @@ def add_config_files(self, flist):
470477
continue
471478

472479
# Validate the format of the JSON file based on the schema_lib.json
473-
schema_root = os.path.dirname(__file__)
480+
schema_root = os.path.dirname(os.path.abspath(__file__))
474481
schema_path = os.path.join(schema_root, "schema_lib.json")
475482
schema_file = json_file_to_dict(schema_path)
476483

477-
resolver = RefResolver("file://{}/".format(schema_root),
478-
schema_file)
484+
url = moves.urllib.request.pathname2url(schema_path)
485+
uri = moves.urllib_parse.urljoin("file://", url)
486+
487+
resolver = RefResolver(uri, schema_file)
479488
validator = Draft4Validator(schema_file, resolver=resolver)
480489

481490
errors = sorted(validator.iter_errors(cfg))

tools/config/definitions.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"type": [
5454
"integer",
5555
"string",
56-
"boolean"
56+
"boolean",
57+
"null"
5758
]
5859
},
5960
"required": {
@@ -70,7 +71,8 @@
7071
"type": [
7172
"string",
7273
"integer",
73-
"boolean"
74+
"boolean",
75+
"null"
7476
]
7577
},
7678
"config_parameter_base": {
@@ -90,4 +92,4 @@
9092
},
9193
"additionalProperties": false
9294
}
93-
}
95+
}

tools/config/schema_app.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
"type": "object",
66
"properties": {
77
"name": {
8-
"$ref": "file:definitions.json#/name_definition"
8+
"$ref": "definitions.json#/name_definition"
99
},
1010
"config": {
11-
"$ref": "file:definitions.json#/config_definition"
11+
"$ref": "definitions.json#/config_definition"
1212
},
1313
"target_overrides": {
14-
"$ref": "file:definitions.json#/target_overrides_definition"
14+
"$ref": "definitions.json#/target_overrides_definition"
1515
},
1616
"macros": {
17-
"$ref": "file:definitions.json#/macro_definition"
17+
"$ref": "definitions.json#/macro_definition"
1818
},
1919
"artifact_name": {
2020
"type": "string"

tools/config/schema_lib.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
"type": "object",
66
"properties": {
77
"name": {
8-
"$ref": "file:definitions.json#/name_definition"
8+
"$ref": "definitions.json#/name_definition"
99
},
1010
"config": {
11-
"$ref": "file:definitions.json#/config_definition"
11+
"$ref": "definitions.json#/config_definition"
1212
},
1313
"target_overrides": {
14-
"$ref": "file:definitions.json#/target_overrides_definition"
14+
"$ref": "definitions.json#/target_overrides_definition"
1515
},
1616
"macros": {
17-
"$ref": "file:definitions.json#/macro_definition"
17+
"$ref": "definitions.json#/macro_definition"
1818
}
1919
},
2020
"required": [

tools/test/config/config_test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ def test_init_app_config(target):
108108

109109
config = Config(target, app_config=app_config)
110110

111-
mock_json_file_to_dict.assert_called_with(app_config)
111+
mock_json_file_to_dict.assert_any_call("app_config")
112+
112113
assert config.app_config_data == mock_return
113114

114115

@@ -149,7 +150,7 @@ def test_init_no_app_config_with_dir(target):
149150
config = Config(target, [directory])
150151

151152
mock_isfile.assert_called_with(path)
152-
mock_json_file_to_dict.assert_called_once_with(path)
153+
mock_json_file_to_dict.assert_any_call(path)
153154
assert config.app_config_data == mock_return
154155

155156

@@ -171,5 +172,5 @@ def test_init_override_app_config(target):
171172

172173
config = Config(target, [directory], app_config=app_config)
173174

174-
mock_json_file_to_dict.assert_called_once_with(app_config)
175+
mock_json_file_to_dict.assert_any_call(app_config)
175176
assert config.app_config_data == mock_return
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"K64F": {
3-
"exception_msg": "Unknown key(s)"
3+
"exception_msg": "Additional properties are not allowed ('unknown_key' was unexpected)"
44
}
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"K64F": {
3-
"exception_msg": "Unknown key(s)"
3+
"exception_msg": "Additional properties are not allowed ('unknown_key' was unexpected)"
44
}
55
}

0 commit comments

Comments
 (0)