From 48294837e8f0dc3c074df1f5ea19c8c1a01eb6b7 Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Tue, 3 May 2022 13:13:54 +0200 Subject: [PATCH] Include opt-in rules when listing tags and rules With PR #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: #2068 Signed-off-by: Daniel Ziegenberg --- .github/workflows/tox.yml | 3 ++- docs/rules.rst | 2 +- src/ansiblelint/cli.py | 8 ++++-- src/ansiblelint/rules/__init__.py | 12 +++++++-- test/fixtures/list-rules-tests/.yamllint | 2 ++ test/test_list_rules.py | 33 ++++++++++++++++++++++++ 6 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/list-rules-tests/.yamllint create mode 100644 test/test_list_rules.py diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index d27abb9db46..86893d2eec8 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -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')" diff --git a/docs/rules.rst b/docs/rules.rst index f3b970b0dd3..95de210f70a 100644 --- a/docs/rules.rst +++ b/docs/rules.rst @@ -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: diff --git a/src/ansiblelint/cli.py b/src/ansiblelint/cli.py index 7a180d6534b..4fc609e124a 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. For listing rules only the following formats " + "for argument -f are supported: {plain, rich, rst}", ) parser.add_argument( "-f", @@ -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", diff --git a/src/ansiblelint/rules/__init__.py b/src/ansiblelint/rules/__init__.py index ae46cca92f9..a0b4436d72f 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 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]: 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..5c71fc0b30d --- /dev/null +++ b/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_with_verbosity_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_with_verbosity_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