Skip to content

Commit

Permalink
Fix auto capitalization for name[prefix] rule (#3922)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
audgirka and pre-commit-ci[bot] authored Dec 6, 2023
1 parent 179266c commit d16eb88
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 849
PYTEST_REQPASS: 850
steps:
- uses: actions/checkout@v4
with:
Expand Down
8 changes: 8 additions & 0 deletions examples/roles/name_prefix/tasks/test.transformed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: test | Not cap
ansible.builtin.debug:
msg: not cap

- name: test | Cap
ansible.builtin.debug:
msg: Cap
8 changes: 8 additions & 0 deletions examples/roles/name_prefix/tasks/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: test | not cap
ansible.builtin.debug:
msg: not cap

- name: test | Cap
ansible.builtin.debug:
msg: Cap
13 changes: 10 additions & 3 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,16 @@ def transform(
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:]}"
task_name = target_task["name"]
if "|" in task_name: # if using prefix
[file_name, update_task_name] = task_name.split("|")
target_task[
"name"
] = f"{file_name.strip()} | {update_task_name.strip()[:1].upper()}{update_task_name.strip()[1:]}"
else:
target_task[
"name"
] = f"{target_task['name'][:1].upper()}{target_task['name'][1:]}"
match.fixed = True


Expand Down
7 changes: 7 additions & 0 deletions test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ def fixture_runner_result(
True,
id="invalid_transform",
),
pytest.param(
"examples/roles/name_prefix/tasks/test.yml",
1,
True,
True,
id="name_case_with_prefix",
),
),
)
@mock.patch.dict(os.environ, {"ANSIBLE_LINT_WRITE_TMP": "1"}, clear=True)
Expand Down

0 comments on commit d16eb88

Please sign in to comment.