summaryrefslogtreecommitdiffstats
path: root/test/test_profiles.py
blob: 6f7c34f037499f3602780ca1d8e4559aade3a3ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Tests for the --profile feature."""
import platform
import subprocess
import sys

from _pytest.capture import CaptureFixture

from ansiblelint.rules import RulesCollection, filter_rules_with_profile
from ansiblelint.rules.risky_shell_pipe import ShellWithoutPipefail


def test_profile_min() -> None:
    """Asserts our ability to unload rules based on profile."""
    collection = RulesCollection()
    assert len(collection.rules) == 4, "Unexpected number of implicit rules."
    # register one extra rule that we know not to be part of "min" profile

    collection.register(ShellWithoutPipefail())
    assert len(collection.rules) == 5, "Failed to register new rule."

    filter_rules_with_profile(collection.rules, "min")
    assert (
        len(collection.rules) == 3
    ), "Failed to unload rule that is not part of 'min' profile."


def test_profile_listing(capfd: CaptureFixture[str]) -> None:
    """Test that run without arguments it will detect and lint the entire repository."""
    cmd = [
        sys.executable,
        "-m",
        "ansiblelint",
        "-P",
    ]
    result = subprocess.run(cmd, check=False).returncode
    assert result == 0

    out, err = capfd.readouterr()

    # Confirmation that it runs in auto-detect mode
    assert "command-instead-of-module" in out
    if sys.version_info < (3, 9):
        assert "ansible-lint is no longer tested" in err
    else:
        # On WSL we might see this warning on stderr:
        # [WARNING]: Ansible is being run in a world writable directory
        # WSL2 has "WSL2" in platform name but WSL1 has "microsoft":
        platform_name = platform.platform().lower()
        if all(word not in platform_name for word in ["wsl", "microsoft"]):
            assert err == "", platform_name