fix(dev): run ansible-lint against the clone's pinned collections from any shell
The hook scripts written by dev-setup.sh exported ANSIBLE_HOME and ANSIBLE_COLLECTIONS_PATH, so only a hook run through git commit saw the pinned collections; a bare pre-commit run fell back to the control host's ~/.ansible, the drift #118 removes. The ansible-lint hook now runs scripts/ansible-lint.sh, which uses .ansible/collections of the clone when ANSIBLE_COLLECTIONS_PATH is unset, fails with the fix when they are missing, and leaves a preset path (CI's /opt/collections) alone. dev-setup.sh keeps installing the collections and no longer edits the hook scripts beyond the PATH block it always wrote. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG
This commit is contained in:
1 parent
42ad8de88f
commit
bdb34cf98e
7 files changed
+123
-53
No files matched your search
@@ -0,0 +1,87 @@
|
||||
# SPDX-FileCopyrightText: Defame1297 and contributors
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Tests for scripts/ansible-lint.sh against a throwaway copy of the script and a stub ansible-lint.
|
||||
|
||||
Run with: python3 -m unittest discover -s tests/scripts
|
||||
"""
|
||||
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
SCRIPT = pathlib.Path(__file__).resolve().parents[2] / "scripts" / "ansible-lint.sh"
|
||||
|
||||
# Prints what the wrapper hands to ansible-lint: its arguments, directory and the two variables.
|
||||
STUB = """#!/bin/sh
|
||||
echo "args=$*"
|
||||
echo "cwd=$(pwd -P)"
|
||||
echo "home=${ANSIBLE_HOME-unset}"
|
||||
echo "path=${ANSIBLE_COLLECTIONS_PATH-unset}"
|
||||
"""
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which("bash"), "bash is required")
|
||||
class AnsibleLintWrapperTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = pathlib.Path(tempfile.mkdtemp(prefix="kamino-lintwrap-")).resolve()
|
||||
self.addCleanup(shutil.rmtree, self.tmp, True)
|
||||
self.repo = self.tmp / "repo"
|
||||
(self.repo / "scripts").mkdir(parents=True)
|
||||
shutil.copy(SCRIPT, self.repo / "scripts" / "ansible-lint.sh")
|
||||
self.bin = self.tmp / "bin"
|
||||
self.bin.mkdir()
|
||||
stub = self.bin / "ansible-lint"
|
||||
stub.write_text(STUB)
|
||||
stub.chmod(0o755)
|
||||
|
||||
def run_wrapper(self, cwd=None, **env):
|
||||
base = {"PATH": f"{self.bin}:{os.environ['PATH']}", "HOME": str(self.tmp)}
|
||||
return subprocess.run(
|
||||
["bash", str(self.repo / "scripts" / "ansible-lint.sh")],
|
||||
env={**base, **env},
|
||||
cwd=str(cwd or self.repo),
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
|
||||
def install_collections(self):
|
||||
(self.repo / ".ansible" / "collections" / "ansible_collections").mkdir(
|
||||
parents=True
|
||||
)
|
||||
|
||||
def test_uses_the_clone_collections_when_the_variable_is_unset(self):
|
||||
self.install_collections()
|
||||
result = self.run_wrapper()
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertIn("args=--offline --nocolor", result.stdout)
|
||||
self.assertIn(f"home={self.repo / '.ansible'}", result.stdout)
|
||||
self.assertIn(f"path={self.repo / '.ansible' / 'collections'}", result.stdout)
|
||||
|
||||
def test_leaves_a_preset_collections_path_alone(self):
|
||||
# CI sets it (the ci image bakes the collections into /opt/collections), even with none in the clone.
|
||||
result = self.run_wrapper(ANSIBLE_COLLECTIONS_PATH="/opt/collections")
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertIn("path=/opt/collections", result.stdout)
|
||||
self.assertIn("home=unset", result.stdout)
|
||||
|
||||
def test_fails_with_the_fix_when_the_collections_are_not_installed(self):
|
||||
result = self.run_wrapper()
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn("scripts/dev-setup.sh", result.stderr)
|
||||
self.assertNotIn("args=", result.stdout)
|
||||
|
||||
def test_runs_from_the_repository_root_whatever_the_caller_directory(self):
|
||||
self.install_collections()
|
||||
elsewhere = self.tmp / "elsewhere"
|
||||
elsewhere.mkdir()
|
||||
result = self.run_wrapper(cwd=elsewhere)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertIn(f"cwd={self.repo}", result.stdout)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -146,12 +146,6 @@ 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]:
|
||||
@@ -174,29 +168,6 @@ class DevSetupTest(unittest.TestCase):
|
||||
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)
|
||||
@@ -205,9 +176,6 @@ 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()
|
||||
|
||||
Reference in new issue
Block a user