Unverified Commit fdafb065 authored by mvdbeek's avatar mvdbeek
Browse files

Relax validation of XML test assertion parsing

when tool source is XML. We can't really encode these attributes as JSON
in XML, and while we could make it work it doesn't feel great to force
tool authors to use such a hypothetical syntax.

I think we still keep all the nice validation here, it's just a little
more lenient.
parent 539441dd
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -148,6 +148,8 @@ class TestsAssertionValidation(Linter):
            lint_ctx.warn("Failed to parse test dictionaries from tool - cannot lint assertions")
            return
        assert "tests" in raw_tests_dict
        # This really only allows coercion from strings, the values themselves will still be validated
        strict_validation = tool_source.language != "xml"
        for test_idx, test in enumerate(raw_tests_dict["tests"], start=1):
            # TODO: validate command, command_version, element tests. What about children?
            for output in test["outputs"]:
@@ -156,7 +158,7 @@ class TestsAssertionValidation(Linter):
                for raw_assert in asserts_raw:
                    to_yaml_assertions.append({"that": raw_assert["tag"], **raw_assert.get("attributes", {})})
                try:
                    assertion_list.model_validate(to_yaml_assertions)
                    assertion_list.model_validate(to_yaml_assertions, strict=strict_validation)
                except Exception as e:
                    error_str = _cleanup_pydantic_error(e)
                    lint_ctx.warn(
+23 −0
Original line number Diff line number Diff line
@@ -850,6 +850,23 @@ VALID_CENTER_OF_MASS = """
    </tests>
</tool>
"""
ASSERTS_STRING_COERCION = """
<tool id="id" name="name">
    <outputs>
        <data name="data_name" format="ome.tiff"/>
    </outputs>
    <tests>
        <test>
            <output name="data_name">
               <assert_contents>
                    <!-- channels is defined as an integer, so coercion from string must be applied on validation -->
                    <has_image_channels channels="3" />
                </assert_contents>
            </output>
        </test>
    </tests>
</tool>
"""
TESTS_VALID = """
<tool id="id" name="name">
    <outputs>
@@ -1945,6 +1962,12 @@ def test_tests_asserts(lint_ctx):
    assert len(lint_ctx.error_messages) == 9


def test_tests_asserts_string_coercion(lint_ctx):
    tool_source = get_xml_tool_source(ASSERTS_STRING_COERCION)
    run_lint_module(lint_ctx, tests, tool_source)
    assert len(lint_ctx.warn_messages) == 0, lint_ctx.warn_messages


def test_tests_assertion_models_valid(lint_ctx):
    tool_source = get_xml_tool_source(VALID_CENTER_OF_MASS)
    run_lint_module(lint_ctx, tests, tool_source)