From c87060a537ea6d90633ec9a5f221a725f49864c0 Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Mon, 29 Jan 2024 14:36:40 +0530 Subject: [PATCH] Fix scenario when role has no `dependencies` section in `meta/main.yml` (#3993) --- .config/dictionary.txt | 1 + .github/workflows/tox.yml | 2 +- .../roles/test-no-deps-role/meta/main.yml | 55 +++++++++++++++++++ src/ansiblelint/rules/role_name.py | 18 +++++- src/ansiblelint/schemas/__store__.json | 2 +- src/ansiblelint/schemas/rulebook.json | 42 ++++++++++++++ src/ansiblelint/testing/__init__.py | 1 + 7 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 examples/roles/test-no-deps-role/meta/main.yml diff --git a/.config/dictionary.txt b/.config/dictionary.txt index 32a104e0dc..6ee44f2f0e 100644 --- a/.config/dictionary.txt +++ b/.config/dictionary.txt @@ -1,5 +1,6 @@ Adrián Autobuild +audgirka CLICOLOR CODENOTIFY CODEOWNERS diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index bc2e5f2561..bbeb0be144 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: 851 + PYTEST_REQPASS: 852 steps: - uses: actions/checkout@v4 with: diff --git a/examples/roles/test-no-deps-role/meta/main.yml b/examples/roles/test-no-deps-role/meta/main.yml new file mode 100644 index 0000000000..1d80cc3261 --- /dev/null +++ b/examples/roles/test-no-deps-role/meta/main.yml @@ -0,0 +1,55 @@ +--- +galaxy_info: + author: audgirka + description: your role description + company: Red Hat + role_name: test_no_deps_role # if absent directory name hosting role is used instead + namespace: foo # if absent, author is used instead + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: GPL-2.0-or-later + + min_ansible_version: "2.1" + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. +# Skipping deps for testing scenario when no role deps are present +# dependencies: [] +# List your role dependencies here, one per line. Be sure to remove the '[]' above, +# if you add dependencies to this list. diff --git a/src/ansiblelint/rules/role_name.py b/src/ansiblelint/rules/role_name.py index 0212aa5a6d..f8d9723296 100644 --- a/src/ansiblelint/rules/role_name.py +++ b/src/ansiblelint/rules/role_name.py @@ -95,7 +95,7 @@ def matchyaml(self, file: Lintable) -> list[MatchError]: return result if file.kind == "meta": - for role in file.data["dependencies"]: + for role in file.data.get("dependencies", []): role_name = role["role"] if "/" in role_name: result.append( @@ -206,3 +206,19 @@ def test_role_deps_path_names( assert result.tag == expected_errors[idx][0] assert result.lineno == expected_errors[idx][1] assert len(results) == failure + + @pytest.mark.parametrize( + ("test_file", "failure"), + (pytest.param("examples/roles/test-no-deps-role", 0, id="no_deps"),), + ) + def test_role_no_deps( + default_rules_collection: RulesCollection, + test_file: str, + failure: int, + ) -> None: + """Test role if no dependencies are present in meta/main.yml.""" + results = Runner( + test_file, + rules=default_rules_collection, + ).run() + assert len(results) == failure diff --git a/src/ansiblelint/schemas/__store__.json b/src/ansiblelint/schemas/__store__.json index ad8dee0d3b..09a46ee036 100644 --- a/src/ansiblelint/schemas/__store__.json +++ b/src/ansiblelint/schemas/__store__.json @@ -48,7 +48,7 @@ "url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/role-arg-spec.json" }, "rulebook": { - "etag": "8a421671574fa65a57fb0d08e45879bfa005f271aa8c0243982a84b49fb0fe54", + "etag": "baba5774a46fcc2bc8c4a8c2f25b49df64a0856e415dbf601b0559f215e55968", "url": "https://raw.githubusercontent.com/ansible/ansible-rulebook/main/ansible_rulebook/schema/ruleset_schema.json" }, "tasks": { diff --git a/src/ansiblelint/schemas/rulebook.json b/src/ansiblelint/schemas/rulebook.json index 0f13a9daef..6321f08925 100644 --- a/src/ansiblelint/schemas/rulebook.json +++ b/src/ansiblelint/schemas/rulebook.json @@ -206,6 +206,9 @@ }, { "$ref": "#/$defs/shutdown-action" + }, + { + "$ref": "#/$defs/pg-notify-action" } ] } @@ -244,6 +247,9 @@ }, { "$ref": "#/$defs/shutdown-action" + }, + { + "$ref": "#/$defs/pg-notify-action" } ] } @@ -510,6 +516,42 @@ ], "additionalProperties": false }, + "pg-notify-action": { + "type": "object", + "properties": { + "pg_notify": { + "type": "object", + "properties": { + "dsn": { + "type": "string" + }, + "channel": { + "type": "string" + }, + "event": { + "type": [ + "string", + "object" + ] + }, + "remove_meta": { + "type": "boolean", + "default": false + } + }, + "required": [ + "dsn", + "channel", + "event" + ], + "additionalProperties": false + } + }, + "required": [ + "pg_notify" + ], + "additionalProperties": false + }, "post-event-action": { "type": "object", "properties": { diff --git a/src/ansiblelint/testing/__init__.py b/src/ansiblelint/testing/__init__.py index a19a1e0d1d..fe15c98264 100644 --- a/src/ansiblelint/testing/__init__.py +++ b/src/ansiblelint/testing/__init__.py @@ -155,4 +155,5 @@ def run_ansible_lint( cwd=cwd, env=_env, text=True, + encoding="utf-8", )