-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sanity rule with check for bad and disallowed ignores (#3102)
- Loading branch information
1 parent
5b070f0
commit b9d96df
Showing
18 changed files
with
280 additions
and
10 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
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 @@ | ||
../../src/ansiblelint/rules/sanity.md |
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 @@ | ||
plugins/module_utils/ansible_example_module.py import-3.6!skip # comment |
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,2 @@ | ||
plugins/module_utils/ansible_example_module.py compile-2.6!skip | ||
plugins/module_utils/ansible_example_module.py import-2.6!skip |
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,2 @@ | ||
plugins/module_utils/ansible_example_module.incorrect-3.6!skip | ||
#plugins/module_utils/ansible_example_module.py import-3.6!skip |
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,49 @@ | ||
# sanity | ||
|
||
This rule checks the `tests/sanity/ignore-x.x.txt` file for disallowed ignores. | ||
This rule is extremely opinionated and enforced by Partner Engineering. The | ||
currently allowed ruleset is subject to change, but is starting at a minimal | ||
number of allowed ignores for maximum test enforcement. Any commented-out ignore | ||
entries are not evaluated. | ||
|
||
This rule can produce messages like: | ||
|
||
- `sanity[cannot-ignore]` - Ignore file contains {test} at line {line_num}, | ||
which is not a permitted ignore. | ||
- `sanity[bad-ignore]` - Ignore file entry at {line_num} is formatted | ||
incorrectly. Please review. | ||
|
||
Currently allowed ignores are: | ||
|
||
- `validate-modules:missing-gplv3-license` | ||
- `import-2.6` | ||
- `import-2.6!skip` | ||
- `import-2.7` | ||
- `import-2.7!skip` | ||
- `import-3.5` | ||
- `import-3.5!skip` | ||
- `compile-2.6` | ||
- `compile-2.6!skip` | ||
- `compile-2.7` | ||
- `compile-2.7!skip` | ||
- `compile-3.5` | ||
- `compile-3.5!skip` | ||
|
||
## Problematic code | ||
|
||
``` | ||
# tests/sanity/ignore-x.x.txt | ||
plugins/module_utils/ansible_example_module.py import-3.6!skip | ||
``` | ||
|
||
``` | ||
# tests/sanity/ignore-x.x.txt | ||
plugins/module_utils/ansible_example_module.oops-3.6!skip | ||
``` | ||
|
||
## Correct code | ||
|
||
``` | ||
# tests/sanity/ignore-x.x.txt | ||
plugins/module_utils/ansible_example_module.py import-2.7!skip | ||
``` |
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,133 @@ | ||
"""Implementation of sanity rule.""" | ||
from __future__ import annotations | ||
|
||
import sys | ||
from typing import TYPE_CHECKING | ||
|
||
from ansiblelint.rules import AnsibleLintRule | ||
|
||
# Copyright (c) 2018, Ansible Project | ||
|
||
|
||
if TYPE_CHECKING: | ||
from typing import Any | ||
|
||
from ansiblelint.errors import MatchError | ||
from ansiblelint.file_utils import Lintable | ||
|
||
|
||
class CheckSanityIgnoreFiles(AnsibleLintRule): | ||
"""Ignore entries in sanity ignore files must match an allow list.""" | ||
|
||
id = "sanity" | ||
description = ( | ||
"Identifies non-allowed entries in the `tests/sanity/ignore*.txt files." | ||
) | ||
severity = "MEDIUM" | ||
tags = ["experimental"] | ||
version_added = "v6.14.0" | ||
|
||
# Partner Engineering defines this list. Please contact PE for changes. | ||
allowed_ignores = [ | ||
"validate-modules:missing-gplv3-license", | ||
"import-2.6", | ||
"import-2.6!skip", | ||
"import-2.7", | ||
"import-2.7!skip", | ||
"import-3.5", | ||
"import-3.5!skip", | ||
"compile-2.6", | ||
"compile-2.6!skip", | ||
"compile-2.7", | ||
"compile-2.7!skip", | ||
"compile-3.5", | ||
"compile-3.5!skip", | ||
] | ||
|
||
def matchyaml(self, file: Lintable) -> list[MatchError]: | ||
"""Evaluate sanity ignore lists for disallowed ignores. | ||
:param file: Input lintable file that is a match for `sanity-ignore-file` | ||
:returns: List of errors matched to the input file | ||
""" | ||
results: list[MatchError] = [] | ||
test = "" | ||
|
||
if file.kind != "sanity-ignore-file": | ||
return [] | ||
|
||
with open(file.abspath, encoding="utf-8") as ignore_file: | ||
entries = ignore_file.read().splitlines() | ||
|
||
for line_num, entry in enumerate(entries, 1): | ||
if entry and entry[0] != "#": | ||
try: | ||
if "#" in entry: | ||
entry, _ = entry.split("#") | ||
(_, test) = entry.split() | ||
if test not in self.allowed_ignores: | ||
results.append( | ||
self.create_matcherror( | ||
message=f"Ignore file contains {test} at line {line_num}, which is not a permitted ignore.", | ||
tag="sanity[cannot-ignore]", | ||
linenumber=line_num, | ||
filename=file, | ||
) | ||
) | ||
|
||
except ValueError: | ||
results.append( | ||
self.create_matcherror( | ||
message=f"Ignore file entry at {line_num} is formatted incorrectly. Please review.", | ||
tag="sanity[bad-ignore]", | ||
linenumber=line_num, | ||
filename=file, | ||
) | ||
) | ||
|
||
return results | ||
|
||
|
||
# testing code to be loaded only with pytest or when executed the rule file | ||
if "pytest" in sys.modules: | ||
import pytest | ||
|
||
from ansiblelint.rules import RulesCollection # pylint: disable=ungrouped-imports | ||
from ansiblelint.runner import Runner # pylint: disable=ungrouped-imports | ||
|
||
@pytest.mark.parametrize( | ||
("test_file", "failures", "tags"), | ||
( | ||
pytest.param( | ||
"examples/sanity_ignores/tests/sanity/ignore-2.14.txt", | ||
0, | ||
"sanity[cannot-ignore]", | ||
id="pass", | ||
), | ||
pytest.param( | ||
"examples/sanity_ignores/tests/sanity/ignore-2.15.txt", | ||
1, | ||
"sanity[bad-ignore]", | ||
id="fail0", | ||
), | ||
pytest.param( | ||
"examples/sanity_ignores/tests/sanity/ignore-2.13.txt", | ||
1, | ||
"sanity[cannot-ignore]", | ||
id="fail1", | ||
), | ||
), | ||
) | ||
def test_sanity_ignore_files( | ||
default_rules_collection: RulesCollection, | ||
test_file: str, | ||
failures: int, | ||
tags: str, | ||
) -> None: | ||
"""Test rule matches.""" | ||
default_rules_collection.register(CheckSanityIgnoreFiles()) | ||
results = Runner(test_file, rules=default_rules_collection).run() | ||
for result in results: | ||
assert result.rule.id == CheckSanityIgnoreFiles().id | ||
assert result.tag == tags | ||
assert len(results) == failures |
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
Oops, something went wrong.