-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn on using quiet with cmd.run (#207)
Add cmd.run quiet deprecation check - Warn on using the quiet argument with cmd.run - 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 Fixes #40
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2020 Warpnet B.V. | ||
|
||
import re | ||
from saltlint.linter.rule import Rule | ||
from saltlint.utils import get_rule_skips_from_text | ||
|
||
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"^.+\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 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)) | ||
|
||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# -*- 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 | ||
getpip3: | ||
cmd.run: | ||
- name: /usr/bin/python /usr/local/sbin/get-pip.py | ||
- quiet # noqa: 901 | ||
''' | ||
|
||
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) |