Skip to content

Commit

Permalink
Warn on using quiet with cmd.run (#207)
Browse files Browse the repository at this point in the history
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
jbouter authored Nov 28, 2020
1 parent 56d5a88 commit 70f34af
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
39 changes: 39 additions & 0 deletions saltlint/rules/CmdRunQuietRule.py
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
9 changes: 9 additions & 0 deletions saltlint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
61 changes: 61 additions & 0 deletions tests/unit/TestCmdRunQuietRule.py
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)

0 comments on commit 70f34af

Please sign in to comment.