c_agri_trade/scripts/run_tests.sh

152 lines
5.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Multi-level test runner (UP-AI v1.0, process doc §8.4).
#
# Dispatches to pytest / Playwright / Bruno underneath one command surface,
# scoped to the whole project, one iteration, one use case, or one FR.
# Tagging convention (07_test_design/test_strategy.md §5.4):
# pytest -> marker named after the lowercased, underscored identifier
# (UC-011 -> uc_011, FR-CBC-004 -> fr_cbc_004, STORY-042 -> story_042,
# iteration-02 -> iteration_02)
# Playwright -> literal @-prefixed token in the spec's title/describe string
# (UC-011 -> @UC-011)
# Bruno -> a `tags` array in each request/folder's `meta` block; this
# script greps .bru files for a matching tag and runs only
# the matched files, since the Bruno CLI has no native tag filter.
#
# Usage:
# scripts/run_tests.sh --scope project
# scripts/run_tests.sh --scope iteration-02
# scripts/run_tests.sh --scope uc-011
# scripts/run_tests.sh --scope fr-cbc-004
#
# `--scope iteration-NN` re-runs the cumulative regression suite for every
# already-Delivered FR in addition to that iteration's own tests, per Gate
# 14.1's regression-suite-health criterion — it does not filter pytest/
# Playwright/Bruno any differently from a plain iteration tag; the
# regression-suite-wide re-run is achieved by iteration tags also being
# applied to every regression case folded in from a prior iteration
# (07_test_design/regression_test_cases.md records which iteration a
# regression case is tagged with in addition to its origin iteration).
set -euo pipefail
repo_root="$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
workspace_dir="$repo_root/05_implementation_workspace"
bruno_root="$workspace_dir"
env_name="dev"
scope=""
usage() {
printf 'Usage: %s --scope <project|iteration-NN|uc-xxx|fr-xxx> [--env <dev|uat|ci>]\n' "$0" >&2
exit 1
}
while [[ $# -gt 0 ]]; do
case "$1" in
--scope)
scope="${2:-}"
shift 2
;;
--env)
env_name="${2:-dev}"
shift 2
;;
-h|--help)
usage
;;
*)
printf 'Unknown argument: %s\n' "$1" >&2
usage
;;
esac
done
[[ -n "$scope" ]] || usage
# Resolve scope into a pytest marker name, a Playwright @-tag, and a Bruno
# grep token. "project" means "run everything, no filter."
pytest_marker=""
playwright_tag=""
bruno_tag=""
if [[ "$scope" != "project" ]]; then
# normalize: lowercase, hyphens -> underscores for the pytest marker;
# uppercase-with-hyphens for the Playwright/Bruno @-tag.
lower_underscored="$(printf '%s' "$scope" | tr '[:upper:]' '[:lower:]' | tr '-' '_')"
upper_hyphenated="$(printf '%s' "$scope" | tr '[:lower:]' '[:upper:]')"
pytest_marker="$lower_underscored"
playwright_tag="@${upper_hyphenated}"
bruno_tag="${upper_hyphenated}"
fi
printf '== run_tests.sh: scope=%s env=%s ==\n' "$scope" "$env_name"
# ---- pytest (backend unit/integration) ----
pytest_found=0
if command -v pytest >/dev/null 2>&1; then
while IFS= read -r -d '' tests_dir; do
pytest_found=1
printf -- '-- pytest: %s\n' "$tests_dir"
if [[ "$scope" == "project" ]]; then
pytest "$tests_dir"
else
pytest "$tests_dir" -m "$pytest_marker"
fi
done < <(find "$workspace_dir" -type d -name tests -not -path '*/node_modules/*' -not -path '*/.venv/*' -print0 2>/dev/null)
fi
if [[ "$pytest_found" -eq 0 ]]; then
printf -- '-- pytest: no tests/ directory found under %s yet (skipped)\n' "$workspace_dir"
fi
# ---- Playwright (browser E2E / smoke) ----
playwright_found=0
while IFS= read -r -d '' config; do
playwright_found=1
project_dir="$(dirname "$config")"
printf -- '-- playwright: %s\n' "$config"
(
cd "$project_dir"
if [[ "$scope" == "project" ]]; then
npx playwright test
else
npx playwright test --grep "$playwright_tag"
fi
)
done < <(find "$workspace_dir" -maxdepth 3 \( -name 'playwright.config.ts' -o -name 'playwright.config.js' \) -not -path '*/node_modules/*' -print0 2>/dev/null)
if [[ "$playwright_found" -eq 0 ]]; then
printf -- '-- playwright: no playwright.config.* found under %s yet (skipped)\n' "$workspace_dir"
fi
# ---- Bruno (API collections) ----
bruno_found=0
bruno_files=()
while IFS= read -r -d '' bru_file; do
if [[ "$scope" == "project" ]]; then
bruno_files+=("$bru_file")
elif grep -q "$bruno_tag" "$bru_file" 2>/dev/null; then
bruno_files+=("$bru_file")
fi
done < <(find "$bruno_root" -type f -name '*.bru' -path '*/bruno/*' -print0 2>/dev/null)
if [[ "${#bruno_files[@]}" -gt 0 ]]; then
bruno_found=1
if ! command -v bru >/dev/null 2>&1; then
printf -- '-- bruno: %d matching .bru file(s) found but the `bru` CLI is not installed (skipped)\n' "${#bruno_files[@]}"
else
declare -A bruno_folders_seen=()
for bru_file in "${bruno_files[@]}"; do
folder="$(dirname "$bru_file")"
if [[ -z "${bruno_folders_seen[$folder]:-}" ]]; then
bruno_folders_seen["$folder"]=1
printf -- '-- bruno: %s (env=%s)\n' "$folder" "$env_name"
bru run "$folder" --env "$env_name"
fi
done
fi
fi
if [[ "$bruno_found" -eq 0 ]]; then
printf -- '-- bruno: no matching .bru collections found under %s yet (skipped)\n' "$bruno_root"
fi
printf '== run_tests.sh: done ==\n'