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.
pull/5022/head
Steven Cartmell 2017-09-12 16:02:17 +01:00
parent 7010010ffc
commit b1ea3881be
7 changed files with 40 additions and 28 deletions

View File

@ -16,7 +16,9 @@ limitations under the License.
""" """
from copy import deepcopy from copy import deepcopy
from six import moves
import json import json
import six
import os import os
from os.path import dirname, abspath, exists, join, isabs from os.path import dirname, abspath, exists, join, isabs
import sys import sys
@ -407,13 +409,18 @@ class Config(object):
ConfigException("Could not parse mbed app configuration from %s" ConfigException("Could not parse mbed app configuration from %s"
% self.app_config_location)) % self.app_config_location))
# Validate the format of the JSON file based on the schema_app.json
schema_root = os.path.dirname(__file__)
schema_path = os.path.join(schema_root, "schema_app.json")
schema_file = json_file_to_dict(schema_path)
resolver = RefResolver("file://{}/".format(schema_root), schema_file) if self.app_config_location is not None:
validator = Draft4Validator(schema_file, resolver=resolver) # Validate the format of the JSON file based on schema_app.json
schema_root = os.path.dirname(os.path.abspath(__file__))
schema_path = os.path.join(schema_root, "schema_app.json")
schema = json_file_to_dict(schema_path)
url = moves.urllib.request.pathname2url(schema_path)
uri = moves.urllib_parse.urljoin("file://", url)
resolver = RefResolver(uri, schema)
validator = Draft4Validator(schema, resolver=resolver)
errors = sorted(validator.iter_errors(self.app_config_data)) errors = sorted(validator.iter_errors(self.app_config_data))
@ -470,12 +477,14 @@ class Config(object):
continue continue
# Validate the format of the JSON file based on the schema_lib.json # Validate the format of the JSON file based on the schema_lib.json
schema_root = os.path.dirname(__file__) schema_root = os.path.dirname(os.path.abspath(__file__))
schema_path = os.path.join(schema_root, "schema_lib.json") schema_path = os.path.join(schema_root, "schema_lib.json")
schema_file = json_file_to_dict(schema_path) schema_file = json_file_to_dict(schema_path)
resolver = RefResolver("file://{}/".format(schema_root), url = moves.urllib.request.pathname2url(schema_path)
schema_file) uri = moves.urllib_parse.urljoin("file://", url)
resolver = RefResolver(uri, schema_file)
validator = Draft4Validator(schema_file, resolver=resolver) validator = Draft4Validator(schema_file, resolver=resolver)
errors = sorted(validator.iter_errors(cfg)) errors = sorted(validator.iter_errors(cfg))

View File

@ -53,7 +53,8 @@
"type": [ "type": [
"integer", "integer",
"string", "string",
"boolean" "boolean",
"null"
] ]
}, },
"required": { "required": {
@ -70,7 +71,8 @@
"type": [ "type": [
"string", "string",
"integer", "integer",
"boolean" "boolean",
"null"
] ]
}, },
"config_parameter_base": { "config_parameter_base": {

View File

@ -5,16 +5,16 @@
"type": "object", "type": "object",
"properties": { "properties": {
"name": { "name": {
"$ref": "file:definitions.json#/name_definition" "$ref": "definitions.json#/name_definition"
}, },
"config": { "config": {
"$ref": "file:definitions.json#/config_definition" "$ref": "definitions.json#/config_definition"
}, },
"target_overrides": { "target_overrides": {
"$ref": "file:definitions.json#/target_overrides_definition" "$ref": "definitions.json#/target_overrides_definition"
}, },
"macros": { "macros": {
"$ref": "file:definitions.json#/macro_definition" "$ref": "definitions.json#/macro_definition"
}, },
"artifact_name": { "artifact_name": {
"type": "string" "type": "string"

View File

@ -5,16 +5,16 @@
"type": "object", "type": "object",
"properties": { "properties": {
"name": { "name": {
"$ref": "file:definitions.json#/name_definition" "$ref": "definitions.json#/name_definition"
}, },
"config": { "config": {
"$ref": "file:definitions.json#/config_definition" "$ref": "definitions.json#/config_definition"
}, },
"target_overrides": { "target_overrides": {
"$ref": "file:definitions.json#/target_overrides_definition" "$ref": "definitions.json#/target_overrides_definition"
}, },
"macros": { "macros": {
"$ref": "file:definitions.json#/macro_definition" "$ref": "definitions.json#/macro_definition"
} }
}, },
"required": [ "required": [

View File

@ -108,7 +108,8 @@ def test_init_app_config(target):
config = Config(target, app_config=app_config) config = Config(target, app_config=app_config)
mock_json_file_to_dict.assert_called_with(app_config) mock_json_file_to_dict.assert_any_call("app_config")
assert config.app_config_data == mock_return assert config.app_config_data == mock_return
@ -149,7 +150,7 @@ def test_init_no_app_config_with_dir(target):
config = Config(target, [directory]) config = Config(target, [directory])
mock_isfile.assert_called_with(path) mock_isfile.assert_called_with(path)
mock_json_file_to_dict.assert_called_once_with(path) mock_json_file_to_dict.assert_any_call(path)
assert config.app_config_data == mock_return assert config.app_config_data == mock_return
@ -171,5 +172,5 @@ def test_init_override_app_config(target):
config = Config(target, [directory], app_config=app_config) config = Config(target, [directory], app_config=app_config)
mock_json_file_to_dict.assert_called_once_with(app_config) mock_json_file_to_dict.assert_any_call(app_config)
assert config.app_config_data == mock_return assert config.app_config_data == mock_return

View File

@ -1,5 +1,5 @@
{ {
"K64F": { "K64F": {
"exception_msg": "Unknown key(s)" "exception_msg": "Additional properties are not allowed ('unknown_key' was unexpected)"
} }
} }

View File

@ -1,5 +1,5 @@
{ {
"K64F": { "K64F": {
"exception_msg": "Unknown key(s)" "exception_msg": "Additional properties are not allowed ('unknown_key' was unexpected)"
} }
} }