Skip to content

Commit

Permalink
Fix key error for name[casing] rule (#3987)
Browse files Browse the repository at this point in the history
  • Loading branch information
audgirka authored Jan 18, 2024
1 parent f45acb2 commit e53aa16
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 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: 850
PYTEST_REQPASS: 851
steps:
- uses: actions/checkout@v4
with:
Expand Down
15 changes: 15 additions & 0 deletions examples/roles/name_casing/tasks/main.transformed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: Test nested tasks within block and always
block:
- name: Test1
ansible.builtin.debug:
msg: Foo

- name: Test2
ansible.builtin.debug:
msg: Bar

always:
- name: From always block to be auto fixed as name[casing] scenario
ansible.builtin.debug:
msg: Baz
15 changes: 15 additions & 0 deletions examples/roles/name_casing/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: Test nested tasks within block and always
block:
- name: test1
ansible.builtin.debug:
msg: Foo

- name: Test2
ansible.builtin.debug:
msg: Bar

always:
- name: from always block to be auto fixed as name[casing] scenario
ansible.builtin.debug:
msg: Baz
21 changes: 10 additions & 11 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +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
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
task_name = target_task.get("name", None)
if 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"{task_name[:1].upper()}{task_name[1:]}"
match.fixed = True


if "pytest" in sys.modules:
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/schemas/__store__.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json"
},
"meta": {
"etag": "097a20155bc7936b6eae292a556bb38202d34a0a333ff5cbaaa1b4d3a4cc7bf5",
"etag": "fb58deb0f5f2a3b77ba298764f74bc6e3bd38a761f368a50bb285042f6260354",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/meta.json"
},
"meta-runtime": {
Expand Down
2 changes: 2 additions & 0 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ def _get_path_to_task_in_nested_tasks_block(
continue
next_task_key = task_keys_by_index.get(task_index + 1, None)
if next_task_key is not None:
if task.lc.data[next_task_key][2] < lineno:
continue
next_task_key_line_index = task.lc.data[next_task_key][0]
else:
next_task_key_line_index = None
Expand Down
9 changes: 8 additions & 1 deletion test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,14 @@ def fixture_runner_result(
1,
True,
True,
id="name_case_with_prefix",
id="name_casing_prefix",
),
pytest.param(
"examples/roles/name_casing/tasks/main.yml",
2,
True,
True,
id="name_case",
),
),
)
Expand Down

0 comments on commit e53aa16

Please sign in to comment.