#!/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// 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-.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-.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 "$@"