Skip to content

Shell: a variable takes the values of every assignment that reaches it; a container may hold the project - #525

Merged
lefticus merged 9 commits into
cpp-best-practices:mainfrom
leftibot:container-tests
Dec 2, 2026
Merged

lefticus merged 9 commits into
cpp-best-practices:mainfrom
leftibot:container-tests

Conversation

@leftibot

@leftibot leftibot commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #524 — merge after it; until then this diff includes its commits.

Part of #482: the pi-hole row. The PR it is stacked on is #524.

What blocked pi-hole

CI runs DISTRO=… bash test/run.sh, and that script was already followed. The script:

  1. picks its container runtime with if …; then RUNTIME="docker"; elif …; then RUNTIME="podman"; else …; exit 1; fi;
  2. moves into "${SCRIPT_DIR}";
  3. inside a function, runs bats in a container of an image it builds from the checkout: "${RUNTIME}" run … bash -euo pipefail -c 'cd /etc/.pihole/test; exec libs/bats/bin/bats "$@"'.

Three things stopped this from counting:

  • "${RUNTIME}" was never resolved.
  • Nothing was expanded in a directory that a variable names.
  • bash -euo pipefail -c wasn't read as a command string.
  • cd /etc/.pihole/test went outside the project.

What changes

  • Variable values from every assignment that reaches a command (facts/shell_values.py, new). The analysis walks a script's syntax tree along every path:

    • both branches of an if, and each arm of a case (plus no arm, unless a *) arm exists);
    • a loop's body run any number of times, to a fixpoint;
    • a function's body at each place it is called, with local variables kept to it;
    • nothing after exit, and a function's own path ends at return;
    • pipelines and subshells set nothing after them.

    A variable is known only when every assignment that reaches it is known: text, other known variables, or the value the command running the script gives it. A command substitution, read, getopts, mapfile, printf -v, unset, let, arithmetic, ${X:=…}, arrays, for, eval, source or . leave it unknown. A command that names a variable with several values becomes one command per value (docker and podman), up to most_values in policy/limits.toml (new [shell] table entry; 8). A function body is followed once per set of values it is called with, four at most; after that, what it assigns is unknown. "$@" is the script file's own arguments (./format.sh --lint), until a shift or set, and not inside functions. This replaces the old set-once constants (_Constants), which were unsound across branches: if x; then A=1; fi; $A resolved A to 1.

  • Expanders that read no project files run in any directory (CommandExpander.anywhere: launchers, xargs, find -exec, shell command strings, docker run). Expanders that read project files (makefiles, package scripts) still run only in the project's own directories.

  • Containers. docker run/podman run already launched their command in the directory where they ran. That command now also gets container=<engine> in its environment, as podman sets it. An absolute cd inside the container moves to the container's own directory (IN_CONTAINER, $(container)/etc/.pihole/test) instead of out of the project. A run there covers the project heuristically, like one in a directory a variable names.

  • bash -euo pipefail -c '…': a short-option cluster that ends in o takes the next word.

Corpus, before → after (61 projects: pi-hole, every corpus project whose workflows use docker run, and 30 that run shell scripts)

Project Score Change
pi-hole 73.58 → 78.65 tests-run WARN → PASS: bats, run by bats in the container (docker and podman)
SwiftFormat 47.79 → 48.64 code-formatting: its Swift is now formatted by CI. ./format.sh --lint runs "$FORMAT_DIR/$ASSET" . --cache ignore "$@", with ASSET set in each case arm and "$@" = --lint. The check stays FAIL for Ruby and Shell.
woo 43.91 → 43.91 platform-matrix message: its Travis job's benchmark/run-benchmark benchmark/go/run is followed through $@ to go build, so the auxiliary Go benchmark has 2 OSes. Still WARN.

The other 58 projects are unchanged, including nvm, whose nvm.sh calls its functions from everywhere (run time is the same as on the base, about 1.5 minutes).

Existing tests and snapshots changed

  • tests/test_unread_runners.py: docker run's launched command carries container in its environment (test_an_image_named_by_an_expression… now expects it).
  • tests/test_policy.py: the limits table test now covers [shell].
  • tests/test_expansion.py::test_a_variable_assigned_twice_or_from_a_command_is_not_resolved is unchanged and still passes. With T=ruff; T=black; $T, $T now resolves to black, which is correct, so it is still never ruff.
  • No snapshots change.

For the maintainer

  • A run in a container counts heuristically, because the container may hold the project (copied in by the image, or mounted). The validator doesn't read the Dockerfile to check this. pi-hole's image ADDs the checkout to /etc/.pihole.
  • SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" is still not resolved. It is a command substitution, so the directory stays unknown. Launchers now work there, but makefiles and package scripts in it are not read.

Coverage

codecov/patch was 98.66%. tests/test_shell_values.py adds ShellValuesPathTests, which covers:

  • values put together past most_values (three systems by three architectures)
  • nothing after exit
  • an array declared without a value
  • a function called again with the same values
  • a function that always exits
  • a local that leaves the caller's value as it was

Two pieces of dead code are removed:

  • _Analysis.value, which nothing called.
  • A loop's check for a None state after its passes. A loop always starts from a state, so the check could never be true. join is split so that the loop uses extended (a state joined with a path that may have ended).

The loop pass count and function call count are now policy/limits.toml's shell.loop_passes and shell.function_calls, following #517's change. Patch coverage locally: 334/334 for this PR's own diff, 1137/1137 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 (a7c9613).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #525      +/-   ##
==========================================
+ Coverage   99.44%   99.46%   +0.01%     
==========================================
  Files         374      379       +5     
  Lines       27339    28286     +947     
  Branches     3440     3622     +182     
==========================================
+ Hits        27188    28135     +947     
  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 and others added 5 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
…t; a container may hold the project

pi-hole's CI runs `test/run.sh`, which picks its container runtime with
`if …; then RUNTIME="docker"; elif …; then RUNTIME="podman"; else exit 1; fi`,
cds into `"${SCRIPT_DIR}"`, and runs bats in a container of the image it
builds from the checkout: `"${RUNTIME}" run … bash -euo pipefail -c 'cd
/etc/.pihole/test; exec libs/bats/bin/bats "$@"'`. None of it was followed.

- facts/shell_values.py follows a script's syntax tree along every path
  (if, case, loops, functions where they are called, nothing after exit)
  and gives each command the values of the assignments that reach it; a
  variable is known only when every one of them is. It replaces the
  set-once constants: a command naming a variable of several values is a
  command for each, up to policy/limits.toml's new `most_values`. A
  function's body is followed where it is called, once per set of values,
  four at most; `"$@"` is what the script file was run with, until a shift
  (SwiftFormat's `./format.sh --lint`).
- Expanders that read none of the project's files (launchers, a shell's
  command string, docker run) run in a directory the project does not
  hold too, such as one a variable names.
- docker run's command runs in a container: it carries `container` in its
  environment (as podman sets it), and an absolute `cd` there moves to the
  container's own directory (IN_CONTAINER), not out of the project: a run
  there covers the project heuristically, as one in a directory a variable
  names does.
- `bash -euo pipefail -c '…'` is a command string: an option cluster that
  ends in `o` takes the next word.

Part of cpp-best-practices#482.

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

Tests: values put together past most_values (three systems by three
architectures), nothing after exit, an array declared without a value, a
function called again with the same values, one that always exits, and a
local that leaves the caller's value as it was.

_Analysis.value was never called; it is removed. A loop starts from a
state, so what it holds is never None: join is split so that the loop
uses `extended` (a state joined with a path that may have ended), and the
check for None after its passes, which nothing could reach, is gone.

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

Beside most_values, the times a loop's body is followed and the sets of
values a function's body is followed with are limits, not constants.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
@lefticus
lefticus merged commit 6e9cca6 into cpp-best-practices:main Dec 2, 2026
35 checks passed
@lefticus
lefticus deleted the container-tests 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