Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix single quotation detection in file mode #273

Merged
merged 2 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes in **salt-lint** are documented below.
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).

## [Unreleased]
### Fixed
- 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))