Skip to content

Commit a233236

Browse files
authored
fix: single quotation in file mode (#273)
Indicate a single quotation in the file mode as a problem. Closes #248 Signed-off-by: Roald Nefs <info@roaldnefs.com>
1 parent 0f1b284 commit a233236

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
## [Unreleased]
77
### Fixed
88
- 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)).
9+
- Ensure a single missing quote in the file mode is also detected as incorrect quotation of a file mode ([#273](https://github.com/warpnet/salt-lint/pull/273)).
910

1011
## [0.7.0] (2021-11-01)
1112
### Added

saltlint/rules/FileModeQuotationRule.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,25 @@ class FileModeQuotationRule(Rule):
1717
tags = ['formatting']
1818
version_added = 'v0.0.3'
1919

20-
regex = re.compile(r"^\s+- ((dir_)|(file_))?mode: [0-9]{3,4}")
20+
regex = re.compile(
21+
r"""^\s+
22+
-\ # whitespace escaped due to re.VERBOSE
23+
(?:dir_|file_)?mode # file_mode, dir_mode or mode
24+
:\ # whitespace escaped duo to re.VERBOSE
25+
(?:
26+
(\d{3,4}) # mode without quotation
27+
|
28+
(['"]\d{3,4} # mode prefixed with quotation
29+
(?:
30+
$ # end of line
31+
| # or
32+
[^\d'"] # ending quotation missing
33+
)
34+
)
35+
)
36+
""",
37+
re.VERBOSE
38+
)
2139

2240
def match(self, file, line):
2341
return self.regex.search(line)

tests/unit/TestFileModeQuotationRule.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@
3030
- dir_mode: 0775
3131
'''
3232

33+
MODE_MISSING_QUOTATION_LINE = '''
34+
testfile:
35+
file.managed:
36+
- name: /tmp/badfile
37+
- user: root
38+
- group: root
39+
- mode: "0700
40+
- file_mode: '0660
41+
- dir_mode: 0775"
42+
'''
43+
44+
3345
class TestModeQuotationRule(unittest.TestCase):
3446
collection = RulesCollection()
3547

@@ -44,3 +56,7 @@ def test_statement_positive(self):
4456
def test_statement_negative(self):
4557
results = self.runner.run_state(BAD_MODE_QUOTATION_LINE)
4658
self.assertEqual(3, len(results))
59+
60+
def test_missing_quotes(self):
61+
results = self.runner.run_state(MODE_MISSING_QUOTATION_LINE)
62+
self.assertEqual(3, len(results))

0 commit comments

Comments
 (0)