Skip to content

CMake: an executable built from test sources holds C and C++ tests - #524

Merged
lefticus merged 6 commits into
cpp-best-practices:mainfrom
leftibot:cmake-test-executables
Dec 2, 2026
Merged

lefticus merged 6 commits into
cpp-best-practices:mainfrom
leftibot:cmake-test-executables

Conversation

@leftibot

@leftibot leftibot commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #522 — merge after it; until then this diff includes its commits. (#522 is stacked on #517 → #515 → #512.)

Part of #482: the pybind11 and luanti rows.

What changes

pybind11 defines its C++ tests only as executables in tests/. Custom targets run them (add_custom_target(cpptest COMMAND "$<TARGET_FILE:test_with_catch>"), and check, which depends on test_pure_cpp), and CI builds those targets. luanti builds its unit tests into the engine, and CI runs ./bin/luanti --run-unittests. Before this PR, neither was a C/C++ test definition, so tests-run said "no tests found".

  • The CMake reader now reads add_executable and target_sources. A test executable is an executable with at least one test source: a source in a test directory, or a source whose name says it is a test (test.cpp, test_ban.cpp, parser_test.cc, ParserTests.cpp). It is recorded as a Setting (tool CMake, key test executable) with whether every source is a test source. Two cases are skipped:

    • an executable that CTest's tests already run (add_test's command or a *_discover_tests target) once CTest is enabled, because it is already CTest's definition;
    • an imported or alias executable.
  • There is a new test runner, a test executable (build_systems/cmake/ctest.py), for the CMake languages. It counts as a run in two cases:

    • a custom target's command names the executable, as $<TARGET_FILE:name> or as the bare target name (CMake replaces that with the built file);
    • CI names the built file by path (./bin/luanti, bin\Release\luanti.exe), not a bare program name found on the PATH.

    An executable built only from test sources counts whatever arguments it gets. One built together with the product's sources counts only when an option asks for its tests. Its definitions count for every unit when they are in a unit of tests (pybind11's tests/ is a CMake project of its own).

  • New policy table policy/tests.toml ([test_executables] option_words = ["test"]), added to TABLES in policy_tables.py, with a table test.

Corpus, before → after (score, tests-run)

Project Score tests-run
pybind11 60.18 → 65.01 FAIL → WARN (C++ now passes, run by a test executable; the WARN is auxiliary Shell)
luanti 42.82 → 46.78 FAIL → FAIL (C and C++ now pass; JVM, Python and Shell still have no tests)
glfw 43.27 → 46.01 FAIL → WARN (its tests/ executables are test executables, and CI doesn't run them)
curl 54.18 → 56.58 FAIL → ERROR (its test executables are found; whether CI runs them is unknown, because of CI scripts that aren't in the repository)

cJSON, CLI11, expected, Catch2, cmake_template, doctest, fmt, spdlog, llama.cpp and abseil: unchanged.

Existing tests and snapshots changed

  • tests/checks/test_tests_run.py: the "no tests found" and "no CI configuration runs …" wordings now list the new runner (NO_C_TESTS, new NOT_RUN_C).
  • Snapshots, where the new runner is in the known tools and the wording: bare_c, bare_cpp, bare_fortran, buildable, registry, well_configured_c, well_configured_cpp, well_configured_fortran (.json only; their executables are CTest's, so no facts change).

For the maintainer

  • glfw's tests/ programs are mostly interactive test programs. They now count as tests that CI doesn't run (WARN), where before there were none (FAIL). The rule is the test role's: a source in a test directory is a test source.
  • CI running a CTest-registered executable directly (not through ctest) still isn't credited, as before. The executable stays CTest's definition only.
  • The option-word heuristic (--run-unittests) applies only to executables built with product sources too.

Coverage

codecov/patch was 98.86%. All of the missed lines were #522's, and they are fixed there. This PR's own lines were already covered. Patch coverage locally: 64/64 for this PR's own diff, 803/803 for the stack against main.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.46%. Comparing base (3dbe391) to head (f2b3ae7).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #524      +/-   ##
==========================================
+ Coverage   99.44%   99.46%   +0.01%     
==========================================
  Files         374      378       +4     
  Lines       27339    27991     +652     
  Branches     3440     3563     +123     
==========================================
+ Hits        27188    27840     +652     
  Misses        151      151              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

leftibot and others added 4 commits December 1, 2026 14:39
…make check tests

Autotools projects (jq, tmux, curl, libzmq) had no C build system, so their
warning flags and `make check` tests were missed, and every make command CI
ran was unknown ("the makefile: none is found where make runs"): configure
writes the makefile, so it is not in the checkout.

- configure.ac is evaluated without running m4 or the shell
  (build_systems/autotools/configure_syntax.py, configure.py): macro calls
  and shell statements, `if`/`case` branches, AS_IF, AS_CASE, AC_ARG_ENABLE,
  AC_ARG_WITH, the flag macros (AX_CHECK_COMPILE_FLAG, AX_APPEND_COMPILE_FLAGS,
  AX_APPEND_FLAG, gl_WARN_ADD) where the compiler accepts the flag, macros
  the file defines, AC_SUBST and AM_CONDITIONAL. `test "x$GCC" = xyes` is
  the GCC-like compilers; any other test is a requirement as written.
  tmux's LTO probe, which sets -Werror for one compile check and restores
  CFLAGS, leaves no -Werror.
- Each Makefile.am is read with the configure.ac above it (automake.py):
  AM_CFLAGS, AM_CXXFLAGS, AM_CPPFLAGS and each built target's own flags,
  through Automake conditionals, with configure's substitutions expanded.
- Autotools (Makefile.am) is a build system of C and C++; `make check`,
  `distcheck` or `recheck` where a Makefile.am is (or in a build tree
  outside the checkout) runs the tests TESTS and check_PROGRAMS name.
- Where no makefile is in the checkout but a Makefile.am is, make reads the
  Makefile.am's rules, and Automake's own targets (policy/automake.toml) are
  known, not unknown.

Part of cpp-best-practices#470

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
…ctices#470)

Brackets around a shell test (an m4 quote, stripped), several assignments
on a line, a case pattern on a line of its own, AS_IF and AS_CASE without
their last branch, AC_SUBST with a value, unquoted arguments holding
parentheses, a body-less AC_DEFUN, a stray else or fi, an assignment no
build reaches, a configure.ac past the bound on conditions, and a
Makefile.am variable set only where it is not used.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
)

Like configure.ac's most_defined_calls, how deep a Makefile.am reference
is followed is a limit, not a constant in the code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
…ystem (cpp-best-practices#470)

The maintainer asked that the message read the same for every build
system: "warnings are not treated as errors in CI configuration or the
build files", rather than listing CMake files, presets and meson.build
(leaving out configure.ac), or fpm's flags for Fortran. The hints, which
say how in each build system, are unchanged, as is CMake's own presets
finding. test_warnings_as_errors' SEARCHED and the bare_c, bare_cpp,
bare_fortran and buildable snapshots change to match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
@leftibot
leftibot force-pushed the cmake-test-executables branch from 9913f8c to ac3ccb5 Compare December 1, 2026 22:22
leftibot and others added 2 commits December 1, 2026 18:20
…pp-best-practices#470)

Conditions are kept for the whole process, so a second reading of the
same configure.ac builds no new nodes and never reaches the lowered
bound. mutmut runs the suite more than once in one process, so the test
failed in its clean run on cpp-best-practices#532 and cpp-best-practices#533. Each reading now gets names of
its own, and the test reads twice to hold that in place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
pybind11 defines its C++ tests only as executables of tests/ that custom
targets run (`add_custom_target(cpptest COMMAND $<TARGET_FILE:test_with_catch>)`),
and CI builds the targets; luanti builds its unit tests into the engine and
CI runs `./bin/luanti --run-unittests`. CMake's add_executable and
target_sources are now read: an executable with a source in a test
directory, or named as a test (`test_ban.cpp`), is a test executable, unless
CTest's tests already run it. A run of one, by a custom target's command or
by CI naming the built file, runs its tests; one built with the product's
sources too runs them only when an option asks for them (policy/tests.toml).

Part of cpp-best-practices#482.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
@leftibot
leftibot force-pushed the cmake-test-executables branch from ac3ccb5 to f2b3ae7 Compare December 2, 2026 01:51
@lefticus
lefticus merged commit 16ffbbb into cpp-best-practices:main Dec 2, 2026
35 checks passed
@lefticus
lefticus deleted the cmake-test-executables branch December 2, 2026 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants