diff --git a/CMakeLists.txt b/CMakeLists.txt index eedf4b8..4776ecf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,26 @@ else() $<$>:-D_FORTIFY_SOURCE=2>) endif() +# --- Code coverage instrumentation (opt-in) --------------------------------- +# Configure with -DENABLE_COVERAGE=ON to build gcov-instrumented binaries; pair it +# with the `coverage` / `coverage-diff` targets defined at the end of this file. +# Off by default, so normal and CI builds are completely unaffected. +option(ENABLE_COVERAGE "Build with code-coverage instrumentation (--coverage)" OFF) +if(ENABLE_COVERAGE) + if(MSVC) + message(FATAL_ERROR "ENABLE_COVERAGE is only supported with GCC/Clang (gcov), not MSVC") + endif() + # Coverage needs an unoptimized build for accurate line<->statement mapping. + # Force Debug so the warnings block's _FORTIFY_SOURCE define is skipped (it is + # gated on non-Debug, and errors under -O0 "requires optimization"; we cannot + # simply append -U_FORTIFY_SOURCE because CMake de-duplicates it) and we inherit + # -O0 -g from the Debug config. + set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type" FORCE) + message(STATUS "Code coverage instrumentation ENABLED - forcing Debug build type (--coverage)") + add_compile_options(--coverage) + add_link_options(--coverage) +endif() + # Find dependencies find_package(OpenSSL REQUIRED) @@ -553,6 +573,97 @@ else() message(STATUS "asciidoctor not found - 'docs' target disabled (gem install asciidoctor)") endif() +# Code coverage reports (opt-in; requires -DENABLE_COVERAGE=ON at configure time). +# +# cmake -S . -B build-cov -DENABLE_COVERAGE=ON +# cmake --build build-cov # instrumented binaries +# ctest --test-dir build-cov # run tests -> .gcda data +# cmake --build build-cov --target coverage # full report (summary+HTML+XML) +# cmake --build build-cov --target coverage-diff # patch coverage vs a base branch +# +# `coverage` overall report for src/ + include/hotrod via gcovr: a terminal +# summary, HTML at build-cov/coverage/index.html, and a Cobertura +# coverage.xml (the input diff-cover consumes). +# `coverage-diff` coverage of ONLY the lines changed vs COVERAGE_DIFF_BASE +# (default origin/main) via diff-cover; HTML at coverage/patch.html. +# Set -DCOVERAGE_DIFF_THRESHOLD= to fail below a patch %. +# `coverage-reset` delete stale .gcda counters before a fresh run. +# +# Run the tests yourself first (ctest, or a subset) so a report reflects exactly the +# suites you ran — integration suites need Docker. These targets only aggregate the +# data; they do not build or run tests. +if(ENABLE_COVERAGE) + find_program(GCOVR_EXECUTABLE gcovr) + find_program(DIFF_COVER_EXECUTABLE diff-cover) + + set(COVERAGE_DIFF_BASE "origin/main" CACHE STRING + "Base git ref that coverage-diff compares the working branch against") + set(COVERAGE_DIFF_THRESHOLD "0" CACHE STRING + "Fail coverage-diff when patch line coverage is below this percent (0 = report only)") + + # Shared gcovr invocation: project sources only (relative to the source root), + # excluding the tests. Positional arg is the search root for the .gcda data. + set(_gcovr_common + --root ${CMAKE_SOURCE_DIR} + ${CMAKE_BINARY_DIR} + --filter src/ + --filter include/hotrod/ + --exclude tests/ + # Run gcov serially. With many .gcda in one directory (e.g. a full ctest + # run), parallel gcov workers race on the intermediate *.gcov.json.gz files + # they write into the shared working dir, which surfaces as gcovr's + # "Output file ... doesn't exist but no error from GCOV detected" + # (a SanityCheckError that --gcov-ignore-errors cannot catch). Serial is + # slower but reliable. + -j 1 + # Work around GCC bug 68080: gcov can emit a negative hit count on a branch + # line, which gcovr treats as fatal by default. Downgrade it to a per-file + # warning so the report still generates (the affected counts are unreliable + # but rare). https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68080 + --gcov-ignore-parse-errors negative_hits.warn_once_per_file) + + if(GCOVR_EXECUTABLE) + add_custom_target(coverage + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/coverage + COMMAND ${GCOVR_EXECUTABLE} ${_gcovr_common} + --print-summary + --html-details ${CMAKE_BINARY_DIR}/coverage/index.html + --xml ${CMAKE_BINARY_DIR}/coverage.xml + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMENT "Coverage: terminal summary + coverage/index.html + coverage.xml" + VERBATIM USES_TERMINAL) + message(STATUS "gcovr found - 'coverage' target enabled") + else() + message(STATUS "gcovr not found - 'coverage'/'coverage-diff' disabled (pip install gcovr)") + endif() + + if(GCOVR_EXECUTABLE AND DIFF_COVER_EXECUTABLE) + # Refresh the XML, then report coverage of only the changed lines. Runs from + # the source root so diff-cover can shell out to `git diff`; gcovr's absolute + # --root keeps the XML paths repo-relative, which is what diff-cover matches. + add_custom_target(coverage-diff + # Create the report dir so diff-cover's --format html:... can write it + # even when coverage-diff is run standalone (without the coverage target). + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/coverage + COMMAND ${GCOVR_EXECUTABLE} ${_gcovr_common} --xml ${CMAKE_BINARY_DIR}/coverage.xml + COMMAND ${DIFF_COVER_EXECUTABLE} ${CMAKE_BINARY_DIR}/coverage.xml + --compare-branch=${COVERAGE_DIFF_BASE} + --format html:${CMAKE_BINARY_DIR}/coverage/patch.html + --fail-under=${COVERAGE_DIFF_THRESHOLD} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMENT "Patch coverage vs ${COVERAGE_DIFF_BASE} (diff-cover) -> coverage/patch.html" + VERBATIM USES_TERMINAL) + message(STATUS "diff-cover found - 'coverage-diff' target enabled") + elseif(GCOVR_EXECUTABLE) + message(STATUS "diff-cover not found - 'coverage-diff' disabled (pip install diff-cover)") + endif() + + add_custom_target(coverage-reset + COMMAND find ${CMAKE_BINARY_DIR} -name "*.gcda" -delete + COMMENT "Deleting stale .gcda counters for a clean coverage run" + VERBATIM) +endif() + # Installation install(TARGETS hotrod-client EXPORT hotrod-client-targets diff --git a/README.md b/README.md index c7d6c7a..073a42c 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,25 @@ Integration tests use Docker Compose to manage multi-node Infinispan clusters an verify failover, rebalancing, and hash-aware routing. See [docs/STATUS.md](docs/STATUS.md) for the current coverage breakdown. +### Code coverage (opt-in) + +Configure with `-DENABLE_COVERAGE=ON` (GCC/Clang; forces a Debug build) and run the +tests, then aggregate. Requires [`gcovr`](https://gcovr.com); the patch target also +needs [`diff-cover`](https://pypi.org/project/diff-cover/). + +```bash +cmake -S . -B build-cov -DENABLE_COVERAGE=ON +cmake --build build-cov +ctest --test-dir build-cov # run the suites you want measured +cmake --build build-cov --target coverage # full report -> build-cov/coverage/index.html +cmake --build build-cov --target coverage-diff # patch coverage vs origin/main +``` + +`coverage-diff` reports coverage of only the lines changed versus a base ref +(`-DCOVERAGE_DIFF_BASE=`, default `origin/main`); set +`-DCOVERAGE_DIFF_THRESHOLD=` to fail below a patch percentage. `coverage-reset` +clears stale counters between runs. + ## Documentation The user guide lives in [`documentation/`](documentation/) (AsciiDoc). Render it diff --git a/docs/DECISIONS.md b/docs/DECISIONS.md index f011e42..bba33b9 100644 --- a/docs/DECISIONS.md +++ b/docs/DECISIONS.md @@ -1464,3 +1464,75 @@ call `scripts/build-docs.sh`, so "renders locally" means "renders on Pages". **Remaining (phases 2–3, tracked in #26):** adopt the Go conventions (`:brandname:`, docinfo jsTree TOC, `AI-CODE.md`, `[source,cpp]`), reorder chapters to Go order, flesh out the stub topics, and restructure `README.md`. + +## 2026-12-03 — Opt-in code-coverage CMake targets (full + patch/diff) + +**Decision:** added `-DENABLE_COVERAGE=ON` plus three custom targets — `coverage` +(whole-project gcovr report: terminal summary + HTML + Cobertura XML), `coverage-diff` +(patch coverage of only the lines changed vs a base branch, via `diff-cover`), and +`coverage-reset` (wipe stale `.gcda`). Off by default; normal and CI builds are +untouched. Tools (`gcovr`, `diff-cover`) are discovered with `find_program`, so a +target simply isn't created when its tool is absent. + +**Why:** there is no coverage gate in the repo and none is being added — these are +local/dev reporting aids. The patch-coverage target in particular answers "how well +tested are the lines this PR changes" (`diff-cover coverage.xml --compare-branch`), +which is more useful for review than a drifting whole-repo percentage. + +**Gotchas found while wiring it:** +- `ENABLE_COVERAGE` forces `CMAKE_BUILD_TYPE=Debug`. Coverage needs `-O0` for + accurate line mapping, but under a non-Debug type the warnings block defines + `_FORTIFY_SOURCE=2`, which errors at `-O0` ("requires optimization") under + `-Werror`. Appending `-U_FORTIFY_SOURCE` does NOT help — CMake de-duplicates the + repeated flag. Forcing Debug skips the fortify define (it is gated on non-Debug) + and yields `-O0 -g` for free. +- `gcovr` must run from the **source root** with relative `--filter`s; running it + from the build dir with an absolute `--root` matches no files (reports 0/0). Both + targets use `WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}` and pass the build dir as the + `.gcda` search path. + +**Config knobs:** `COVERAGE_DIFF_BASE` (default `origin/main`) and +`COVERAGE_DIFF_THRESHOLD` (default `0` = report only; set a percent to fail below it). + +**Alternatives:** a CI coverage gate (Codecov/Coveralls) — not done; this is +local-only for now, and can be layered on later using the same Cobertura XML. + +## 2026-12-04 — Coverage: work around gcov negative-hit parse error (GCC bug 68080) + +**What:** the `coverage`/`coverage-diff` gcovr invocation now passes +`--gcov-ignore-parse-errors negative_hits.warn_once_per_file`. + +**Why:** with GCC + a non-trivial data set, gcov can emit a *negative* branch hit +count (e.g. `src/codec/HeaderCodec.cpp:48`), which gcovr treats as a fatal parse +error and aborts the whole report. This is the long-standing GCC bug +https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68080. The flag (gcovr's own +recommended remedy) downgrades it to a once-per-file warning so the report still +generates; only the affected, rare branch counts are unreliable. Did not surface in +the initial unit-only smoke test — it needs the richer .gcda a full/integration run +produces. + +## 2026-12-04 — Coverage: run gcovr serially to avoid gcov intermediate-file race + +**What:** the shared gcovr invocation now passes `-j 1` (serial gcov). + +**Why:** on a large data set (a full `ctest` run leaves many `.gcda` in one tree), +gcovr's parallel gcov workers race on the intermediate `*.gcov.json.gz` files gcov +writes into the shared working directory. A file one worker expects is clobbered by +another, surfacing as `SanityCheckError: Output file ...doesn't exist but no error +from GCOV detected` (note the mangled `/home/##...` path). That error is +an `AssertionError`, and gcovr's `--gcov-ignore-errors` only catches `RuntimeError`, +so it cannot be ignored — serial execution is the fix. Did not reproduce with a +small unit+one-integration data set (few `.gcda`); only a many-`.gcda` run triggers +the race, which is why the first smoke tests passed. Trade-off: slower aggregation, +but reliable. Stacks with the earlier negative-hits parse-error tolerance. + +## 2026-12-04 — Coverage: coverage-diff creates its own report directory + +**What:** the `coverage-diff` target now runs `cmake -E make_directory .../coverage` +before diff-cover. + +**Why:** diff-cover's `--format html:.../coverage/patch.html` does not create the +parent directory, so running `coverage-diff` standalone (without first running +`coverage`, which does the mkdir) crashed with `FileNotFoundError: .../coverage/ +patch.html`. Each coverage target must be independently runnable; the earlier smoke +tests masked this by always running `coverage` first. diff --git a/docs/STATUS.md b/docs/STATUS.md index f12c711..7f064bf 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -4,7 +4,7 @@ > If any other doc disagrees with this file, this file wins. Point-in-time > snapshots live in [`archive/`](archive/) and are historical only. > -> **Last updated:** 2026-12-02 +> **Last updated:** 2026-12-04 --- @@ -251,6 +251,7 @@ full list (Steps 10–12, benchmarks, TLS, code TODOs). | Codecs | `src/codec/`, `src/hash/`, `src/auth/`, `src/topology/` | | Unit tests | `tests/unit/` | | Integration tests | `tests/integration/` (see `README_TOPOLOGY.md` there) | +| Code coverage (opt-in) | configure `-DENABLE_COVERAGE=ON`; targets `coverage` (full) / `coverage-diff` (patch vs base branch) / `coverage-reset`. Needs `gcovr` (+ `diff-cover` for the diff target). each target runs standalone; gcovr runs serial (`-j 1`) and tolerant of GCC gcov glitches — see [`DECISIONS.md`](DECISIONS.md) | | Why decisions were made | [`DECISIONS.md`](DECISIONS.md) | | Historical milestone log (frozen at Step 9) | [`archive/PROGRESS.md`](archive/PROGRESS.md) | | Superseded snapshots | [`archive/`](archive/) |