summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/rules/meta_incorrect.py
blob: 4252254e07788a5838e0ae32a80fa91379037471 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Implementation of meta-incorrect rule."""
# Copyright (c) 2018, Ansible Project
from __future__ import annotations

import sys
from typing import TYPE_CHECKING

from ansiblelint.constants import LINE_NUMBER_KEY
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
    from ansiblelint.errors import MatchError
    from ansiblelint.file_utils import Lintable


class MetaChangeFromDefaultRule(AnsibleLintRule):
    """meta/main.yml default values should be changed."""

    id = "meta-incorrect"
    field_defaults = [
        ("author", "your name"),
        ("description", "your description"),
        ("company", "your company (optional)"),
        ("license", "license (GPLv2, CC-BY, etc)"),
        ("license", "license (GPL-2.0-or-later, MIT, etc)"),
    ]
    values = ", ".join(sorted({f[0] for f in field_defaults}))
    description = (
        f"You should set appropriate values in meta/main.yml for these fields: {values}"
    )
    severity = "HIGH"
    tags = ["metadata"]
    version_added = "v4.0.0"

    def matchyaml(self, file: Lintable) -> list[MatchError]:
        if file.kind != "meta" or not file.data:
            return []

        galaxy_info = file.data.get("galaxy_info", None)
        if not galaxy_info:
            return []

        results = []
        for field, default in self.field_defaults:
            value = galaxy_info.get(field, None)
            if value and value == default:
                results.append(
                    self.create_matcherror(
                        filename=file,
                        lineno=file.data[LINE_NUMBER_KEY],
                        message=f"Should change default metadata: {field}",
                    ),
                )

        return results


if "pytest" in sys.modules:
    from ansiblelint.rules import RulesCollection  # pylint: disable=ungrouped-imports
    from ansiblelint.runner import Runner  # pylint: disable=ungrouped-imports

    def test_default_galaxy_info(
        default_rules_collection: RulesCollection,
    ) -> None:
        """Test for meta-incorrect."""
        results = Runner(
            "examples/roles/meta_incorrect_fail",
            rules=default_rules_collection,
        ).run()
        for result in results:
            assert result.rule.id == "meta-incorrect"
        assert len(results) == 4

        assert "Should change default metadata: author" in str(results)
        assert "Should change default metadata: description" in str(results)
        assert "Should change default metadata: company" in str(results)
        assert "Should change default metadata: license" in str(results)