Skip to content

Commit

Permalink
Merge branch 'main' into add-subdirs-task-prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored May 13, 2024
2 parents 00fd580 + e8a71e5 commit 45c4900
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 23 deletions.
20 changes: 10 additions & 10 deletions .config/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ansible-compat==24.5.1a0
ansible-core==2.16.6
astroid==3.1.0
attrs==23.2.0
babel==2.14.0
babel==2.15.0
beautifulsoup4==4.12.3
black==24.4.2
boolean-py==4.0
Expand All @@ -22,7 +22,7 @@ click==8.1.7
colorama==0.4.6
coverage==7.5.1
coverage-enable-subprocess==1.0
cryptography==42.0.6
cryptography==42.0.7
csscompressor==0.9.5
cssselect2==0.7.0
defusedxml==0.7.1
Expand All @@ -32,7 +32,7 @@ exceptiongroup==1.2.1
execnet==2.1.1
filelock==3.14.0
ghp-import==2.1.0
griffe==0.44.0
griffe==0.45.0
htmlmin2==0.1.13
idna==3.7
iniconfig==2.0.0
Expand All @@ -53,17 +53,17 @@ mccabe==0.7.0
mdurl==0.1.2
mergedeep==1.3.4
mkdocs==1.6.0
mkdocs-ansible==24.3.0
mkdocs-ansible==24.3.1
mkdocs-autorefs==1.0.1
mkdocs-gen-files==0.5.0
mkdocs-get-deps==0.2.0
mkdocs-htmlproofer-plugin==1.2.1
mkdocs-macros-plugin==1.0.5
mkdocs-material==9.5.21
mkdocs-material==9.5.22
mkdocs-material-extensions==1.3.1
mkdocs-minify-plugin==0.8.0
mkdocs-monorepo-plugin==1.1.0
mkdocstrings==0.25.0
mkdocstrings==0.25.1
mkdocstrings-python==1.10.0
mypy==1.10.0
mypy-extensions==1.0.0
Expand All @@ -73,7 +73,7 @@ paginate==0.5.6
pathspec==0.12.1
pillow==10.3.0
pip==24.0
pipdeptree==2.19.1
pipdeptree==2.20.0
platformdirs==4.2.1
pluggy==1.5.0
psutil==5.9.8
Expand All @@ -90,10 +90,10 @@ python-slugify==8.0.4
pyyaml==6.0.1
pyyaml-env-tag==0.1
referencing==0.35.1
regex==2024.4.28
regex==2024.5.10
requests==2.31.0
rich==13.7.1
rpds-py==0.18.0
rpds-py==0.18.1
ruamel-yaml==0.18.6
six==1.16.0
soupsieve==2.5
Expand All @@ -102,7 +102,7 @@ termcolor==2.4.0
text-unidecode==1.3
tinycss2==1.3.0
tomli==2.0.1
tomlkit==0.12.4
tomlkit==0.12.5
types-jsonschema==4.22.0.20240501
types-pyyaml==6.0.12.20240311
typing-extensions==4.11.0
Expand Down
4 changes: 2 additions & 2 deletions .config/requirements-lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ black==24.4.2
bracex==2.4
cffi==1.16.0
click==8.1.7
cryptography==42.0.6
cryptography==42.0.7
filelock==3.14.0
jinja2==3.1.4
jsonschema==4.22.0
Expand All @@ -28,7 +28,7 @@ pygments==2.18.0
pyyaml==6.0.1
referencing==0.35.1
rich==13.7.1
rpds-py==0.18.0
rpds-py==0.18.1
ruamel-yaml==0.18.6
subprocess-tee==0.4.1
tomli==2.0.1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 860
PYTEST_REQPASS: 865
steps:
- uses: actions/checkout@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ repos:
types: [file, yaml]
entry: yamllint --strict
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.4.3"
rev: "v0.4.4"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
13 changes: 12 additions & 1 deletion examples/playbooks/rule-no-tabs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@
- name: Should not trigger no-tabs rules # noqa fqcn
lineinfile:
path: some.txt
regexp: ^\t$
regexp: "^\t$"
line: string with \t inside
# Disabled as attempt to mock it would trigger an error validating its arguments
# - name: Should not trigger no-tabs rules # noqa fqcn
# win_lineinfile:
# path: some.txt
# regexp: "^\t$"
# line: string with \t inside
- name: Should not trigger no-tabs rules
community.windows.win_lineinfile:
path: some.txt
regexp: "^\t$"
line: string with \t inside
- name: Should not trigger inside jinja
vars:
Expand Down
1 change: 1 addition & 0 deletions requirements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ collections:
- community.docker
- community.general
- community.molecule
- community.windows
2 changes: 1 addition & 1 deletion src/ansiblelint/_mockings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _make_module_stub(module_name: str, options: Options) -> None:
path.mkdir(exist_ok=True, parents=True)
_write_module_stub(
filename=module_file,
name=module_file,
name=module_name,
namespace=namespace,
collection=collection,
)
Expand Down
20 changes: 17 additions & 3 deletions src/ansiblelint/rules/no_tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class NoTabsRule(AnsibleLintRule):
("lineinfile", "insertbefore"),
("lineinfile", "regexp"),
("lineinfile", "line"),
("win_lineinfile", "insertafter"),
("win_lineinfile", "insertbefore"),
("win_lineinfile", "regexp"),
("win_lineinfile", "line"),
("ansible.builtin.lineinfile", "insertafter"),
("ansible.builtin.lineinfile", "insertbefore"),
("ansible.builtin.lineinfile", "regexp"),
Expand All @@ -37,6 +41,10 @@ class NoTabsRule(AnsibleLintRule):
("ansible.legacy.lineinfile", "insertbefore"),
("ansible.legacy.lineinfile", "regexp"),
("ansible.legacy.lineinfile", "line"),
("community.windows.win_lineinfile", "insertafter"),
("community.windows.win_lineinfile", "insertbefore"),
("community.windows.win_lineinfile", "regexp"),
("community.windows.win_lineinfile", "line"),
]

def matchtask(
Expand Down Expand Up @@ -69,6 +77,12 @@ def test_no_tabs_rule(default_rules_collection: RulesCollection) -> None:
"examples/playbooks/rule-no-tabs.yml",
rules=default_rules_collection,
).run()
assert results[0].lineno == 10
assert results[0].message == NoTabsRule().shortdesc
assert len(results) == 2
expected_results = [
(10, NoTabsRule().shortdesc),
(13, NoTabsRule().shortdesc),
]
for i, expected in enumerate(expected_results):
assert len(results) >= i + 1
assert results[i].lineno == expected[0]
assert results[i].message == expected[1]
assert len(results) == len(expected), results
32 changes: 29 additions & 3 deletions src/ansiblelint/transformer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# cspell:ignore classinfo
"""Transformer implementation."""

from __future__ import annotations
Expand Down Expand Up @@ -33,6 +34,17 @@ class Transformer:
pre-requisite for the planned rule-specific transforms.
"""

DUMP_MSG = "Rewriting yaml file:"
FIX_NA_MSG = "Rule specific fix not available for:"
FIX_NE_MSG = "Rule specific fix not enabled for:"
FIX_APPLY_MSG = "Applying rule specific fix for:"
FIX_FAILED_MSG = "Rule specific fix failed for:"
FIX_ISSUE_MSG = (
"Please file an issue for this with the task or playbook that caused the error."
)
FIX_APPLIED_MSG = "Rule specific fix applied for:"
FIX_NOT_APPLIED_MSG = "Rule specific fix not applied for:"

def __init__(self, result: LintResult, options: Options):
"""Initialize a Transformer instance."""
self.write_set = self.effective_write_set(options.write_list)
Expand Down Expand Up @@ -113,7 +125,7 @@ def run(self) -> None:
self._do_transforms(file, ruamel_data or data, file_is_yaml, matches)

if file_is_yaml:
_logger.debug("Dumping %s using YAML (%s)", file, yaml.version)
_logger.debug("%s %s, version=%s", self.DUMP_MSG, file, yaml.version)
# noinspection PyUnboundLocalVariable
file.content = yaml.dumps(ruamel_data)

Expand All @@ -129,14 +141,16 @@ def _do_transforms(
) -> None:
"""Do Rule-Transforms handling any last-minute MatchError inspections."""
for match in sorted(matches):
match_id = f"{match.tag}/{match.match_type} {match.filename}:{match.lineno}"
if not isinstance(match.rule, TransformMixin):
logging.debug("%s %s", self.FIX_NA_MSG, match_id)
continue
if self.write_set != {"all"}:
rule = cast(AnsibleLintRule, match.rule)
rule_definition = set(rule.tags)
rule_definition.add(rule.id)
if rule_definition.isdisjoint(self.write_set):
# rule transform not requested. Skip it.
logging.debug("%s %s", self.FIX_NE_MSG, match_id)
continue
if file_is_yaml and not match.yaml_path:
data = cast(CommentedMap | CommentedSeq, data)
Expand All @@ -148,4 +162,16 @@ def _do_transforms(
"playbook",
):
match.yaml_path = get_path_to_task(file, match.lineno, data)
match.rule.transform(match, file, data)

logging.debug("%s %s", self.FIX_APPLY_MSG, match_id)
try:
match.rule.transform(match, file, data)
except Exception as exc: # pylint: disable=broad-except
_logger.error("%s %s", self.FIX_FAILED_MSG, match_id) # noqa: TRY400
_logger.exception(exc) # noqa: TRY401
_logger.error(self.FIX_ISSUE_MSG) # noqa: TRY400
continue
if match.fixed:
_logger.debug("%s %s", self.FIX_APPLIED_MSG, match_id)
else:
_logger.error("%s %s", self.FIX_NOT_APPLIED_MSG, match_id)
Loading

0 comments on commit 45c4900

Please sign in to comment.