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

Warn on using quiet with cmd.run #207

Merged
merged 3 commits into from
Nov 28, 2020
Merged
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
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)