From d16eb88bba922be204333d34e23f3be8b92147a7 Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Wed, 6 Dec 2023 14:30:24 +0530 Subject: [PATCH] Fix auto capitalization for name[prefix] rule (#3922) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .github/workflows/tox.yml | 2 +- .../roles/name_prefix/tasks/test.transformed.yml | 8 ++++++++ examples/roles/name_prefix/tasks/test.yml | 8 ++++++++ src/ansiblelint/rules/name.py | 13 ++++++++++--- test/test_transformer.py | 7 +++++++ 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 examples/roles/name_prefix/tasks/test.transformed.yml create mode 100644 examples/roles/name_prefix/tasks/test.yml diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 0c15f43e16..43f97f1967 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -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: diff --git a/examples/roles/name_prefix/tasks/test.transformed.yml b/examples/roles/name_prefix/tasks/test.transformed.yml new file mode 100644 index 0000000000..eb6e116b6f --- /dev/null +++ b/examples/roles/name_prefix/tasks/test.transformed.yml @@ -0,0 +1,8 @@ +--- +- name: test | Not cap + ansible.builtin.debug: + msg: not cap + +- name: test | Cap + ansible.builtin.debug: + msg: Cap diff --git a/examples/roles/name_prefix/tasks/test.yml b/examples/roles/name_prefix/tasks/test.yml new file mode 100644 index 0000000000..679332a81e --- /dev/null +++ b/examples/roles/name_prefix/tasks/test.yml @@ -0,0 +1,8 @@ +--- +- name: test | not cap + ansible.builtin.debug: + msg: not cap + +- name: test | Cap + ansible.builtin.debug: + msg: Cap diff --git a/src/ansiblelint/rules/name.py b/src/ansiblelint/rules/name.py index 57d6689b3c..fe2ace9572 100644 --- a/src/ansiblelint/rules/name.py +++ b/src/ansiblelint/rules/name.py @@ -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 diff --git a/test/test_transformer.py b/test/test_transformer.py index 759f9cbf80..2be1c09073 100644 --- a/test/test_transformer.py +++ b/test/test_transformer.py @@ -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)