From 69a8ecadbaa9bcb3640f890633a7448fa89c3c80 Mon Sep 17 00:00:00 2001 From: Nolan Woods Date: Fri, 1 Jul 2022 16:20:42 -0700 Subject: [PATCH] Refactor anchor tests --- tests/unit/test_loaders.py | 66 +++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/tests/unit/test_loaders.py b/tests/unit/test_loaders.py index 22e500cc3..8644c8aee 100644 --- a/tests/unit/test_loaders.py +++ b/tests/unit/test_loaders.py @@ -2,6 +2,7 @@ import pathlib import pytest +import ruamel.yaml.composer from check_jsonschema.loaders import BadFileTypeError, InstanceLoader, SchemaLoader from check_jsonschema.loaders.instance.json5 import ENABLED as JSON5_ENABLED @@ -55,11 +56,11 @@ def test_schemaloader_local_yaml_data(tmp_path, filename): a: type: object properties: - b: &anchor + b: type: array items: type: integer - c: &anchor + c: type: string """ ) @@ -80,6 +81,38 @@ def test_schemaloader_local_yaml_data(tmp_path, filename): } +@pytest.mark.parametrize( + "filename", + [ + "schema.yaml", + ], +) +@pytest.mark.filterwarnings("ignore:ReusedAnchorWarning") +def test_schemaloader_local_yaml_dup_anchor(tmp_path, filename): + f = tmp_path / filename + f.write_text( + """ +--- +"$schema": https://json-schema.org/draft/2020-12/schema +type: object +properties: + a: + type: object + properties: + b: &anchor + type: array + items: + type: integer + c: &anchor + type: string +""" + ) + try: + SchemaLoader(str(f)) + except ruamel.yaml.composer.ComposerError as e: + raise AssertionError(f"YAML loader does not support duplicate anchors {e}") + + @pytest.mark.parametrize( "schemafile", [ @@ -130,10 +163,10 @@ def test_instanceloader_yaml_data(tmp_path, filename, default_ft): f.write_text( """\ a: - b: &anchor + b: - 1 - 2 - c: &anchor d + c: d """ ) loader = InstanceLoader([str(f)], default_filetype=default_ft) @@ -141,6 +174,31 @@ def test_instanceloader_yaml_data(tmp_path, filename, default_ft): assert data == [(str(f), {"a": {"b": [1, 2], "c": "d"}})] +@pytest.mark.parametrize( + "filename", + [ + "foo.yaml", + ], +) +@pytest.mark.filterwarnings("ignore:ReusedAnchorWarning") +def test_instanceloader_yaml_dup_anchor(tmp_path, filename): + f = tmp_path / filename + f.write_text( + """\ +a: + b: &anchor + - 1 + - 2 + c: &anchor d +""" + ) + loader = InstanceLoader([str(f)]) + try: + list(loader.iter_files()) + except ruamel.yaml.composer.ComposerError as e: + raise AssertionError(f"YAML loader does not support duplicate anchors {e}") + + def test_instanceloader_unknown_type(tmp_path): f = tmp_path / "foo" # no extension here f.write_text("{}") # json data (could be detected as either)