Skip to content

Commit

Permalink
Allow configurations for each rule stored in ansiblelint.config.options
Browse files Browse the repository at this point in the history
Add ansiblelint.config.options.rules: Dict[str, Any] to store
configurations for each rule and utilize them from each rule to allow
customize behaviors of rules more flexibly.

Signed-Off-By: Satoru SATOH <satoru.satoh@gmail.com>
  • Loading branch information
ssato committed Apr 30, 2021
1 parent 25751a8 commit 09291dc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/ansiblelint/config.py
Expand Up @@ -5,7 +5,7 @@
import sys
from argparse import Namespace
from functools import lru_cache
from typing import Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple

from packaging.version import Version

Expand Down Expand Up @@ -84,6 +84,7 @@
extra_vars=None,
enable_list=[],
skip_action_validation=True,
rules=dict(), # Placeholder to set and keep configurations for each rule.
)

# Used to store detected tag deprecations
Expand All @@ -93,6 +94,11 @@
collection_list: List[str] = []


def get_rule_config(rule_id: str) -> Dict[str, Any]:
"""Get configurations for the rule ``rule_id``."""
return options.rules.get(rule_id, dict())


@lru_cache()
def ansible_collections_path() -> str:
"""Return collection path variable for current version of Ansible."""
Expand Down
14 changes: 12 additions & 2 deletions src/ansiblelint/rules/__init__.py
Expand Up @@ -7,8 +7,9 @@
import re
from argparse import Namespace
from collections import defaultdict
from functools import lru_cache
from importlib.abc import Loader
from typing import Iterator, List, Optional, Set, Union
from typing import Any, Dict, Iterator, List, Optional, Set, Union

import ansiblelint.utils
from ansiblelint._internal.rules import (
Expand All @@ -17,7 +18,7 @@
LoadingFailureRule,
RuntimeErrorRule,
)
from ansiblelint.config import options
from ansiblelint.config import get_rule_config, options
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.skip_utils import append_skipped_rules, get_rule_skips_from_line
Expand All @@ -26,6 +27,15 @@


class AnsibleLintRule(BaseRule):
@property
def rule_config(self) -> Dict[str, Any]:
return get_rule_config(self.id)

@lru_cache()
def get_config(self, key: str) -> Any:
"""Return a configured value for given key string."""
return self.rule_config.get(key, None)

def __repr__(self) -> str:
"""Return a AnsibleLintRule instance representation."""
return self.id + ": " + self.shortdesc
Expand Down
14 changes: 14 additions & 0 deletions test/TestAnsibleLintRule.py
@@ -1,7 +1,21 @@
import pytest

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


def test_unjinja():
text = "{{ a }} {% b %} {# try to confuse parsing inside a comment { {{}} } #}"
output = "JINJA_EXPRESSION JINJA_STATEMENT JINJA_COMMENT"
assert AnsibleLintRule.unjinja(text) == output


@pytest.mark.parametrize('rule_config', (dict(), dict(foo=True, bar=1)))
def test_rule_config(rule_config, monkeypatch):
rule_id = 'rule-0'
monkeypatch.setattr(AnsibleLintRule, 'id', rule_id)
monkeypatch.setitem(options.rules, rule_id, rule_config)

rule = AnsibleLintRule()
assert set(rule.rule_config.items()) == set(rule_config.items())
assert all(rule.get_config(k) == v for k, v in rule_config.items())

0 comments on commit 09291dc

Please sign in to comment.