| 1 | """Implementation of NoFreeFormRule.""" |
| 2 | |
| 3 | from __future__scip-python python python-stdlib 3.11 __future__/__init__: import annotationsscip-python python python-stdlib 3.11 __future__/annotations.annotations. |
| 4 | |
| 5 | import functoolsscip-python python python-stdlib 3.11 functools/__init__: |
| 6 | import rescip-python python python-stdlib 3.11 re/__init__: |
| 7 | import sysscip-python python python-stdlib 3.11 sys/__init__: |
| 8 | from typingscip-python python python-stdlib 3.11 typing/__init__: import TYPE_CHECKINGscip-python python python-stdlib 3.11 typing/TYPE_CHECKING.TYPE_CHECKING., Anyscip-python python python-stdlib 3.11 typing/Any. |
| 9 | |
| 10 | from ansible.errorsscip-python python ansible 14.0.0 `ansible.errors`/__init__: import AnsibleParserError |
| 11 | from ansible.parsing.splitterscip-python python ansible 14.0.0 `ansible.parsing.splitter`/__init__: import split_args |
| 12 | from ruamel.yaml.scalarstringscip-python python ansible-lint 26 `ruamel.yaml.scalarstring`/__init__: import DoubleQuotedScalarString, SingleQuotedScalarString |
| 13 | |
| 14 | from ansiblelint.constantsscip-python python ansible-lint 26 `ansiblelint.constants`/__init__: import INCLUSION_ACTION_NAMESscip-python python ansible-lint 26 `src.ansiblelint.constants`/INCLUSION_ACTION_NAMES. |
| 15 | from ansiblelint.rulesscip-python python ansible-lint 26 `ansiblelint.rules`/__init__: import AnsibleLintRulescip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#, TransformMixinscip-python python ansible-lint 26 `src.ansiblelint.rules`/TransformMixin# |
| 16 | from ansiblelint.rules.key_orderscip-python python ansible-lint 26 `ansiblelint.rules.key_order`/__init__: import task_property_sorterscip-python python ansible-lint 26 `src.ansiblelint.rules.key_order`/task_property_sorter(). |
| 17 | |
| 18 | if TYPE_CHECKINGscip-python python python-stdlib 3.11 typing/TYPE_CHECKING.TYPE_CHECKING.: |
| 19 | from ruamel.yaml.commentsscip-python python ansible-lint 26 `ruamel.yaml.comments`/__init__: import CommentedMap, CommentedSeq |
| 20 | |
| 21 | from ansiblelint.errorsscip-python python ansible-lint 26 `ansiblelint.errors`/__init__: import MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError# |
| 22 | from ansiblelint.file_utilsscip-python python ansible-lint 26 `ansiblelint.file_utils`/__init__: import Lintablescip-python python ansible-lint 26 `src.ansiblelint.file_utils`/Lintable# |
| 23 | from ansiblelint.utilsscip-python python ansible-lint 26 `ansiblelint.utils`/__init__: import Taskscip-python python ansible-lint 26 `src.ansiblelint.utils`/Task# |
| 24 | |
| 25 | |
| 26 | class NoFreeFormRulescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#(AnsibleLintRulescip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#, TransformMixinscip-python python ansible-lint 26 `src.ansiblelint.rules`/TransformMixin#): |
| 27 | """Rule for detecting discouraged free-form syntax for action modules.""" |
| 28 | |
| 29 | idscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#id. = "no-free-form" |
| 30 | descriptionscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#description. = "Avoid free-form inside files as it can produce subtle bugs." |
| 31 | severityscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#severity. = "MEDIUM" |
| 32 | tagsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#tags. = ["syntax", "risk"] |
| 33 | version_changedscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#version_changed. = "6.8.0" |
| 34 | needs_raw_taskscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#needs_raw_task. = True |
| 35 | cmd_shell_rescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#cmd_shell_re. = rescip-python python python-stdlib 3.11 re/__init__:.compilescip-python python python-stdlib 3.11 re/compile().( |
| 36 | r"(chdir|creates|executable|removes|stdin|stdin_add_newline|warn)=", |
| 37 | ) |
| 38 | _idsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_ids. = { |
| 39 | "no-free-form[raw]": "Avoid embedding `executable=` inside raw calls, use explicit args dictionary instead.", |
| 40 | "no-free-form[raw-non-string]": "Passing a non string value to `raw` module is neither documented or supported.", |
| 41 | } |
| 42 | |
| 43 | @staticmethodscip-python python python-stdlib 3.11 builtins/staticmethod# |
| 44 | def _has_unmatched_quotescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_has_unmatched_quote().(valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_has_unmatched_quote().(val): strscip-python python python-stdlib 3.11 builtins/str#) -> boolscip-python python python-stdlib 3.11 builtins/bool#: |
| 45 | """Detect values starting with a quote but lacking a closing match.""" |
| 46 | if not valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_has_unmatched_quote().(val): |
| 47 | return False |
| 48 | if valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_has_unmatched_quote().(val)[0] in "\"'": |
| 49 | if len(valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_has_unmatched_quote().(val)) == 1: |
| 50 | return True |
| 51 | return valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_has_unmatched_quote().(val)[-1] != valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_has_unmatched_quote().(val)[0] |
| 52 | return False |
| 53 | |
| 54 | @staticmethodscip-python python python-stdlib 3.11 builtins/staticmethod# |
| 55 | def _normalize_valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_normalize_value().(valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_normalize_value().(val): strscip-python python python-stdlib 3.11 builtins/str#) -> Anyscip-python python python-stdlib 3.11 typing/Any.: |
| 56 | """Normalize quoted values while keeping Jinja/spacing intact.""" |
| 57 | if len(valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_normalize_value().(val)) >= 2 and valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_normalize_value().(val)[0] == valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_normalize_value().(val)[-1] and valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_normalize_value().(val)[0] in "\"'": |
| 58 | return ( |
| 59 | DoubleQuotedScalarString if valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_normalize_value().(val)[0] == '"' else SingleQuotedScalarString |
| 60 | )(valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_normalize_value().(val)[1:-1]) |
| 61 | return valscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_normalize_value().(val) |
| 62 | |
| 63 | @classmethodscip-python python python-stdlib 3.11 builtins/classmethod# |
| 64 | def _parse_module_optsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_module_opts().(clsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_module_opts().(cls), valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_module_opts().(value): strscip-python python python-stdlib 3.11 builtins/str#) -> dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.]: |
| 65 | """Parse module opts, falling back to cmd on unbalanced quotes.""" |
| 66 | try: |
| 67 | parts = split_args(valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_module_opts().(value)) # type: ignore[no-untyped-call] |
| 68 | except AnsibleParserError: |
| 69 | return {"cmd": valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_module_opts().(value)} |
| 70 | module_opts: dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.] = {} |
| 71 | cmd_parts: listscip-python python python-stdlib 3.11 builtins/list#[strscip-python python python-stdlib 3.11 builtins/str#] = [] |
| 72 | for part in parts: |
| 73 | if "=" in part: |
| 74 | key, raw_value = part.split("=", 1) |
| 75 | if clsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_module_opts().(cls)._has_unmatched_quotescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_has_unmatched_quote().(raw_value): |
| 76 | return {"cmd": valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_module_opts().(value)} |
| 77 | module_opts[key] = clsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_module_opts().(cls)._normalize_valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_normalize_value().(raw_value) |
| 78 | else: |
| 79 | cmd_parts.appendscip-python python python-stdlib 3.11 builtins/list#append().(part) |
| 80 | if cmd_parts: |
| 81 | module_opts["cmd"] = " ".joinscip-python python python-stdlib 3.11 builtins/str#join().(cmd_parts) |
| 82 | return module_opts |
| 83 | |
| 84 | @classmethodscip-python python python-stdlib 3.11 builtins/classmethod# |
| 85 | def _parse_raw_valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_raw_value().(clsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_raw_value().(cls), valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_raw_value().(value): strscip-python python python-stdlib 3.11 builtins/str#) -> tuplescip-python python python-stdlib 3.11 builtins/tuple#[strscip-python python python-stdlib 3.11 builtins/str#, dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.]]: |
| 86 | """Parse raw module string, extracting executable when possible.""" |
| 87 | try: |
| 88 | parts = split_args(valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_raw_value().(value)) # type: ignore[no-untyped-call] |
| 89 | except AnsibleParserError: |
| 90 | return valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_raw_value().(value), {} |
| 91 | exec_key_val: dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.] = {} |
| 92 | raw_cmd_parts: listscip-python python python-stdlib 3.11 builtins/list#[strscip-python python python-stdlib 3.11 builtins/str#] = [] |
| 93 | for part in parts: |
| 94 | if part.startswith("executable="): |
| 95 | _, raw_value = part.split("=", 1) |
| 96 | exec_key_val["executable"] = clsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_raw_value().(cls)._normalize_valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_normalize_value().(raw_value) |
| 97 | else: |
| 98 | raw_cmd_parts.appendscip-python python python-stdlib 3.11 builtins/list#append().(part) |
| 99 | return " ".joinscip-python python python-stdlib 3.11 builtins/str#join().(raw_cmd_parts), exec_key_val |
| 100 | |
| 101 | @staticmethodscip-python python python-stdlib 3.11 builtins/staticmethod# |
| 102 | def _sorted_module_optsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_sorted_module_opts().(module_optsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_sorted_module_opts().(module_opts): dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.]) -> dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.]: |
| 103 | sorted_module_opts: dictscip-python python python-stdlib 3.11 builtins/dict#[strscip-python python python-stdlib 3.11 builtins/str#, Anyscip-python python python-stdlib 3.11 typing/Any.] = {} |
| 104 | for key in sortedscip-python python python-stdlib 3.11 builtins/sorted().( |
| 105 | module_optsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_sorted_module_opts().(module_opts).keysscip-python python python-stdlib 3.11 builtins/dict#keys().(), |
| 106 | keyscip-python python python-stdlib 3.11 builtins/sorted().(key)=functoolsscip-python python python-stdlib 3.11 functools/__init__:.cmp_to_keyscip-python python python-stdlib 3.11 functools/cmp_to_key().(task_property_sorterscip-python python ansible-lint 26 `src.ansiblelint.rules.key_order`/task_property_sorter().), |
| 107 | ): |
| 108 | sorted_module_opts[key] = module_optsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_sorted_module_opts().(module_opts)[key] |
| 109 | return sorted_module_opts |
| 110 | |
| 111 | def matchtaskscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().( |
| 112 | selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(self), |
| 113 | taskscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(task): Taskscip-python python ansible-lint 26 `src.ansiblelint.utils`/Task#, |
| 114 | filescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(file): Lintablescip-python python ansible-lint 26 `src.ansiblelint.file_utils`/Lintable# | None = None, |
| 115 | ) -> listscip-python python python-stdlib 3.11 builtins/list#[MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#]: |
| 116 | results: listscip-python python python-stdlib 3.11 builtins/list#[MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#] = [] |
| 117 | action = taskscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(task)["action"]["__ansible_module_original__"] |
| 118 | |
| 119 | if action in INCLUSION_ACTION_NAMESscip-python python ansible-lint 26 `src.ansiblelint.constants`/INCLUSION_ACTION_NAMES.: |
| 120 | return results |
| 121 | |
| 122 | action_value = taskscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(task)["__raw_task__"].get(action, None) |
| 123 | if taskscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(task)["action"].get("__ansible_module__", None) == "raw": |
| 124 | if isinstance(action_value, strscip-python python python-stdlib 3.11 builtins/str#): |
| 125 | if "executable=" in action_value: |
| 126 | results.appendscip-python python python-stdlib 3.11 builtins/list#append().( |
| 127 | selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(self).create_matcherrorscip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().( |
| 128 | messagescip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(message)="Avoid embedding `executable=` inside raw calls, use explicit args dictionary instead.", |
| 129 | linenoscip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(lineno)=taskscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(task).linescip-python python ansible-lint 26 `src.ansiblelint.utils`/Task#line()., |
| 130 | filenamescip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(filename)=filescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(file), |
| 131 | tagscip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(tag)=f"{selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(self).idscip-python python ansible-lint 26 `src.ansiblelint._internal.rules`/BaseRule#id.}[raw]", |
| 132 | ), |
| 133 | ) |
| 134 | else: |
| 135 | results.appendscip-python python python-stdlib 3.11 builtins/list#append().( |
| 136 | selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(self).create_matcherrorscip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().( |
| 137 | messagescip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(message)="Passing a non string value to `raw` module is neither documented or supported.", |
| 138 | linenoscip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(lineno)=taskscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(task).linescip-python python ansible-lint 26 `src.ansiblelint.utils`/Task#line()., |
| 139 | filenamescip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(filename)=filescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(file), |
| 140 | tagscip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(tag)=f"{selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(self).idscip-python python ansible-lint 26 `src.ansiblelint._internal.rules`/BaseRule#id.}[raw-non-string]", |
| 141 | ), |
| 142 | ) |
| 143 | elif isinstance(action_value, strscip-python python python-stdlib 3.11 builtins/str#) and "=" in action_value: |
| 144 | fail = False |
| 145 | if taskscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(task)["action"].get("__ansible_module__") in ( |
| 146 | "ansible.builtin.command", |
| 147 | "ansible.builtin.shell", |
| 148 | "ansible.windows.win_command", |
| 149 | "ansible.windows.win_shell", |
| 150 | "command", |
| 151 | "shell", |
| 152 | "win_command", |
| 153 | "win_shell", |
| 154 | ): |
| 155 | if selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(self).cmd_shell_rescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#cmd_shell_re..searchscip-python python python-stdlib 3.11 re/Pattern#search().(action_value): |
| 156 | fail = True |
| 157 | else: |
| 158 | fail = True |
| 159 | if fail: |
| 160 | results.appendscip-python python python-stdlib 3.11 builtins/list#append().( |
| 161 | selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(self).create_matcherrorscip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().( |
| 162 | messagescip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(message)=f"Avoid using free-form when calling module actions. ({action})", |
| 163 | linenoscip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(lineno)=taskscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(task).linescip-python python ansible-lint 26 `src.ansiblelint.utils`/Task#line()., |
| 164 | filenamescip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(filename)=filescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#matchtask().(file), |
| 165 | detailsscip-python python ansible-lint 26 `src.ansiblelint.rules`/AnsibleLintRule#create_matcherror().(details)=action, |
| 166 | ), |
| 167 | ) |
| 168 | return results |
| 169 | |
| 170 | def transformscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().( |
| 171 | selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(self), |
| 172 | matchscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match): MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#, |
| 173 | lintablescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(lintable): Lintablescip-python python ansible-lint 26 `src.ansiblelint.file_utils`/Lintable#, |
| 174 | datascip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(data): CommentedMap | CommentedSeq | strscip-python python python-stdlib 3.11 builtins/str#, |
| 175 | ) -> None: |
| 176 | if "no-free-form" in matchscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match).tagscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#tag.: |
| 177 | task = selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(self).seekscip-python python ansible-lint 26 `src.ansiblelint.rules`/TransformMixin#seek().(matchscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match).yaml_pathscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#yaml_path., datascip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(data)) |
| 178 | |
| 179 | if matchscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match).tagscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#tag. == "no-free-form": |
| 180 | target_module = matchscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match).detailsscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#details. |
| 181 | |
| 182 | for _ in rangescip-python python python-stdlib 3.11 builtins/range#(len(task)): |
| 183 | k, v = task.popitem(False) |
| 184 | # identify module as key and process its value |
| 185 | if k == target_module and isinstance(v, strscip-python python python-stdlib 3.11 builtins/str#): |
| 186 | module_opts = selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(self)._parse_module_optsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_module_opts().(v) |
| 187 | task[k] = selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(self)._sorted_module_optsscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_sorted_module_opts().(module_opts) |
| 188 | else: |
| 189 | task[k] = v |
| 190 | |
| 191 | matchscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match).fixedscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#fixed. = True |
| 192 | elif matchscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match).tagscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#tag. == "no-free-form[raw]": |
| 193 | for _ in rangescip-python python python-stdlib 3.11 builtins/range#(len(task)): |
| 194 | k, v = task.popitem(False) |
| 195 | if isinstance(v, strscip-python python python-stdlib 3.11 builtins/str#) and "executable" in v: |
| 196 | raw_value, exec_key_val = selfscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(self)._parse_raw_valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#_parse_raw_value().(v) |
| 197 | task[k] = raw_value |
| 198 | if exec_key_val: |
| 199 | task["args"] = exec_key_val |
| 200 | else: |
| 201 | task[k] = v |
| 202 | matchscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match).fixedscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#fixed. = True |
| 203 | |
| 204 | |
| 205 | if "pytest" in sysscip-python python python-stdlib 3.11 sys/__init__:.modulesscip-python python python-stdlib 3.11 sys/modules.modules.: |
| 206 | import pytestscip-python python pytest 9.0.3 pytest/__init__: |
| 207 | |
| 208 | # pylint: disable=ungrouped-imports |
| 209 | from ansiblelint.rulesscip-python python ansible-lint 26 `ansiblelint.rules`/__init__: import RulesCollectionscip-python python ansible-lint 26 `src.ansiblelint.rules`/RulesCollection# |
| 210 | from ansiblelint.runnerscip-python python ansible-lint 26 `ansiblelint.runner`/__init__: import Runnerscip-python python ansible-lint 26 `src.ansiblelint.runner`/Runner# |
| 211 | |
| 212 | @pytestscip-python python pytest 9.0.3 pytest/__init__:.mark.parametrize( |
| 213 | ("file", "expected"), |
| 214 | ( |
| 215 | pytestscip-python python pytest 9.0.3 pytest/__init__:.param("examples/playbooks/rule-no-free-form-pass.yml", 0, id="pass"), |
| 216 | pytestscip-python python pytest 9.0.3 pytest/__init__:.param("examples/playbooks/rule-no-free-form-fail.yml", 6, id="fail"), |
| 217 | ), |
| 218 | ) |
| 219 | def test_rule_no_free_formscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/test_rule_no_free_form().( |
| 220 | default_rules_collectionscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/test_rule_no_free_form().(default_rules_collection): RulesCollectionscip-python python ansible-lint 26 `src.ansiblelint.rules`/RulesCollection#, |
| 221 | filescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/test_rule_no_free_form().(file): strscip-python python python-stdlib 3.11 builtins/str#, |
| 222 | expectedscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/test_rule_no_free_form().(expected): intscip-python python python-stdlib 3.11 builtins/int#, |
| 223 | ) -> None: |
| 224 | """Validate that rule works as intended.""" |
| 225 | results = Runnerscip-python python ansible-lint 26 `src.ansiblelint.runner`/Runner#(filescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/test_rule_no_free_form().(file), rulesscip-python python ansible-lint 26 `src.ansiblelint.runner`/Runner#__init__().(rules)=default_rules_collectionscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/test_rule_no_free_form().(default_rules_collection)).runscip-python python ansible-lint 26 `src.ansiblelint.runner`/Runner#run().() |
| 226 | |
| 227 | rule_results = [r for r in results if r.rulescip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#rule..idscip-python python ansible-lint 26 `src.ansiblelint._internal.rules`/BaseRule#id. == NoFreeFormRulescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#.idscip-python python ansible-lint 26 `src.ansiblelint._internal.rules`/BaseRule#id.] |
| 228 | |
| 229 | for result in rule_results: |
| 230 | assert result.rulescip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#rule..idscip-python python ansible-lint 26 `src.ansiblelint._internal.rules`/BaseRule#id. == NoFreeFormRulescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#.idscip-python python ansible-lint 26 `src.ansiblelint._internal.rules`/BaseRule#id., result |
| 231 | assert len(rule_results) == expectedscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/test_rule_no_free_form().(expected) |
| 232 | |
| 233 | def test_no_free_form_transform_error_handlingscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/test_no_free_form_transform_error_handling().() -> None: |
| 234 | """Test that transform handles malformed quoted strings.""" |
| 235 | from ruamel.yaml.commentsscip-python python ansible-lint 26 `ruamel.yaml.comments`/__init__: import CommentedMap |
| 236 | |
| 237 | from ansiblelint.errorsscip-python python ansible-lint 26 `ansiblelint.errors`/__init__: import MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError# |
| 238 | |
| 239 | rule = NoFreeFormRulescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#() |
| 240 | task = CommentedMap({"ansible.builtin.shell": 'chdir=" /tmp echo foo'}) |
| 241 | match = MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#( |
| 242 | messagescip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#message.="test", |
| 243 | rulescip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#rule.=rule, |
| 244 | detailsscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#details.="ansible.builtin.shell", |
| 245 | tagscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#tag.="no-free-form", |
| 246 | ) |
| 247 | |
| 248 | rule.transformscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match, None, task) # type: ignore[arg-type] |
| 249 | assert task["ansible.builtin.shell"] == {"cmd": 'chdir=" /tmp echo foo'} |
| 250 | |
| 251 | def test_no_free_form_transform_jinja_with_spacesscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/test_no_free_form_transform_jinja_with_spaces().() -> None: |
| 252 | """Test that Jinja expressions with spaces are preserved.""" |
| 253 | from ruamel.yaml.commentsscip-python python ansible-lint 26 `ruamel.yaml.comments`/__init__: import CommentedMap |
| 254 | |
| 255 | from ansiblelint.errorsscip-python python ansible-lint 26 `ansiblelint.errors`/__init__: import MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError# |
| 256 | |
| 257 | rule = NoFreeFormRulescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#() |
| 258 | task = CommentedMap( |
| 259 | {"ansible.builtin.dnf": "name={{ item }} state=latest"}, |
| 260 | ) |
| 261 | match = MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#( |
| 262 | messagescip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#message.="test", |
| 263 | rulescip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#rule.=rule, |
| 264 | detailsscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#details.="ansible.builtin.dnf", |
| 265 | tagscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#tag.="no-free-form", |
| 266 | ) |
| 267 | |
| 268 | rule.transformscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match, None, task) # type: ignore[arg-type] |
| 269 | assert task["ansible.builtin.dnf"]["name"] == "{{ item }}" |
| 270 | assert task["ansible.builtin.dnf"]["state"] == "latest" |
| 271 | assert "cmd" not in task["ansible.builtin.dnf"] |
| 272 | |
| 273 | def test_no_free_form_transform_unmatched_quote_valuescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/test_no_free_form_transform_unmatched_quote_value().() -> None: |
| 274 | """Test that malformed quoted values fall back to cmd.""" |
| 275 | from ruamel.yaml.commentsscip-python python ansible-lint 26 `ruamel.yaml.comments`/__init__: import CommentedMap |
| 276 | |
| 277 | from ansiblelint.errorsscip-python python ansible-lint 26 `ansiblelint.errors`/__init__: import MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError# |
| 278 | |
| 279 | rule = NoFreeFormRulescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#() |
| 280 | task = CommentedMap( |
| 281 | {"ansible.builtin.command": 'name="foo"bar chdir=/tmp'}, |
| 282 | ) |
| 283 | match = MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#( |
| 284 | messagescip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#message.="test", |
| 285 | rulescip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#rule.=rule, |
| 286 | detailsscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#details.="ansible.builtin.command", |
| 287 | tagscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#tag.="no-free-form", |
| 288 | ) |
| 289 | |
| 290 | rule.transformscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match, None, task) # type: ignore[arg-type] |
| 291 | assert task["ansible.builtin.command"] == { |
| 292 | "cmd": 'name="foo"bar chdir=/tmp', |
| 293 | } |
| 294 | |
| 295 | def test_no_free_form_transform_raw_unbalanced_executablescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/test_no_free_form_transform_raw_unbalanced_executable().() -> None: |
| 296 | """Test raw transform fallback when executable value is unbalanced.""" |
| 297 | from ruamel.yaml.commentsscip-python python ansible-lint 26 `ruamel.yaml.comments`/__init__: import CommentedMap |
| 298 | |
| 299 | from ansiblelint.errorsscip-python python ansible-lint 26 `ansiblelint.errors`/__init__: import MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError# |
| 300 | |
| 301 | rule = NoFreeFormRulescip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#() |
| 302 | task = CommentedMap( |
| 303 | {"ansible.builtin.raw": 'executable="/bin/bash echo foo'}, |
| 304 | ) |
| 305 | match = MatchErrorscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#( |
| 306 | messagescip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#message.="test", |
| 307 | rulescip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#rule.=rule, |
| 308 | tagscip-python python ansible-lint 26 `src.ansiblelint.errors`/MatchError#tag.="no-free-form[raw]", |
| 309 | ) |
| 310 | |
| 311 | rule.transformscip-python python ansible-lint 26 `src.ansiblelint.rules.no_free_form`/NoFreeFormRule#transform().(match, None, task) # type: ignore[arg-type] |
| 312 | assert task["ansible.builtin.raw"] == 'executable="/bin/bash echo foo' |
| 313 | assert "args" not in task |