compareOrCreateGolden function
void
compareOrCreateGolden(})
Core golden file comparison logic.
- If
updateis true: writeactualtogoldenPath, return (pass). - If golden file does not exist:
createMissingtrue: create it withactual, print info message, return (pass).createMissingfalse: throwTestFailurenaming the missing file.
- If golden file exists: read expected, compare with
actual.- Match: return (pass).
- Mismatch: throw
TestFailurewith a readable line-by-line diff.
update defaults to updateGoldens; createMissing defaults to true,
except on CI (CI=true).
Implementation
void compareOrCreateGolden(String actual, String goldenPath, {bool? update, bool? createMissing}) {
final shouldUpdate = update ?? updateGoldens;
final file = File(goldenPath);
if (shouldUpdate) {
_writeGolden(file, actual);
// ignore: avoid_print
print('Golden file updated: $goldenPath');
return;
}
if (!file.existsSync()) {
if (!(createMissing ?? !_onCi)) {
fail(
'Golden file missing: $goldenPath\n\n'
'Create it with a local run (or TRELLIS_UPDATE_GOLDENS=true dart test) and commit it.',
);
}
_writeGolden(file, actual);
// ignore: avoid_print
print('Golden file created: $goldenPath (first run)');
return;
}
final expected = file.readAsStringSync();
if (expected == actual) return;
fail(_buildDiff(expected, actual, goldenPath));
}