Skip to content

Commit

Permalink
Allow tabs inside jinja strings
Browse files Browse the repository at this point in the history
Fixes: #4020
  • Loading branch information
ssbarnea committed May 9, 2024
1 parent aaff090 commit 05609f0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
46 changes: 46 additions & 0 deletions a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import importlib_metadata
from packaging.requirements import Requirement
from packaging.specifiers import SpecifierSet


class Req:
"""Utility class for working with package dependencies."""

reqs: dict[str, SpecifierSet]

def __init__(self) -> None:
"""Load linter metadata requirements."""
self.reqs = {}
for req_str in importlib_metadata.metadata("ansible-lint").json[
"requires_dist"
]:
req = Requirement(req_str)
# print(a.name)
if req.name:
# breakpoint()
self.reqs[req.name] = req.specifier
# breakpoint()

def contains(self, req_name: str, req_version: str) -> bool:
"""Verify if given version is matching current metadata depedendencies."""
if req_name not in self.reqs:
return False
return all(specifier.contains(req_version) for specifier in self.reqs[req_name])


req = Req()
print(req.contains("ansible-core", "2.2"))
# breakpoint()

# def _process_requires_dist(
# self,
# value: list[str],
# ) -> list[requirements.Requirement]:
# reqs = []
# try:
# for req in value:
# reqs.append(requirements.Requirement(req))
# except requirements.InvalidRequirement as exc:
# raise self._invalid_metadata(f"{req!r} is invalid for {{field}}", cause=exc)
# else:
# return reqs
6 changes: 6 additions & 0 deletions examples/playbooks/rule-no-tabs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@
path: some.txt
regexp: ^\t$
line: string with \t inside
- name: Should not trigger inside jinja
vars:
deep:
"some{{ '\t' }}stuff": true
ansible.builtin.debug:
msg: "{{ 'foo' + '\t' + 'bar' }}"
25 changes: 25 additions & 0 deletions src/ansiblelint/requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Utilities for checking python packages requirements."""

import importlib_metadata
from packaging.requirements import Requirement
from packaging.specifiers import SpecifierSet


class Reqs:
"""Utility class for working with package dependencies."""

reqs: dict[str, SpecifierSet]

def __init__(self, name: str = "ansible-lint") -> None:
"""Load linter metadata requirements."""
self.reqs = {}
for req_str in importlib_metadata.metadata(name).json["requires_dist"]:
req = Requirement(req_str)
if req.name:
self.reqs[req.name] = req.specifier

def contains(self, req_name: str, req_version: str) -> bool:
"""Verify if given version is matching current metadata depedendencies."""
if req_name not in self.reqs:
return False
return all(specifier.contains(req_version) for specifier in self.reqs[req_name])
10 changes: 8 additions & 2 deletions src/ansiblelint/rules/no_tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import TYPE_CHECKING

from ansiblelint.rules import AnsibleLintRule
from ansiblelint.text import has_jinja
from ansiblelint.yaml_utils import nested_items_path

if TYPE_CHECKING:
Expand Down Expand Up @@ -45,9 +46,14 @@ def matchtask(
) -> bool | str:
action = task["action"]["__ansible_module__"]
for k, v, _ in nested_items_path(task):
if isinstance(k, str) and "\t" in k:
if isinstance(k, str) and "\t" in k and not has_jinja(k):
return True
if isinstance(v, str) and "\t" in v and (action, k) not in self.allow_list:
if (
isinstance(v, str)
and "\t" in v
and (action, k) not in self.allow_list
and not has_jinja(v)
):
return True
return False

Expand Down

0 comments on commit 05609f0

Please sign in to comment.