build(dev): install the pinned collections in dev-setup

dev-setup.sh installs requirements.yml into .ansible/collections of the clone and
exports ANSIBLE_HOME and ANSIBLE_COLLECTIONS_PATH from the hook scripts, so local
ansible-lint resolves the collection versions CI does and not the control host's
~/.ansible (#118, part of #80). The install scopes the path to its target so a
copy elsewhere is not skipped as already installed. A hook written by the previous
version gains only the new block.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG
This commit is contained in:
Defame1297andClaude Sonnet 5.5 committed 2026-11-14 13:52:09 +00:00
1 parent 12fe748bdd
commit bb57077fb3
3 files changed
+96 -9

No files matched your search

+63 -1
View File
@@ -28,7 +28,8 @@ if [ "$1" = "-m" ] && [ "$2" = "venv" ]; then
cp "$STUB_DIR/python" "$3/bin/python"
cp "$STUB_DIR/pip" "$3/bin/pip"
cp "$STUB_DIR/pre-commit" "$3/bin/pre-commit"
chmod +x "$3/bin/python" "$3/bin/pip" "$3/bin/pre-commit"
cp "$STUB_DIR/ansible-galaxy" "$3/bin/ansible-galaxy"
chmod +x "$3/bin/python" "$3/bin/pip" "$3/bin/pre-commit" "$3/bin/ansible-galaxy"
exit 0
fi
exit 2
@@ -36,6 +37,13 @@ exit 2
PIP_STUB = "#!/bin/sh\nexit 0\n"
# Records the collections path and the arguments of each call, one line per call.
GALAXY_STUB = """#!/bin/sh
printf '%s|%s\\n' "${ANSIBLE_COLLECTIONS_PATH:-}" "$*" >> "$STUB_DIR/galaxy.log"
if [ "${STUB_GALAXY_FAIL:-}" = "1" ]; then echo "stub galaxy failure" >&2; exit 1; fi
exit 0
"""
# Mimics `pre-commit install`: refuses when core.hooksPath has any value, otherwise writes
# the two hooks into the repository's hooks directory. The hook body prints PATH.
PRECOMMIT_STUB = """#!/bin/sh
@@ -67,6 +75,7 @@ class DevSetupTest(unittest.TestCase):
("python", PYTHON_STUB),
("pip", PIP_STUB),
("pre-commit", PRECOMMIT_STUB),
("ansible-galaxy", GALAXY_STUB),
):
path = self.stubs / name
path.write_text(body)
@@ -101,6 +110,7 @@ class DevSetupTest(unittest.TestCase):
(repo / "scripts").mkdir(parents=True)
shutil.copy(SCRIPT, repo / "scripts" / "dev-setup.sh")
(repo / "requirements-dev.txt").write_text("")
(repo / "requirements.yml").write_text("---\ncollections: []\n")
self.git(repo, "init", "--quiet")
return repo
@@ -136,8 +146,57 @@ class DevSetupTest(unittest.TestCase):
for name in ("pre-commit", "commit-msg"):
text = self.hook(repo, name).read_text()
self.assertIn("# kamino-dev-setup: virtualenv first on PATH", text)
self.assertIn("# kamino-dev-setup: collections from this clone", text)
self.assertIn(f"export ANSIBLE_HOME={repo / '.ansible'}", text)
self.assertIn(
f"export ANSIBLE_COLLECTIONS_PATH={repo / '.ansible' / 'collections'}",
text,
)
self.assertEqual(self.hooks_z(repo), b"")
def galaxy_calls(self) -> list[str]:
return (self.stubs / "galaxy.log").read_text().splitlines()
def test_collections_are_installed_from_requirements_into_the_clone(self) -> None:
repo = self.make_repo()
self.assertEqual(self.run_setup(repo).returncode, 0)
target = repo / ".ansible" / "collections"
# The path is scoped to the target for the install, so a copy elsewhere is not skipped.
self.assertEqual(
self.galaxy_calls(),
[f"{target}|collection install -r requirements.yml -p {target}"],
)
def test_failed_collection_install_fails_the_setup(self) -> None:
repo = self.make_repo()
result = self.run_setup(repo, STUB_GALAXY_FAIL="1")
self.assertNotEqual(result.returncode, 0)
self.assertIn("stub galaxy failure", result.stderr)
self.assertNotIn("kamino-dev-setup", self.hook(repo).read_text())
def test_hook_of_the_previous_version_gains_only_the_collections_path(
self,
) -> None:
repo = self.make_repo()
self.assertEqual(self.run_setup(repo).returncode, 0)
hook = self.hook(repo)
# What the previous version of the script wrote: the PATH block, no collections block.
lines = hook.read_text().splitlines()
old = [
line
for line in lines
if "collections from this clone" not in line
and "ANSIBLE_COLLECTIONS_PATH" not in line
and "ANSIBLE_HOME" not in line
]
hook.write_text("\n".join(old) + "\n")
self.assertEqual(self.run_setup(repo).returncode, 0)
text = hook.read_text()
self.assertEqual(text.count("kamino-dev-setup: virtualenv first"), 1)
self.assertEqual(text.count("kamino-dev-setup: collections from this clone"), 1)
self.assertEqual(text.count("export ANSIBLE_COLLECTIONS_PATH="), 1)
self.assertEqual(text.count("export ANSIBLE_HOME="), 1)
def test_rerun_is_idempotent(self) -> None:
repo = self.make_repo()
self.assertEqual(self.run_setup(repo).returncode, 0)
@@ -146,6 +205,9 @@ class DevSetupTest(unittest.TestCase):
self.assertEqual(second_run.returncode, 0, second_run.stderr)
self.assertEqual(self.hook(repo).read_text(), first)
self.assertEqual(first.count("kamino-dev-setup: virtualenv first"), 1)
self.assertEqual(
first.count("kamino-dev-setup: collections from this clone"), 1
)
def test_own_absolute_hooks_path_is_lifted_and_restored(self) -> None:
repo = self.make_repo()