Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow additional collections in only-builtins #2710

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Expand Up @@ -163,7 +163,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: 714
PYTEST_REQPASS: 715

steps:
- name: Activate WSL1
Expand Down
@@ -0,0 +1,6 @@
# Mock modules or roles in order to pass ansible-playbook --syntax-check
mock_modules:
- fake_namespace.fake_collection.fake_module

only_builtins_allow_collections:
- fake_namespace.fake_collection
1 change: 1 addition & 0 deletions src/ansiblelint/cli.py
Expand Up @@ -457,6 +457,7 @@ def merge_config(file_config: dict[Any, Any], cli_config: Namespace) -> Namespac
"mock_modules": [],
"mock_roles": [],
"enable_list": [],
"only_builtins_allow_collections": [],
# do not include "write_list" here. See special logic below.
}

Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/config.py
Expand Up @@ -122,6 +122,7 @@
mock_modules=[],
mock_roles=[],
loop_var_prefix=None,
only_builtins_allow_collections=[],
var_naming_pattern=None,
offline=False,
project_dir=".", # default should be valid folder (do not use None here)
Expand Down
30 changes: 25 additions & 5 deletions src/ansiblelint/rules/only_builtins.py
Expand Up @@ -4,6 +4,7 @@
import sys
from typing import Any

from ansiblelint.config import options
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule

Expand All @@ -23,11 +24,16 @@ class OnlyBuiltinsRule(AnsibleLintRule):
def matchtask(
self, task: dict[str, Any], file: Lintable | None = None
) -> bool | str:
fqcn_builtin = task["action"]["__ansible_module_original__"].startswith(
"ansible.builtin."
module = task["action"]["__ansible_module_original__"]

is_builtin = module.startswith("ansible.builtin.") or module in builtins

is_manually_allowed = any(
module.startswith(f"{prefix}.")
for prefix in options.only_builtins_allow_collections
)
non_fqcn_builtin = task["action"]["__ansible_module_original__"] in builtins
return not fqcn_builtin and not non_fqcn_builtin and not is_nested_task(task)

return not is_builtin and not is_manually_allowed and not is_nested_task(task)


# testing code to be loaded only with pytest or when executed the rule file
Expand All @@ -36,7 +42,7 @@ def matchtask(
# pylint: disable=ungrouped-imports
import pytest

from ansiblelint.constants import VIOLATIONS_FOUND_RC
from ansiblelint.constants import SUCCESS_RC, VIOLATIONS_FOUND_RC
from ansiblelint.testing import RunFromText, run_ansible_lint

SUCCESS_PLAY = """
Expand All @@ -63,6 +69,20 @@ def test_only_builtin_fail() -> None:
assert "1 failure(s)" in result.stderr
assert "only-builtins" in result.stdout

def test_only_builtins_allow_collections() -> None:
"""Test rule doesn't match."""
conf_path = "examples/playbooks/.ansible-lint-only-builtins-allow-collections"
result = run_ansible_lint(
f"--config-file={conf_path}",
"--strict",
"--warn-list=",
"--enable-list",
"only-builtins",
"examples/playbooks/rule-only-builtins.yml",
)
assert "only-builtins" not in result.stdout
assert result.returncode == SUCCESS_RC

@pytest.mark.parametrize(
"rule_runner", (OnlyBuiltinsRule,), indirect=["rule_runner"]
)
Expand Down
7 changes: 7 additions & 0 deletions src/ansiblelint/schemas/ansible-lint-config.json
Expand Up @@ -51,6 +51,13 @@
"title": "Loop Var Prefix",
"type": "string"
},
"only_builtins_allow_collections": {
"items": {
"type": "string"
},
"title": "Only Builtins Allow Collections",
"type": "array"
},
"mock_modules": {
"items": {
"type": "string"
Expand Down