Migrate frontend_server_test to be null safe.

Change-Id: If31640eaa2de3ac91df377f4e9e4195e2b5cd641
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/260744
Commit-Queue: Siva Annamalai <asiva@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
This commit is contained in:
asiva 2022-09-23 15:57:39 +00:00 committed by Commit Bot
parent 7bfacba429
commit 1a8335b42b

View file

@ -2,7 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a // for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE.md file. // BSD-style license that can be found in the LICENSE.md file.
// @dart = 2.9
// ignore_for_file: empty_catches // ignore_for_file: empty_catches
import 'dart:async'; import 'dart:async';
@ -41,12 +40,12 @@ class _MockedBinaryPrinterFactory implements BinaryPrinterFactory {
typedef VerifyCompile = void Function(String entryPoint, ArgResults opts); typedef VerifyCompile = void Function(String entryPoint, ArgResults opts);
typedef VerifyInvalidate = void Function(Uri uri); typedef VerifyInvalidate = void Function(Uri uri);
typedef VerifyRecompileDelta = void Function(String entryPoint); typedef VerifyRecompileDelta = void Function(String? entryPoint);
typedef Verify = void Function(); typedef Verify = void Function();
nopVerifyCompile(String entryPoint, ArgResults opts) {} nopVerifyCompile(String entryPoint, ArgResults opts) {}
nopVerifyInvalidate(Uri uri) {} nopVerifyInvalidate(Uri uri) {}
nopVerifyRecompileDelta(String entryPoint) {} nopVerifyRecompileDelta(String? entryPoint) {}
nopVerify() {} nopVerify() {}
class _MockedCompiler implements CompilerInterface { class _MockedCompiler implements CompilerInterface {
@ -63,7 +62,7 @@ class _MockedCompiler implements CompilerInterface {
} }
@override @override
Future<Null> recompileDelta({String entryPoint}) async { Future<Null> recompileDelta({String? entryPoint}) async {
verifyRecompileDelta(entryPoint); verifyRecompileDelta(entryPoint);
} }
@ -81,7 +80,7 @@ class _MockedCompiler implements CompilerInterface {
Future<bool> compile( Future<bool> compile(
String entryPoint, String entryPoint,
ArgResults opts, { ArgResults opts, {
IncrementalCompiler generator, IncrementalCompiler? generator,
}) async { }) async {
verifyCompile(entryPoint, opts); verifyCompile(entryPoint, opts);
return true; return true;
@ -105,7 +104,7 @@ class _MockedIncrementalCompiler implements IncrementalCompiler {
bool get initialized => false; bool get initialized => false;
@override @override
Future<IncrementalCompilerResult> compile({List<Uri> entryPoints}) async { Future<IncrementalCompilerResult> compile({List<Uri>? entryPoints}) async {
return Future<IncrementalCompilerResult>.value( return Future<IncrementalCompilerResult>.value(
IncrementalCompilerResult(Component())); IncrementalCompilerResult(Component()));
} }
@ -292,7 +291,7 @@ void main() async {
expect(uri.path, contains('file${counter++}.dart')); expect(uri.path, contains('file${counter++}.dart'));
invalidated += 1; invalidated += 1;
}; };
final verifyR = (String entryPoint) { final verifyR = (String? entryPoint) {
expect(invalidated, equals(2)); expect(invalidated, equals(2));
expect(entryPoint, equals(null)); expect(entryPoint, equals(null));
recompileDeltaCalled.sendPort.send(true); recompileDeltaCalled.sendPort.send(true);
@ -319,7 +318,7 @@ void main() async {
test('recompile one file with widget cache does not fail', () async { test('recompile one file with widget cache does not fail', () async {
final recompileDeltaCalled = ReceivePort(); final recompileDeltaCalled = ReceivePort();
bool invalidated = false; bool invalidated = false;
final verifyR = (String entryPoint) { final verifyR = (String? entryPoint) {
expect(invalidated, equals(true)); expect(invalidated, equals(true));
expect(entryPoint, equals(null)); expect(entryPoint, equals(null));
recompileDeltaCalled.sendPort.send(true); recompileDeltaCalled.sendPort.send(true);
@ -355,7 +354,7 @@ void main() async {
expect(uri.path, contains('file${counter++}.dart')); expect(uri.path, contains('file${counter++}.dart'));
invalidated += 1; invalidated += 1;
}; };
final verifyR = (String entryPoint) { final verifyR = (String? entryPoint) {
expect(invalidated, equals(2)); expect(invalidated, equals(2));
expect(entryPoint, equals('file2.dart')); expect(entryPoint, equals('file2.dart'));
recompileDeltaCalled.sendPort.send(true); recompileDeltaCalled.sendPort.send(true);
@ -438,7 +437,7 @@ void main() async {
expect(uri.path, contains('file${counter++}.dart')); expect(uri.path, contains('file${counter++}.dart'));
invalidate += 1; invalidate += 1;
}; };
final verifyR = (String entryPoint) { final verifyR = (String? entryPoint) {
expect(compile, equals(true)); expect(compile, equals(true));
expect(invalidate, equals(2)); expect(invalidate, equals(2));
expect(acceptDelta, equals(true)); expect(acceptDelta, equals(true));
@ -474,7 +473,7 @@ void main() async {
'--incremental', '--incremental',
]; ];
Directory tempDir; late Directory tempDir;
setUp(() { setUp(() {
tempDir = Directory.systemTemp.createTempSync(); tempDir = Directory.systemTemp.createTempSync();
}); });
@ -490,7 +489,7 @@ void main() async {
final IOSink ioSink = IOSink(stdoutStreamController.sink); final IOSink ioSink = IOSink(stdoutStreamController.sink);
ReceivePort receivedResult = ReceivePort(); ReceivePort receivedResult = ReceivePort();
String boundaryKey; String? boundaryKey;
stdoutStreamController.stream stdoutStreamController.stream
.transform(utf8.decoder) .transform(utf8.decoder)
.transform(const LineSplitter()) .transform(const LineSplitter())
@ -501,7 +500,7 @@ void main() async {
boundaryKey = s.substring(RESULT_OUTPUT_SPACE.length); boundaryKey = s.substring(RESULT_OUTPUT_SPACE.length);
} }
} else { } else {
if (s.startsWith(boundaryKey)) { if (s.startsWith(boundaryKey!)) {
boundaryKey = null; boundaryKey = null;
receivedResult.sendPort.send(true); receivedResult.sendPort.send(true);
} }
@ -563,7 +562,7 @@ void main() async {
computePlatformBinariesLocation().resolve('ddc_sdk.dill'); computePlatformBinariesLocation().resolve('ddc_sdk.dill');
final sdkRoot = computePlatformBinariesLocation(); final sdkRoot = computePlatformBinariesLocation();
Directory tempDir; late Directory tempDir;
setUp(() { setUp(() {
var systemTempDir = Directory.systemTemp; var systemTempDir = Directory.systemTemp;
tempDir = systemTempDir.createTempSync('frontendServerTest'); tempDir = systemTempDir.createTempSync('frontendServerTest');
@ -619,7 +618,7 @@ void main() async {
frontendServer.compileExpression('2+2', file.uri, isStatic: null); frontendServer.compileExpression('2+2', file.uri, isStatic: null);
count += 1; count += 1;
} else if (count == 1) { } else if (count == 1) {
expect(result.errorsCount, isNull); expect(result.errorsCount, equals(0));
// Previous request should have failed because isStatic was blank // Previous request should have failed because isStatic was blank
expect(compiledResult.status, isNull); expect(compiledResult.status, isNull);
@ -693,7 +692,7 @@ void main() async {
} else if (count == 2) { } else if (count == 2) {
// Third request is to 'compile-expression-to-js' that fails // Third request is to 'compile-expression-to-js' that fails
// due to non-web target // due to non-web target
expect(result.errorsCount, isNull); expect(result.errorsCount, equals(0));
expect(compiledResult.status, isNull); expect(compiledResult.status, isNull);
frontendServer.compileExpression('2+2', file.uri, isStatic: false); frontendServer.compileExpression('2+2', file.uri, isStatic: false);
@ -1552,9 +1551,9 @@ class BarState extends State<FizzWidget> {
group('http uris', () { group('http uris', () {
var host = 'localhost'; var host = 'localhost';
File dillFile; late File dillFile;
int port; late int port;
HttpServer server; late HttpServer server;
setUp(() async { setUp(() async {
dillFile = File('${tempDir.path}/app.dill'); dillFile = File('${tempDir.path}/app.dill');
@ -1726,7 +1725,7 @@ class BarState extends State<FizzWidget> {
test('OK', () async { test('OK', () async {
await runWithServer((requestChannel) async { await runWithServer((requestChannel) async {
await requestChannel.sendRequest<Uint8List>('dill.put', { await requestChannel.sendRequest<Uint8List?>('dill.put', {
'uri': 'vm:dill', 'uri': 'vm:dill',
'bytes': Uint8List(256), 'bytes': Uint8List(256),
}); });
@ -1755,7 +1754,7 @@ class BarState extends State<FizzWidget> {
test('OK', () async { test('OK', () async {
await runWithServer((requestChannel) async { await runWithServer((requestChannel) async {
await requestChannel.sendRequest<Uint8List>('dill.remove', { await requestChannel.sendRequest<Uint8List?>('dill.remove', {
'uri': 'vm:dill', 'uri': 'vm:dill',
}); });
}); });
@ -1907,7 +1906,7 @@ void main(List<String> arguments, SendPort sendPort) {
test('compile to JavaScript weak null safety', () async { test('compile to JavaScript weak null safety', () async {
var file = File('${tempDir.path}/foo.dart')..createSync(); var file = File('${tempDir.path}/foo.dart')..createSync();
file.writeAsStringSync("// @dart = 2.9\nmain() {\n}\n"); file.writeAsStringSync("main() {\n}\n");
var packages = File('${tempDir.path}/.dart_tool/package_config.json') var packages = File('${tempDir.path}/.dart_tool/package_config.json')
..createSync() ..createSync()
..writeAsStringSync(jsonEncode({ ..writeAsStringSync(jsonEncode({
@ -1939,7 +1938,7 @@ void main(List<String> arguments, SendPort sendPort) {
test('compile to JavaScript weak null safety then non-existent file', test('compile to JavaScript weak null safety then non-existent file',
() async { () async {
var file = File('${tempDir.path}/foo.dart')..createSync(); var file = File('${tempDir.path}/foo.dart')..createSync();
file.writeAsStringSync("// @dart = 2.9\nmain() {\n}\n"); file.writeAsStringSync("main() {\n}\n");
var packages = File('${tempDir.path}/.dart_tool/package_config.json') var packages = File('${tempDir.path}/.dart_tool/package_config.json')
..createSync() ..createSync()
..writeAsStringSync(jsonEncode({ ..writeAsStringSync(jsonEncode({
@ -2970,7 +2969,7 @@ e() {
}, timeout: Timeout.factor(8)); }, timeout: Timeout.factor(8));
test('compile with(out) warning', () async { test('compile with(out) warning', () async {
Future runTest({bool hideWarnings}) async { Future runTest({bool hideWarnings = true}) async {
var file = File('${tempDir.path}/foo.dart')..createSync(); var file = File('${tempDir.path}/foo.dart')..createSync();
file.writeAsStringSync(""" file.writeAsStringSync("""
main() {} main() {}
@ -3045,10 +3044,10 @@ Uri computePlatformBinariesLocation() {
} }
class CompilationResult { class CompilationResult {
String filename; late String filename;
int errorsCount; int errorsCount = 0;
CompilationResult.parse(String filenameAndErrorCount) { CompilationResult.parse(String? filenameAndErrorCount) {
if (filenameAndErrorCount == null) { if (filenameAndErrorCount == null) {
return; return;
} }
@ -3063,10 +3062,10 @@ class OutputParser {
bool expectSources = true; bool expectSources = true;
StreamController<Result> _receivedResults; StreamController<Result> _receivedResults;
List<String> _receivedSources; List<String>? _receivedSources;
String _boundaryKey; String? _boundaryKey;
bool _readingSources; bool _readingSources = false;
OutputParser(this._receivedResults); OutputParser(this._receivedResults);
void listener(String s) { void listener(String s) {
@ -3080,7 +3079,8 @@ class OutputParser {
return; return;
} }
if (s.startsWith(_boundaryKey)) { var bKey = _boundaryKey!;
if (s.startsWith(bKey)) {
// First boundaryKey separates compiler output from list of sources // First boundaryKey separates compiler output from list of sources
// (if we expect list of sources, which is indicated by receivedSources // (if we expect list of sources, which is indicated by receivedSources
// being not null) // being not null)
@ -3091,29 +3091,29 @@ class OutputParser {
// Second boundaryKey indicates end of frontend server response // Second boundaryKey indicates end of frontend server response
expectSources = true; expectSources = true;
_receivedResults.add(Result( _receivedResults.add(Result(
s.length > _boundaryKey.length s.length > bKey.length
? s.substring(_boundaryKey.length + 1) ? s.substring(bKey.length + 1)
: null, : null,
_receivedSources)); _receivedSources!));
_boundaryKey = null; _boundaryKey = null;
} else { } else {
if (_readingSources) { if (_readingSources) {
if (_receivedSources == null) { if (_receivedSources == null) {
_receivedSources = <String>[]; _receivedSources = <String>[];
} }
_receivedSources.add(s); _receivedSources!.add(s);
} }
} }
} }
} }
class Result { class Result {
String status; String? status;
List<String> sources; List<String> sources;
Result(this.status, this.sources); Result(this.status, this.sources);
void expectNoErrors({String filename}) { void expectNoErrors({String? filename}) {
var result = CompilationResult.parse(status); var result = CompilationResult.parse(status);
expect(result.errorsCount, equals(0)); expect(result.errorsCount, equals(0));
if (filename != null) { if (filename != null) {
@ -3225,10 +3225,10 @@ class FrontendServer {
/// [boundaryKey] is used as the boundary-key in the communication with the /// [boundaryKey] is used as the boundary-key in the communication with the
/// frontend server. /// frontend server.
// TODO(johnniwinther): Use (required) named arguments. // TODO(johnniwinther): Use (required) named arguments.
void recompile(Uri invalidatedUri, void recompile(Uri? invalidatedUri,
{String boundaryKey = 'abc', {String boundaryKey = 'abc',
List<Uri> invalidatedUris, List<Uri>? invalidatedUris,
String entryPoint}) { String? entryPoint}) {
invalidatedUris ??= [if (invalidatedUri != null) invalidatedUri]; invalidatedUris ??= [if (invalidatedUri != null) invalidatedUri];
outputParser.expectSources = true; outputParser.expectSources = true;
inputStreamController.add('recompile ' inputStreamController.add('recompile '
@ -3251,7 +3251,7 @@ class FrontendServer {
/// frontend server. /// frontend server.
// TODO(johnniwinther): Use (required) named arguments. // TODO(johnniwinther): Use (required) named arguments.
void compileExpression(String expression, Uri library, void compileExpression(String expression, Uri library,
{String boundaryKey = 'abc', String className = '', bool isStatic}) { {String boundaryKey = 'abc', String className = '', bool? isStatic}) {
// 'compile-expression <boundarykey> // 'compile-expression <boundarykey>
// expression // expression
// definitions (one per line) // definitions (one per line)