10000 name[casing]: Add transform to automatically fix this during --write … · ansible/ansible-lint@d81a27b · GitHub
[go: up one dir, main page]

Skip to content

Commit d81a27b

Browse files
ssbarneaMarkus Teufelberger
andauthored
name[casing]: Add transform to automatically fix this during --write (#3268)
Signed-off-by: Markus Teufelberger <mteufelberger@mgit.at> Co-authored-by: Markus Teufelberger <mteufelberger@mgit.at>
1 parent 2a787ab commit d81a27b

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
6060
# Number of expected test passes, safety measure for accidental skip of
6161
# tests. Update value if you add/remove tests.
62-
PYTEST_REQPASS: 793
62+
PYTEST_REQPASS: 794
6363
steps:
6464
- name: Activate WSL1
6565
if: "contains(matrix.shell, 'wsl')"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
- name: This lacks a capitalization
3+
hosts: localhost
4+
tasks: []

examples/playbooks/name-case.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
- name: this lacks a capitalization
3+
hosts: localhost
4+
tasks: []

src/ansiblelint/rules/name.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
from ansiblelint.constants import LINE_NUMBER_KEY
1010
from ansiblelint.errors import MatchError
11-
from ansiblelint.rules import AnsibleLintRule
11+
from ansiblelint.rules import AnsibleLintRule, TransformMixin
1212

1313
if TYPE_CHECKING:
14+
from ruamel.yaml.comments import CommentedMap, CommentedSeq
15+
1416
from ansiblelint.file_utils import Lintable # noqa: F811
1517

1618

17-
class NameRule(AnsibleLintRule):
19+
class NameRule(AnsibleLintRule, TransformMixin):
1820
"""Rule for checking task and play names."""
1921

2022
id = "name"
@@ -141,6 +143,20 @@ def _check_name(
141143
)
142144
return results
143145

146+
def transform(
147+
self,
148+
match: MatchError,
149+
lintable: Lintable,
150+
data: CommentedMap | CommentedSeq | str,
151+
) -> None:
152+
if match.tag == "name[casing]":
153+
target_task = self.seek(match.yaml_path, data)
154+
# Not using capitalize(), since that rewrites the rest of the name to lower case
155+
target_task[
156+
"name"
157+
] = f"{target_task['name'][:1].upper()}{target_task['name'][1:]}"
158+
match.fixed = True
159+
144160

145161
if "pytest" in sys.modules: # noqa: C901
146162
from ansiblelint.config import options

test/test_transformer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def fixture_runner_result(
6464
"examples/playbooks/vars/empty_vars.yml", 0, False, id="empty_vars"
6565
),
6666
pytest.param("examples/playbooks/vars/strings.yml", 0, True, id="strings"),
67+
pytest.param("examples/playbooks/name-case.yml", 1, True, id="name_case"),
6768
),
6869
)
6970
def test_transformer( # pylint: disable=too-many-arguments, too-many-locals

0 commit comments

Comments
 (0)
0