Skip to content

Commit

Permalink
Implement galaxy[no-runtime] check for meta/runtime.yml file (#3162)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Mar 9, 2023
1 parent 4af51fc commit f698051
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')"
Expand Down
4 changes: 2 additions & 2 deletions examples/collection/galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Empty file.
Empty file.
Empty file.
Empty file added examples/meta/meta/runtime.yml
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions src/ansiblelint/rules/galaxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
86 changes: 48 additions & 38 deletions src/ansiblelint/rules/galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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")
Expand All @@ -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]
1 change: 1 addition & 0 deletions test/schemas/test/meta/runtime.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requires_ansible: ">=2.12,<2.14"

0 comments on commit f698051

Please sign in to comment.