Skip to content

Commit

Permalink
Include opt-in rules when 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 should also list
the opt-in rules.

Fixes: ansible#2068

Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
  • Loading branch information
ziegenberg committed May 4, 2022
1 parent cd32348 commit d3656dc
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 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: 613

steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
2 changes: 1 addition & 1 deletion docs/rules.rst
Expand Up @@ -27,7 +27,7 @@ The following shows the available tags in an example set of rules, and the
rules associated with each tag:


.. command-output:: ansible-lint -v -T
.. command-output:: ansible-lint -T
:cwd: ..
:returncode: 0
:nostderr:
Expand Down
8 changes: 6 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. For listing rules only the following formats "
"for argument -f are supported: {plain, rich, rst}",
)
parser.add_argument(
"-f",
Expand Down Expand Up @@ -316,7 +317,10 @@ 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.",
)
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 listing all rules or tags
if any(
[
"opt-in" not in obj.tags,
obj.id in self.options.enable_list,
self.options.listrules,
self.options.listtags,
]
):
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 @@
---
{}
33 changes: 33 additions & 0 deletions test/test_list_rules.py
@@ -0,0 +1,33 @@
"""Tests related to our logging/verbosity setup."""

import os

from ansiblelint.testing import run_ansible_lint


def test_list_rules_includes_opt_in_rules() -> None:
"""Checks that listing rules 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("-L", fakerole, cwd=cwd)

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


def test_list_tags_includes_opt_in_rules() -> None:
"""Checks that listing tags 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_tags = run_ansible_lint("-L", fakerole, cwd=cwd)

assert ("opt-in" in result_list_tags.stdout) is True

0 comments on commit d3656dc

Please sign in to comment.