From a233236c7c0ab890c8174fb8bd14ecbe6bc8236c Mon Sep 17 00:00:00 2001 From: Roald Nefs Date: Mon, 8 Nov 2021 20:22:17 +0100 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + saltlint/rules/FileModeQuotationRule.py | 20 +++++++++++++++++++- tests/unit/TestFileModeQuotationRule.py | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6630714..3ea4106 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [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)). +- 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)). ## [0.7.0] (2021-11-01) ### Added diff --git a/saltlint/rules/FileModeQuotationRule.py b/saltlint/rules/FileModeQuotationRule.py index 07a21eb..38bbae7 100644 --- a/saltlint/rules/FileModeQuotationRule.py +++ b/saltlint/rules/FileModeQuotationRule.py @@ -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) diff --git a/tests/unit/TestFileModeQuotationRule.py b/tests/unit/TestFileModeQuotationRule.py index b2d2088..698fa5e 100644 --- a/tests/unit/TestFileModeQuotationRule.py +++ b/tests/unit/TestFileModeQuotationRule.py @@ -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() @@ -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))