diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 9d90e21a8b4..67b33a5c1bb 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -156,7 +156,7 @@ 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: 610 + PYTEST_REQPASS: 614 steps: - name: Activate WSL1 diff --git a/src/ansiblelint/cli.py b/src/ansiblelint/cli.py index 7a180d6534b..4687891680c 100644 --- a/src/ansiblelint/cli.py +++ b/src/ansiblelint/cli.py @@ -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", @@ -316,7 +317,12 @@ 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. For listing tags only the " + "following formats are supported: {plain, rich, rst}", ) parser.add_argument( "-v", diff --git a/src/ansiblelint/rules/__init__.py b/src/ansiblelint/rules/__init__.py index ae46cca92f9..a449a6d943e 100644 --- a/src/ansiblelint/rules/__init__.py +++ b/src/ansiblelint/rules/__init__.py @@ -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]: diff --git a/test/fixtures/list-rules-tests/.yamllint b/test/fixtures/list-rules-tests/.yamllint new file mode 100644 index 00000000000..d9e1a256f6c --- /dev/null +++ b/test/fixtures/list-rules-tests/.yamllint @@ -0,0 +1,2 @@ +--- +{} diff --git a/test/test_list_rules.py b/test/test_list_rules.py new file mode 100644 index 00000000000..6b61b0c7124 --- /dev/null +++ b/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