Skip to content

Commit

Permalink
Merge branch 'main' into test-job-template
Browse files Browse the repository at this point in the history
  • Loading branch information
bzwei authored Feb 7, 2023
2 parents bfdb4ac + 7ca2bab commit 59fa875
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
8 changes: 4 additions & 4 deletions ansible_rulebook/condition_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import logging
import sys

from pyparsing import (
Combine,
Expand All @@ -33,6 +32,7 @@
)

from ansible_rulebook.exception import (
ConditionParsingException,
SelectattrOperatorException,
SelectOperatorException,
)
Expand Down Expand Up @@ -306,6 +306,6 @@ def parse_condition(condition_string: str) -> Condition:
try:
return condition.parseString(condition_string, parse_all=True)[0]
except ParseException as pe:
print(pe.explain(depth=0), file=sys.stderr)
logger.error(pe.explain(depth=0))
raise
msg = f"Error parsing: {condition_string}. {pe}"
logger.debug(pe.explain(depth=0))
raise ConditionParsingException(msg)
10 changes: 10 additions & 0 deletions ansible_rulebook/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ class SelectattrOperatorException(Exception):
pass


class InvalidIdentifierException(Exception):

pass


class SelectOperatorException(Exception):

pass


class ConditionParsingException(Exception):

pass
8 changes: 7 additions & 1 deletion ansible_rulebook/json_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
)
from ansible_rulebook.exception import (
InvalidAssignmentException,
InvalidIdentifierException,
VarsKeyMissingException,
)
from ansible_rulebook.rule_types import (
Expand Down Expand Up @@ -99,7 +100,12 @@ def visit_condition(parsed_condition: ConditionTypes, variables: Dict):
f"vars does not contain key: {key}"
)
else:
raise Exception(f"Unhandled identifier {parsed_condition.value}")
msg = (
f"Invalid identifier : {parsed_condition.value} "
+ "Should start with event., events.,fact., facts. or vars."
)
raise InvalidIdentifierException(msg)

elif isinstance(parsed_condition, String):
return {
"String": substitute_variables(parsed_condition.value, variables)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

from ansible_rulebook.condition_parser import parse_condition
from ansible_rulebook.exception import (
ConditionParsingException,
InvalidAssignmentException,
InvalidIdentifierException,
SelectattrOperatorException,
SelectOperatorException,
)
Expand Down Expand Up @@ -500,3 +502,25 @@ def test_invalid_nested_assignment_operator():
parse_condition("events.first.abc.xyz << fact.range.i == 'Hello'"),
{},
)


def test_invalid_identifier():
with pytest.raises(InvalidIdentifierException):
visit_condition(
parse_condition("event is defined"),
{},
)


@pytest.mark.parametrize(
"invalid_condition",
[
"event. is defined",
],
)
def test_invalid_conditions(invalid_condition):
with pytest.raises(ConditionParsingException):
visit_condition(
parse_condition(invalid_condition),
{},
)

0 comments on commit 59fa875

Please sign in to comment.