Skip to content

Commit

Permalink
Add has_jinja and has_glob to text module (#2794)
Browse files Browse the repository at this point in the history
This should make it easier to reuse these utility functions.

Related: #2749
  • Loading branch information
ssbarnea committed Dec 8, 2022
1 parent 5124ecc commit 110f8ed
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 728
PYTEST_REQPASS: 741

steps:
- name: Activate WSL1
Expand Down
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ repos:
rev: v2.15.8
hooks:
- id: pylint
args:
- --output-format=colorized
additional_dependencies:
- ansible-compat>=2.2.6
- ansible-core>=2.14.0
Expand Down
11 changes: 4 additions & 7 deletions src/ansiblelint/rules/deprecated_bare_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
from __future__ import annotations

import os
import re
from typing import TYPE_CHECKING, Any

from ansiblelint.rules import AnsibleLintRule
from ansiblelint.text import has_glob, has_jinja

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
Expand All @@ -45,9 +45,6 @@ class UsingBareVariablesIsDeprecatedRule(AnsibleLintRule):
tags = ["deprecations"]
version_added = "historic"

_jinja = re.compile(r"{[{%].*[%}]}", re.DOTALL)
_glob = re.compile("[][*?]")

def matchtask(
self, task: dict[str, Any], file: Lintable | None = None
) -> bool | str:
Expand Down Expand Up @@ -80,13 +77,13 @@ def matchtask(
def _matchvar(
self, varstring: str, task: dict[str, Any], loop_type: str
) -> bool | str:
if isinstance(varstring, str) and not self._jinja.match(varstring):
if isinstance(varstring, str) and not has_jinja(varstring):
valid = loop_type == "with_fileglob" and bool(
self._jinja.search(varstring) or self._glob.search(varstring)
has_jinja(varstring) or has_glob(varstring)
)

valid |= loop_type == "with_filetree" and bool(
self._jinja.search(varstring) or varstring.endswith(os.sep)
has_jinja(varstring) or varstring.endswith(os.sep)
)
if not valid:
message = (
Expand Down
16 changes: 16 additions & 0 deletions src/ansiblelint/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
from __future__ import annotations

import re
from functools import lru_cache

RE_HAS_JINJA = re.compile(r"{[{%#].*[%#}]}", re.DOTALL)
RE_HAS_GLOB = re.compile("[][*?]")


def strip_ansi_escape(data: str | bytes) -> str:
Expand Down Expand Up @@ -32,3 +36,15 @@ def removeprefix(self: str, prefix: str) -> str:
if self.startswith(prefix):
return self[len(prefix) :]
return self[:]


@lru_cache(maxsize=None)
def has_jinja(value: str) -> bool:
"""Return true if a string seems to contain jinja templating."""
return bool(isinstance(value, str) and RE_HAS_JINJA.search(value))


@lru_cache(maxsize=None)
def has_glob(value: str) -> bool:
"""Return true if a string looks like having a glob pattern."""
return bool(isinstance(value, str) and RE_HAS_GLOB.search(value))
39 changes: 39 additions & 0 deletions test/test_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Tests for text module."""
from typing import Any

import pytest

from ansiblelint.text import has_glob, has_jinja


@pytest.mark.parametrize(
("value", "expected"),
(
pytest.param("", False, id="0"),
pytest.param("{{ }}", True, id="1"),
pytest.param("foo {# #} bar", True, id="2"),
pytest.param("foo \n{% %} bar", True, id="3"),
pytest.param(None, False, id="4"),
pytest.param(42, False, id="5"),
pytest.param(True, False, id="6"),
),
)
def test_has_jinja(value: Any, expected: bool) -> None:
"""Tests for has_jinja()."""
assert has_jinja(value) == expected


@pytest.mark.parametrize(
("value", "expected"),
(
pytest.param("", False, id="0"),
pytest.param("*", True, id="1"),
pytest.param("foo.*", True, id="2"),
pytest.param(None, False, id="4"),
pytest.param(42, False, id="5"),
pytest.param(True, False, id="6"),
),
)
def test_has_glob(value: Any, expected: bool) -> None:
"""Tests for has_jinja()."""
assert has_glob(value) == expected

0 comments on commit 110f8ed

Please sign in to comment.