summaryrefslogtreecommitdiffstats
path: root/test/test_profiles.py
blob: a40382c8308c222d982788c5d8635f2a54fb8c34 (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
51
52
53
54
55
56
57
58
59
60
"""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
from ansiblelint.text import strip_ansi_escape


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
    # 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()
    err_lines = []
    for line in strip_ansi_escape(err).splitlines():
        if "SyntaxWarning:" in line:
            continue
        if (
            "Skipped installing collection dependencies due to running in offline mode."
            in line
        ):
            continue
        err_lines.append(line)
    if all(word not in platform_name for word in ["wsl", "microsoft"]) and err_lines:
        assert (
            not err_lines
        ), f"Unexpected stderr output found while running on {platform_name} platform:\n{err_lines}"