Skip to content

Commit

Permalink
Improved coverage for empty string compare and galaxy rules (#2963)
Browse files Browse the repository at this point in the history
Co-authored-by: Ajinkya Udgirkar <[email protected]>
Co-authored-by: Sorin Sbarnea <[email protected]>
  • Loading branch information
3 people committed Feb 1, 2023
1 parent f79a9bc commit ffefdd1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 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: 771
PYTEST_REQPASS: 774

steps:
- name: Activate WSL1
Expand Down
13 changes: 13 additions & 0 deletions examples/no_collection_version/galaxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
name: galaxy_no_version
namespace: test
readme: ../README.md
authors:
- John
description: your collection description
license:
- GPL
- Apache

dependencies: {}
repository: http:https://example.com/repository
8 changes: 8 additions & 0 deletions src/ansiblelint/rules/empty_string_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def matchtask(
/sbin/shutdown -t now
echo $var == ""
when: ansible_os_family
- name: Shut down
shell: |
/sbin/shutdown -t now
echo $var == ""
when: [ansible_os_family]
"""

FAIL_PLAY = """
Expand All @@ -75,6 +80,9 @@ def matchtask(
- name: Shut down
command: /sbin/shutdown -t now
when: ansible_os_family !=""
- name: Shut down
command: /sbin/shutdown -t now
when: False
"""

@pytest.mark.parametrize(
Expand Down
38 changes: 34 additions & 4 deletions src/ansiblelint/rules/galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from functools import total_ordering
from typing import TYPE_CHECKING, Any

import pytest

from ansiblelint.constants import LINE_NUMBER_KEY
from ansiblelint.errors import MatchError
from ansiblelint.rules import AnsibleLintRule
Expand Down Expand Up @@ -39,6 +41,10 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
filename=file,
)
)
# returning here as it does not make sense
# to continue for version check below
return results

version = data.get("version")
if Version(version) < Version("1.0.0"):
results.append(
Expand Down Expand Up @@ -88,16 +94,18 @@ def __init__(self, version_string: str):

def __eq__(self, other: object) -> bool:
"""Implement equality comparison."""
other = _coerce(other)
if not isinstance(other, Version):
try:
other = _coerce(other)
except NotImplementedError:
return NotImplemented

return self.components == other.components

def __lt__(self, other: Version) -> bool:
"""Implement lower-than operation."""
other = _coerce(other)
if not isinstance(other, Version):
try:
other = _coerce(other)
except NotImplementedError:
return NotImplemented

return self.components < other.components
Expand Down Expand Up @@ -135,6 +143,15 @@ def test_galaxy_collection_version_negative() -> None:
errs = bad_runner.run()
assert len(errs) == 1

def test_galaxy_no_collection_version() -> None:
"""Test for no collection version in galaxy."""
collection = RulesCollection()
collection.register(GalaxyRule())
failure = "examples/no_collection_version/galaxy.yml"
bad_runner = Runner(failure, rules=collection)
errs = bad_runner.run()
assert len(errs) == 1

def test_changelog_present() -> None:
"""Positive test for finding a changelog."""
collection = RulesCollection()
Expand All @@ -151,3 +168,16 @@ def test_changelog_missing() -> None:
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")
assert v == Version("1.0.0")

def test_coerce() -> None:
"""Test for _coerce function."""
assert _coerce("1.0") == Version("1.0")
assert _coerce(1.0) == Version("1.0")
expected = "Unable to coerce object type"
with pytest.raises(NotImplementedError, match=expected):
_coerce(type(Version))

0 comments on commit ffefdd1

Please sign in to comment.