diff --git a/lib/galaxy/tool_util/linters/xml_order.py b/lib/galaxy/tool_util/linters/xml_order.py index 7fb2bd261331..82d17f4e5ac7 100644 --- a/lib/galaxy/tool_util/linters/xml_order.py +++ b/lib/galaxy/tool_util/linters/xml_order.py @@ -1,8 +1,15 @@ -"""This module contains a linting functions for tool XML block order. +"""This module contains a linter for tool XML block order. For more information on the IUC standard for XML block order see - https://github.com/galaxy-iuc/standards. """ +from typing import TYPE_CHECKING + +from galaxy.tool_util.lint import Linter + +if TYPE_CHECKING: + from galaxy.tool_util.lint import LintContext + from galaxy.tool_util.parser.interface import ToolSource # https://github.com/galaxy-iuc/standards # https://github.com/galaxy-iuc/standards/pull/7/files @@ -43,21 +50,25 @@ ] -# Ensure the XML blocks appear in the correct order prescribed -# by the tool author best practices. -def lint_xml_order(tool_xml, lint_ctx): - tool_root = tool_xml.getroot() +class XMLOrder(Linter): + @classmethod + def lint(cls, tool_source: "ToolSource", lint_ctx: "LintContext"): + tool_xml = getattr(tool_source, "xml_tree", None) + if not tool_xml: + return + tool_root = tool_xml.getroot() - if tool_root.attrib.get("tool_type", "") == "data_source": - tag_ordering = DATASOURCE_TAG_ORDER - else: - tag_ordering = TAG_ORDER + if tool_root.attrib.get("tool_type", "") == "data_source": + tag_ordering = DATASOURCE_TAG_ORDER + else: + tag_ordering = TAG_ORDER - last_tag = None - last_key = None - for elem in tool_root: - tag = elem.tag - if tag in tag_ordering: + last_tag = None + last_key = None + for elem in tool_root: + tag = elem.tag + if tag not in tag_ordering: + continue key = tag_ordering.index(tag) if last_key: if last_key > key: @@ -66,5 +77,3 @@ def lint_xml_order(tool_xml, lint_ctx): ) last_tag = tag last_key = key - else: - lint_ctx.info(f"Unknown tag [{tag}] encountered, this may result in a warning in the future.", node=elem) diff --git a/test/unit/tool_util/test_tool_linters.py b/test/unit/tool_util/test_tool_linters.py index 5026a5a63a39..3df95db07f90 100644 --- a/test/unit/tool_util/test_tool_linters.py +++ b/test/unit/tool_util/test_tool_linters.py @@ -1778,13 +1778,11 @@ def test_tests_compare_attrib_incompatibility(lint_ctx): def test_xml_order(lint_ctx): - tool_xml_tree = get_xml_tree(XML_ORDER) - run_lint_module(lint_ctx, xml_order, tool_xml_tree) - assert "Unknown tag [wrong_tag] encountered, this may result in a warning in the future." in lint_ctx.info_messages - assert "Best practice violation [stdio] elements should come before [command]" in lint_ctx.warn_messages - assert len(lint_ctx.info_messages) == 1 + tool_source = get_xml_tool_source(XML_ORDER) + run_lint_module(lint_ctx, xml_order, tool_source) + assert lint_ctx.warn_messages == ["Best practice violation [stdio] elements should come before [command]"] + assert not lint_ctx.info_messages assert not lint_ctx.valid_messages - assert len(lint_ctx.warn_messages) == 1 assert not lint_ctx.error_messages