Skip to content

Commit

Permalink
Merge pull request #16684 from davelopez/tool_linter_validate_regex
Browse files Browse the repository at this point in the history
Include `regex` when linting validators
  • Loading branch information
mvdbeek authored Sep 14, 2023
2 parents 2cb65be + c96b9a4 commit a1d9c81
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/galaxy/tool_util/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ def __init__(self, level: str, message: str, **kwargs):
def __eq__(self, other) -> bool:
"""
add equal operator to easy lookup of a message in a
List[LintMessage] which is usefull in tests
List[LintMessage] which is useful in tests.
If the other object is a string, it is loosely checked if the
string is contained in the message.
"""
if isinstance(other, str):
return self.message == other
return other in self.message
if isinstance(other, LintMessage):
return self.message == other.message
return False
Expand Down
10 changes: 7 additions & 3 deletions lib/galaxy/tool_util/linters/inputs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""This module contains a linting functions for tool inputs."""
import ast
import re
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -395,14 +396,17 @@ def lint_inputs(tool_source: "ToolSource", lint_ctx: "LintContext"):
f"Parameter [{param_name}]: attribute '{attrib}' is incompatible with validator of type '{vtype}'",
node=validator,
)
if vtype == "expression":
if vtype in ["expression", "regex"]:
if validator.text is None:
lint_ctx.error(
f"Parameter [{param_name}]: expression validators are expected to contain text", node=validator
f"Parameter [{param_name}]: {vtype} validators are expected to contain text", node=validator
)
else:
try:
re.compile(validator.text)
if vtype == "regex":
re.compile(validator.text)
else:
ast.parse(validator.text, mode="eval")
except Exception as e:
lint_ctx.error(
f"Parameter [{param_name}]: '{validator.text}' is no valid regular expression: {str(e)}",
Expand Down
8 changes: 6 additions & 2 deletions test/unit/tool_util/test_tool_linters.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@
<validator type="in_range">TEXT</validator>
<validator type="regex" filename="blah"/>
<validator type="expression"/>
<validator type="expression">[</validator>
<validator type="regex">[</validator>
<validator type="expression">(</validator>
<validator type="expression">value and "," not in value</validator>
<validator type="value_in_data_table"/>
</param>
<param name="another_param_name" type="data" format="bed">
Expand Down Expand Up @@ -1384,10 +1386,12 @@ def test_inputs_validator_incompatibilities(lint_ctx):
in lint_ctx.error_messages
)
assert "Parameter [param_name]: expression validators are expected to contain text" in lint_ctx.error_messages
assert "Parameter [param_name]: regex validators are expected to contain text" in lint_ctx.error_messages
assert (
"Parameter [param_name]: '[' is no valid regular expression: unterminated character set at position 0"
in lint_ctx.error_messages
)
assert "Parameter [param_name]: '(' is no valid regular expression" in lint_ctx.error_messages
assert (
"Parameter [another_param_name]: 'metadata' validators need to define the 'check' or 'skip' attribute(s)"
in lint_ctx.error_messages
Expand All @@ -1407,7 +1411,7 @@ def test_inputs_validator_incompatibilities(lint_ctx):
assert len(lint_ctx.info_messages) == 1
assert not lint_ctx.valid_messages
assert len(lint_ctx.warn_messages) == 1
assert len(lint_ctx.error_messages) == 9
assert len(lint_ctx.error_messages) == 11


def test_inputs_validator_correct(lint_ctx):
Expand Down

0 comments on commit a1d9c81

Please sign in to comment.