Files
computer-playbook/tests/unit/python/utils/update/test_galaxy.py
T
kevinveenbirkenbachandClaude Opus 5 94baeb7502 fix(lint): meet the repository rules the collection work broke
The gate flagged six of my own changes. INFINITO_ANSIBLE_COLLECTIONS_SOURCE
reached default.env without a handler, so the generator dropped it
silently; ansible.sh then carried a shell-side default for the same key,
a second source that drifts. Both are now one declaration, read as
required, so a missing key fails loudly instead of falling back.

File reads route through the session cache except where the same run
rewrites the file, which is the documented opt-out and now says so per
call. The updater reads yaml through utils.cache.yaml rather than calling
safe_load itself.

restore_collection_history.sh was committed without its executable bit,
and the new workflow's name was 31 characters where the dispatch menu
truncates at 25; the explanatory wording lives in run-name instead.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-16 21:47:10 +02:00

128 lines
4.2 KiB
Python

import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
from utils.update.galaxy import apply_updates, collect_pins, find_outdated_updates
GALAXY = """---
collections:
- name: community.general
version: 13.2.0
- name: kewlfft.aur
version: 0.13.0
"""
GIT = """---
collections:
- name: community.general
source: https://github.com/ansible-collections/community.general.git
type: git
version: 13.2.0
- name: kewlfft.aur
source: https://github.com/kewlfft/ansible-aur.git
type: git
version: master
"""
def _read(root: Path, name: str) -> str:
# nocheck: cache-read -- apply_updates rewrote this file inside the same test
return (root / "requirements" / name).read_text(encoding="utf-8")
def _repo(root: Path) -> Path:
(root / "requirements").mkdir()
(root / "requirements" / "requirements.galaxy.yml").write_text(
GALAXY, encoding="utf-8"
)
(root / "requirements" / "requirements.git.yml").write_text(GIT, encoding="utf-8")
return root
class TestCollectPins(unittest.TestCase):
def test_only_collections_pinned_to_a_semver_in_both_files_are_collected(self):
with TemporaryDirectory() as tmp:
root = _repo(Path(tmp))
pins = collect_pins(root)
self.assertEqual([pin.fqcn for pin in pins], ["community.general"])
def test_a_version_the_two_files_disagree_on_is_not_collected(self):
with TemporaryDirectory() as tmp:
root = _repo(Path(tmp))
(root / "requirements" / "requirements.galaxy.yml").write_text(
GALAXY.replace("13.2.0", "13.4.0"), encoding="utf-8"
)
self.assertEqual(collect_pins(root), [])
class TestFindOutdatedUpdates(unittest.TestCase):
def test_a_higher_tag_of_the_same_depth_is_an_update(self):
with TemporaryDirectory() as tmp:
root = _repo(Path(tmp))
with patch(
"utils.update.galaxy.git_ls_remote_tags",
return_value=["13.1.0", "13.2.0", "13.4.0"],
):
updates = find_outdated_updates(root)
self.assertEqual([u.latest for u in updates], ["13.4.0"])
def test_the_newest_tag_already_pinned_is_no_update(self):
with TemporaryDirectory() as tmp:
root = _repo(Path(tmp))
with patch(
"utils.update.galaxy.git_ls_remote_tags",
return_value=["13.1.0", "13.2.0"],
):
self.assertEqual(find_outdated_updates(root), [])
def test_a_tag_of_a_different_depth_is_not_a_candidate(self):
with TemporaryDirectory() as tmp:
root = _repo(Path(tmp))
with patch(
"utils.update.galaxy.git_ls_remote_tags",
return_value=["13.2.0", "14"],
):
self.assertEqual(find_outdated_updates(root), [])
class TestApplyUpdates(unittest.TestCase):
def test_both_requirement_files_are_bumped_together(self):
with TemporaryDirectory() as tmp:
root = _repo(Path(tmp))
with patch(
"utils.update.galaxy.git_ls_remote_tags",
return_value=["13.4.0"],
):
updates = find_outdated_updates(root)
changed = apply_updates(root, updates)
self.assertEqual(len(changed), 2)
galaxy_text = _read(root, "requirements.galaxy.yml")
git_text = _read(root, "requirements.git.yml")
self.assertIn("version: 13.4.0", galaxy_text)
self.assertIn("version: 13.4.0", git_text)
self.assertNotIn("13.2.0", galaxy_text)
self.assertNotIn("13.2.0", git_text)
def test_an_entry_outside_the_update_keeps_its_version(self):
with TemporaryDirectory() as tmp:
root = _repo(Path(tmp))
with patch(
"utils.update.galaxy.git_ls_remote_tags",
return_value=["13.4.0"],
):
apply_updates(root, find_outdated_updates(root))
self.assertIn("version: master", _read(root, "requirements.git.yml"))
self.assertIn("version: 0.13.0", _read(root, "requirements.galaxy.yml"))
if __name__ == "__main__":
unittest.main()