From d81a27b9ee594e247ec760373924141ebaa0f18d Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 12 Apr 2023 08:11:17 +0100 Subject: [PATCH] name[casing]: Add transform to automatically fix this during --write (#3268) Signed-off-by: Markus Teufelberger Co-authored-by: Markus Teufelberger --- .github/workflows/tox.yml | 2 +- examples/playbooks/name-case.transformed.yml | 4 ++++ examples/playbooks/name-case.yml | 4 ++++ src/ansiblelint/rules/name.py | 20 ++++++++++++++++++-- test/test_transformer.py | 1 + 5 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 examples/playbooks/name-case.transformed.yml create mode 100644 examples/playbooks/name-case.yml diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 454567801c..d24ce77db3 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -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')" diff --git a/examples/playbooks/name-case.transformed.yml b/examples/playbooks/name-case.transformed.yml new file mode 100644 index 0000000000..03b8c46e8c --- /dev/null +++ b/examples/playbooks/name-case.transformed.yml @@ -0,0 +1,4 @@ +--- +- name: This lacks a capitalization + hosts: localhost + tasks: [] diff --git a/examples/playbooks/name-case.yml b/examples/playbooks/name-case.yml new file mode 100644 index 0000000000..5480d2c697 --- /dev/null +++ b/examples/playbooks/name-case.yml @@ -0,0 +1,4 @@ +--- +- name: this lacks a capitalization + hosts: localhost + tasks: [] diff --git a/src/ansiblelint/rules/name.py b/src/ansiblelint/rules/name.py index c0a640512a..f67a4f53cc 100644 --- a/src/ansiblelint/rules/name.py +++ b/src/ansiblelint/rules/name.py @@ -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" @@ -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 diff --git a/test/test_transformer.py b/test/test_transformer.py index 3ef812de34..d7853d296b 100644 --- a/test/test_transformer.py +++ b/test/test_transformer.py @@ -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