# Coverage and the gates

> What the tests reach, measured in the build image and held per file to a floor that only rises; every tools/*_check.sh and what it refuses; how each gate is seen red on a planted mistake, how to add one, and the files of the repository that only ever move one way.

Source: https://omardev29.github.io/rmp-docs/framework/contributing/coverage-and-gates.html

Two kinds of check hold the framework to what it says about itself. Coverage measures what the tests reach, and does not let it fall. The gates read the repository for mistakes that have happened once and can be described precisely enough for a machine to refuse — a rule written in a script cannot be forgotten the way a rule in a note is.

## Coverage, measured in the image

A test that never runs a line cannot find what is wrong with it. `rmp test coverage` builds the unit tests, the headless layout test and `tests/input_play.cpp` — the one program that drives the real desktop runner — with clang's source-based coverage, runs them, and reads which lines of `src/rmp/` ran. The tests link the whole framework library for it, so a file no test reaches is in the report at 0% instead of missing from it. It is a debug build of its own, without the sanitizers, which say nothing about which lines ran and make the run several times slower.

Which lines a compiler counts, and which tests a machine can run — the sound device, the test locales — differ between machines, and the numbers were measured inside the pinned build image. So the floors are a gate there, and anywhere else the run prints them. The coverage CI job runs it in the image, and that is the run to believe; on your machine, run it there too:

```shell
rmp test coverage
tools/dev_shell.sh bash tools/coverage_check.sh
tools/dev_shell.sh bash tools/coverage_check.sh --update
```

CarefulThe image sees the checkout at another path than your machine does, and CMake refuses to configure a build folder again from another source directory. A folder the image configured — `build/coverage`, `build/fuzz` — is removed before the same script runs on your machine, and the other way round: `rm -rf build/coverage`.

The run writes `build/coverage/`: the report it read, a summary, this run's list of untested functions, and `build/coverage/html/`, every source file with the count of each line beside it — where to look for the lines a new test should reach.

Before anything else the script builds, runs and reads back a program of two functions, one called and one not, and fails — never skips — when clang's profile runtime is missing or its tools are of another release of LLVM: a coverage run that measured nothing would print every floor as a note and pass.

### A floor per file, which only rises

`tools/coverage_floor.txt` has one line per file of `src/rmp/`: its path and the share of its lines the tests run, in percent, rounded down.

- **A file below its line fails.** And a file two points or more above it fails too, until the line is raised in the commit that earned it: a ratchet nobody has to remember to turn is one that does not stall. `--update`, in the image, raises the lines to what the run measured.
- **A new file of `src/rmp/` comes with its line**, in the same commit. A file with lines and no floor fails, and so does a floor for a file that is gone.
- **Nothing lowers a floor but a hand.** `--update` never does; a floor that has to come down is an edit somebody writes and a reviewer reads, with the reason in the commit.

The floors are the per-line export's: each line of a file counted once. The html index and `llvm-cov report` count a line once for every function that maps it — a lambda's lines twice — so their percentages are not the floor's, and the floor is what fails.

### The public functions no test calls, which only shrink

`tools/untested_api.txt` names every public function of `include/rmp/` — in `rmp`, outside every `detail`, public, not implicit, defaulted or deleted — that no test calls. clang reads the headers to find them, and a template counts as called when any instance of it runs. A new public function no test calls fails: write the test. A listed one a test calls now, or one that is gone, fails until its line goes, which `--update` does.

### Lines no test can run

Some lines cannot run under a test: a GPU call in a program with no window, a file of two gigabytes. They go between two comment lines, each on a line of its own, and are counted apart instead of against the file. The first says why:

```text
// RMP_COVERAGE_OFF(<why>)
...
// RMP_COVERAGE_ON
```

The Aseprite reader's refusal of a file past `int`'s range is [one](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/src/rmp/assets/aseprite.cpp#L417-L420). A marker with no reason fails, and so does an OFF with no ON. The markers are for lines that cannot run, not for lines nobody has written a test for yet: those are what the floor is for.

## The gates

The framework's rule is that every deterministic mistake must be catchable by a gate, not by memory. If a kind of bug can be described precisely, something automated rejects it — a clang-tidy check, a rejection in `tools/configure.py`, a test, or a script. The scripts are `tools/*_check.sh`, and each one reads the repository for one kind of mistake:

| Script | Refuses | Run by |
|---|---|---|
| `tools/repo_check.sh` | a file a build produces tracked by git — a CMake cache, an object file, a log — and a tracked file the `.gitignore` ignores | `rmp test repo` |
| `tools/seam_check.sh` | a file of the framework that reads the clock, the devices, the screen or the random state outside the seams; inside `rmp`, a raylib type whose name the framework has taken written without its leading `::` | `rmp test seam` |
| `tools/workflow_check.sh` | a reusable workflow that asks for a permission, an input or a secret its caller does not give it, which GitHub answers with a run of no jobs and no logs; a step that throws a build's output away | `rmp test workflows` |
| `tools/portable_check.sh` | in `tools/` and the `rmp` launcher, what macOS's bash 3.2 or the BSDs do not have — `mapfile`, `readlink -f`, `sed -i` without an argument and the rest; a byte that is not ASCII in the BSD jobs' inline script | `rmp test portable` |
| `tools/shell_pattern_check.sh` | `A && B \|\| C` in a script, the launcher or a workflow's `run:` block, a line continued with `\` read as one: when `B` fails, `C` runs too | `rmp test shell` |
| `tools/naming_check.sh` | the naming rules clang-tidy cannot see, in every branch of every `#if`: R1 to R8 — [Naming](https://omardev29.github.io/rmp-docs/guidelines/naming.html) | `rmp test naming` |
| `tools/style_check.sh` | the style rules clang-tidy cannot see, in every branch of every `#if`: S1 to S8 — a C cast, `NULL`, a `TODO`, something newer than the oldest compiler a game is built with, a user-defined literal, `i++` where `++i` says the same, a silent interface call outside a frame, iostreams in the library | `rmp test style` |
| `tools/ownership_check.sh` | O1, memory owned by hand in the framework; O2, a raylib `Load*` or `Unload*` outside the file that owns what it makes — [Pointers and RAII](https://omardev29.github.io/rmp-docs/guidelines/pointers-and-raii.html) | `rmp test ownership` |
| `tools/cppcheck_check.sh` | a cppcheck finding in `src/rmp`; a suppression with no reason, or one that matches nothing any more | `rmp test cppcheck` |
| `tools/header_check.sh` | a public header that does not compile on its own, or does not carry the `.toml`'s values | `rmp test headers` |
| `tools/header_cost_check.sh` | a public header whose preprocessed size moved off its line in `tools/header_budget.txt`, either way; a header with no line | `rmp test cost` |
| `tools/versions_check.sh` | a pin in `thirdparty/FROZEN_VERSIONS.md` that disagrees with the file that controls it, or, inside the image, with the image's own manifest | `rmp test config` |
| `tools/examples_check.sh` | an example with its own `main()` or its own loop; one with no link in `examples/README.md` | `rmp test examples` |
| `tools/render_check.sh` | a game that does not boot, draw and exit cleanly under the software renderer; in the framework, a frame that does not hash to `tests/fixtures/render_hash.txt` | `rmp test render`; the macOS and BSD jobs |
| `tools/sanitize_check.sh` | the unit and layout tests failing, or a sanitizer report, built with gcc and warnings as errors | `rmp test sanitize`; the `deep` job |
| `tools/coverage_check.sh` | a file below its floor or above it, an untested public function — [above](https://omardev29.github.io/rmp-docs/framework/contributing/coverage-and-gates.html#coverage) | `rmp test coverage`; the `coverage` job |
| `tools/shipped_check.sh` | a release that does not find its files when it is started from another folder | the macOS and BSD jobs, and the plain C one |
| `tools/glibc_check.sh` | a Linux release that needs a newer glibc than `[linux] glibc` allows | the Linux release jobs |

A few gates are Python, because what they read is easier read that way:

- `tools/pointer_check.py`, `rmp test pointers`: a raw pointer or a C string in a declaration a game can name, read from the headers' syntax tree.
- `tools/license_db.py --check` and `tools/configure.py --check`, both in `rmp test config`: a vendored component with no licence row, and a `.toml` that cannot work.
- `tools/size_check.py`, `rmp test size` and every release job: a shipped binary off its size budget.
- `tools/lint_debt.py`, behind `rmp lint`: clang-tidy, held to what the tree still owes — [below](https://omardev29.github.io/rmp-docs/framework/contributing/coverage-and-gates.html#lint-debt).

[Testing](https://omardev29.github.io/rmp-docs/framework/testing.html#gates) lists every stage of `rmp test` with what it fails on, and [Enforcement](https://omardev29.github.io/rmp-docs/guidelines/enforcement.html) says which rule of the style guide each check holds.

## Every gate, seen red

A gate is a test, and a test nobody has seen fail is a hope with a name. `rmp test gates` runs `tests/gates_red_test.py`, which runs each script the way `rmp test` and CI run it — on a scratch tree: the script and what it reads, copied from the repository's tracked files into a temporary folder. Then it plants a mistake in the scratch, and the script has to say FAIL and name it; and on the tree as it is, it has to pass.

```text
class SeamCheckTest(GateTest):
    SCRIPT = "seam_check.sh"
    PATHS = ("src/rmp", "include/rmp")

    def test_the_tree_as_it_is_passes(self):
        self.assert_green()

    def test_planted_a_clock_read_in_a_new_file(self):
        self.tree.plant("src/rmp/later.cpp",
                        "#include <raylib.h>\nnamespace rmp {\n"
                        "float later() { return GetFrameTime(); }\n}\n")
        self.assert_red("src/rmp/later.cpp reads time, input, randomness or the screen directly",
                        "3:float later() { return GetFrameTime(); }")
```

Every script of `tools/*_check.sh` is in the file's `GATES` list, and a test holds the list to the folder: a new script fails until it has its line. The line says `here` — a class in this file, with a test of the tree as it is and at least one `test_planted_` — or names the class elsewhere that plants it red, and why there. The render check, for one, is planted in `tests/configure_test.py`, with a stand-in for CMake and for the game.

Planting is not a formality. It found holes in gates that were passing: the repository check let through an object file whose name git quotes, the seam check read a `//` inside a string as a comment and missed the call after it, and the glibc check called a file that is no ELF at all static. Each hole has its planted case in the file.

```shell
rmp test gates
python3 -m unittest discover -s tests -p gates_red_test.py -k SeamCheckTest -v
```

## Adding a gate

Start from the mistake, written as one sentence a machine could check. Then pick the instrument that understands the most of it:

- a clang-tidy check, or a query of the framework's own in `.clang-tidy`, for what the compiler parses — one branch of every `#if`;
- a rejection in `tools/configure.py`, with its case in `tests/configure_test.py`, for a `.toml` that cannot work;
- a unit test, for a behaviour;
- a script in `tools/`, for what none of those see: every branch of every `#if`, the shell, the workflows, the repository itself.

A query of the framework's own in `.clang-tidy` has one trap: it fails in silence. Its regular expression takes no C escapes — `"\\.h$"` matches nothing; write `[.]` for a dot — and a query that does not parse is one warning line and exit 0, after which the check never runs. `tools/lint_debt.py` fails on that line, and a test in `tests/configure_test.py` shows each check of the style guide firing on a probe of the wrong way and staying quiet on the right one: a new query gets its probe there.

A script is wired in five places, and tests of the command and of the gates hold the last three:

1. **The script**, `tools/<what>_check.sh`. Its header says what it refuses and why — the mistake that happened, so nobody "fixes" the rule back — and how to run it. It prints a `FAIL` line that names the file and line of every finding, and exits 1. It runs on macOS's bash 3.2 and on the BSDs, and never writes `A && B || C`: `rmp test portable` and `rmp test shell` read every script in `tools/`. One that reads C or C++ blanks the comments and strings first, through `tools/cpp_text.py`, so a comment may name what is forbidden.
2. **A stage** of `rmp test`, in `STAGES` in `tools/rmp.py`: a name, the summary `rmp help test` prints, and the command.
3. **A step in CI**, in the `lint` job of `.github/workflows/ci.yml`, or in the job of its own the test of the command names for it: every stage runs in CI, and a test of `rmp` holds each to its job. A gate a game does not have is a step gated on the framework, as the seam's is.
4. **Seen red**: its line in `GATES` and its class in `tests/gates_red_test.py`, as [above](https://omardev29.github.io/rmp-docs/framework/contributing/coverage-and-gates.html#gates-red).
5. **Whether a game gets it.** `rmp new` copies a list of files, and everything in `tools/` is the framework's unless that list names it. A gate a game's own CI runs is named there, or a test of `rmp new` fails on the game that lacks it.

The documentation site says what each stage refuses, so a new stage is a line on [Testing](https://omardev29.github.io/rmp-docs/framework/testing.html#gates), and a new rule of the style guide a row of [Enforcement](https://omardev29.github.io/rmp-docs/guidelines/enforcement.html#table), the day the site moves to the commit that has it.

## Files that only move one way

A rule the tree does not yet keep everywhere cannot be switched on as a gate, and leaving it off lets it slip further. So what is still owed is written down, and the gate holds the list: it may get shorter, and it may not get longer. Every one of them fails both ways — a new debt, and a debt that was paid and is still listed — so the list is always exactly what is left. None is ever made longer by a tool: that is an edit by hand, with its reason, for a reviewer to read.

| The file | Moves | Fails on |
|---|---|---|
| `tools/coverage_floor.txt` | up | a file under its line, or two points over it; a file with no line — [above](https://omardev29.github.io/rmp-docs/framework/contributing/coverage-and-gates.html#floors) |
| `tools/untested_api.txt` | shorter | a public function no test calls that is not listed; a listed one that is called, or gone |
| `tests/property/waivers.txt` | shorter | a public function no property covers that is not listed; a listed one a property covers, or that is gone — [Properties](https://omardev29.github.io/rmp-docs/framework/contributing/properties-fuzzing-mutation.html#covers) |
| the `floor` of `tests/mutation/floors/<module>.txt` | up | a score under it; a floor under the score rounded down to 5 — [Mutation](https://omardev29.github.io/rmp-docs/framework/contributing/properties-fuzzing-mutation.html#mutation) |
| its `equivalent` lines | shorter | a listed mutant a test kills, or one that is gone |
| `tests/e2e/pending.txt` | shorter, and empty | an example of a played area with no script and no line; a line for one that has a script — [Examples](https://omardev29.github.io/rmp-docs/framework/contributing/examples-and-e2e.html#played) |
| `tools/lint_debt.txt` | down | a warning not owed; a count that grew; a count above what is left — [below](https://omardev29.github.io/rmp-docs/framework/contributing/coverage-and-gates.html#lint-debt) |
| `tools/pointer_ratchet.txt` | shorter | a pointer in a `detail` namespace or a private member that is not listed; a line that matches nothing any more |
| the allowlist in `tools/seam_check.sh` | shorter | another file reading the clock, the devices or the screen; a listed file that no longer does |

Two more files are budgets rather than ratchets, and are held both ways because a number nobody updated hides the next change as surely as a growth nobody wrote down. `tools/size_budget.txt` is the size of every binary a release ships: within 1.5 % of its line it passes in silence, past that with a warning that names the new number, and past 10 % it fails. `tools/header_budget.txt` is what each public header costs to include, measured and held in the image; it fails a header that grew past its line and a line the header is well under.

## `tools/lint_debt.txt`

`rmp lint` is clang-tidy over `src/`, `tests/` and `examples/`, with the framework's `.clang-tidy`: the same run on your machine and in CI's `tidy` job. It reads the compile database of the `lint` preset, which it configures, and a file with no entry there is a failure rather than a guess at its flags.

What the tree still owes the style guide is `tools/lint_debt.txt`, one line per check and file — the check, the file, and how many warnings it owes:

```text
custom-rmp-raw-pointer  src/rmp/input/input.cpp  4
```

- **A warning nobody owes fails.** A new warning is fixed, or carries a `NOLINT` naming the check, with its reason, on the line it is about. Nothing is ever added to the file.
- **More than a line owes fails**, and every warning of that check and file is printed, because the new one cannot be told from the old ones.
- **Fewer than a line owes fails too**: the debt was paid, and the line comes down to what is left, so the next warning is caught.

```shell
rmp lint
bash tools/lint.sh debt
rmp lint fix
```

`bash tools/lint.sh debt` runs the same check and writes the lines down to what is left: run it after paying a debt, and commit the file with the fix. `rmp lint fix` applies what clang-tidy is sure of, one file at a time, and then formats. Read every change it makes: `misc-const-correctness`'s fix has written `const char const *`, and made const a buffer a test writes into.

The clang-tidy has to be the one `thirdparty/FROZEN_VERSIONS.md` pins, and is run with `--experimental-custom-checks`: without that flag the framework's own checks in `.clang-tidy` are not registered, and clang-tidy does not say so. [Enforcement](https://omardev29.github.io/rmp-docs/guidelines/enforcement.html#debt) has what the tree still owes, check by check.
