comment: raise an exception when an invalid option is provided (#84984)

Co-authored-by: Matt Clay <matt@mystile.com>
Signed-off-by: Abhijeet Kasurde <Akasurde@redhat.com>
(cherry picked from commit 1daa8412d5)
pull/85040/head
Abhijeet Kasurde 9 months ago committed by Matt Martz
parent 89a4900b61
commit ffbf121182
No known key found for this signature in database
GPG Key ID: 40832D88E9FC91D8

@ -0,0 +1,3 @@
---
minor_changes:
- comment filter - Improve the error message shown when an invalid ``style`` argument is provided.

@ -26,7 +26,7 @@ from jinja2.filters import do_map, do_select, do_selectattr, do_reject, do_rejec
from jinja2.environment import Environment
from ansible._internal._templating import _lazy_containers
from ansible.errors import AnsibleFilterError, AnsibleTypeError
from ansible.errors import AnsibleFilterError, AnsibleTypeError, AnsibleTemplatePluginError
from ansible.module_utils.datatag import native_type_name
from ansible.module_utils.common.json import get_encoder, get_decoder
from ansible.module_utils.six import string_types, integer_types, text_type
@ -405,6 +405,13 @@ def comment(text, style='plain', **kw):
}
}
if style not in comment_styles:
raise AnsibleTemplatePluginError(
message=f"Invalid style {style!r}.",
help_text=f"Available styles: {', '.join(comment_styles)}",
obj=style,
)
# Pointer to the right comment type
style_params = comment_styles[style]

@ -646,6 +646,19 @@
- '"boo!"|comment(decoration="") == "boo!\n"'
- '"boo!"|comment(prefix="\n", prefix_count=20) == "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n# boo!\n#"'
- name: Invalid style value for comment
set_fact:
foo: '{{ "boo" | comment(style="invalid") }}'
ignore_errors: yes
register: invalid_style_comment
- name: Verify invalid comment value fails
assert:
that:
- invalid_style_comment is failed
- "'Invalid style' in invalid_style_comment.msg"
- name: Verify subelements throws on invalid obj
set_fact:
foo: '{{True|subelements("foo")}}'

Loading…
Cancel
Save