04 / RELIABLE ADMINISTRATION TOOLS · LESSON 15

Test preview and apply guarantees

Write filesystem assertions that reject unsafe preview and broken apply behavior.

16 min · learn at your pace

Test what must not change

For a maintenance tool, correctness includes the absence of unwanted writes. A preview that prints a convincing plan but modifies the target has failed its contract. A test suite should inspect filesystem bytes after preview, after apply and after a repeated application.

The supplied trusted helper write_setting accepts a target path, a value and a mode. Your test harness creates an isolated temporary configuration, then calls that helper. Keeping test files separate from the real exercise configuration makes the tests repeatable and easy to clean up.

Compare exact results

Use cmp -s against expected files instead of merely looking for a substring. A helper that appends a correct line to an old setting is still wrong. Save each mismatch in a counter and continue to the remaining assertions so every promised behavior is exercised.

The checker substitutes deliberately broken helpers: one writes during preview, one appends during apply and one never applies. Your harness must reject them. A printed “passed” message alone cannot award completion, because trusted checks outside learner Bash also inspect the exit status and expected behavior. Explicit counters are clearer here than relying on set -e and its context-dependent exceptions.

Try an example.sh
source ./lib/settings.sh
printf 'retention_days=14\n' > rehearsal.conf
write_setting rehearsal.conf 30 preview
cat rehearsal.conf
YOUR TURN

Put it into practice.

Source trusted lib/settings.sh. In an isolated temporary directory, create a target containing retention_days=14 and a newline. Test these three guarantees, in order: write_setting target 30 preview leaves exact original bytes; write_setting target 30 apply writes exactly retention_days=30 plus a newline; applying 30 again leaves those same bytes. Run all three assertions and clean up temporary files. Print 3 tests passed and a newline only when all pass. Otherwise exit nonzero with Administration tests failed on stderr, and no success message.

See an explained solution
One way to solve it.sh
source ./lib/settings.sh || exit 1
tmp=$(mktemp -d "${TMPDIR:-/tmp}/rocks-tests.XXXXXX") || exit 1
trap 'rm -rf "$tmp"' EXIT
printf 'retention_days=14\n' > "$tmp/original"
printf 'retention_days=30\n' > "$tmp/expected"
cp "$tmp/original" "$tmp/target" || exit 1
failures=0
write_setting "$tmp/target" 30 preview || failures=$((failures+1))
cmp -s "$tmp/target" "$tmp/original" || failures=$((failures+1))
write_setting "$tmp/target" 30 apply || failures=$((failures+1))
cmp -s "$tmp/target" "$tmp/expected" || failures=$((failures+1))
write_setting "$tmp/target" 30 apply || failures=$((failures+1))
cmp -s "$tmp/target" "$tmp/expected" || failures=$((failures+1))
[ "$failures" -eq 0 ] || { printf 'Administration tests failed\n' >&2;exit 1; }
printf '3 tests passed\n'

The harness verifies non-mutation during preview and exact replacement during both apply calls. Byte comparisons reject appended or incomplete settings, while substituted faulty libraries prove the assertions can detect each promised failure mode.