Skip to content

Commit

Permalink
fix: false positive for spaces in Jinja variable (#272)
Browse files Browse the repository at this point in the history
Fix false positive when detecting missing spaces in Jinja variables
when the Jinja statement is nested in literal braces.

Closes #257

Signed-off-by: Roald Nefs <info@roaldnefs.com>
  • Loading branch information
roaldnefs authored Nov 8, 2021
1 parent 54fead1 commit 0f1b284
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes in **salt-lint** are documented below.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- False positive when detecting missing spaces in Jinja variables when the Jinja statement is nested in literal braces ([#272](https://github.com/warpnet/salt-lint/pull/272)).

## [0.7.0] (2021-11-01)
### Added
Expand Down
4 changes: 3 additions & 1 deletion saltlint/rules/JinjaVariableHasSpacesRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class JinjaVariableHasSpacesRule(JinjaRule):
severity = 'LOW'
version_added = 'v0.0.1'

bracket_regex = re.compile(r"{{[^ \-\+\d]|{{[-\+][^ ]|[^ \-\+\d]}}|[^ {][-\+\d]}}")
bracket_regex = re.compile(
r"{{[^ {}\-\+\d]|{{[-\+][^ {}]|[^ {}\-\+\d]}}|[^ {}][-\+\d]}}"
)

def match(self, file, line):
return self.bracket_regex.search(line)
13 changes: 13 additions & 0 deletions tests/unit/TestJinjaVariableHasSpaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
{{"{{0}}" }}
'''

NESTED_LITERAL_BRACES = '''
- name: '{${{ key }}}'
'''

class TestJinjaVariableHasSpaces(unittest.TestCase):
collection = RulesCollection()

Expand Down Expand Up @@ -98,3 +102,12 @@ def test_variable_bad_ends_with_integer(self):
def test_variable_bad_ends_with_integer_right(self):
results = self.runner.run_state(BAD_VARIABLE_ENDING_IN_INTEGER_RIGHT)
self.assertEqual(1, len(results))

def test_nested_literal_braces(self):
"""
Check if Jinja variables inside nested literal braces are identified
correctly. See related GitHub issue:
https://github.com/warpnet/salt-lint/issues/257
"""
results = self.runner.run_state(NESTED_LITERAL_BRACES)
self.assertEqual(0, len(results))

0 comments on commit 0f1b284

Please sign in to comment.