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

feat(rules): spaces are now needed around parenthesis #326

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions saltlint/rules/CompoundMatchSpacesRule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from saltlint.linter.rule import Rule
import re

class CompoundMatchSpacesRule(Rule):
id = '220'
shortdesc = 'Ensure spaces around parentheses in compound matches'
description = 'Compound matches should have spaces around parentheses to ensure proper parsing by Salt.'
severity = 'HIGH'
tags = ['formatting', 'compound-match']
version_added = 'v0.9.2'

regex = re.compile(r'\([^ ]|[^ ]\)')

def match(self, file, line):
if not file["path"].endswith("top.sls"):
return self.regex.search(line)
56 changes: 56 additions & 0 deletions tests/unit/TestCompoundMatchSpacesRule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import unittest
from saltlint.linter.rule import Rule
import re
from saltlint.rules import CompoundMatchSpacesRule
class CompoundMatchSpacesRule(Rule):
id = '220'
shortdesc = 'Ensure spaces around parentheses in compound matches'
description = 'Compound matches should have spaces around parentheses to ensure proper parsing by Salt.'
severity = 'HIGH'
tags = ['formatting', 'compound-match']
version_added = 'v0.9.2'

regex = re.compile(r'\([^ ]|[^ ]\)')

def match(self, file, line):
if not file["path"].endswith("top.sls"):
return self.regex.search(line)
return None


class TestCompoundMatchSpacesRule(unittest.TestCase):
def setUp(self):
self.rule = CompoundMatchSpacesRule()

def test_no_match_non_top_sls(self):
file = {"path": "init.sls"}
line = "some line without parentheses"
self.assertIsNone(self.rule.match(file, line))

def test_no_match_top_sls(self):
file = {"path": "top.sls"}
line = "No match (line)"
self.assertIsNone(self.rule.match(file, line))

def test_match_missing_space_after_open_paren(self):
file = {"path": "some_state.sls"}
line = "compound_match: G@os:(Ubuntu or CentOS)"
self.assertIsNotNone(self.rule.match(file, line))

def test_match_missing_space_before_close_paren(self):
file = {"path": "some_state.sls"}
line = "compound_match: G@os:( Ubuntu or CentOS)"
self.assertIsNotNone(self.rule.match(file, line))

def test_match_missing_spaces_both_sides(self):
file = {"path": "some_state.sls"}
line = "compound_match: G@os:(Ubuntu or CentOS )"
self.assertIsNotNone(self.rule.match(file, line))

def test_no_match_correct_spacing(self):
file = {"path": "some_state.sls"}
line = "compound_match: G@os: ( Ubuntu or CentOS )"
self.assertIsNone(self.rule.match(file, line))

if __name__ == '__main__':
unittest.main()