Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions scripts/affected-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
# scripts/affected-tests.sh list the suites covering the diff vs the base ref
# scripts/affected-tests.sh --run ... and run them, sequentially
# scripts/affected-tests.sh --run --jobs N ... N shell suites at a time
# scripts/affected-tests.sh --run --budget S ... timed against an S-second budget, a
# suite over its share failing (run-plugin-tests.sh
# BUDGET)
# scripts/affected-tests.sh --run --write-not-run FILE
# ... and write each selected suite it did not run
# to FILE, one per line (exit 3 names them); a
Expand Down Expand Up @@ -260,6 +263,8 @@ replay_range=""
against_ref=""
not_run_file=""
jobs=1
budget=""
budget_given=0
# Whether --shard was SUPPLIED, tracked apart from its value: `--shard=` with an
# empty right-hand side is what an unset environment variable produces, and it
# must not read as "no shard requested" and run everything on every leg.
Expand Down Expand Up @@ -312,6 +317,13 @@ while [[ $# -gt 0 ]]; do
shift
;;
--jobs=*) jobs="${1#--jobs=}" ;;
--budget)
need_value "$1" $# "${2:-}" "seconds, a positive integer"
budget="$2"
budget_given=1
shift
;;
--budget=*) budget="${1#--budget=}" budget_given=1 ;;
--base)
need_value "$1" $# "${2:-}" "a ref"
base_ref="$2"
Expand Down Expand Up @@ -367,6 +379,10 @@ if [[ ! "$jobs" =~ ^[1-9][0-9]*$ ]]; then
echo "error: --jobs wants a positive integer; got: $jobs" >&2
exit 2
fi
if [[ "$budget_given" -eq 1 && ! "$budget" =~ ^[1-9][0-9]*$ ]]; then
echo "error: --budget wants a positive number of seconds; got: $budget" >&2
exit 2
fi
if [[ "$shard_given" -eq 1 ]] && ! parse_shard "$shard_spec"; then
echo "error: --shard wants <index>/<total> with total >= 1 and 0 <= index < total; got: $shard_spec" >&2
exit 2
Expand Down Expand Up @@ -1597,7 +1613,8 @@ fi
# per-suite print lock and scripts/run-plugin-tests-serial.txt, the suites that
# must never overlap anything. Three is the proven ceiling on a 4-vCPU runner:
# at four, suites failed by producing empty output from an external command
# (#3694).
# (#3694). --budget S hands the selection there too, at any job count, since
# that runner times each suite against its share.
#
# Only *.test.sh is executable HERE. The other three ecosystems are run by their
# own lanes, with invocations this script cannot derive from a suite path
Expand Down Expand Up @@ -1625,11 +1642,11 @@ if venv_bin=$(pytest_python::venv_bin); then
fi

failed=0
if [[ ${#runnable[@]} -gt 0 ]] && [[ "$jobs" -gt 1 ]]; then
echo "Running ${#runnable[@]} selected shell suite(s) across up to $jobs job(s)." >&2
if [[ ${#runnable[@]} -gt 0 ]] && [[ "$jobs" -gt 1 || -n "$budget" ]]; then
echo "Running ${#runnable[@]} selected shell suite(s) across up to $jobs job(s)${budget:+ within a ${budget}s budget}." >&2
list="$WORK_DIR/selection.txt"
printf '%s\n' "${runnable[@]}" >"$list"
bash "$SCRIPT_DIR/run-plugin-tests.sh" --jobs "$jobs" --suites-from "$list" || failed=1
bash "$SCRIPT_DIR/run-plugin-tests.sh" --jobs "$jobs" ${budget:+--budget "$budget"} --suites-from "$list" || failed=1
elif [[ ${#runnable[@]} -gt 0 ]]; then
echo "Running ${#runnable[@]} selected shell suite(s) sequentially." >&2
n=0
Expand Down
23 changes: 23 additions & 0 deletions scripts/affected-tests.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,29 @@ for bad in 0 x '' -1; do
fi
done

# --budget hands the selection to run-plugin-tests.sh even at one job, since
# that runner owns the per-suite share.
out="$(cd "$repo" && MARKER_FILE="$marker" bash scripts/affected-tests.sh --run --budget 600 lib/widget.sh 2>&1)"
RC=$?
if [[ "$RC" -eq 0 ]] && contains "$out" 'within a 600s budget' && contains "$out" 's of the 600s budget.'; then
ok "--run --budget 600 runs the selection through run-plugin-tests.sh against the budget"
else
fail "--run --budget 600 did not run the selection against the budget (rc=$RC): $out"
fi
for bad in 0 x -1 '' '='; do
if [[ "$bad" == = ]]; then
(cd "$repo" && bash scripts/affected-tests.sh --run --budget= lib/widget.sh >/dev/null 2>&1)
else
(cd "$repo" && bash scripts/affected-tests.sh --run --budget "$bad" lib/widget.sh >/dev/null 2>&1)
fi
RC=$?
if [[ "$RC" -eq 2 ]]; then
ok "--budget '$bad' is a usage error (rc=2)"
else
fail "--budget '$bad' should exit 2, got rc=$RC"
fi
done

# --- --shard is a partition, not a filter -----------------------------------
# The claim CI makes when it fans one selection across four runners is that the
# legs together ARE the selection and no suite ran twice. Proved on the fixture
Expand Down
29 changes: 22 additions & 7 deletions scripts/check-affected.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
# once (default: $CHECK_AFFECTED_JOBS,
# else one less than the core count,
# at most 8)
# scripts/check-affected.sh --budget S time the selected shell suites against
# an S-second budget (default:
# $CHECK_AFFECTED_BUDGET, else 120); a
# suite over its share fails
# (scripts/run-plugin-tests.sh BUDGET)
# scripts/check-affected.sh --base <ref> diff against <ref> (default: origin/main)
# scripts/check-affected.sh --no-fetch skip refreshing <ref> before the
# run resolves it to a commit
Expand Down Expand Up @@ -57,7 +62,8 @@
# modes the pull_request run uses
# - changed-skills: scripts/check-changed-skills.sh <base>, when a skill
# file changed
# - tests (--tests): scripts/affected-tests.sh --run --base <base>, after
# - tests (--tests): scripts/affected-tests.sh --run --jobs <N>
# --budget <S> --base <base>, after
# scripts/setup-env.sh --node installs any missing or
# stale locked npm packages, then the selected Node
# suites of scripts/outside-node-packages.txt through
Expand Down Expand Up @@ -94,7 +100,7 @@
set -uo pipefail

usage() {
echo "usage: $(basename "$0") [--tests] [--jobs N] [--base <ref>] [--no-fetch] [--uncommitted]" >&2
echo "usage: $(basename "$0") [--tests] [--jobs N] [--budget S] [--base <ref>] [--no-fetch] [--uncommitted]" >&2
exit 2
}

Expand All @@ -105,11 +111,14 @@ run_tests=0
# (scripts/affected-tests.sh, #3694). At most 8: past 8 the wall time is the
# longest suite's (#10534), and on a host where several sessions run suites at
# once, 23 jobs failed suites that passed at 4 and 6 (#10719).
cores="$(nproc 2>/dev/null || echo 2)"
cores="$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2)"
[[ "$cores" =~ ^[1-9][0-9]*$ ]] || cores=2
jobs=$((cores > 1 ? cores - 1 : 1))
((jobs <= 8)) || jobs=8
jobs="${CHECK_AFFECTED_JOBS:-$jobs}"
[[ "$jobs" =~ ^[1-9][0-9]*$ ]] || jobs=1
budget="${CHECK_AFFECTED_BUDGET:-120}"
[[ "$budget" =~ ^[1-9][0-9]*$ ]] || budget=120
fetch=1
uncommitted=0
base=
Expand All @@ -124,6 +133,12 @@ while (($#)); do
shift
;;
--jobs=*) jobs="${1#--jobs=}" ;;
--budget)
[[ $# -ge 2 ]] || usage
budget="$2"
shift
;;
--budget=*) budget="${1#--budget=}" ;;
--base)
[[ $# -ge 2 && -n "$2" ]] || usage
base="$2"
Expand All @@ -134,7 +149,7 @@ while (($#)); do
esac
shift
done
[[ "$jobs" =~ ^[1-9][0-9]*$ ]] || usage
[[ "$jobs" =~ ^[1-9][0-9]*$ && "$budget" =~ ^[1-9][0-9]*$ ]] || usage

tools="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" || exit 2
top="$(git -C "${CHECK_DATA_ROOT:-$tools}" rev-parse --show-toplevel 2>/dev/null)" || {
Expand Down Expand Up @@ -198,7 +213,7 @@ if ((uncommitted)); then
# A shallow clone's boundary, so history walks stop where the clone does.
[[ ! -f "$shallow" ]] || cp "$shallow" "$snap/tree/.git/shallow" || exit 2
git -C "$snap/tree" -c core.hooksPath=/dev/null checkout --quiet --detach "$commit" || exit 2
args=(--base "$base" --no-fetch --jobs "$jobs")
args=(--base "$base" --no-fetch --jobs "$jobs" --budget "$budget")
((run_tests)) && args+=(--tests)
# A snapshot of this tree's own work tree is no foreign code, so the inner
# run's tests run here, where the selector reads the same uncommitted edits.
Expand Down Expand Up @@ -629,7 +644,7 @@ elif ((run_tests)); then
local -a pkgs=() exclusions=() node=() unrun=()
# Suites run only when the data tree is this tree, so CHECK_DATA_ROOT adds nothing here, and a suite
# that inherits it points its checkers at the real tree instead of its fixtures.
env -u CHECK_DATA_ROOT "$tools/scripts/affected-tests.sh" --run --jobs "$jobs" --base "$base" --write-not-run "$out.not-run" || rc=$?
env -u CHECK_DATA_ROOT "$tools/scripts/affected-tests.sh" --run --jobs "$jobs" --budget "$budget" --base "$base" --write-not-run "$out.not-run" || rc=$?
((rc == 3)) || return "$rc"
# shellcheck source=lib/read-list.sh
. "$tools/scripts/lib/read-list.sh" &&
Expand Down Expand Up @@ -677,7 +692,7 @@ elif ((run_tests)); then
if ((npm_failed)); then
skip tests "npm dependencies did not install"
else
run_check tests "scripts/affected-tests.sh --run --jobs $jobs --base $base" affected
run_check tests "scripts/affected-tests.sh --run --jobs $jobs --budget $budget --base $base" affected
fi
fi

Expand Down
12 changes: 12 additions & 0 deletions scripts/check-affected.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ check "a check that could not run is a skip, not a failure" \

run -- --base base --tests
check "--tests runs the affected suites" '[[ $RC == 0 && $OUT == *"passed"* ]]'
# A passing check's output is not printed, so the stub logs its arguments.
args_log="$tmp/affected-args.log"
run -u CHECK_AFFECTED_JOBS -u CHECK_AFFECTED_BUDGET STUB_ARGS_LOG="$args_log" -- --base base --tests
check "--tests passes a default job count of at most 8 and the 120 s budget" \
'[[ $RC == 0 && $(cat "$args_log") =~ ^"--run --jobs "[1-8]" --budget 120 " ]]'
run CHECK_AFFECTED_JOBS=5 CHECK_AFFECTED_BUDGET=300 STUB_ARGS_LOG="$args_log" -- --base base --tests
check "CHECK_AFFECTED_JOBS and CHECK_AFFECTED_BUDGET override the defaults" \
'[[ $RC == 0 && $(cat "$args_log") == "--run --jobs 5 --budget 300 "* ]]'
run STUB_ARGS_LOG="$args_log" -- --base base --tests --budget 45
check "--budget sets the budget passed to affected-tests" '[[ $RC == 0 && $(cat "$args_log") == *" --budget 45 "* ]]'
run CHECK_DATA_ROOT="$repo" -- --base base --tests
check "--tests keeps a caller's CHECK_DATA_ROOT from the suites" '[[ $RC == 0 && $ERR != *"saw CHECK_DATA_ROOT"* ]]'
run STUB_AFFECTED_TESTS=3 STUB_NOT_RUN=$'eco/test_x.py\neco/X.Tests.ps1\n' -- --base base --tests
Expand Down Expand Up @@ -590,6 +600,8 @@ run -- --bogus
check "an unknown flag is a usage error" '[[ $RC == 2 ]]'
run -- --jobs 0
check "a --jobs count below one is a usage error" '[[ $RC == 2 && $ERR == *"--jobs N"* ]]'
run -- --budget 0
check "a --budget below one second is a usage error" '[[ $RC == 2 && $ERR == *"--budget S"* ]]'
run -- --base no-such-ref
check "an unresolvable base is exit 2" '[[ $RC == 2 && $ERR == *"merge-base"* ]]'

Expand Down
76 changes: 72 additions & 4 deletions scripts/run-plugin-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# affected-suite selector, not through a step of its own.
#
# scripts/run-plugin-tests.sh [--strict-skips] [--jobs N] [--root DIR] [--shard I/N]
# [--suites-from FILE]
# [--suites-from FILE] [--budget SECONDS]
# scripts/run-plugin-tests.sh --list [--root DIR]
#
# Each test is self-contained and cwd-independent; an individual test SKIPs
Expand Down Expand Up @@ -37,6 +37,17 @@
# suite never starts late and outlasts the rest of its leg; a suite the file
# does not list counts 0 and keeps its sorted place after the listed ones.
#
# BUDGET. --budget SECONDS is the wall clock the whole run aims to finish in,
# for the local affected-only run (scripts/check-affected.sh --tests). A run
# over it reports the overrun and its three slowest suites, and does not fail.
# Each suite gets a share, and a suite that takes longer than its share fails
# by name: the share is its scripts/suite-seconds.txt median times
# $PLUGIN_TEST_SHARE_MARGIN (default 3), and never less than
# $PLUGIN_TEST_SHARE_DEFAULT seconds (default 30), the share of a suite the file
# does not list. The medians are CI runner measurements, so the margin covers a
# slower host and a loaded job pool. Without --budget no suite is timed against
# anything, which is how CI runs.
#
# SHARDING. --shard I/N keeps leg I of N of the sorted discovered suites, by
# index modulo N, with scripts/affected-tests.sh's semantics: the union of legs
# 0..N-1 is the whole corpus and no two legs share a suite, so N runners run it
Expand Down Expand Up @@ -102,7 +113,7 @@ SECONDS_LIST="${PLUGIN_TEST_SECONDS_LIST:-$script_dir/suite-seconds.txt}"
ADVISORY_LIST="${PLUGIN_TEST_ADVISORY_LIST:-$script_dir/run-plugin-tests-advisory.txt}"

usage() {
echo "usage: run-plugin-tests.sh [--strict-skips] [--jobs N] [--root DIR] [--shard I/N] [--suites-from FILE] | --list [--root DIR]" >&2
echo "usage: run-plugin-tests.sh [--strict-skips] [--jobs N] [--root DIR] [--shard I/N] [--suites-from FILE] [--budget SECONDS] | --list [--root DIR]" >&2
exit 2
}

Expand All @@ -121,8 +132,8 @@ parse_shard() {
}

# --worker <NNNNNN:suite>: internal. Runs ONE suite with its output captured
# under $PLUGIN_TEST_LOG_DIR, records its exit status beside the capture, then
# prints the whole block under the print lock. Reached only from the dispatch
# under $PLUGIN_TEST_LOG_DIR, records its exit status and wall seconds beside
# the capture, then prints the whole block under the print lock. Reached only from the dispatch
# below; it exits 0 regardless of the suite's result so xargs never stops
# early, and the summary reads the recorded status instead.
#
Expand All @@ -142,7 +153,9 @@ if [[ "${1:-}" == "--worker" ]]; then
log="$log_dir/$key"
rc=0
test_git_env::isolate "$log.gitconfig" || exit 2
start=$SECONDS
bash "$suite" >"$log.out" 2>&1 || rc=$?
printf '%s\n' "$((SECONDS - start))" >"$log.secs"
printf '%s\n' "$rc" >"$log.rc"
# noclobber opens the lock with O_EXCL inside bash itself. `mkdir` is not a
# safe lock everywhere: uutils mkdir (Ubuntu 25.10+) lets two racers both
Expand All @@ -164,10 +177,19 @@ jobs="${PLUGIN_TEST_JOBS:-1}"
root=""
shard_spec="0/1"
suites_from=""
budget=""
budget_given=0
list=0
while (($# > 0)); do
case "$1" in
--strict-skips) strict_skips=1 ;;
--budget)
[[ $# -ge 2 ]] || usage
budget="$2"
budget_given=1
shift
;;
--budget=*) budget="${1#--budget=}" budget_given=1 ;;
--list) list=1 ;;
--suites-from)
[[ $# -ge 2 ]] || usage
Expand Down Expand Up @@ -200,6 +222,16 @@ if [[ ! "$jobs" =~ ^[1-9][0-9]*$ ]]; then
echo "error: --jobs must be a positive integer (got '$jobs')" >&2
exit 2
fi
share_margin="${PLUGIN_TEST_SHARE_MARGIN:-3}"
share_default="${PLUGIN_TEST_SHARE_DEFAULT:-30}"
if ((budget_given)); then
for v in "--budget:$budget" "PLUGIN_TEST_SHARE_MARGIN:$share_margin" "PLUGIN_TEST_SHARE_DEFAULT:$share_default"; do
if [[ ! "${v#*:}" =~ ^[1-9][0-9]*$ ]]; then
echo "error: ${v%%:*} must be a positive integer (got '${v#*:}')" >&2
exit 2
fi
done
fi
if ! parse_shard "$shard_spec"; then
echo "error: --shard wants <index>/<total> with total >= 1 and 0 <= index < total (got '$shard_spec')" >&2
exit 2
Expand Down Expand Up @@ -319,6 +351,19 @@ if ((${#parallel_suites[@]} > 1)) && [[ -s "$SECONDS_LIST" ]]; then
sort -s -t $'\t' -k1,1nr | cut -f2-)
fi

# suite_share <suite>: its median times the margin, floored at the default.
declare -A median=()
if [[ -n "$budget" && -s "$SECONDS_LIST" ]]; then
while read -r path secs _; do
[[ "$path" == \#* || ! "$secs" =~ ^[0-9]+$ ]] || median["$path"]="$secs"
done <"$SECONDS_LIST"
fi
suite_share() {
local share=$((${median[$1]:-0} * share_margin))
((share > share_default)) || share="$share_default"
printf '%s' "$share"
}

log_dir="$(mktemp -d)"
trap 'rm -rf "$log_dir"' EXIT
export PLUGIN_TEST_LOG_DIR="$log_dir"
Expand All @@ -338,13 +383,15 @@ if ((${#is_advisory[@]} > 0)); then
echo "Advisory nonce: $advisory_nonce"
fi

run_start=$SECONDS
for t in ${serial_suites[@]+"${serial_suites[@]}"}; do
bash "$runner" --worker "$t"
done
if [[ ${#parallel_suites[@]} -gt 0 ]]; then
# xargs appends one indexed suite path after `--worker` per invocation.
printf '%s\0' "${parallel_suites[@]}" | xargs -0 -n 1 -P "$jobs" bash "$runner" --worker
fi
run_seconds=$((SECONDS - run_start))

# suite_annotate <error|warning> <suite> <message>: one annotation titled by the
# suite, so a log reader can group results by suite. Escapes follow actions/toolkit
Expand All @@ -368,6 +415,7 @@ advisory_failed=0
total_optional_skips=0
total_discriminating_skips=0
skipped_suites=()
timings=()

for i in "${!tests[@]}"; do
t="${tests[$i]}"
Expand All @@ -386,6 +434,16 @@ for i in "${!tests[@]}"; do
suite_annotate error "$t" "$t failed (exit $rc)"
failed=1
fi
if [[ -n "$budget" ]]; then
secs="$(<"$log.secs")"
timings+=("$secs"$'\t'"$t")
share="$(suite_share "$t")"
if ((secs > share)); then
echo "FAIL: $t took ${secs}s, over its ${share}s share of the budget" >&2
suite_annotate error "$t" "$t took ${secs}s, over its ${share}s share of the budget"
failed=1
fi
fi
opt_skips="$(grep -c '^SKIP:' "$log.out" 2>/dev/null || true)"
disc_skips="$(grep -c '^DISCRIMINATING SKIP:' "$log.out" 2>/dev/null || true)"
total_optional_skips=$((total_optional_skips + opt_skips))
Expand All @@ -408,6 +466,16 @@ if ((${#skipped_suites[@]} > 0)); then
done
fi

if [[ -n "$budget" ]] && ((run_seconds > budget)); then
{
echo "OVER BUDGET: the suites took ${run_seconds}s, over the ${budget}s budget. Slowest:"
printf '%s\n' ${timings[@]+"${timings[@]}"} | sort -t $'\t' -k1,1nr | head -n 3 |
while IFS=$'\t' read -r secs t; do echo " ${secs}s $t"; done
} >&2
elif [[ -n "$budget" ]]; then
echo "The suites took ${run_seconds}s of the ${budget}s budget."
fi

if ((total_discriminating_skips > 0)); then
{
echo "DISCRIMINATING SKIP(s) mean discriminating coverage did not run —"
Expand Down
Loading
Loading