blog.rust.gay

Behave finds the CLI bugs a Rust rewrite's test suite misses

On November 13, a developer named Emir Mrkaljevic announced Behave on the Rust users forum. The pitch is simple enough to fit in a sentence: run the original command-line program as a black box, record what it does, then replay the same inputs against the rewrite and print every difference. Running that process against uutils, the Rust rewrites of coreutils, findutils, sed, and procps, led to 40 upstream bug reports and 47 fix pull requests, 33 of which were merged.

That is a useful result for uutils. It is also a useful result for anyone who has ever ported a command-line tool and assumed that passing the existing test suite meant the port was compatible. The bugs Behave found were not crashes or missing features. Most were small: a missing error message, a different exit code, an empty-operand edge case. Those are exactly the differences a hand-written test suite tends to skip, and exactly the differences that break shell scripts in production.

A rewrite is a compatibility project

The Rust rewrite of GNU coreutils is usually described as a memory-safety project, and that framing is correct as far as it goes. Rust removes whole classes of bugs: buffer overflows, use-after-free, data races. For utilities that parse untrusted input and run on every system, that is a real improvement. But a coreutils replacement is not judged only on whether it is memory-safe. It is judged on whether existing scripts keep working.

A shell script does not call sort through a typed API. It calls it with flags, pipes, files, environment variables, and a set of assumptions about what the output will look like and what the exit status will mean. The script may check $? to decide whether to continue. It may parse stderr to decide whether a warning is fatal. It may rely on the exact behavior of an empty operand, a missing file, or a flag combination that the documentation does not emphasize. None of that is captured by a unit test that checks the happy path.

This is a specific instance of a general problem. Once a program has users, its observable behavior becomes an interface, even the parts that were never specified. Command-line tools are particularly exposed because their interface is text: stdout, stderr, exit codes, and the formatting of error messages. A rewrite can be internally cleaner and still be incompatible, because compatibility is about the surface, not the implementation.

GNU coreutils has decades of accumulated behavior. Some of it is documented in the manual. Some of it is encoded in the test suite. Some of it is folklore, preserved by users who noticed that a particular version did a particular thing and wrote a script around it. A rewrite that aims to replace coreutils has to decide which of that behavior is intentional, which is accidental but load-bearing, and which can safely change. That is a compatibility project, not just a port.

The oracle problem

The hard part of testing a rewrite is knowing what the correct output is. If you write the test by hand, you are encoding your understanding of the original program. That understanding may be incomplete. You may test the cases you thought of, and miss the ones you did not. You may assert the behavior you expect rather than the behavior the original actually has. The test passes, the rewrite matches the test, and the difference survives.

This is the oracle problem: to test a program, you need an independent source of truth. For a rewrite, the best source of truth is often the original program itself. Run both, feed them the same input, and compare the output. That is differential testing, and it is an old idea. What Behave adds is a practical recording format and a workflow that fits a rewrite.

Behave runs the original binary with many inputs: flags, stdin, files, empty arguments, error paths. It records stdout, stderr, and the exit code of each run as plain YAML test cases. It then replays the same cases against the rewrite and prints every difference. The recorded cases are not a specification in the formal sense. They are a corpus of observed behavior, which is often more useful than a specification because it captures the details that the documentation leaves out.

The YAML format matters more than it might seem. A recorded case is text, so it can live in the repository, be reviewed in a pull request, and run in CI. It is not tied to Behave's implementation language. Behave itself is written in Go and ships as a single binary under the MIT license, but it does not care what language either side is in. A Rust rewrite can be tested against a C original, a Go rewrite against a Rust original, or two versions of the same tool against each other.

That language independence is a large part of the tool's usefulness. The Rust community is not the only group rewriting old command-line tools. Go, Zig, and other languages have their own rewrites, and the same compatibility questions apply. A recording format that is just YAML and a runner that is just a binary can be adopted without buying into a particular ecosystem.

Why hand-written tests miss the boring bugs

The 40 bugs Behave found in uutils are instructive precisely because they are boring. A missing error message is not a security vulnerability. A different exit code is not a crash. An empty-operand edge case is not a feature gap. But each of them can break a script that depends on the original behavior.

Consider exit codes. A shell script may run a command in a conditional and branch on whether it succeeded. If the original returns 1 for a particular error and the rewrite returns 2, the script's branch changes. The script may still run, but it may take the wrong path. If the script is part of a build system or a deployment pipeline, the wrong path can be expensive. The rewrite's unit tests may all pass, because they check the successful case and maybe one error case, not the full matrix of exit statuses.

Consider error messages. A script may grep stderr for a specific phrase to decide whether a failure is retryable. If the rewrite changes the wording, the grep fails, and the script treats a retryable error as fatal. Again, the rewrite is not wrong in any obvious sense. It is just different, and the difference is observable.

Consider empty operands. Many command-line tools have special behavior when an argument is empty, missing, or a dash. The behavior may be documented in a footnote, or not documented at all. A hand-written test suite tends to focus on representative inputs, not on the boundary cases that are easy to overlook. Behave's recording approach naturally covers them, because it runs the original with a broad set of inputs rather than a curated list.

There is a deeper reason hand-written tests miss these cases. When you write a test, you encode what you think the program should do. When you record the original, you encode what it actually does. The gap between those two is where compatibility bugs live. The gap is not a sign that the test author was careless. It is a structural consequence of testing against an internal model rather than against the reference implementation.

What 40 bugs and 33 merged fixes prove

The numbers are modest in absolute terms, and they should not be overread. Forty bug reports is not a complete audit of uutils. Forty-seven pull requests is not a guarantee that the rewrites are now compatible. The fact that 33 were merged is a sign that the reports were actionable, not a sign that the remaining 14 were invalid.

What the numbers do show is that differential testing finds real defects that the existing process missed. The uutils project has tests. It has contributors who care about compatibility. It has been developed for years. If a relatively simple recording tool can find 40 issues in that environment, the environment had a gap. That is the useful takeaway, and it is more important than the exact count.

The merge rate also says something about the quality of the reports. A bug report that says "the output is different" is hard to act on. A report that includes the exact input, the original stdout, the rewrite's stdout, the original stderr, the rewrite's stderr, and both exit codes is easy to reproduce and easy to fix. Behave's recorded cases are designed to produce that kind of report. The tool is not just finding differences; it is packaging them in a form that maintainers can use.

That packaging is a significant part of the value. Open-source maintainers are busy, and a vague bug report is a burden. A minimal, reproducible case is a gift. If a tool can turn a differential test failure into a minimal case automatically, it reduces the friction between finding a bug and fixing it. That is one reason the uutils maintainers merged so many of the PRs: the work was already done.

The limits of recording

Differential testing is powerful, but it is not a complete compatibility strategy. The first limitation is nondeterminism. Some programs produce output that varies between runs: timestamps, random values, process IDs, locale-dependent formatting, hash ordering. A recorded case that captures one run may not match the next. Behave can record the output, but it cannot know which parts are stable and which are incidental. The user has to normalize or exclude the unstable parts, and that requires judgment.

The second limitation is the environment. A command-line tool's behavior can depend on the file system, the locale, the terminal, the user's permissions, the available memory, and the versions of libraries it links against. A recording made on one machine may not reproduce on another. The corpus has to be run in a controlled environment, and the environment has to be documented. That is manageable, but it is work.

The third limitation is interactivity. Behave runs the original binary as a black box with inputs. That works well for filters and formatters that read stdin and write stdout. It works less well for tools that prompt the user, open a terminal, or depend on timing. A recording can capture a scripted interaction, but it cannot capture the full space of interactive behavior.

The fourth limitation is performance. A recorded case checks output, not speed. A rewrite can be perfectly compatible and still be too slow, or use too much memory, or behave differently under load. Differential testing can be extended to compare resource usage, but that is a separate dimension and a noisier one.

The fifth limitation is that the original may be wrong. Recording the original preserves its bugs as well as its features. If the rewrite fixes a bug, the differential test will report a difference, and the team has to decide whether the difference is an improvement or a regression. That decision is a judgment call, not something the tool can make. A compatibility corpus needs an allowlist for intentional differences, and the allowlist needs to be reviewed.

The sixth limitation is coverage. Behave runs the original with "lots of inputs," but no finite corpus covers every combination of flags, files, and environment. The tool improves coverage relative to a hand-written suite, but it does not prove equivalence. A rewrite can pass every recorded case and still differ on an input no one recorded. Differential testing shifts the burden of proof; it does not eliminate it.

Where it fits in CI

The natural place for Behave is CI, running against a pinned version of the original binary. The corpus lives in the repository as YAML. The CI job builds the rewrite, runs the recorded cases, and fails if any output differs. That gives the team a regression signal on every pull request, and it makes compatibility a continuous property rather than a one-time audit.

There are practical details. The original binary has to be available in the CI environment, which means a container image or a downloaded artifact. The version has to be pinned, because the original can change too. The corpus has to be maintained: new cases are added when a bug is found, and old cases are updated when a difference is accepted. The allowlist of intentional differences has to be explicit, so that a real regression is not silently absorbed.

The workflow also fits the way rewrites are actually developed. A contributor finds a difference, records a case, fixes the rewrite, and adds the case to the corpus. The case becomes a regression test that prevents the difference from coming back. Over time, the corpus grows into a detailed description of the original's behavior, written in the original's own outputs. That is a more durable artifact than a prose specification, because it is executable.

Behave also ships an MCP server, which lets an agent-driven workflow record and replay cases. That is a sign of the times, and it is potentially useful: an agent can run the original, capture the behavior, and propose a fix, with the recorded case as evidence. But the same caution applies. An agent can generate a plausible fix that makes the recorded case pass while breaking something else. The corpus is a safety net, not a substitute for review.

What to watch

The first thing to watch is adoption. A tool that found 40 bugs in one project is interesting. A tool that becomes part of the standard CI pipeline for rewrites is infrastructure. The difference depends on whether maintainers find it useful enough to integrate, and whether the corpus format survives contact with real projects.

The second is the corpus. The value of differential testing grows with the breadth of the recorded cases. If the uutils corpus becomes a shared, well-maintained description of GNU behavior, it will be useful beyond uutils. If it stays a collection of one-off cases, it will be useful mainly to the person who recorded them.

The third is integration with the rewrites themselves. The most valuable outcome would be for uutils and similar projects to adopt differential testing as a first-class part of their process, not as an external audit. That means recording cases when bugs are found, running the corpus in CI, and treating a difference as a bug until proven otherwise. The tool is only half of that; the process is the other half.

The fourth is the limits. Differential testing is not a proof of compatibility, and it is not a substitute for understanding the original's semantics. The teams that get the most from it will be the ones that combine it with a clear compatibility policy, a curated allowlist, and human review of the differences that matter.

The fifth is the broader pattern. Behave is a small tool with a general idea: when you rewrite a system, record the old system's behavior and compare. That idea applies to command-line tools, network protocols, file formats, and APIs. The Rust ecosystem is full of rewrites, and the same compatibility questions recur. A tool that makes differential testing easy is useful not because it is clever, but because it lowers the cost of doing the boring, necessary work of checking that the new thing behaves like the old thing.

Sources


← back to posts