Skip to content

Commit

Permalink
name[casing]: Add transform to automatically fix this during --write (#…
Browse files Browse the repository at this point in the history
…3268)

Signed-off-by: Markus Teufelberger <mteufelberger@mgit.at>
Co-authored-by: Markus Teufelberger <mteufelberger@mgit.at>
  • Loading branch information
ssbarnea and Markus Teufelberger authored Apr 12, 2023
1 parent 2a787ab commit d81a27b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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: 793
PYTEST_REQPASS: 794
steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
4 changes: 4 additions & 0 deletions examples/playbooks/name-case.transformed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- name: This lacks a capitalization
hosts: localhost
tasks: []
4 changes: 4 additions & 0 deletions examples/playbooks/name-case.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- name: this lacks a capitalization
hosts: localhost
tasks: []
20 changes: 18 additions & 2 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

from ansiblelint.constants import LINE_NUMBER_KEY
from ansiblelint.errors import MatchError
from ansiblelint.rules import AnsibleLintRule
from ansiblelint.rules import AnsibleLintRule, TransformMixin

if TYPE_CHECKING:
from ruamel.yaml.comments import CommentedMap, CommentedSeq

from ansiblelint.file_utils import Lintable # noqa: F811


class NameRule(AnsibleLintRule):
class NameRule(AnsibleLintRule, TransformMixin):
"""Rule for checking task and play names."""

id = "name"
Expand Down Expand Up @@ -141,6 +143,20 @@ def _check_name(
)
return results

def transform(
self,
match: MatchError,
lintable: Lintable,
data: CommentedMap | CommentedSeq | str,
) -> None:
if match.tag == "name[casing]":
target_task = self.seek(match.yaml_path, data)
# Not using capitalize(), since that rewrites the rest of the name to lower case
target_task[
"name"
] = f"{target_task['name'][:1].upper()}{target_task['name'][1:]}"
match.fixed = True


if "pytest" in sys.modules: # noqa: C901
from ansiblelint.config import options
Expand Down
1 change: 1 addition & 0 deletions test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def fixture_runner_result(
"examples/playbooks/vars/empty_vars.yml", 0, False, id="empty_vars"
),
pytest.param("examples/playbooks/vars/strings.yml", 0, True, id="strings"),
pytest.param("examples/playbooks/name-case.yml", 1, True, id="name_case"),
),
)
def test_transformer( # pylint: disable=too-many-arguments, too-many-locals
Expand Down

0 comments on commit d81a27b

Please sign in to comment.