Skip to content

Commit 0f1b284

Browse files
authored
fix: false positive for spaces in Jinja variable (#272)
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>
1 parent 54fead1 commit 0f1b284

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes in **salt-lint** are documented below.
44
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).
55

66
## [Unreleased]
7+
### Fixed
8+
- 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)).
79

810
## [0.7.0] (2021-11-01)
911
### Added

saltlint/rules/JinjaVariableHasSpacesRule.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class JinjaVariableHasSpacesRule(JinjaRule):
1414
severity = 'LOW'
1515
version_added = 'v0.0.1'
1616

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

1921
def match(self, file, line):
2022
return self.bracket_regex.search(line)

tests/unit/TestJinjaVariableHasSpaces.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
{{"{{0}}" }}
5252
'''
5353

54+
NESTED_LITERAL_BRACES = '''
55+
- name: '{${{ key }}}'
56+
'''
57+
5458
class TestJinjaVariableHasSpaces(unittest.TestCase):
5559
collection = RulesCollection()
5660

@@ -98,3 +102,12 @@ def test_variable_bad_ends_with_integer(self):
98102
def test_variable_bad_ends_with_integer_right(self):
99103
results = self.runner.run_state(BAD_VARIABLE_ENDING_IN_INTEGER_RIGHT)
100104
self.assertEqual(1, len(results))
105+
106+
def test_nested_literal_braces(self):
107+
"""
108+
Check if Jinja variables inside nested literal braces are identified
109+
correctly. See related GitHub issue:
110+
https://github.com/warpnet/salt-lint/issues/257
111+
"""
112+
results = self.runner.run_state(NESTED_LITERAL_BRACES)
113+
self.assertEqual(0, len(results))

0 commit comments

Comments
 (0)