Skip to content

Commit

Permalink
Modify VariableHasSpacesRule to check for spaces around |
Browse files Browse the repository at this point in the history
  • Loading branch information
nirmal-j-patel committed Jun 7, 2022
1 parent 7c5c220 commit 0c8875f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
15 changes: 15 additions & 0 deletions examples/playbooks/var-spacing.yml
Expand Up @@ -8,6 +8,12 @@
- name: good variable format
ansible.builtin.debug:
msg: "Value: {{ good_format }}"
- name: good variable filter format
ansible.builtin.debug:
msg: "{{ good_format | filter }}"
- name: good variable filter format
ansible.builtin.debug:
msg: "Value: {{ good_format | filter }}"
- name: jinja escaping allowed
ansible.builtin.debug:
msg: "{{ '{{' }}"
Expand All @@ -30,6 +36,15 @@
- name: bad variable format
ansible.builtin.debug:
msg: "{{bad_format }}"
- name: bad variable filter format
ansible.builtin.debug:
msg: "{{ bad_format|filter }}"
- name: bad variable filter format
ansible.builtin.debug:
msg: "Value: {{ bad_format |filter }}"
- name: bad variable filter format
ansible.builtin.debug:
msg: "{{ bad_format| filter }}"
- name: not a jinja variable # noqa var-spacing
ansible.builtin.debug:
# spell-checker: disable-next-line
Expand Down
5 changes: 5 additions & 0 deletions examples/playbooks/vars/var-spacing.yml
Expand Up @@ -3,6 +3,8 @@
# ".bad_var_1", ".bad_var_2", ".bad_var_3", ".invalid_multiline_nested_json", ".invalid_nested_json"
good_var_1: "{{ good_format }}"
good_var_2: "Value: {{ good_format }}"
good_var_3: "{{ good_format | filter }}"
good_var_4: "Value: {{ good_format | filter }}"
jinja_escape_1: "{{ '{{' }}"
jinja_escape_2: docker info --format '{{ '{{' }}json .Swarm.LocalNodeState{{ '}}' }}' | tr -d '"'
jinja_whitespace_control: |
Expand All @@ -12,6 +14,9 @@ jinja_whitespace_control: |
bad_var_1: "{{bad_format}}"
bad_var_2: "Value: {{ bad_format}}"
bad_var_3: "{{bad_format }}"
bad_var_4: "{{ bad_format|filter }}"
bad_var_5: "Value: {{ bad_format |filter }}"
bad_var_6: "{{ bad_format| filter }}"
# spell-checker: disable-next-line
non_jinja_var: "data = ${lookup{$local_part}lsearch{/etc/aliases}}" # noqa: var-spacing
json_inside_jinja: "{{ {'test': {'subtest': variable}} }}"
Expand Down
22 changes: 16 additions & 6 deletions src/ansiblelint/rules/var_spacing.py
Expand Up @@ -19,24 +19,29 @@


class VariableHasSpacesRule(AnsibleLintRule):
"""Variables should have spaces before and after: {{ var_name }}."""
"""Variables should have spaces before and after: {{ var_name }}. Filters should have spaces before and after: {{ var_name | filter }}."""

id = "var-spacing"
description = "Variables should have spaces before and after: ``{{ var_name }}``"
description = "Variables should have spaces before and after: ``{{ var_name }}``. Filters should have spaces before and after: ``{{ var_name | filter }}.``"
severity = "LOW"
tags = ["formatting"]
version_added = "v4.0.0"

bracket_regex = re.compile(r"{{[^{\n' -]|[^ '\n}-]}}", re.MULTILINE | re.DOTALL)
exclude_json_re = re.compile(r"[^{]{'\w+': ?[^{]{.*?}}", re.MULTILINE | re.DOTALL)
pipe_spacing_regex = re.compile(
r"{{.*(?<=[^ \n\t])[|].*|.*[|](?=[^ \n\t]).*}}", re.MULTILINE | re.DOTALL
)

def matchtask(
self, task: Dict[str, Any], file: Optional[Lintable] = None
) -> Union[bool, str]:
for _, v, _ in nested_items_path(task):
if isinstance(v, str):
cleaned = self.exclude_json_re.sub("", v)
if bool(self.bracket_regex.search(cleaned)):
if bool(self.bracket_regex.search(cleaned)) or bool(
self.pipe_spacing_regex.search(cleaned)
):
return self.shortdesc.format(var_name=v)
return False

Expand All @@ -51,7 +56,9 @@ def matchyaml(self, file: Lintable) -> List["MatchError"]:
for k, v, path in nested_items_path(data):
if isinstance(v, AnsibleUnicode):
cleaned = self.exclude_json_re.sub("", v)
if bool(self.bracket_regex.search(cleaned)):
if bool(self.bracket_regex.search(cleaned)) or bool(
self.pipe_spacing_regex.search(cleaned)
):
path_elem = [
f"[{i}]" if isinstance(i, int) else i for i in path + [k]
]
Expand Down Expand Up @@ -85,7 +92,7 @@ def matchyaml(self, file: Lintable) -> List["MatchError"]:
@pytest.fixture(name="error_expected_lines")
def fixture_error_expected_lines() -> List[int]:
"""Return list of expected error lines."""
return [24, 27, 30, 56, 67]
return [30, 33, 36, 39, 42, 45, 71, 82]

@pytest.fixture(name="test_playbook")
def fixture_test_playbook() -> str:
Expand Down Expand Up @@ -119,14 +126,17 @@ def fixture_error_expected_details_varsfile() -> List[str]:
".bad_var_1",
".bad_var_2",
".bad_var_3",
".bad_var_4",
".bad_var_5",
".bad_var_6",
".invalid_multiline_nested_json",
".invalid_nested_json",
]

@pytest.fixture(name="error_expected_lines_varsfile")
def fixture_error_expected_lines_varsfile() -> List[int]:
"""Return list of expected error lines."""
return [12, 13, 14, 27, 33]
return [14, 15, 16, 17, 18, 19, 32, 38]

@pytest.fixture(name="test_varsfile_path")
def fixture_test_varsfile_path() -> str:
Expand Down

0 comments on commit 0c8875f

Please sign in to comment.