Revert "Allow Flutter golden file tests to be flaky (#114450)" (#114902)

This reverts commit 53e6876226.
This commit is contained in:
Zachary Anderson
2022-11-08 09:26:55 -08:00
committed by GitHub
parent a1432a9c5d
commit a84e369bd2
35 changed files with 1351 additions and 2093 deletions

View File

@@ -98,14 +98,14 @@ class TestGoldenComparator {
return _processManager.start(command, environment: environment);
}
Future<String?> compareGoldens(Uri testUri, Uint8List bytes, Uri goldenKey, bool? updateGoldens, Map<String, dynamic>? customProperties) async {
Future<String?> compareGoldens(Uri testUri, Uint8List bytes, Uri goldenKey, bool? updateGoldens) async {
final File imageFile = await (await tempDir.createTemp('image')).childFile('image').writeAsBytes(bytes);
final TestGoldenComparatorProcess? process = await _processForTestFile(testUri);
if (process == null) {
return 'process was null';
}
process.sendCommand(imageFile, goldenKey, updateGoldens, customProperties);
process.sendCommand(imageFile, goldenKey, updateGoldens);
final Map<String, dynamic> result = await process.getResponse();
@@ -152,13 +152,11 @@ class TestGoldenComparatorProcess {
await process.exitCode;
}
void sendCommand(File imageFile, Uri? goldenKey, bool? updateGoldens, Map<String, dynamic>? customProperties) {
void sendCommand(File imageFile, Uri? goldenKey, bool? updateGoldens) {
final Object command = jsonEncode(<String, dynamic>{
'imageFile': imageFile.path,
'key': goldenKey.toString(),
'update': updateGoldens,
if (customProperties != null)
'customProperties': customProperties,
});
_logger.printTrace('Preparing to send command: $command');
process.stdin.writeln(command);
@@ -170,22 +168,7 @@ class TestGoldenComparatorProcess {
return streamIterator.current;
}
/// Generates the source code for the comparator process for the test file.
///
/// If a test configuation exists for the tested package, uses its
/// implementation. Otherwise, uses the default implementation.
static String generateBootstrap(File testFile, Uri testUri, {required Logger logger}) {
final File? webTestConfigFile = findWebTestConfigFile(testFile, logger);
if (webTestConfigFile != null) {
return _generateBootstrapWithWebTestConfig(webTestConfigFile, testFile, testUri);
} else {
return _generateBasicBootstrap(testFile, testUri, logger: logger);
}
}
// Generates the bootstrap used by tests that either don't have a test
// configuration file, or don't have a `flutter_web_test_config.dart`.
static String _generateBasicBootstrap(File testFile, Uri testUri, {required Logger logger}) {
final File? testConfigFile = findTestConfigFile(testFile, logger);
// Generate comparator process for the file.
return '''
@@ -228,19 +211,6 @@ void main() async {
}
}
${testConfigFile != null ? '});' : ''}
}
''';
}
// Generates the bootstrap used by tests that have a `flutter_web_test_config.dart`.
static String _generateBootstrapWithWebTestConfig(File webTestConfigFile, File testFile, Uri testUri) {
return '''
import 'package:flutter_test/flutter_test.dart';
import '${Uri.file(webTestConfigFile.path)}' as web_test_config;
void main() async {
final String testUri = '$testUri';
goldenFileComparator = LocalFileComparator(Uri.parse(testUri));
await web_test_config.startWebTestHostConfiguration(testUri);
}
''';
}

View File

@@ -343,9 +343,10 @@ class FlutterWebPlatform extends PlatformPlugin {
}
Future<shelf.Response> _goldenFileHandler(shelf.Request request) async {
if (request.method == 'POST' && request.url.path.contains('flutter_goldens')) {
final String requestJson = await request.readAsString();
final Map<String, Object?> body = json.decode(requestJson) as Map<String, Object?>;
if (request.url.path.contains('flutter_goldens')) {
final Map<String, Object?> body = json.decode(await request.readAsString()) as Map<String, Object?>;
final Uri goldenKey = Uri.parse(body['key']! as String);
final Uri testUri = Uri.parse(body['testUri']! as String);
final num width = body['width']! as num;
final num height = body['height']! as num;
Uint8List bytes;
@@ -382,10 +383,7 @@ class FlutterWebPlatform extends PlatformPlugin {
return shelf.Response.ok('Unknown error, bytes is null');
}
final Uri goldenKey = Uri.parse(body['key']! as String);
final Uri testUri = Uri.parse(body['testUri']! as String);
final Map<String, dynamic>? customProperties = body['customProperties'] as Map<String, dynamic>?;
final String? errorMessage = await _testGoldenComparator.compareGoldens(testUri, bytes, goldenKey, updateGoldens, customProperties);
final String? errorMessage = await _testGoldenComparator.compareGoldens(testUri, bytes, goldenKey, updateGoldens);
return shelf.Response.ok(errorMessage ?? 'true');
} else {
return shelf.Response.notFound('Not Found');

View File

@@ -9,36 +9,23 @@ import '../base/logger.dart';
/// test harness if it exists in the project directory hierarchy.
const String _kTestConfigFileName = 'flutter_test_config.dart';
/// The name of the web test configuration file that will be discovered by the
/// test harness if it exists in the project directory hierarchy.
const String _kWebTestConfigFileName = 'flutter_web_test_config.dart';
/// The name of the file that signals the root of the project and that will
/// cause the test harness to stop scanning for configuration files.
const String _kProjectRootSentinel = 'pubspec.yaml';
/// Find the `flutter_test_config.dart` file for a specific test file.
File? findTestConfigFile(File testFile, Logger logger) {
return _findConfigFile(testFile, _kTestConfigFileName, logger);
}
/// Find the `flutter_web_test_config.dart` file for a specific test file.
File? findWebTestConfigFile(File testFile, Logger logger) {
return _findConfigFile(testFile, _kWebTestConfigFileName, logger);
}
File? _findConfigFile(File testFile, String configFileName, Logger logger) {
File? testConfigFile;
Directory directory = testFile.parent;
while (directory.path != directory.parent.path) {
final File configFile = directory.childFile(configFileName);
final File configFile = directory.childFile(_kTestConfigFileName);
if (configFile.existsSync()) {
logger.printTrace('Discovered $configFileName in ${directory.path}');
logger.printTrace('Discovered $_kTestConfigFileName in ${directory.path}');
testConfigFile = configFile;
break;
}
if (directory.childFile(_kProjectRootSentinel).existsSync()) {
logger.printTrace('Stopping scan for $configFileName; '
logger.printTrace('Stopping scan for $_kTestConfigFileName; '
'found project root at ${directory.path}');
break;
}

View File

@@ -224,8 +224,7 @@ String generateTestEntrypoint({
Future<void> main() async {
ui.debugEmulateFlutterTesterEnvironment = true;
await ui.webOnlyInitializePlatform();
webTestUri = Uri.parse('${Uri.file(absolutePath)}');
webGoldenComparator = DefaultWebGoldenComparator(webTestUri);
webGoldenComparator = DefaultWebGoldenComparator(Uri.parse('${Uri.file(absolutePath)}'));
(ui.window as dynamic).debugOverrideDevicePixelRatio(3.0);
(ui.window as dynamic).webOnlyDebugPhysicalSizeOverride = const ui.Size(2400, 1800);

View File

@@ -41,13 +41,13 @@ void main() {
final MemoryIOSink ioSink = mockProcess.stdin as MemoryIOSink;
final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess, logger: BufferLogger.test());
process.sendCommand(imageFile, goldenKey, false, <String, String>{'additional data' : 'data'});
process.sendCommand(imageFile, goldenKey, false);
final Map<String, dynamic> response = await process.getResponse();
final String stringToStdin = ioSink.getAndClear();
expect(response, expectedResponse);
expect(stringToStdin, '{"imageFile":"test_image_file","key":"file://golden_key/","update":false,"customProperties":{"additional data":"data"}}\n');
expect(stringToStdin, '{"imageFile":"test_image_file","key":"file://golden_key/","update":false}\n');
});
testWithoutContext('can handle multiple requests', () async {
@@ -64,21 +64,18 @@ void main() {
final MemoryIOSink ioSink = mockProcess.stdin as MemoryIOSink;
final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess, logger: BufferLogger.test());
process.sendCommand(imageFile, goldenKey, false, null);
process.sendCommand(imageFile, goldenKey, false);
final Map<String, dynamic> response1 = await process.getResponse();
process.sendCommand(imageFile2, goldenKey2, true, null);
process.sendCommand(imageFile2, goldenKey2, true);
final Map<String, dynamic> response2 = await process.getResponse();
final String stringToStdin = ioSink.getAndClear();
expect(response1, expectedResponse1);
expect(response2, expectedResponse2);
expect(
stringToStdin,
'{"imageFile":"test_image_file","key":"file://golden_key/","update":false}\n'
'{"imageFile":"second_test_image_file","key":"file://second_golden_key/","update":true}\n');
expect(stringToStdin, '{"imageFile":"test_image_file","key":"file://golden_key/","update":false}\n{"imageFile":"second_test_image_file","key":"file://second_golden_key/","update":true}\n');
});
testWithoutContext('ignores anything that does not look like JSON', () async {
@@ -97,7 +94,7 @@ Other JSON data after the initial data
final MemoryIOSink ioSink = mockProcess.stdin as MemoryIOSink;
final TestGoldenComparatorProcess process = TestGoldenComparatorProcess(mockProcess,logger: BufferLogger.test());
process.sendCommand(imageFile, goldenKey, false, null);
process.sendCommand(imageFile, goldenKey, false);
final Map<String, dynamic> response = await process.getResponse();
final String stringToStdin = ioSink.getAndClear();

View File

@@ -61,7 +61,7 @@ void main() {
webRenderer: WebRendererMode.html,
);
final String? result = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false, null);
final String? result = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false);
expect(result, null);
});
@@ -90,7 +90,7 @@ void main() {
webRenderer: WebRendererMode.canvaskit,
);
final String? result = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false, null);
final String? result = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false);
expect(result, 'some message');
});
@@ -123,10 +123,10 @@ void main() {
webRenderer: WebRendererMode.html,
);
final String? result1 = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false, null);
final String? result1 = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false);
expect(result1, 'some message');
final String? result2 = await comparator.compareGoldens(testUri, imageBytes, goldenKey2, false, null);
final String? result2 = await comparator.compareGoldens(testUri, imageBytes, goldenKey2, false);
expect(result2, 'some other message');
});
@@ -168,10 +168,10 @@ void main() {
webRenderer: WebRendererMode.canvaskit,
);
final String? result1 = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false, null);
final String? result1 = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false);
expect(result1, 'some message');
final String? result2 = await comparator.compareGoldens(testUri2, imageBytes, goldenKey2, false, null);
final String? result2 = await comparator.compareGoldens(testUri2, imageBytes, goldenKey2, false);
expect(result2, 'some other message');
});
@@ -203,7 +203,7 @@ void main() {
webRenderer: WebRendererMode.html,
);
final String? result = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false, null);
final String? result = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false);
expect(result, null);
await comparator.close();

View File

@@ -117,7 +117,7 @@ void main() {
final List<String> allowedPaths = <String>[
fileSystem.path.join(flutterTools, 'lib', 'src', 'test', 'flutter_platform.dart'),
fileSystem.path.join(flutterTools, 'lib', 'src', 'test', 'flutter_web_platform.dart'),
fileSystem.path.join(flutterTools, 'lib', 'src', 'test', 'flutter_goldens.dart'),
fileSystem.path.join(flutterTools, 'lib', 'src', 'test', 'test_wrapper.dart'),
];
bool isNotAllowed(FileSystemEntity entity) => allowedPaths.every((String path) => path != entity.path);