From 95f1977a3662bb7ad7d75150577c6a2da36ed42c Mon Sep 17 00:00:00 2001 From: Jeffrey Bouter Date: Sat, 28 Nov 2020 23:18:28 +0100 Subject: [PATCH 1/3] Add cmd.run quiet deprecation check - Warn on using the quiet argument with cmd.run Signed-off-by: Jeffrey Bouter --- README.md | 23 +++++++++++++ saltlint/rules/CmdRunQuietRule.py | 32 ++++++++++++++++++ tests/unit/TestCmdRunQuietRule.py | 56 +++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 saltlint/rules/CmdRunQuietRule.py create mode 100644 tests/unit/TestCmdRunQuietRule.py diff --git a/README.md b/README.md index 5a02f7e..fbc3364 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,10 @@ Optionally override the default file selection as follows: ## List of rules +### Formatting + +Disable formatting checks using `-x formatting` + Rule | Description :-:|:-- [201](https://github.com/warpnet/salt-lint/wiki/201) | Trailing whitespace @@ -180,6 +184,25 @@ Rule | Description [212](https://github.com/warpnet/salt-lint/wiki/212) | Most files should not contain irregular spaces [213](https://github.com/warpnet/salt-lint/wiki/213) | SaltStack recommends using `cmd.run` together with `onchanges`, rather than `cmd.wait` +### Jinja + +Disable jinja checks using `-x jinja` + +Rule | Description +:-:|:-- +[202](https://github.com/warpnet/salt-lint/wiki/202) | Jinja statement should have spaces before and after: `{% statement %}` +[206](https://github.com/warpnet/salt-lint/wiki/206) | Jinja variables should have spaces before and after `{{ var_name }}` +[209](https://github.com/warpnet/salt-lint/wiki/209) | Jinja comment should have spaces before and after: `{# comment #}` +[211](https://github.com/warpnet/salt-lint/wiki/211) | `pillar.get` or `grains.get` should be formatted differently + +### Deprecations + +Disable deprecation checks using `-x deprecation` + +Rule | Description +:-:|:-- +[901](https://github.com/warpnet/salt-lint/wiki/901) | Using the `quiet` argument with `cmd.run` is deprecated. Use `output_loglevel: quiet` + ## False Positives: Skipping Rules Some rules are bit of a rule of thumb. To skip a specific rule for a specific task, inside your state add `# noqa [rule_id]` at the end of the line. You can skip multiple rules via a space-separated list. Example: diff --git a/saltlint/rules/CmdRunQuietRule.py b/saltlint/rules/CmdRunQuietRule.py new file mode 100644 index 0000000..0060e80 --- /dev/null +++ b/saltlint/rules/CmdRunQuietRule.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2020 Warpnet B.V. + +import re +from saltlint.linter.rule import Rule + + +class CmdRunQuietRule(Rule): + id = '901' + shortdesc = 'Using the quiet argument with cmd.run is deprecated. Use output_loglevel: quiet' + description = 'Using the quiet argument with cmd.run is deprecated. Use output_loglevel: quiet' + + severity = 'HIGH' + tags = ['deprecation'] + version_added = 'develop' + + regex = re.compile(r"^\s{2}cmd\.run:(?:\n.+)+\n^\s{4}- quiet\s?.*", re.MULTILINE) + + def matchtext(self, file, text): + results = [] + + for match in re.finditer(self.regex, text): + # Get the location of the last character in the regex match + end = match.end() + # Get the line number of the last character + lines = text[:end].splitlines() + line_no = len(lines) + # Append the match to the results + results.append((line_no, lines[-1], self.shortdesc)) + + return results + diff --git a/tests/unit/TestCmdRunQuietRule.py b/tests/unit/TestCmdRunQuietRule.py new file mode 100644 index 0000000..03dc3fd --- /dev/null +++ b/tests/unit/TestCmdRunQuietRule.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2020 Warpnet B.V. + +import unittest + +from saltlint.linter.collection import RulesCollection +from saltlint.rules.CmdRunQuietRule import CmdRunQuietRule +from tests import RunFromText + + +GOOD_QUIET_STATE = ''' +getpip: + cmd.run: + - name: /usr/bin/python /usr/local/sbin/get-pip.py + - unless: which pip + - require: + - pkg: python + - file: /usr/local/sbin/get-pip.py + - output_loglevel: quiet +''' + +BAD_QUIET_STATE = ''' +getpip: + cmd.run: + - name: /usr/bin/python /usr/local/sbin/get-pip.py + - unless: which pip + - require: + - pkg: python + - file: /usr/local/sbin/get-pip.py + - quiet # This is the ninth line + +getpip2: + cmd.run: + - name: /usr/bin/python /usr/local/sbin/get-pip.py + - quiet +''' + +class TestCmdRunQuietRule(unittest.TestCase): + collection = RulesCollection() + + def setUp(self): + self.collection.register(CmdRunQuietRule()) + + def test_statement_positive(self): + runner = RunFromText(self.collection) + results = runner.run_state(GOOD_QUIET_STATE) + self.assertEqual(0, len(results)) + + def test_statement_negative(self): + runner = RunFromText(self.collection) + results = runner.run_state(BAD_QUIET_STATE) + self.assertEqual(2, len(results)) + + # Check line numbers of the results + self.assertEqual(9, results[0].linenumber) + self.assertEqual(14, results[1].linenumber) From 4ccd821f75f754fabf8025a0a83d7d906199a595 Mon Sep 17 00:00:00 2001 From: Jeffrey Bouter Date: Sat, 28 Nov 2020 23:21:54 +0100 Subject: [PATCH 2/3] Fix trailing newline Signed-off-by: Jeffrey Bouter --- saltlint/rules/CmdRunQuietRule.py | 1 - 1 file changed, 1 deletion(-) diff --git a/saltlint/rules/CmdRunQuietRule.py b/saltlint/rules/CmdRunQuietRule.py index 0060e80..f3a746b 100644 --- a/saltlint/rules/CmdRunQuietRule.py +++ b/saltlint/rules/CmdRunQuietRule.py @@ -29,4 +29,3 @@ def matchtext(self, file, text): results.append((line_no, lines[-1], self.shortdesc)) return results - From bd85210dce0d7d327fc4c269e197fe92beb43bd3 Mon Sep 17 00:00:00 2001 From: Jeffrey Bouter Date: Sat, 28 Nov 2020 23:45:04 +0100 Subject: [PATCH 3/3] Add option for rule skipping - Add support for multiline regex rule skipping in utils.py - Add skipping example to unit test - Add skipping functionality to check TODO: Move skipping functionality to global function Signed-off-by: Jeffrey Bouter --- saltlint/rules/CmdRunQuietRule.py | 14 +++++++++++--- saltlint/utils.py | 9 +++++++++ tests/unit/TestCmdRunQuietRule.py | 5 +++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/saltlint/rules/CmdRunQuietRule.py b/saltlint/rules/CmdRunQuietRule.py index f3a746b..5dd68ce 100644 --- a/saltlint/rules/CmdRunQuietRule.py +++ b/saltlint/rules/CmdRunQuietRule.py @@ -3,7 +3,7 @@ import re from saltlint.linter.rule import Rule - +from saltlint.utils import get_rule_skips_from_text class CmdRunQuietRule(Rule): id = '901' @@ -14,17 +14,25 @@ class CmdRunQuietRule(Rule): tags = ['deprecation'] version_added = 'develop' - regex = re.compile(r"^\s{2}cmd\.run:(?:\n.+)+\n^\s{4}- quiet\s?.*", re.MULTILINE) + regex = re.compile(r"^.+\n^\s{2}cmd\.run:(?:\n.+)+\n^\s{4}- quiet\s?.*", re.MULTILINE) def matchtext(self, file, text): results = [] for match in re.finditer(self.regex, text): - # Get the location of the last character in the regex match + # Get the location of the regex match + start = match.start() end = match.end() + # Get the line number of the last character lines = text[:end].splitlines() line_no = len(lines) + + # Skip result if noqa for this rule ID is found in section + section = text[start:end] + if self.id in get_rule_skips_from_text(section): + continue + # Append the match to the results results.append((line_no, lines[-1], self.shortdesc)) diff --git a/saltlint/utils.py b/saltlint/utils.py index a0334b2..dbcdf23 100644 --- a/saltlint/utils.py +++ b/saltlint/utils.py @@ -31,3 +31,12 @@ def get_rule_skips_from_line(line): noqa_text = line.split('# noqa')[1] rule_id_list = noqa_text.split() return rule_id_list + + +def get_rule_skips_from_text(text): + rule_id_list = [] + for line in text.splitlines(): + rule_id_list.extend(get_rule_skips_from_line(line)) + + # Return a list of unique ids + return list(set(rule_id_list)) diff --git a/tests/unit/TestCmdRunQuietRule.py b/tests/unit/TestCmdRunQuietRule.py index 03dc3fd..ffb4c55 100644 --- a/tests/unit/TestCmdRunQuietRule.py +++ b/tests/unit/TestCmdRunQuietRule.py @@ -33,6 +33,11 @@ cmd.run: - name: /usr/bin/python /usr/local/sbin/get-pip.py - quiet + +getpip3: + cmd.run: + - name: /usr/bin/python /usr/local/sbin/get-pip.py + - quiet # noqa: 901 ''' class TestCmdRunQuietRule(unittest.TestCase):