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
3 changes: 3 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ jobs:
- name: Identify-changes tests
run: ./scripts/quality-gate/fixtures-identify-changes/run.sh

- name: Unexpected-exit tests
run: ./scripts/quality-gate/fixtures-unexpected-exit/run.sh

- name: Mark-API-shipped tests
run: ./scripts/quality-gate/fixtures-mark-api/run.sh

Expand Down
5 changes: 3 additions & 2 deletions scripts/quality-gate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Unit tests are the `Unit` and `Contract` categories. Packing also validates each
| `--bypass-format` | stage 1b without failing |
| `--overwrite-baseline` | also saves lower warning counts to the baseline |

Exit codes: 0 pass, 1 fail, 2 incomplete, 3 usage or environment error. The report is `<dir>/.gate/report.json`, with logs in `<dir>/.gate/logs/`. A failed build lists its errors; in CI, a failed gate job keeps `.gate/` as an artifact.
Exit codes: 0 pass, 1 fail, 2 incomplete, 3 usage or environment error. Any other stop is a bug in the gate: it prints the command that failed and writes no report. The report is `<dir>/.gate/report.json`, with logs in `<dir>/.gate/logs/`. A failed build lists its errors; in CI, a failed gate job keeps `.gate/` as an artifact.

## Configuration

Expand All @@ -65,7 +65,7 @@ An intended API break is accepted with the `CompatibilitySuppressions.xml` from
| `build-and-test.yml` | Gate (unit) | `--unit`; on a PR, compares coverage with master's; on master, saves coverage and the packages to release |
| `build-and-test.yml` | Gate (E2E) | starts a local Supabase while building, then `--e2e` |
| `build-and-test.yml` | Waiting for the test suite to finish | passes when both parts pass; the check to require |
| `build-and-test.yml` | Quality gate tests | the five test suites below, when `scripts/quality-gate/` changes |
| `build-and-test.yml` | Quality gate tests | the six test suites below, when `scripts/quality-gate/` changes |
| `mutation.yml` | Mutation testing | Stryker on changed files, then `--mutation` |
| `release.yml` | Publish tested packages | pushes the packages the gate built for the released commit |

Expand All @@ -79,6 +79,7 @@ An intended API break is accepted with the `CompatibilitySuppressions.xml` from
./scripts/quality-gate/fixtures-compare-baselines/run.sh
./scripts/quality-gate/fixtures-compare-coverage/run.sh
./scripts/quality-gate/fixtures-identify-changes/run.sh
./scripts/quality-gate/fixtures-unexpected-exit/run.sh
./scripts/quality-gate/fixtures-mark-api/run.sh
```

Expand Down
85 changes: 85 additions & 0 deletions scripts/quality-gate/fixtures-unexpected-exit/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env bash
set -uo pipefail

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LIB="$HERE/../lib"
GATE="$HERE/../gate.sh"
RUNROOT="${FIXTURE_RUNROOT:-${TMPDIR:-/tmp}/unexpected-exit-tests.$$}"

if [[ -t 1 && -z "${NO_COLOR:-}" ]]; then
RED=$'\033[31m'; GRN=$'\033[32m'; DIM=$'\033[2m'; BLD=$'\033[1m'; OFF=$'\033[0m'
else RED=""; GRN=""; DIM=""; BLD=""; OFF=""; fi

mkdir -p "$RUNROOT"
trap '[[ -n "${FIXTURE_KEEP:-}" ]] || rm -rf "$RUNROOT"' EXIT

PASS=0; FAIL=0

script() {
cat <<SCRIPT
set -euo pipefail
source "$LIB/common.sh"; source "$LIB/report.sh"
trap 'report_unexpected_exit \$? "\$BASH_COMMAND"' EXIT
NO_COLOR=1
$1
SCRIPT
}

check() {
local name="$1" want_rc="$2" want_text="$3" refuse_text="$4" rc="$5" out="$6"
local miss=""
[[ "$rc" == "$want_rc" ]] || miss+="exit $rc, expected $want_rc; "
[[ -z "$want_text" ]] || grep -qF -- "$want_text" "$out" || miss+="no '$want_text'; "
[[ -z "$refuse_text" ]] || ! grep -qF -- "$refuse_text" "$out" || miss+="unexpected '$refuse_text'; "
if [[ -z "$miss" ]]; then
printf " ${GRN}PASS${OFF} %s\n" "$name"; PASS=$((PASS+1))
else
printf " ${RED}FAIL${OFF} %s\n ${DIM}%s output: %s${OFF}\n" "$name" "$miss" "$(tr '\n' ' ' <"$out")"
FAIL=$((FAIL+1))
fi
}

library_case() {
local name="$1" body="$2" want_rc="$3" want_text="$4" refuse_text="$5"
local out="$RUNROOT/${name// /-}.log"
bash <(script "$body") >"$out" 2>&1
check "$name" "$want_rc" "$want_text" "$refuse_text" "$?" "$out"
}

gate_case() {
local name="$1" want_rc="$2" want_text="$3" refuse_text="$4"; shift 4
local out="$RUNROOT/${name// /-}.log"
bash "$GATE" "$@" >"$out" 2>&1
check "$name" "$want_rc" "$want_text" "$refuse_text" "$?" "$out"
}

library_case "crash in a function names the command and the last stage" \
'add 1a "Build" blocking "Core" PASS "ok" ""
broken() { local v; v="$(jq -r .a <<<"{bad")"; }
broken' \
5 'stopped unexpectedly (exit 5) on: v="$(jq -r .a' ''
library_case "crash names the last stage reported" \
'add 1a "Build" blocking "Core" PASS "ok" ""
false' \
1 'The last stage it reported: Build (Core).' ''
library_case "crash before any stage" \
'false' \
1 'No report was written: this is a bug in the gate.' 'The last stage'
library_case "tolerated failure stays quiet" \
'code=$(false) || code=""
GATE_EXITED_ON_PURPOSE=1; exit 1' \
1 '' 'stopped unexpectedly'
library_case "die prints only its message" \
'die "no .sln at /repo"' \
3 'no .sln at /repo' 'stopped unexpectedly'
gate_case "gate: unknown option prints only the usage error" \
3 'unknown option: --nope' 'stopped unexpectedly' --nope
gate_case "gate: --help stays quiet" \
0 'Usage: gate.sh' 'stopped unexpectedly' --help

echo
if [[ $FAIL -eq 0 ]]; then
echo "${GRN}${BLD}cases: $PASS/$((PASS+FAIL)) passed${OFF}"; exit 0
else
echo "${RED}${BLD}cases: $FAIL/$((PASS+FAIL)) failed${OFF}"; exit 1
fi
13 changes: 8 additions & 5 deletions scripts/quality-gate/gate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ source "$SELF_DIR/lib/warnings.sh"
source "$SELF_DIR/lib/coverage.sh"
source "$SELF_DIR/lib/stages.sh"

trap 'report_unexpected_exit $? "$BASH_COMMAND"' EXIT

usage() {
cat <<'EOF'
Usage: gate.sh [dir | project.csproj] [options]
Expand Down Expand Up @@ -38,23 +40,23 @@ while [[ $# -gt 0 ]]; do
--unit) MODE="unit"; shift ;;
--e2e) MODE="e2e"; shift ;;
-h|--help) usage; exit 0 ;;
-*) echo "unknown option: $1 (try --help)" >&2; exit 3 ;;
-*) die "unknown option: $1 (try --help)" ;;
*) PACKAGE_DIR="$1"; shift ;;
esac
done

if [[ -f "$PACKAGE_DIR" ]]; then
case "$PACKAGE_DIR" in
*.csproj) PACKAGE_DIR="$(dirname "$PACKAGE_DIR")" ;;
*) echo "expected a package directory or a .csproj, got a file: $PACKAGE_DIR" >&2; exit 3 ;;
*) die "expected a package directory or a .csproj, got a file: $PACKAGE_DIR" ;;
esac
elif [[ ! -d "$PACKAGE_DIR" ]]; then
echo "no such directory: $PACKAGE_DIR" >&2; exit 3
die "no such directory: $PACKAGE_DIR"
fi
PACKAGE_DIR="$(cd "$PACKAGE_DIR" && pwd -P)"

have dotnet || { echo "dotnet not on PATH (DOTNET_ROOT=$DOTNET_ROOT)" >&2; exit 3; }
have jq || { echo "jq not found — required to read baselines. brew install jq" >&2; exit 3; }
have dotnet || die "dotnet not on PATH (DOTNET_ROOT=$DOTNET_ROOT)"
have jq || die "jq not found — required to read baselines. brew install jq"

CONFIG=""; _d="$PACKAGE_DIR"
while [[ "$_d" != "/" && -n "$_d" ]]; do
Expand Down Expand Up @@ -145,4 +147,5 @@ case $EXIT in 0) V="PASS" ;; 1) V="FAIL" ;; 2) V="INCOMPLETE" ;; esac

write_report "$V" "$EXIT"
print_verdict "$V" "$EXIT"
GATE_EXITED_ON_PURPOSE=1
exit $EXIT
24 changes: 19 additions & 5 deletions scripts/quality-gate/lib/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ else RED=""; GRN=""; YEL=""; DIM=""; BLD=""; OFF=""; fi

have() { command -v "$1" >/dev/null 2>&1; }

GATE_EXITED_ON_PURPOSE=""

die() { echo "$1" >&2; GATE_EXITED_ON_PURPOSE=1; exit 3; }

report_unexpected_exit() {
local rc="$1" command="$2" last=""
[[ $rc -eq 0 || -n "$GATE_EXITED_ON_PURPOSE" ]] && return 0
if [[ -n "${R_ID+set}" && ${#R_ID[@]} -gt 0 ]]; then
local i=$(( ${#R_ID[@]} - 1 )); last="${R_NAME[$i]}${R_PKG[$i]:+ (${R_PKG[$i]})}"
fi
echo "gate.sh stopped unexpectedly (exit $rc) on: $command" >&2
echo "${last:+The last stage it reported: $last. }No report was written: this is a bug in the gate." >&2
}

bget() {
[[ -f "$BASELINE" ]] || { echo ""; return; }
jq -r "$1 // empty" "$BASELINE"
Expand Down Expand Up @@ -52,12 +66,12 @@ discover_scope() {
local root slns projects=() p pkg
root="$(git -C "$PACKAGE_DIR" rev-parse --show-toplevel 2>/dev/null || echo "$PACKAGE_DIR")"; root="$(cd "$root" && pwd -P)"
slns="$(find "$root" -maxdepth 1 -name '*.sln' 2>/dev/null | sort)"
[[ -n "$slns" ]] || { echo "no .sln at $root: the gate builds the solution" >&2; exit 3; }
[[ "$(count_lines "$slns")" -eq 1 ]] || { echo "more than one .sln at $root: $(echo $slns)" >&2; exit 3; }
[[ -n "$slns" ]] || die "no .sln at $root: the gate builds the solution"
[[ "$(count_lines "$slns")" -eq 1 ]] || die "more than one .sln at $root: $(echo $slns)"
SLN="$slns"

while IFS= read -r p; do [[ -n "$p" ]] && projects+=("$p"); done < <(solution_projects)
[[ ${#projects[@]} -gt 0 ]] || { echo "$(basename "$SLN") lists no project" >&2; exit 3; }
[[ ${#projects[@]} -gt 0 ]] || die "$(basename "$SLN") lists no project"

for p in "${projects[@]}"; do
is_test_project "$p" || continue
Expand All @@ -67,10 +81,10 @@ discover_scope() {
PKG_DIR+=("$pkg"); PKG_NAME+=("$(basename "$pkg")"); PKG_TEST+=("$p")
PKG_PROJ+=("$(first_production_project "$pkg" "${projects[@]}")")
done
[[ ${#PKG_DIR[@]} -gt 0 ]] || { echo "no package with a test project under $PACKAGE_DIR in $(basename "$SLN")" >&2; exit 3; }
[[ ${#PKG_DIR[@]} -gt 0 ]] || die "no package with a test project under $PACKAGE_DIR in $(basename "$SLN")"
local i
for i in "${!PKG_DIR[@]}"; do
[[ -n "${PKG_PROJ[$i]}" ]] || { echo "${PKG_NAME[$i]} has a test project but no other project in $(basename "$SLN")" >&2; exit 3; }
[[ -n "${PKG_PROJ[$i]}" ]] || die "${PKG_NAME[$i]} has a test project but no other project in $(basename "$SLN")"
done

if [[ ${#PKG_DIR[@]} -eq 1 && "$PACKAGE_DIR" == "${PKG_DIR[0]}/"* ]]; then PACKAGE_DIR="${PKG_DIR[0]}"; fi
Expand Down