compareOrCreateGolden function
Core golden file comparison logic. Package-private for testability.
- If
updateis true: writeactualtogoldenPath, return (pass). - If golden file does not exist: create it with
actual, print info message, return (pass). - If golden file exists: read expected, compare with
actual.- Match: return (pass).
- Mismatch: throw
TestFailurewith a readable line-by-line diff.
Implementation
void compareOrCreateGolden(String actual, String goldenPath, {bool? update}) {
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()) {
_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));
}