dart fix: report when --compare-to-golden has unchanged files

I was pretty confused about what was being compared until this change
was able to highlight that no edits were made.

Bug: https://github.com/dart-lang/sdk/issues/47678
Change-Id: I1d37070437658063d4b84f2fb5f4a33923bf5d92
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/221840
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Sam Rawlins 2021-12-01 20:44:06 +00:00 committed by Commit Bot
parent 780bc26e1b
commit ed086ad778

View file

@ -7,6 +7,7 @@ import 'dart:io' as io;
import 'package:analysis_server_client/protocol.dart' hide AnalysisError;
import 'package:intl/intl.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import '../analysis_server.dart';
@ -220,15 +221,22 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
}
try {
var expectedCode = expectFile.readAsStringSync();
var actualCode =
fileContentCache[filePath] ?? originalFile.readAsStringSync();
var actualIsOriginal = !fileContentCache.containsKey(filePath);
var actualCode = actualIsOriginal
? originalFile.readAsStringSync()
: fileContentCache[filePath];
// Use a whitespace insensitive comparison.
if (_compressWhitespace(actualCode) !=
_compressWhitespace(expectedCode)) {
result.failCount++;
// TODO(brianwilkerson) Do a better job of displaying the differences.
// It's very hard to see the diff with large files.
_reportFailure(filePath, actualCode, expectedCode);
_reportFailure(
filePath,
actualCode,
expectedCode,
actualIsOriginal: actualIsOriginal,
);
} else {
result.passCount++;
}
@ -314,12 +322,17 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
/// Report that the [actualCode] produced by applying fixes to the content of
/// [filePath] did not match the [expectedCode].
void _reportFailure(String filePath, String actualCode, String expectedCode) {
void _reportFailure(String filePath, String actualCode, String expectedCode,
{@required bool actualIsOriginal}) {
log.stdout('Failed when applying fixes to $filePath');
log.stdout('Expected:');
log.stdout(expectedCode);
log.stdout('');
if (actualIsOriginal) {
log.stdout('Actual (original code was unchanged):');
} else {
log.stdout('Actual:');
}
log.stdout(actualCode);
}