-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
219 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
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,45 @@ | ||
# -*- 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 | ||
from saltlint.utils import LANGUAGE_SLS | ||
|
||
|
||
class NestedDictRule(Rule): | ||
id = "219" | ||
shortdesc = "Nested dictionaries (in context or defaults) should be over-indented" | ||
description = "Nested dictionaries (in context or defaults) should be over-indented" | ||
|
||
severity = "HIGH" | ||
languages = [LANGUAGE_SLS] | ||
tags = ["formatting"] | ||
version_added = "develop" | ||
|
||
regex = re.compile( | ||
r"^(\s+)-\s+(context|defaults):[^{[\n]*\n^\1\s{0,3}[^-{[\s]*:\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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2020 Warpnet B.V. | ||
|
||
import unittest | ||
|
||
from saltlint.linter.collection import RulesCollection | ||
from saltlint.rules.NestedDictRule import NestedDictRule | ||
from tests import RunFromText | ||
|
||
|
||
GOOD_NESTED_DICT_STATE = """ | ||
/etc/http/conf/http.conf: | ||
file.managed: | ||
- source: salt://apache/http.conf | ||
- template: jinja | ||
- context: | ||
custom_var: "override" | ||
- defaults: | ||
custom_var: "default value" | ||
other_var: 123 | ||
/etc/http/conf/http.conf: | ||
file.managed: | ||
- source: salt://apache/http.conf | ||
- mode: 644 | ||
- template: jinja | ||
- context: { custom_var: "override" } | ||
- defaults: { | ||
custom_var: "default value", | ||
other_var: 123 | ||
} | ||
/etc/http/conf/http.conf: | ||
file.managed: | ||
- source: salt://apache/http.conf | ||
- template: jinja | ||
- context: | ||
custom_var: "override" | ||
- defaults: | ||
custom_var: "default value" | ||
other_var: 123 | ||
/etc/http/conf/http.conf: | ||
file.managed: | ||
- source: salt://apache/http.conf | ||
- template: jinja | ||
- context: | ||
custom_var: "override" # noqa: 219 | ||
- defaults: | ||
custom_var: "default value" | ||
other_var: 123 | ||
/etc/http/conf/http.conf: | ||
file.managed: | ||
- source: salt://apache/http.conf | ||
- template: jinja | ||
- context: # noqa: 219 | ||
custom_var: "override" | ||
- defaults: | ||
custom_var: "default value" | ||
other_var: 123 | ||
""" | ||
|
||
BAD_NESTED_DICT_STATE = """ | ||
/etc/http/conf/http.conf: | ||
file.managed: | ||
- source: salt://apache/http.conf | ||
- template: jinja | ||
- context: | ||
custom_var: "override" | ||
- defaults: | ||
custom_var: "default value" | ||
other_var: 123 | ||
/etc/http/conf/http.conf: | ||
file.managed: | ||
- source: salt://apache/http.conf | ||
- template: jinja | ||
- context: | ||
custom_var: "override" | ||
- defaults: | ||
custom_var: "default value" | ||
other_var: 123 | ||
/etc/http/conf/http.conf: | ||
file.managed: | ||
- source: salt://apache/http.conf | ||
- template: jinja | ||
- context: | ||
custom_var: "override" | ||
- defaults: | ||
custom_var: "default value" | ||
other_var: 123 | ||
""" | ||
|
||
|
||
class TestNestedDictRule(unittest.TestCase): | ||
collection = RulesCollection() | ||
|
||
def setUp(self): | ||
self.collection.register(NestedDictRule()) | ||
|
||
def test_statement_positive(self): | ||
runner = RunFromText(self.collection) | ||
results = runner.run_state(GOOD_NESTED_DICT_STATE) | ||
self.assertEqual(0, len(results)) | ||
|
||
def test_statement_negative(self): | ||
runner = RunFromText(self.collection) | ||
results = runner.run_state(BAD_NESTED_DICT_STATE) | ||
self.assertEqual(4, len(results)) | ||
|
||
# Check line numbers of the results | ||
self.assertEqual(7, results[0].linenumber) | ||
self.assertEqual(19, results[1].linenumber) | ||
self.assertEqual(27, results[2].linenumber) | ||
self.assertEqual(29, results[3].linenumber) |