Skip to content

Commit

Permalink
make xml_order a Linter
Browse files Browse the repository at this point in the history
  • Loading branch information
bernt-matthias committed Nov 26, 2023
1 parent 8a16c62 commit 63693f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
41 changes: 25 additions & 16 deletions lib/galaxy/tool_util/linters/xml_order.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
10 changes: 4 additions & 6 deletions test/unit/tool_util/test_tool_linters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 63693f1

Please sign in to comment.