-
Notifications
You must be signed in to change notification settings - Fork 32
Add ratcheting coverage-floor gate to CI #649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fpigeonjr
merged 3 commits into
master
from
gh-627-add-ratcheting-coverage-floor-gate-to-ci
Aug 27, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # AGENTS.md | ||
|
|
||
| Guidance for AI coding agents working in this repository. | ||
|
|
||
| ## Repo shape | ||
|
|
||
| This is a **raw-source, dual-workspace** Angular library, not a normal Angular CLI app: | ||
|
|
||
| - **Root** (`src/ui-kit/`, `src/formly/`) — the `@gsa-sam/sam-ui-elements` library itself. It is published as raw `.ts`/`.scss` source (no build step, no `dist/`) — consumers compile it themselves. `index.ts` re-exports `src/ui-kit`; `src/formly/index.ts` is a second, separately-imported entry point. | ||
| - **`test-app/`** — a _separate_ npm workspace (own `package.json`/`package-lock.json`/`node_modules`) that exists solely to host the Angular build tooling and Vitest test runner needed to exercise the root library's specs. Always `npm ci` (root) **and** `npm ci --prefix test-app` before running anything. | ||
| - Specs live next to the source they test under root `src/**/*.spec.ts` (not copied into `test-app`). `test-app/vitest.config.mts` globs both `test-app/src/**/*.spec.ts` and `../src/**/*.spec.ts`, with `allowExternal: true` so istanbul still instruments files outside `test-app`'s own root. | ||
|
|
||
| ## Publish contract — do not break silently | ||
|
|
||
| `scripts/validate-publish-package.mjs` (`npm run validate:publish`) enforces that `npm pack` still includes every file listed in `scripts/consumer-deep-imports.json` — a frozen list of deep `@gsa-sam/sam-ui-elements/src/...` import paths used by real downstream consumers (`iae-sam-front-end` and others). **Renaming or moving a `src/ui-kit` file that appears in that list is a breaking change for consumers even though nothing inside this repo imports it.** Check that file before restructuring `src/ui-kit`. | ||
|
|
||
| ## Testing | ||
|
|
||
| - Run specs: `npm ci && npm ci --prefix test-app && npm --prefix test-app test` (Vitest, `--coverage`, istanbul provider, jsdom environment). Config lives in `test-app/vitest.config.mts` — **coverage options must stay in this root config**; a shared/extended base config would have them silently ignored (falls back to Vitest defaults: html/clover/json instead of lcov). | ||
| - Run one spec or pattern: `cd test-app && npx vitest run --config vitest.config.mts -t "<name filter>"`, or pass a spec path directly. | ||
| - `it.skip`/`describe.skip`-only files (e.g. `label-wrapper`, `fieldset-wrapper`, the fully-commented-out `alert.spec.ts`) are intentional and contain zero runnable tests — `passWithNoTests: true` is set so these don't fail the run; this preserves prior Karma behavior. | ||
| - Coverage is enforced by a ratcheting floor gate: `coverage-floor.json` at the repo root records minimum statements/branches/functions/lines percentages (fractional, e.g. `53.56`). `node scripts/check-coverage.mjs` (wired into CI as `npm run coverage:check`) fails with a clear per-metric message if measured coverage (from `test-app/coverage/coverage-summary.json`) drops below any floor. | ||
| - **`coverage-floor.json` is a ratchet — it only ever moves up, and only via a dedicated bump.** Feature and spec PRs must **not** edit `coverage-floor.json` directly; raising it is a separate, deliberate commit made with `npm run coverage:bump` (rewrites the floor file to the currently measured coverage — preserving istanbul's fractional `pct`, not floored to a whole percent — and never lowering an existing entry). `.github/workflows/ci.yml` also runs `scripts/check-coverage-floor-not-decreased.mjs` on PRs, comparing `coverage-floor.json` against the base branch and failing if any metric's floor decreased, so a floor can't be hand-lowered in the same PR that regresses coverage. This mirrors the ESLint baseline guard below. | ||
| - The gate scripts are covered by `node --test scripts/check-coverage.test.mjs` and `node --test scripts/check-coverage-floor-not-decreased.test.mjs` (Node's built-in test runner, no extra deps). These are **not currently wired into any CI workflow** — run them manually after touching either script. | ||
| - Playwright E2E smoke test (`test-app/e2e/`) runs via `npm --prefix test-app run test:e2e`, gated separately in `.github/workflows/e2e.yml`. | ||
|
|
||
| ## Lint | ||
|
|
||
| - `npm run lint` (root) / `npm --prefix test-app run lint` run `ng lint` (ESLint) per workspace. | ||
| - A ratcheting warning-baseline gate works the same way as coverage: `eslint-baseline.json` (keys `root`, `test-app`) + `scripts/check-lint-baseline.mjs` fail the build if warnings exceed the recorded baseline, or if there are _any_ errors (errors always fail regardless of the baseline). `--bump` only ever lowers the baseline. | ||
| - `.github/workflows/lint.yml` additionally runs `scripts/check-baseline-not-increased.mjs`, comparing `eslint-baseline.json` on the PR branch against the base branch — this closes the hole where a contributor could raise the ceiling by hand-editing the JSON in the same PR that adds new warnings. The `--bump` scripts are the only sanctioned way to change either baseline file; same rule as `coverage-floor.json` above. | ||
| - Both `scripts/check-lint-baseline.test.mjs` and `scripts/check-baseline-not-increased.test.mjs` exist and pass but, like the coverage gate tests, are **not run in CI** — run `node --test scripts/*.test.mjs` manually when touching any gate script. | ||
|
|
||
| ## Formatting | ||
|
|
||
| - `npm run format:check` / `npm run format` (Prettier). Applies to the whole repo — remember to run it on new root-level `scripts/*.mjs` files too, not just `src/`. | ||
|
|
||
| ## CI workflows | ||
|
|
||
| - `.github/workflows/ci.yml` — installs both workspaces, runs `npm --prefix test-app test`, then `npm run coverage:check`. | ||
| - `.github/workflows/lint.yml` — format check, baseline-not-increased guard, `ng lint` + baseline gate for both workspaces. | ||
| - `.github/workflows/e2e.yml` — Playwright smoke test. | ||
| - `.github/workflows/publish.yml` — npm Trusted Publisher (OIDC) flow, gated on `validate:publish`; only trigger is a GitHub Release (or a dry-run `workflow_dispatch`). | ||
|
|
||
| ## Angular / TypeScript quirks | ||
|
|
||
| - Root `tsconfig.json` targets `es2015`/`commonjs` with `strictNullChecks: false` — this is a legacy config for the raw-source library, distinct from `test-app`'s stricter `tsconfig.spec.json` (`es2022`, TestBed/Vitest types). Don't assume one workspace's TS settings apply to the other. | ||
| - `test-app/src/test-setup.ts` shims several `jsdom` gaps Karma never needed to care about: `Element.scrollIntoView`, `Element.innerText` (falls back to `textContent`), and `Element.animate` (Web Animations API used by `@angular/animations`). If a spec hangs or throws on one of these, check this file before adding a per-spec workaround. | ||
| - `@gsa-sam/icons` is a `test-app`-only dependency (not a root peerDependency) but is imported by root library source (e.g. `header-next`); it's aliased explicitly in `vitest.config.mts` since specs run directly against root `src/` and Node's module resolution wouldn't otherwise reach `test-app/node_modules`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "statements": 53.56, | ||
| "branches": 39.77, | ||
| "functions": 50.31, | ||
| "lines": 52.84 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * Guards against a coverage-floor being lowered directly in | ||
| * coverage-floor.json rather than earned via `coverage:bump`. | ||
| * | ||
| * The per-metric gate (check-coverage.mjs) only evaluates the floors | ||
| * committed on the branch being checked, so a contributor could lower a | ||
| * floor (e.g. `statements` from 53 to 30) in the same PR that regresses | ||
| * coverage, and the gate would still pass. This script closes that hole by | ||
| * comparing coverage-floor.json on the PR branch against the base branch's | ||
| * version and failing if any metric's floor decreased — the same shape as | ||
| * check-baseline-not-increased.mjs for eslint-baseline.json. | ||
| * | ||
| * New metric keys that don't exist on the base branch are allowed (they | ||
| * can't be "decreased" if there's nothing to compare against). Any increase | ||
| * or unchanged value is allowed, matching the ratchet-only semantics of | ||
| * `coverage:bump`. | ||
| * | ||
| * Usage: | ||
| * node scripts/check-coverage-floor-not-decreased.mjs <base-floor.json> <head-floor.json> | ||
| */ | ||
| import { readFileSync } from "node:fs"; | ||
| import { resolve } from "node:path"; | ||
|
|
||
| const [baseArg, headArg] = process.argv.slice(2); | ||
|
|
||
| if (!baseArg || !headArg) { | ||
| console.error( | ||
| "Usage: node scripts/check-coverage-floor-not-decreased.mjs <base-floor.json> <head-floor.json>" | ||
| ); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| function loadFloors(label, path) { | ||
| try { | ||
| return JSON.parse(readFileSync(resolve(path), "utf8")); | ||
| } catch (error) { | ||
| console.error(`✖ Could not read ${label} coverage floors at ${path}`); | ||
| console.error(` ${error.message}`); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| const base = loadFloors("base-branch", baseArg); | ||
| const head = loadFloors("pull-request", headArg); | ||
|
|
||
| let hasDecrease = false; | ||
|
|
||
| for (const metric of Object.keys(head)) { | ||
| if (!(metric in base)) { | ||
| // New metric entry — nothing to compare against, so it can't be a | ||
| // decrease from a previously-trusted value. | ||
| continue; | ||
| } | ||
|
|
||
| const baseValue = base[metric]; | ||
| const headValue = head[metric]; | ||
|
|
||
| if (!Number.isFinite(baseValue) || !Number.isFinite(headValue)) { | ||
| console.error( | ||
| `✖ ${metric}: invalid coverage-floor value (base: ${baseValue}, head: ${headValue})` | ||
| ); | ||
| hasDecrease = true; | ||
| continue; | ||
| } | ||
|
|
||
| if (headValue < baseValue) { | ||
| console.error( | ||
| `✖ ${metric}: coverage floor decreased ${baseValue}% → ${headValue}%. ` + | ||
| "The committed floor can only go up (via `npm run coverage:bump`), never down. " + | ||
| "Revert this change to coverage-floor.json." | ||
| ); | ||
| hasDecrease = true; | ||
| } | ||
| } | ||
|
|
||
| if (hasDecrease) { | ||
| process.exit(1); | ||
| } | ||
|
|
||
| console.log( | ||
| "✓ coverage-floor.json: no metric's floor decreased vs. the base branch." | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * Tests for scripts/check-coverage-floor-not-decreased.mjs — the CI-only | ||
| * guard that compares coverage-floor.json on a PR branch against the base | ||
| * branch's version and fails if any metric's floor decreased. | ||
| * | ||
| * Run: node --test scripts/check-coverage-floor-not-decreased.test.mjs | ||
| */ | ||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { execFileSync } from "node:child_process"; | ||
| import { mkdtempSync, writeFileSync, rmSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join, dirname } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const scriptDir = dirname(fileURLToPath(import.meta.url)); | ||
| const script = join(scriptDir, "check-coverage-floor-not-decreased.mjs"); | ||
|
|
||
| function run(args) { | ||
| try { | ||
| const stdout = execFileSync("node", [script, ...args], { | ||
| encoding: "utf8", | ||
| }); | ||
| return { status: 0, stdout, stderr: "" }; | ||
| } catch (error) { | ||
| return { | ||
| status: error.status ?? 1, | ||
| stdout: error.stdout?.toString() ?? "", | ||
| stderr: error.stderr?.toString() ?? "", | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| function withTempDir(fn) { | ||
| const dir = mkdtempSync(join(tmpdir(), "floor-guard-")); | ||
| try { | ||
| return fn(dir); | ||
| } finally { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| } | ||
|
|
||
| function writeFloors(dir, name, data) { | ||
| const path = join(dir, name); | ||
| writeFileSync(path, JSON.stringify(data)); | ||
| return path; | ||
| } | ||
|
|
||
| test("passes when the floor is unchanged", () => { | ||
| withTempDir((dir) => { | ||
| const base = writeFloors(dir, "base.json", { | ||
| statements: 53, | ||
| branches: 39, | ||
| functions: 50, | ||
| lines: 52, | ||
| }); | ||
| const head = writeFloors(dir, "head.json", { | ||
| statements: 53, | ||
| branches: 39, | ||
| functions: 50, | ||
| lines: 52, | ||
| }); | ||
| const { status } = run([base, head]); | ||
| assert.equal(status, 0); | ||
| }); | ||
| }); | ||
|
|
||
| test("passes when a floor is raised", () => { | ||
| withTempDir((dir) => { | ||
| const base = writeFloors(dir, "base.json", { | ||
| statements: 53, | ||
| branches: 39, | ||
| functions: 50, | ||
| lines: 52, | ||
| }); | ||
| const head = writeFloors(dir, "head.json", { | ||
| statements: 60, | ||
| branches: 39, | ||
| functions: 50, | ||
| lines: 52, | ||
| }); | ||
| const { status } = run([base, head]); | ||
| assert.equal(status, 0); | ||
| }); | ||
| }); | ||
|
|
||
| test("fails when a floor is lowered", () => { | ||
| withTempDir((dir) => { | ||
| const base = writeFloors(dir, "base.json", { | ||
| statements: 53, | ||
| branches: 39, | ||
| functions: 50, | ||
| lines: 52, | ||
| }); | ||
| const head = writeFloors(dir, "head.json", { | ||
| statements: 30, | ||
| branches: 39, | ||
| functions: 50, | ||
| lines: 52, | ||
| }); | ||
| const { status, stderr } = run([base, head]); | ||
| assert.equal(status, 1); | ||
| assert.match(stderr, /statements: coverage floor decreased 53% → 30%/); | ||
| }); | ||
| }); | ||
|
|
||
| test("fails when any of multiple metrics is lowered", () => { | ||
| withTempDir((dir) => { | ||
| const base = writeFloors(dir, "base.json", { | ||
| statements: 53, | ||
| branches: 39, | ||
| functions: 50, | ||
| lines: 52, | ||
| }); | ||
| const head = writeFloors(dir, "head.json", { | ||
| statements: 53, | ||
| branches: 39, | ||
| functions: 50, | ||
| lines: 10, | ||
| }); | ||
| const { status, stderr } = run([base, head]); | ||
| assert.equal(status, 1); | ||
| assert.match(stderr, /lines: coverage floor decreased 52% → 10%/); | ||
| }); | ||
| }); | ||
|
|
||
| test("allows a brand-new metric entry not present on the base branch", () => { | ||
| withTempDir((dir) => { | ||
| const base = writeFloors(dir, "base.json", { statements: 53 }); | ||
| const head = writeFloors(dir, "head.json", { | ||
| statements: 53, | ||
| "new-metric": 1, | ||
| }); | ||
| const { status } = run([base, head]); | ||
| assert.equal(status, 0); | ||
| }); | ||
| }); | ||
|
|
||
| test("treats a missing base-branch floor file as an empty floor set", () => { | ||
| withTempDir((dir) => { | ||
| const base = writeFloors(dir, "base.json", {}); | ||
| const head = writeFloors(dir, "head.json", { statements: 53 }); | ||
| const { status } = run([base, head]); | ||
| assert.equal(status, 0); | ||
| }); | ||
| }); | ||
|
|
||
| test("fails on a non-numeric floor value", () => { | ||
| withTempDir((dir) => { | ||
| const base = writeFloors(dir, "base.json", { statements: 53 }); | ||
| const head = writeFloors(dir, "head.json", { statements: "fifty-three" }); | ||
| const { status, stderr } = run([base, head]); | ||
| assert.equal(status, 1); | ||
| assert.match(stderr, /invalid coverage-floor value/); | ||
| }); | ||
| }); | ||
|
|
||
| test("exits non-zero with usage when arguments are missing", () => { | ||
| const { status, stderr } = run([]); | ||
| assert.equal(status, 1); | ||
| assert.match(stderr, /Usage:/); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.