Skip Navigation

Characterization tests for a CLI rewrite: record the old binary, replay against the new one

When a command line tool gets rewritten (new language, new version, big refactor), the existing unit tests usually say little about whether it still behaves like the old one. The old binary is the real spec, so I wanted golden master style tests generated from it instead of written by hand.

I built a small open source tool for that, behave (Go, MIT). It reads the tool's --help, generates a few hundred cases (each flag, a few values, some stdin and file inputs), runs them against the original binary and records stdout, stderr, exit code and touched files into plain YAML. Then it replays the same cases against the replacement and groups the differences. Noise like timestamps or paths can be masked with regexes, and the recorded spec can be committed and run in CI.

The main test so far was the Rust rewrites of GNU coreutils, findutils, sed and procps. Most of what it found was not crashes but small contract changes: different exit codes, error message wording, and option parsing corners. That ended up as 44 upstream bug reports and 51 fix PRs, 34 merged.

Two lessons for anyone doing this kind of testing: record from the exact version the rewrite targets (GNU 9.7 and 9.12 differ in a few outputs), and generated cases need a review pass, because some differences are intentional.

I would like to hear how others handle behavioural equivalence testing for rewrites, and where generated characterization tests have bitten you.

github.com

Comments