Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for combining multiple schemas.
- Loading branch information
Showing
7 changed files
with
144 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
click | ||
deepmerge | ||
jsonschema | ||
pygithub | ||
pyyaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,53 @@ | ||
import logging | ||
from pathlib import Path | ||
|
||
import jsonschema | ||
import yaml | ||
from deepmerge import STRATEGY_END, Merger | ||
from deepmerge.exception import InvalidMerge | ||
|
||
logger = logging.getLogger("dirschema") | ||
|
||
SCHEMA_SCHEMA = Path(__file__).parent / "schemas" / "dirschema-v1.yaml" | ||
|
||
|
||
def load_schema(schema): | ||
loaded = yaml.safe_load(schema) | ||
def fail_if_file_is_absent_and_present(config, path, base, nxt): | ||
for f in base.get("files", {}).keys(): | ||
if f in nxt.get("files", {}): | ||
if base["files"][f].get("absent") != nxt["files"][f].get("absent"): | ||
raise InvalidMerge( | ||
f"Cannot merge {base} and {nxt} because {f}" | ||
"is specified as both absent and present" | ||
) | ||
|
||
return STRATEGY_END | ||
|
||
|
||
def allow_same_simple_values(config, path, base, nxt): | ||
if base != nxt: | ||
raise InvalidMerge(f"Cannot merge {path} because values differ ({base}, {nxt})") | ||
|
||
return nxt | ||
|
||
|
||
schema_merger = Merger( | ||
[(list, ["append"]), (dict, [fail_if_file_is_absent_and_present, "merge"])], | ||
[allow_same_simple_values], | ||
[], | ||
) | ||
|
||
|
||
def combine_schemas(schemas): | ||
combined = schemas[0] | ||
|
||
for s in schemas[1:]: | ||
schema_merger.merge(combined, s) | ||
|
||
return combined | ||
|
||
|
||
def load_schemas(*schemas): | ||
loaded = combine_schemas([yaml.safe_load(schema) for schema in schemas]) | ||
schema_schema = yaml.safe_load(open(SCHEMA_SCHEMA).read()) | ||
jsonschema.validate(loaded, schema_schema) | ||
return loaded |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
files: | ||
foo: | ||
contains: | ||
- "foo" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,87 @@ | ||
import pytest | ||
from deepmerge.exception import InvalidMerge | ||
from jsonschema import ValidationError | ||
from yaml import YAMLError | ||
|
||
from dirschema.schema import load_schema | ||
from dirschema.schema import combine_schemas, load_schemas | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"schema", | ||
"schemas", | ||
( | ||
'{"files": {}, "dirs": {}, "allow_extra_files": false, "allow_extra_dirs": false}', | ||
'{"files": {"foo": {"absent": True}}, "dirs": {},' | ||
'"allow_extra_files": false, "allow_extra_dirs": false}', | ||
'{"files": {}, "dirs": {"dir1": {"absent": True}},' | ||
'"allow_extra_files": false, "allow_extra_dirs": false}', | ||
('{"files": {}, "dirs": {}, "allow_extra_files": false, "allow_extra_dirs": false}',), | ||
( | ||
'{"files": {"foo": {"absent": True}}, "dirs": {},' | ||
'"allow_extra_files": false, "allow_extra_dirs": false}', | ||
), | ||
( | ||
'{"files": {}, "dirs": {"dir1": {"absent": True}},' | ||
'"allow_extra_files": false, "allow_extra_dirs": false}', | ||
), | ||
), | ||
) | ||
def test_load_schema_good(schema): | ||
load_schema(schema) | ||
def test_load_schemas_good(schemas): | ||
load_schemas(*schemas) | ||
# No errors! | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"schema,expected", | ||
"schemas,expected", | ||
( | ||
('{"files": {}, "dirs": {}}', ValidationError), | ||
('{"files": {"foo": {"contains": ["blah"], "absent": true}}}', ValidationError), | ||
('{"dirs": {"foo": {"allow_extra_files": true, "absent": true}}}', ValidationError), | ||
('{"files": {"foo": {"absent": false}}, "dirs": {}}', ValidationError), | ||
("][:badyaml:", YAMLError), | ||
(('{"files": {}, "dirs": {}}',), ValidationError), | ||
(('{"files": {"foo": {"contains": ["blah"], "absent": true}}}',), ValidationError), | ||
(('{"dirs": {"foo": {"allow_extra_files": true, "absent": true}}}',), ValidationError), | ||
(('{"files": {"foo": {"absent": false}}, "dirs": {}}',), ValidationError), | ||
(("][:badyaml:",), YAMLError), | ||
), | ||
) | ||
def test_load_schema_invalid(schema, expected): | ||
def test_load_schemas_invalid(schemas, expected): | ||
try: | ||
load_schema(schema) | ||
load_schemas(*schemas) | ||
assert False, "Shouldn't have successfully loaded schema" | ||
except Exception as e: | ||
assert isinstance(e, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"schemas,expected", | ||
( | ||
( | ||
({"files": {"foo": {}}}, {"dirs": {"bar": {}}}), | ||
{"files": {"foo": {}}, "dirs": {"bar": {}}}, | ||
), | ||
( | ||
({"files": {"foo": {}, "bar": {}}}, {"files": {"foo": {"contains": ["foo"]}}}), | ||
{"files": {"foo": {"contains": ["foo"]}, "bar": {}}}, | ||
), | ||
( | ||
({"files": {"foo": {"contains": ["oof"]}}}, {"files": {"foo": {"contains": ["foo"]}}}), | ||
{"files": {"foo": {"contains": ["oof", "foo"]}}}, | ||
), | ||
), | ||
) | ||
def test_can_combine(schemas, expected): | ||
assert combine_schemas(schemas) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"schemas,expected", | ||
( | ||
(({"files": {"foo": {}}}, {"files": {"foo": {"absent": True}}}), "foo"), | ||
( | ||
( | ||
{"dirs": {"foo": {"allow_empty_dirs": True}}}, | ||
{"dirs": {"foo": {"allow_empty_dirs": False}}}, | ||
), | ||
"allow_empty_dirs", | ||
), | ||
), | ||
) | ||
def test_cannot_combine(schemas, expected): | ||
try: | ||
print(schemas) | ||
combine_schemas(schemas) | ||
assert False, "Shouldn't have successfully combined schemas" | ||
except Exception as e: | ||
assert isinstance(e, InvalidMerge) | ||
assert any([expected in a for a in e.args]) |