Files
Kamino/scripts/ansible-test-sanity.sh
Defame1297andClaude Sonnet 5.5 574c0d00f9 ci(scripts): add a wrapper that runs ansible-test sanity on HEAD
ansible-test only runs a collection from an ansible_collections/<namespace>/<name> directory, so scripts/ansible-test-sanity.sh copies HEAD (git archive: the committed files only) into a temporary one, applies the Build job's ignore-file step and runs ansible-test sanity --requirements, passing extra arguments through. The sanity tests carry their own pylint, pep8 and shellcheck settings and also scan the Python under tests/, which ruff and the other hooks do not (the Build check failed on a name only that pylint disallows). The hook that runs it before a push follows in the next commit.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG
2026-11-14 15:49:51 +00:00

61 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# The ansible-test sanity pre-push hook: the Build job's sanity run, on the commit being pushed.
#
# ansible-test only runs a collection from an ansible_collections/<namespace>/<name> directory,
# and a checkout is not one. So this copies HEAD (git archive: the committed files, no untracked
# or unstaged changes, no .git) into a temporary one, as the Build job does with a git worktree,
# and applies the same ignore-file step. ruff and the other hooks cannot stand in for it: the
# sanity tests carry their own pylint, pep8 and shellcheck settings (ansible-test also scans
# the Python under tests/).
#
# Extra arguments go to ansible-test, e.g. `scripts/ansible-test-sanity.sh --test pylint`.
# The first run builds the sanity virtualenvs (minutes, and it needs the network); later ones
# reuse them. HEAD is what runs, so a push of another branch than the checked-out one checks
# the wrong commit.
set -euo pipefail
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$root"
for tool in ansible ansible-test; do
if ! command -v "$tool" >/dev/null 2>&1; then
echo "error: $tool is not on PATH; run scripts/dev-setup.sh and use its virtualenv (.venv/bin)" >&2
exit 1
fi
done
# The Collection's own name, from galaxy.yml, so a rename needs no edit here.
namespace="$(sed -n 's/^namespace: *//p' galaxy.yml)"
name="$(sed -n 's/^name: *//p' galaxy.yml)"
if [ -z "$namespace" ] || [ -z "$name" ]; then
echo "error: no namespace and name in $root/galaxy.yml" >&2
exit 1
fi
core="$(ansible --version | sed -n 's/^ansible \[core \([0-9][0-9]*\.[0-9][0-9]*\).*/\1/p' | head -n 1)"
if [ -z "$core" ]; then
echo "error: cannot read the ansible-core version from 'ansible --version'" >&2
exit 1
fi
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
dest="$work/ansible_collections/$namespace/$name"
mkdir -p "$dest"
git archive HEAD | tar -x -C "$dest"
cd "$dest"
# ansible-test reads tests/sanity/ignore-<core MAJOR.MINOR>.txt and has no wildcard. The committed
# ignore-common.txt applies to every core version: it is copied to the file name of the installed
# core here, in this copy only (plus the lines of a committed ignore-<MAJOR.MINOR>.txt, for a
# finding of one version only). The Build job does the same.
ignore="tests/sanity/ignore-${core}.txt"
{
cat tests/sanity/ignore-common.txt
if [ -f "$ignore" ]; then cat "$ignore"; fi
} >"$work/ignore.new"
mv "$work/ignore.new" "$ignore"
# Not exec: the trap above has to run when ansible-test ends.
ansible-test sanity --requirements "$@"