#!/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 script_dir="$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" repo_root="$script_dir" while [[ "$repo_root" != "/" && ! -f "$repo_root/tech-profile.yaml" && ! -d "$repo_root/04_generated_documentation" ]]; do repo_root="$(dirname "$repo_root")" done if [[ "$repo_root" == "/" ]]; then repo_root="$(CDPATH= cd -- "$script_dir/../.." && pwd)" fi workspace_dir="$repo_root/05_implementation_workspace" bruno_root="$workspace_dir" env_name="dev" scope="" usage() { printf 'Usage: %s --scope [--env ]\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" # Detect configured framework and test runner from tech-profile.yaml backend_framework="fastapi" backend_test_framework="pytest" if [[ -f "$repo_root/tech-profile.yaml" ]]; then read -r backend_framework backend_test_framework < <(python3 -c " import yaml try: with open('$repo_root/tech-profile.yaml') as f: cfg = yaml.safe_load(f) or {} st = cfg.get('stack', {}).get('backend', {}) print(st.get('framework', 'fastapi'), st.get('test_framework', 'pytest')) except Exception: print('fastapi pytest') " 2>/dev/null || echo "fastapi pytest") fi printf -- '-- detected stack: framework=%s test_runner=%s\n' "$backend_framework" "$backend_test_framework" # ---- Backend Test Runner Dispatch ---- if [[ "$backend_framework" == "frappe" || "$backend_test_framework" == "frappe_test_runner" ]]; then # ---- Frappe Framework Test Runner ---- frappe_found=0 frappe_site="${FRAPPE_SITE:-site1.local}" if command -v bench >/dev/null 2>&1; then frappe_found=1 printf -- '-- frappe: executing bench run-tests (site=%s)\n' "$frappe_site" if [[ "$scope" == "project" ]]; then bench --site "$frappe_site" run-tests else bench --site "$frappe_site" run-tests --module "$pytest_marker" 2>/dev/null || \ bench --site "$frappe_site" run-tests --doctype "$upper_hyphenated" 2>/dev/null || \ bench --site "$frappe_site" run-tests fi elif [[ -d "$workspace_dir" ]] && command -v pytest >/dev/null 2>&1; then # Fallback to pytest if python unit tests exist in workspace while IFS= read -r -d '' tests_dir; do frappe_found=1 printf -- '-- pytest (frappe python tests): %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 [[ "$frappe_found" -eq 0 ]]; then printf -- '-- frappe: bench CLI not found in PATH or no tests directory under %s yet (skipped)\n' "$workspace_dir" fi elif [[ "$backend_framework" == "spring_boot" || "$backend_test_framework" == "junit5" ]]; then # ---- Java / Spring Boot Test Runner (Maven / Gradle) ---- java_test_found=0 if [[ -f "$workspace_dir/mvnw" ]] || command -v mvn >/dev/null 2>&1; then java_test_found=1 mvn_cmd="$workspace_dir/mvnw" [[ -x "$mvn_cmd" ]] || mvn_cmd="mvn" printf -- '-- spring_boot: executing %s test\n' "$mvn_cmd" if [[ "$scope" == "project" ]]; then (cd "$workspace_dir" && "$mvn_cmd" test) else (cd "$workspace_dir" && "$mvn_cmd" test -Dtest="*${upper_hyphenated}*") fi elif [[ -f "$workspace_dir/gradlew" ]] || command -v gradle >/dev/null 2>&1; then java_test_found=1 gradle_cmd="$workspace_dir/gradlew" [[ -x "$gradle_cmd" ]] || gradle_cmd="gradle" printf -- '-- spring_boot: executing %s test\n' "$gradle_cmd" (cd "$workspace_dir" && "$gradle_cmd" test) fi if [[ "$java_test_found" -eq 0 ]]; then printf -- '-- spring_boot: no mvnw/gradlew found under %s yet (skipped)\n' "$workspace_dir" fi else # ---- Default Pytest / Python Unittest (FastAPI, Django, etc.) ---- 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) elif [[ -f "$workspace_dir/tests/run_all_tests.py" ]]; then pytest_found=1 printf -- '-- python unittest runner: %s/tests/run_all_tests.py\n' "$workspace_dir" python3 "$workspace_dir/tests/run_all_tests.py" fi if [[ "$pytest_found" -eq 0 ]]; then printf -- '-- pytest: no tests/ directory found under %s yet (skipped)\n' "$workspace_dir" fi 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'