Skip to content

Commit

Permalink
Include opt-in rules when verbosely listing tags and rules
Browse files Browse the repository at this point in the history
With PR ansible#1450 optional rules with the 'opt-in' tag were introduced
and according to the docs, listing rules and tags with `-v` should
also list the opt-in rules.

Fixes: ansible#2068

Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
  • Loading branch information
ziegenberg committed May 3, 2022
1 parent cd32348 commit fad94d4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/tox.yml
Expand Up @@ -155,7 +155,8 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:TOX_PARALLEL_NO_SPINNER
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 611
PYTEST_REQPASS: 615

steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
9 changes: 7 additions & 2 deletions src/ansiblelint/cli.py
Expand Up @@ -218,7 +218,8 @@ def get_cli_parser() -> argparse.ArgumentParser:
dest="listrules",
default=False,
action="store_true",
help="list all the rules",
help="List all the rules. Increase the verbosity level with `-v` to include 'opt-in' "
"rules. For listing rules only the following formats are supported: {plain, rich, rst}",
)
parser.add_argument(
"-f",
Expand Down Expand Up @@ -316,7 +317,11 @@ def get_cli_parser() -> argparse.ArgumentParser:
help="only check rules whose id/tags match these values",
)
parser.add_argument(
"-T", dest="listtags", action="store_true", help="list all the tags"
"-T",
dest="listtags",
action="store_true",
help="List all the tags and the rules they cover. Increase the verbosity level "
"with `-v` to include 'opt-in' tag and its rules.",
)
parser.add_argument(
"-v",
Expand Down
12 changes: 10 additions & 2 deletions src/ansiblelint/rules/__init__.py
Expand Up @@ -344,8 +344,16 @@ def __init__(

def register(self, obj: AnsibleLintRule) -> None:
"""Register a rule."""
# We skip opt-in rules which were not manually enabled
if "opt-in" not in obj.tags or obj.id in self.options.enable_list:
# We skip opt-in rules which were not manually enabled.
# But we do include opt-in rules when verbosely listing all rules or tags
if any(
[
"opt-in" not in obj.tags,
obj.id in self.options.enable_list,
self.options.listrules and self.options.verbosity,
self.options.listtags and self.options.verbosity,
]
):
self.rules.append(obj)

def __iter__(self) -> Iterator[BaseRule]:
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/list-rules-tests/.yamllint
@@ -0,0 +1,2 @@
---
{}
49 changes: 49 additions & 0 deletions test/test_list_rules.py
@@ -0,0 +1,49 @@
"""Tests related to our logging/verbosity setup."""

import os

import pytest

from ansiblelint.testing import run_ansible_lint


@pytest.mark.parametrize(
("result", "verbosity"),
((False, ""), (True, "-v")),
ids=("default verbosity", "more verbose"),
)
def test_list_rules_with_verbosity_includes_opt_in_rules(
result: bool, verbosity: str
) -> None:
"""Checks that listing rules with verbosity level at least set to -v also includes the opt-in rules."""
# Piggyback off the .yamllint in the root of the repo, just for testing.
# We'll "override" it with the one in the fixture.
cwd = os.path.realpath(
os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")
)
fakerole = os.path.join("test", "fixtures", "list-rules-tests")

result_list_rules = run_ansible_lint(verbosity, "-L", fakerole, cwd=cwd)

assert ("opt-in" in result_list_rules.stdout) is result


@pytest.mark.parametrize(
("result", "verbosity"),
((False, ""), (True, "-v")),
ids=("default verbosity", "more verbose"),
)
def test_list_tags_with_verbosity_includes_opt_in_rules(
result: bool, verbosity: str
) -> None:
"""Checks that listing tags with verbosity level at least set to -v also includes the opt-in rules."""
# Piggyback off the .yamllint in the root of the repo, just for testing.
# We'll "override" it with the one in the fixture.
cwd = os.path.realpath(
os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")
)
fakerole = os.path.join("test", "fixtures", "list-rules-tests")

result_list_rules = run_ansible_lint(verbosity, "-L", fakerole, cwd=cwd)

assert ("opt-in" in result_list_rules.stdout) is result

0 comments on commit fad94d4

Please sign in to comment.