From f69805173e54ba81efd48ff63ed8c6e23d401a9b Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Thu, 9 Mar 2023 17:27:29 +0000 Subject: [PATCH] Implement galaxy[no-runtime] check for meta/runtime.yml file (#3162) --- .github/workflows/tox.yml | 2 +- examples/collection/galaxy.yml | 4 +- examples/collection/meta/runtime.yml | 0 .../fail/meta/runtime.yml | 0 .../pass/meta/runtime.yml | 0 examples/meta/meta/runtime.yml | 0 examples/no_changelog/meta/runtime.yml | 0 src/ansiblelint/rules/galaxy.md | 3 + src/ansiblelint/rules/galaxy.py | 86 +++++++++++-------- test/schemas/test/meta/runtime.yml | 1 + 10 files changed, 55 insertions(+), 41 deletions(-) create mode 100644 examples/collection/meta/runtime.yml create mode 100644 examples/galaxy_no_required_tags/fail/meta/runtime.yml create mode 100644 examples/galaxy_no_required_tags/pass/meta/runtime.yml create mode 100644 examples/meta/meta/runtime.yml create mode 100644 examples/no_changelog/meta/runtime.yml create mode 100644 test/schemas/test/meta/runtime.yml diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index e31170e72c..2c35b1f84f 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -70,7 +70,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: 801 + PYTEST_REQPASS: 802 steps: - name: Activate WSL1 if: "contains(matrix.shell, 'wsl')" diff --git a/examples/collection/galaxy.yml b/examples/collection/galaxy.yml index e5094868da..d21efb2402 100644 --- a/examples/collection/galaxy.yml +++ b/examples/collection/galaxy.yml @@ -11,7 +11,7 @@ dependencies: other_namespace.collection2: ">=2.0.0,<3.0.0" anderson55.my_collection: "*" # note: "*" selects the highest version available license: - - GPL # <-- invalid license values based on galaxy schema - - Apache + - GPL # <-- invalid license value based on galaxy schema, a value like GPL-3.0-or-later would work + - Apache-2.0 repository: some-url tags: [networking, test_tag] diff --git a/examples/collection/meta/runtime.yml b/examples/collection/meta/runtime.yml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/galaxy_no_required_tags/fail/meta/runtime.yml b/examples/galaxy_no_required_tags/fail/meta/runtime.yml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/galaxy_no_required_tags/pass/meta/runtime.yml b/examples/galaxy_no_required_tags/pass/meta/runtime.yml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/meta/meta/runtime.yml b/examples/meta/meta/runtime.yml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/no_changelog/meta/runtime.yml b/examples/no_changelog/meta/runtime.yml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/ansiblelint/rules/galaxy.md b/src/ansiblelint/rules/galaxy.md index b974d87541..df4f66ad61 100644 --- a/src/ansiblelint/rules/galaxy.md +++ b/src/ansiblelint/rules/galaxy.md @@ -20,6 +20,9 @@ This rule can produce messages such: equal to `1.0.0` - `galaxy[no-changelog]` - collection is missing a changelog file in expected locations. +- `galaxy[no-runtime]` - Please add a + [meta/runtime.yml](https://docs.ansible.com/ansible/latest/dev_guide/developing_collections_structurehtml#meta-directory-and-runtime-yml) + file. - `galaxy[tags]` - `galaxy.yaml` must have one of the required tags: `application`, `cloud`, `database`, `infrastructure`, `linux`, `monitoring`, `networking`, `security`, `storage`, `tools`, `windows`. diff --git a/src/ansiblelint/rules/galaxy.py b/src/ansiblelint/rules/galaxy.py index cdaf4ed610..69a8330845 100644 --- a/src/ansiblelint/rules/galaxy.py +++ b/src/ansiblelint/rules/galaxy.py @@ -109,6 +109,15 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]: ) ) + if not os.path.isfile(os.path.join(base_path, "meta/runtime.yml")): + results.append( + self.create_matcherror( + message="meta/runtime.yml file not found.", + tag="galaxy[no-runtime]", + filename=file, + ) + ) + return results @@ -178,23 +187,6 @@ def test_galaxy_no_collection_version() -> None: errs = bad_runner.run() assert len(errs) == 1 - def test_changelog_present() -> None: - """Positive test for finding a changelog.""" - collection = RulesCollection() - collection.register(GalaxyRule()) - good_runner = Runner("examples/collection/galaxy.yml", rules=collection) - assert [] == good_runner.run() - - def test_changelog_missing() -> None: - """Negative test for finding a changelog.""" - collection = RulesCollection() - collection.register(GalaxyRule()) - bad_runner = Runner("examples/no_changelog/galaxy.yml", rules=collection) - result = bad_runner.run() - assert len(result) == 1 - for item in result: - assert item.tag == "galaxy[no-changelog]" - def test_version_class() -> None: """Test for version class.""" v = Version("1.0.0") @@ -209,24 +201,42 @@ def test_coerce() -> None: with pytest.raises(NotImplementedError, match=expected): _coerce(type(Version)) - def test_galaxy_tags_pass() -> None: - """Test for required tags.""" - collection = RulesCollection() - collection.register(GalaxyRule()) - bad_runner = Runner( - "examples/galaxy_no_required_tags/pass/galaxy.yml", rules=collection - ) - result = bad_runner.run() - assert len(result) == 0 - - def test_galaxy_tags_fail() -> None: - """Test for required tags.""" - collection = RulesCollection() - collection.register(GalaxyRule()) - bad_runner = Runner( - "examples/galaxy_no_required_tags/fail/galaxy.yml", rules=collection - ) - result = bad_runner.run() - assert len(result) == 1 - for item in result: - assert item.tag == "galaxy[tags]" + @pytest.mark.parametrize( + ("file", "expected"), + ( + pytest.param( + "examples/galaxy_no_required_tags/fail/galaxy.yml", + ["galaxy[tags]"], + id="tags", + ), + pytest.param( + "examples/galaxy_no_required_tags/pass/galaxy.yml", + [], + id="pass", + ), + pytest.param( + "examples/collection/galaxy.yml", + ["schema[galaxy]"], + id="schema", + ), + pytest.param( + "examples/no_changelog/galaxy.yml", + ["galaxy[no-changelog]"], + id="no-changelog", + ), + pytest.param( + "examples/no_collection_version/galaxy.yml", + ["schema[galaxy]", "galaxy[version-missing]"], + id="no-collection-version", + ), + ), + ) + def test_galaxy_rule( + default_rules_collection: RulesCollection, file: str, expected: list[str] + ) -> None: + """Validate that rule works as intended.""" + results = Runner(file, rules=default_rules_collection).run() + + assert len(results) == len(expected) + for index, result in enumerate(results): + assert result.tag == expected[index] diff --git a/test/schemas/test/meta/runtime.yml b/test/schemas/test/meta/runtime.yml new file mode 100644 index 0000000000..6a992c4443 --- /dev/null +++ b/test/schemas/test/meta/runtime.yml @@ -0,0 +1 @@ +requires_ansible: ">=2.12,<2.14"