Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

var-spacing: fix multiline nested JSON false positive #1672

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion examples/playbooks/var-spacing.yml
@@ -1,4 +1,4 @@
# Should raise var-spacing at lines 25, 28, 31
# Should raise var-spacing at tasks line 23, 26, 29, 54, 65
- hosts: all
tasks:
- name: good variable format
Expand Down Expand Up @@ -46,3 +46,25 @@
case2 }}
debug:
var: cases

- name: Valid single line nested JSON false positive
debug:
msg: "{{ {'dummy_2': {'nested_dummy_1': 'value_1', 'nested_dummy_2': value_2}} | combine(dummy_1) }}"

- name: Invalid single line nested JSON
debug:
msg: "{{ {'dummy_2': {'nested_dummy_1': 'value_1', 'nested_dummy_2': value_2}} | combine(dummy_1)}}"

- name: Valid multiline nested JSON false positive
debug:
msg: >-
{{ {'dummy_2': {'nested_dummy_1': value_1,
'nested_dummy_2': value_2}} |
combine(dummy_1) }}

- name: Invalid multiline nested JSON
debug:
msg: >-
{{ {'dummy_2': {'nested_dummy_1': value_1,
'nested_dummy_2': value_2}} |
combine(dummy_1)}}
39 changes: 30 additions & 9 deletions src/ansiblelint/rules/VariableHasSpacesRule.py
Expand Up @@ -3,7 +3,7 @@

import re
import sys
from typing import Any, Dict, Optional, Union
from typing import Any, Dict, List, Optional, Union

from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule
Expand All @@ -20,7 +20,7 @@ class VariableHasSpacesRule(AnsibleLintRule):
version_added = 'v4.0.0'

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

def matchtask(
self, task: Dict[str, Any], file: Optional[Lintable] = None
Expand All @@ -35,15 +35,36 @@ def matchtask(

if 'pytest' in sys.modules:

from ansiblelint.rules import RulesCollection
from ansiblelint.runner import Runner
import pytest

def test_var_spacing() -> None:
"""Verify rule."""
from ansiblelint.rules import RulesCollection # pylint: disable=ungrouped-imports
from ansiblelint.runner import Runner # pylint: disable=ungrouped-imports

@pytest.fixture
def error_expected_lines() -> List[int]:
"""Return list of expected error lines."""
return [23, 26, 29, 54, 65]

@pytest.fixture
def test_playbook() -> str:
"""Return test cases playbook path."""
return "examples/playbooks/var-spacing.yml"

@pytest.fixture
def lint_error_lines(test_playbook: str) -> List[int]:
"""Get VarHasSpacesRules linting results on test_playbook."""
collection = RulesCollection()
collection.register(VariableHasSpacesRule())

lintable = Lintable("examples/playbooks/var-spacing.yml")
lintable = Lintable(test_playbook)
results = Runner(lintable, rules=collection).run()
return list(map(lambda item: item.linenumber, results))

assert len(results) == 3
def test_var_spacing(
error_expected_lines: List[int], lint_error_lines: List[int]
) -> None:
"""Ensure that expected error lines are matching found linting error lines."""
# list unexpected error lines or non-matching error lines
error_lines_difference = list(
set(error_expected_lines).symmetric_difference(set(lint_error_lines))
)
assert len(error_lines_difference) == 0