How TOML Support in AfterConfig Was Tested
TOML in AfterConfig makes one promise on top of the old ones: the output is the vendor’s file with only your changes in it. Comments, spacing and every untouched line come through byte for byte. The tests try to catch it breaking that, or getting a value wrong while keeping the layout.
Reading TOML the way the spec does
AfterConfig has its own TOML reader, since the project uses no outside libraries. Its tests go through the TOML 1.0 spec: every kind of string and escape, integers in hex, octal and binary, underscores in numbers, the four date and time types, dotted keys, inline tables and arrays of tables.
Then come 32 files TOML forbids, each of which must be refused with a line number. A key set twice, a table defined twice, a dotted key reaching into an inline table, an integer past 64 bits, a leading zero, a string left open, and more.
One change at a time
Two tests make one change at a time and check the result.
The first is random. It builds 400 TOML documents from awkward values, with comments between the lines, then sets, deletes or renames a random field. The result must read back with exactly the intended values, and a set must keep every comment.
The second starts from a file written by hand in every spelling TOML allows: hex, underscores, a literal string, a multi-line string, a multi-line array with comments inside, dotted keys, an inline table, arrays of tables and no final newline. It runs 22 changes on that file, and again with Windows line endings. Each must be made on the lines, with no full rewrite, keep the vendor’s comments and line endings, and add no final newline.
What the review found
An independent review spent a while trying to break it before release. It found no serious bug and six medium ones:
- An overlay change that was already in place still rewrote its line, so
0x1F90became8080and a comment inside a multi-line array was lost. - An error message about an unquoted value printed the value, which could be a password.
- A file nested a few million levels deep crashed the program. The JSON reader had the same weakness, and since patch, adapt and record read request bodies with it, that one mattered more.
- Deleting a table took the comment above the next table with it.
- Deleting the last of a table’s dotted keys rewrote the whole file without comments.
- Text set on a float or a date could come out as a bare, invalid date.
The smaller findings included dates like 2026-13-45 being accepted and a byte order mark being refused. All of them were fixed and each has its own test. Nesting is now capped while reading, for TOML and for JSON bodies alike.
The recorded run has 444 tests, all passing with Go’s race detector on, on Linux and macOS. The release smoke test applies the TOML example on every system it ships for and checks the comment lines are still there. The raw results are in test/results in the code repository.