#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" GUARD_SOURCE="${ROOT_DIR}/scripts/check-ui-productization-coverage" RESOLVER_SOURCE="${ROOT_DIR}/scripts/resolve-feature-base" TEMP_ROOT="$(mktemp -d "${TMPDIR:-/tmp}/ui-productization-guard.XXXXXX")" FAILED=0 trap 'rm -rf "${TEMP_ROOT}"' EXIT write_file() { local path="$1" local content="$2" mkdir -p "$(dirname "${path}")" printf '%b' "${content}" > "${path}" } append_file() { local path="$1" local content="$2" mkdir -p "$(dirname "${path}")" printf '%b' "${content}" >> "${path}" } setup_repo() { local case_id="$1" local repo="${TEMP_ROOT}/${case_id}" mkdir -p "${repo}" git -C "${repo}" init -q git -C "${repo}" config user.email "guard-validation@example.test" git -C "${repo}" config user.name "Guard Validation" mkdir -p "${repo}/scripts" cp "${GUARD_SOURCE}" "${repo}/scripts/check-ui-productization-coverage" cp "${RESOLVER_SOURCE}" "${repo}/scripts/resolve-feature-base" chmod +x "${repo}/scripts/check-ui-productization-coverage" "${repo}/scripts/resolve-feature-base" write_file "${repo}/apps/platform/app/Filament/Dashboard.php" ""${output_file}" 2>&1 status=$? set -e local expected_status=0 [[ "${expected}" == "coverage-fail" ]] && expected_status=1 [[ "${expected}" == "resolution-fail" ]] && expected_status=2 if [[ ${status} -eq ${expected_status} ]]; then if [[ "${expected}" != "resolution-fail" ]] && ! grep -q '^branch_family=platform$' "${output_file}"; then FAILED=1 printf 'FAIL %s did not print resolver family diagnostics\n' "${case_id}" >&2 sed 's/^/ /' "${output_file}" >&2 return fi if [[ "${expected}" != "resolution-fail" ]] && ! grep -q '^selected_ref=platform-dev$' "${output_file}"; then FAILED=1 printf 'FAIL %s did not preserve the explicit family-matching ref\n' "${case_id}" >&2 sed 's/^/ /' "${output_file}" >&2 return fi if [[ "${expected}" != "resolution-fail" ]] && ! grep -q '^base_range=[0-9a-f]\{40\}\.\.\.HEAD$' "${output_file}"; then FAILED=1 printf 'FAIL %s did not print a merge-base-derived range\n' "${case_id}" >&2 sed 's/^/ /' "${output_file}" >&2 return fi printf 'PASS %s (%s)\n' "${case_id}" "${expected}" return fi FAILED=1 printf 'FAIL %s expected %s but exit status was %s\n' "${case_id}" "${expected}" "${status}" >&2 sed 's/^/ /' "${output_file}" >&2 } run_resolution_failure_before_diff_case() { local case_id="resolver_failure_stops_before_diff" local repo local status local output_file="${TEMP_ROOT}/${case_id}.out" local shim_directory="${TEMP_ROOT}/${case_id}-bin" local diff_marker="${TEMP_ROOT}/${case_id}.diff-ran" local real_git repo="$(setup_repo "${case_id}")" real_git="$(command -v git)" mkdir -p "${shim_directory}" write_file "${shim_directory}/git" '#!/usr/bin/env bash\nfor argument in "$@"; do\n if [[ "${argument}" == "diff" ]]; then\n : > "${DIFF_MARKER}"\n fi\ndone\nexec "${REAL_GIT}" "$@"\n' chmod +x "${shim_directory}/git" set +e ( cd "${repo}" export PATH="${shim_directory}:${PATH}" export REAL_GIT="${real_git}" export DIFF_MARKER="${diff_marker}" env \ -u TENANTATLAS_BRANCH_FAMILY \ -u TENANTATLAS_WORK_CLASSIFICATION \ -u TENANTATLAS_PR_TARGET \ -u TENANTATLAS_ACTIVE_SPEC \ -u TENANTATLAS_BASE_REF \ -u TENANTATLAS_REPO_SCOPE \ -u GITHUB_BASE_REF \ -u GITEA_BASE_REF \ bash scripts/check-ui-productization-coverage origin/website-dev --branch-family=platform ) >"${output_file}" 2>&1 status=$? set -e if [[ ${status} -eq 2 ]] \ && [[ ! -e "${diff_marker}" ]] \ && grep -q '^reason=wrong-family-ref$' "${output_file}" \ && grep -q '^diff_ran=false$' "${output_file}"; then printf 'PASS %s (resolution-fail)\n' "${case_id}" return fi FAILED=1 printf 'FAIL %s expected resolver exit 2 before any git diff\n' "${case_id}" >&2 sed 's/^/ /' "${output_file}" >&2 } run_case "ui_unchecked_template_heading_fails" "coverage-fail" "case_ui_unchecked_template_heading" run_case "ui_unchecked_spec_checkbox_fails" "coverage-fail" "case_ui_unchecked_spec_checkbox" run_case "ui_checked_impact_spec_passes" "pass" "case_ui_checked_impact_spec" run_case "ui_checked_no_impact_with_rationale_passes" "pass" "case_ui_checked_no_impact_with_rationale" run_case "ui_checked_no_impact_without_rationale_fails" "coverage-fail" "case_ui_checked_no_impact_without_rationale" run_case "ui_real_audit_coverage_artifact_passes" "pass" "case_ui_real_audit_coverage_artifact" run_case "navigation_provider_without_acknowledgment_fails" "coverage-fail" "case_navigation_provider_without_acknowledgment" run_case "navigation_provider_with_checked_acknowledgment_passes" "pass" "case_navigation_provider_with_checked_acknowledgment" run_case "backend_only_diff_passes" "pass" "case_backend_only_diff" run_case "docs_only_diff_passes" "pass" "case_docs_only_diff" run_case "committed_guarded_ui_without_acknowledgment_fails" "coverage-fail" "case_committed_guarded_ui_without_acknowledgment" run_case "staged_guarded_ui_without_acknowledgment_fails" "coverage-fail" "case_staged_guarded_ui_without_acknowledgment" run_case "unstaged_guarded_ui_without_acknowledgment_fails" "coverage-fail" "case_ui_unchecked_template_heading" run_case "untracked_guarded_ui_without_acknowledgment_fails" "coverage-fail" "case_untracked_guarded_ui_without_acknowledgment" run_resolution_failure_before_diff_case if [[ ${FAILED} -ne 0 ]]; then exit 1 fi printf 'All UI/Productization Coverage guard validation cases passed.\n'