Skip to content

Commit

Permalink
fix: single quotation in file mode
Browse files Browse the repository at this point in the history
Indicate a single quotation in the file mode as a problem.

Closes #248

Signed-off-by: Roald Nefs <info@roaldnefs.com>
  • Loading branch information
roaldnefs committed Nov 7, 2021
1 parent 54fead1 commit 15961dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
20 changes: 19 additions & 1 deletion saltlint/rules/FileModeQuotationRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,25 @@ class FileModeQuotationRule(Rule):
tags = ['formatting']
version_added = 'v0.0.3'

regex = re.compile(r"^\s+- ((dir_)|(file_))?mode: [0-9]{3,4}")
regex = re.compile(
r"""^\s+
-\ # whitespace escaped due to re.VERBOSE
(?:dir_|file_)?mode # file_mode, dir_mode or mode
:\ # whitespace escaped duo to re.VERBOSE
(?:
(\d{3,4}) # mode without quotation
|
(['"]\d{3,4} # mode prefixed with quotation
(?:
$ # end of line
| # or
[^\d'"] # ending quotation missing
)
)
)
""",
re.VERBOSE
)

def match(self, file, line):
return self.regex.search(line)
16 changes: 16 additions & 0 deletions tests/unit/TestFileModeQuotationRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
- dir_mode: 0775
'''

MODE_MISSING_QUOTATION_LINE = '''
testfile:
file.managed:
- name: /tmp/badfile
- user: root
- group: root
- mode: "0700
- file_mode: '0660
- dir_mode: 0775"
'''


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

Expand All @@ -44,3 +56,7 @@ def test_statement_positive(self):
def test_statement_negative(self):
results = self.runner.run_state(BAD_MODE_QUOTATION_LINE)
self.assertEqual(3, len(results))

def test_missing_quotes(self):
results = self.runner.run_state(MODE_MISSING_QUOTATION_LINE)
self.assertEqual(3, len(results))

0 comments on commit 15961dd

Please sign in to comment.