diff --git a/Makefile b/Makefile index 9513ab2072..93de3c4154 100755 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ lint: # Linter performs static analysis to catch latent bugs pylint --rcfile .pylintrc samtranslator # Ensure templates adhere to JSON schema - bin/validate.sh + bin/validate_schema.py prepare-companion-stack: pytest -v --no-cov integration/setup -m setup diff --git a/bin/validate.sh b/bin/validate.sh deleted file mode 100755 index d4cbb0e23c..0000000000 --- a/bin/validate.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash -set -euxo pipefail - -# TODO: Enable the valid templates -# TODO: Switch to way-faster Python script -find tests/translator/input -type f -name '*.yaml' | grep -v error_ | grep -v unsupported_resources | grep -v resource_with_invalid_type | shuf | while read -r template; do - jsonschema -i <(cfn-flip --json "${template}") samtranslator/schema/schema.json -done diff --git a/bin/validate_schema.py b/bin/validate_schema.py new file mode 100755 index 0000000000..44771593ff --- /dev/null +++ b/bin/validate_schema.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python + +import json +from pathlib import Path +from typing import Iterator + +from cfn_flip import to_json # type: ignore +from jsonschema import validate + +SCHEMA = json.loads(Path("samtranslator/schema/schema.json").read_bytes()) + + +def get_templates() -> Iterator[Path]: + paths = ( + list(Path("tests/translator/input").glob("**/*.yaml")) + + list(Path("tests/translator/input").glob("**/*.yml")) + + list(Path("integration/resources/templates").glob("**/*.yaml")) + + list(Path("integration/resources/templates").glob("**/*.yml")) + ) + # TODO: Enable (most likely) everything but error_ + skips = [ + "error_", + "unsupported_resources", + "resource_with_invalid_type", + ] + + def should_skip(s: str) -> bool: + for skip in skips: + if skip in s: + return True + return False + + for path in paths: + if not should_skip(str(path)): + yield path + + +def main() -> None: + for path in get_templates(): + print(f"Checking {path}") + obj = json.loads(to_json(path.read_bytes())) + validate(obj, schema=SCHEMA) + + +if __name__ == "__main__": + main()