Explainer

A Test Harness That Refuses to Touch a Real Session

A Bash harness used a fresh temporary environment and a stub executable to test session scripts without touching real sessions—and exposed three bugs.

On this page

Deck: I replaced hand-checked shell scripts with a disposable-session harness: a fresh temporary environment for every case, a stub in place of the real executable, and 48 tests. It passed 48/48 — and the interesting part is the three real bugs it found in the scripts it was guarding.

Why disposable environments instead of real ones

The scripts I was changing manage session state: they decide when to act, dispatch to a command, and report an exit status. Testing them against real sessions is tempting and bad. Real sessions are slow, they are stateful, and a bad test run mutates something a person is using. Worse, a test that touches real state can pass for the wrong reason: the environment already happened to be in the shape the script wanted, so the test confirmed a coincidence.

So the harness builds a fresh temporary environment for each case and hands the script-under-test a stub executable in place of the real one. The stub records that it was invoked and returns a result I chose in advance, which means a test can ask “did the script decide to run this, and did it handle the failure it got back?” without depending on what the real command actually does. Nothing the harness does reaches a real session.

I kept the harness in Bash, using Bash, Python 3, and grep. It added no dependencies and no test-framework setup, which matters for a harness meant to run every time I touch the scripts: the cheaper it is to run, the more often it actually runs. The important boundary was isolation — every case ran against a fresh temporary environment and a stub.

What 48 tests actually covers

As of 2026-08-30 the suite is 48 tests, 48 passing, split into four kinds:

  • 21 decision-point tests. Each pins one branch: given this input and this state, the script must take this path and not that one.
  • 5 exit-code tests. The scripts’ contract with their callers is the exit status, so the status is asserted directly rather than inferred from output text.
  • 9 failure-injection tests. These construct the unhappy inputs: missing helpers, corrupt state, stale files, absent session directories.
  • 13 end-to-end tests. A full invocation through the dispatch path with the stub in place, checking the observable effects on the temporary environment.

That breakdown is honest about what the suite covers and what it does not. It says nothing about concurrency, nothing about the real executable’s behavior, and nothing about environments outside the ones I wrote. 48 passing tests is a statement about 48 questions I thought to ask.

Bug one: a fallback that the shell parsed as something else

The scripts run with set -u, so referencing an unset variable aborts the script instead of silently substituting an empty string. One code path tried to read the last background-process ID with an expression that was meant to be a fallback — “use this value, or this default if it is unset.” Bash parsed it as an indirect expansion instead: a request to treat the variable’s contents as the name of another variable.

Those two forms are easy to confuse, and the difference is the whole bug. A default expansion reads the variable you named and substitutes a value you supplied when it is unset. An indirect expansion hands Bash a name to look up instead of a value to fall back on. In the failure case there was nothing for Bash to resolve, and set -u turns an unresolvable lookup into an abort.

The bug is invisible whenever a background process happens to exist. It appears in exactly the case the defensive read was written to handle. The fix guarded the PID read. The lesson is that a fallback is a code path, and a fallback that has never been executed is not a fallback; it is an untested guess about how the shell parses your punctuation.

Bug two: a guard that ran after the damage

source runs a file inside the caller’s own shell rather than in a child process. That is exactly why the file needed protecting from being sourced: whatever it does lands on the caller, not on a throwaway process. A guard was supposed to block the call. The guard sat below the argument-parsing code, and argument parsing uses shift, which rewrites the caller’s positional parameters. So a source call reached the parser, shift clobbered the caller’s parameters, and only then did the guard say “no.”

That mechanism depends on how source is called, so it is worth stating. source somefile.sh with no arguments leaves the caller’s positional parameters in place, and a shift inside the sourced file rewrites the caller’s own $1, $2, and the rest. source somefile.sh arg1 arg2 gives the sourced file its own positional parameters instead, which confines any shift to them. The reported symptom — the caller’s parameters rewritten — matches the first form, which is why the fix had to run the guard before parsing rather than rely on argument handling to contain the damage.

The order was the whole bug. Moving the guard above argument parsing means it now protects parsing and dispatch instead of reporting after the fact. Nothing about the guard’s logic changed. Its position changed, and behavior followed position.

I find this bug embarrassing to describe in a sentence: code that forbids an action, placed where the action has already happened. It is also the most common shape of guard bug I have seen, and the reason I now ask “where in the flow does this check run?” before “is this check correct?” A check that runs too late is not a weak check. It is a log entry.

Bug three: a directory that Bash tried to execute

An isolated-environment helper took a temporary-directory argument. Somewhere in the plumbing, that argument was passed where a command was expected, so Bash attempted to execute a directory. The failure was not “wrong path” or “missing file”; it was a type error in an untyped language, expressed as an attempt to run something that is not a program.

The fix corrected argument handling: the temporary directory is an argument to a command, not the command itself. What I take from it is that in shell code, the position of an argument is its type. Nothing in the syntax distinguishes “this string names a directory” from “this string names a program to run,” so the review question is always which slot the value landed in — not whether the value is correct.

Tests that check behavior expose guarantees that were only stated

The pattern across all three bugs is the same: each one sat at a boundary I had stated as a guarantee but never exercised. Fallback behavior was assumed safe, but the case with no background process had never run. Sourcing was assumed blocked, but the block came after the mutation. An argument was assumed to be a path, but it landed in the slot the shell reads as a program name. The harness did not invent new requirements. It forced each existing assumption to meet the input that makes it hardest: no background PID, a source call that reaches parsing, an argument in the wrong slot.

That is the value I actually got out of the suite. Not coverage as a number, but a machine that asks each stated guarantee the awkward question, in a fresh environment, every run.

The limit I will not paper over

A Bash harness verifies behavior under the conditions it encodes. It cannot verify the guarantees the scripts promise about real sessions, about concurrent use, or about the executable the stub stands in for. It found three real bugs because those bugs lived in the decision logic the harness can reach: which branch runs, what status comes out, whether a mutation happens before a check. It would not find a bug that only appears when the real command, real state, and real timing line up — and a stub in place of the command, a throwaway environment in place of real state, and a case-by-case run in place of concurrent use are exactly what put that class of bug out of reach.

So I treat 48/48 as a strong signal about specific behaviors and no signal at all about general safety. If someone reads this as “the scripts are proven safe,” they have read a claim I did not make. The harness makes the scripts testable without risking a real session — which is what I built it for, and why all three bugs showed up as a failing test instead of as a broken session.