diff --git a/tests/unit/python/utils/install/test_collections.py b/tests/unit/python/utils/install/test_collections.py index 572a80ebfe..7f552aa32e 100644 --- a/tests/unit/python/utils/install/test_collections.py +++ b/tests/unit/python/utils/install/test_collections.py @@ -1,10 +1,41 @@ import json +import subprocess +import sys import unittest from pathlib import Path from tempfile import TemporaryDirectory +from utils import PROJECT_ROOT +from utils.cache.yaml import load_yaml_any from utils.install.collections import declared_pins, unsatisfied +_REQUIREMENTS = ( + PROJECT_ROOT / "requirements" / "requirements.galaxy.yml", + PROJECT_ROOT / "requirements" / "requirements.git.yml", +) + +_BOOTSTRAP_PROBE = """ +import sys + + +class _NoPyYAML: + def find_spec(self, fullname, path=None, target=None): + if fullname == "yaml" or fullname.startswith("yaml."): + raise ImportError("PyYAML is absent during the lint bootstrap") + return None + + +sys.meta_path.insert(0, _NoPyYAML()) + +from pathlib import Path + +from utils.install.lint.ansible import collections as lint_collections +from utils.install.collections import declared_pins + +for fqcn, version in declared_pins(Path("requirements/requirements.galaxy.yml")): + print(fqcn, version) +""" + REQUIREMENTS = """--- collections: - name: community.general @@ -29,6 +60,36 @@ def _install(collections_dir: Path, fqcn: str, version: str) -> None: ) +class TestAgainstPyYAML(unittest.TestCase): + def test_the_hand_parser_agrees_with_pyyaml(self): + for path in _REQUIREMENTS: + with self.subTest(requirements=path.name): + declared = load_yaml_any(str(path), default_if_missing={}) or {} + expected = [ + (str(entry.get("name")), entry.get("version")) + for entry in declared.get("collections") or [] + if isinstance(entry, dict) and entry.get("name") + ] + expected = [ + (name, None if version is None else str(version)) + for name, version in expected + ] + + self.assertEqual(declared_pins(path), expected) + + def test_the_parser_runs_with_pyyaml_unimportable(self): + result = subprocess.run( + [sys.executable, "-c", _BOOTSTRAP_PROBE], + cwd=str(PROJECT_ROOT), + capture_output=True, + text=True, + check=False, + ) + + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn("community.general", result.stdout) + + class TestDeclaredPins(unittest.TestCase): def test_a_quoted_version_is_read_without_its_quotes(self): body = ( diff --git a/tests/unit/python/utils/update/test_galaxy.py b/tests/unit/python/utils/update/test_galaxy.py index 06f7a2789f..c05a02d292 100644 --- a/tests/unit/python/utils/update/test_galaxy.py +++ b/tests/unit/python/utils/update/test_galaxy.py @@ -28,8 +28,8 @@ collections: 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") + path = root / "requirements" / name + return path.read_text() # nocheck: cache-read -- rewritten by this test def _repo(root: Path) -> Path: diff --git a/utils/install/collections.py b/utils/install/collections.py index c6fd3cf6ba..08223a6616 100644 --- a/utils/install/collections.py +++ b/utils/install/collections.py @@ -15,6 +15,8 @@ import re import sys from pathlib import Path +from utils.cache.files import read_text + _NAME = re.compile(r"^\s*-\s+name:\s*(?P[A-Za-z0-9_.]+)\s*(?:#.*)?$") _VERSION = re.compile( r"^\s*version:\s*(?P[\"']?)(?P[^\s#\"']+)(?P=quote)\s*(?:#.*)?$" @@ -29,7 +31,7 @@ def declared_pins(requirements_file: Path) -> list[tuple[str, str | None]]: """ pins: list[tuple[str, str | None]] = [] try: - lines = requirements_file.read_text(encoding="utf-8").splitlines() + lines = read_text(str(requirements_file)).splitlines() except OSError: return pins @@ -57,7 +59,7 @@ def _installed_version(collections_dir: Path, namespace: str, name: str) -> str collections_dir / "ansible_collections" / namespace / name / "MANIFEST.json" ) try: - payload = json.loads(manifest.read_text(encoding="utf-8")) + payload = json.loads(read_text(str(manifest))) except (OSError, ValueError): return None version = payload.get("collection_info", {}).get("version") diff --git a/utils/update/collection_source.py b/utils/update/collection_source.py index 921faf6457..546ed3488e 100644 --- a/utils/update/collection_source.py +++ b/utils/update/collection_source.py @@ -57,9 +57,8 @@ def load_history(path: Path) -> list[dict[str, float | None]]: path: JSON file holding the rolling window. """ try: - payload = json.loads( - path.read_text(encoding="utf-8") - ) # nocheck: cache-read -- the history file is rewritten every run, a cached copy would go stale + raw = path.read_text() # nocheck: cache-read -- rewritten every run + payload = json.loads(raw) except (OSError, ValueError): return [] return payload if isinstance(payload, list) else []