#!/bin/bash # ============================================================================ # Run only the tests related to files changed vs HEAD (staged, unstaged and # untracked). Quiet: failures plus a one-line summary. Falls back to the full # suite when a change cannot be mapped to tests. Generated by repo_contract. # ============================================================================ set -euo pipefail cd "$(git rev-parse --show-toplevel)" CHANGED=() while IFS= read -r f; do [[ -n "$f" && -e "$f" ]] && CHANGED+=("$f") done < <({ git diff --name-only HEAD 2>/dev/null || true; git ls-files --others --exclude-standard; } | sort -u) if [[ -f pnpm-lock.yaml ]]; then PM=pnpm; elif [[ -f yarn.lock ]]; then PM=yarn; else PM=npm; fi full=0 src=() for f in "${CHANGED[@]}"; do case "$f" in package.json | pnpm-lock.yaml | yarn.lock | package-lock.json) full=1 ;; *.ts | *.tsx | *.js | *.jsx | *.mjs | *.cjs) src+=("$f") ;; esac done full_suite() { "$PM" test --silent; } if [[ $full -eq 0 && ${#CHANGED[@]} -eq 0 ]]; then echo "no changes vs HEAD: nothing to test"; exit 0; fi if [[ $full -eq 0 && ${#src[@]} -eq 0 ]]; then echo "no js/ts changes: nothing to test"; exit 0; fi if [[ $full -eq 0 ]] && grep -qs '"vitest"' package.json; then "$PM" exec vitest related --run --reporter=dot "${src[@]}"; exit $? fi if [[ $full -eq 0 ]] && grep -qs '"jest"' package.json; then "$PM" exec jest --findRelatedTests --silent "${src[@]}"; exit $? fi echo "cannot map changes to tests: running full suite" full_suite