Skip to content

Commit

Permalink
fix: single quotation in file mode (#273)
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 authored Nov 8, 2021
1 parent 0f1b284 commit a233236
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 a233236

Please sign in to comment.