From 7b9daba66f09f1a6029bd0315a42fc42f5821cc2 Mon Sep 17 00:00:00 2001 From: tdejong Date: Tue, 20 Aug 2024 13:54:29 +0200 Subject: [PATCH] feat(rules): spaces are now needed around parenthesis --- saltlint/rules/CompoundMatchSpacesRule.py | 16 +++++++ tests/unit/TestCompoundMatchSpacesRule.py | 56 +++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 saltlint/rules/CompoundMatchSpacesRule.py create mode 100644 tests/unit/TestCompoundMatchSpacesRule.py diff --git a/saltlint/rules/CompoundMatchSpacesRule.py b/saltlint/rules/CompoundMatchSpacesRule.py new file mode 100644 index 0000000..852b74f --- /dev/null +++ b/saltlint/rules/CompoundMatchSpacesRule.py @@ -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) diff --git a/tests/unit/TestCompoundMatchSpacesRule.py b/tests/unit/TestCompoundMatchSpacesRule.py new file mode 100644 index 0000000..1ee092e --- /dev/null +++ b/tests/unit/TestCompoundMatchSpacesRule.py @@ -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()