[dart2js] Add test for deferred loading bundling.

Change-Id: I8720eed34551dbf06364c50ab01cef752a1aca58
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/309462
Reviewed-by: Stephen Adams <sra@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
This commit is contained in:
Nate Biggs 2023-06-14 20:55:20 +00:00 committed by Commit Queue
parent ff378c0e79
commit 9ecdfb68f3
5 changed files with 109 additions and 5 deletions

View file

@ -0,0 +1,60 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Ensure that bundling part files into the same file still allows them to load
// correctly.
import 'dart:io';
import 'package:expect/expect.dart';
import '../helpers/d8_helper.dart';
const String verifyParts = '''
if (!\$__dart_deferred_initializers__) {
throw 'Did not intialize \$__dart_deferred_initializers__';
}
// Expect 'eventLog', 'current', and 2 hashes for part files.
if (Object.keys(\$__dart_deferred_initializers__).length != 4) {
var data = JSON.stringify(\$__dart_deferred_initializers__);
throw '\$__dart_deferred_initializers__ has unexpected format: ' + data;
}
''';
const List<String> dataFiles = [
'deferred_lib1.dart',
'deferred_lib2.dart',
'deferred_main.dart'
];
Future<Directory> createTempDir() {
return Directory.systemTemp.createTemp('dart2js_bundle_parts_test-');
}
Future<void> runTestWithOptions(List<String> options) async {
print('Running with flags: $options');
final tmpDir = await createTempDir();
Uri inUri = Platform.script.resolve('deferred_data/deferred_main.dart');
Uri outUri = tmpDir.uri.resolve('out.js');
await getCompilationResultsForD8(inUri, outUri, options: options);
final part1 = File(tmpDir.uri.resolve('out.js_1.part.js').toFilePath());
final part2 = File(tmpDir.uri.resolve('out.js_2.part.js').toFilePath());
final bundledPartsUri = tmpDir.uri.resolve('out.bundle.js');
final bundledParts = File(bundledPartsUri.toFilePath());
await bundledParts.writeAsBytes(await part1.readAsBytes());
await bundledParts.writeAsBytes(await part2.readAsBytes(),
mode: FileMode.append);
await bundledParts.writeAsString('\n$verifyParts', mode: FileMode.append);
final result = await executeJsWithD8(bundledPartsUri);
if (result.exitCode != 0) {
Expect.fail('Expected exit code 0. D8 results:\n'
'${(result.stdout as String).trim()}');
}
Expect.equals(0, result.exitCode);
}
void main() async {
await runTestWithOptions([]);
await runTestWithOptions(['--minify']);
}

View file

@ -0,0 +1,7 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
class Class1 {
method() => 'Class1.method';
}

View file

@ -0,0 +1,7 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
class Class2 {
method() => 'Class2.method';
}

View file

@ -0,0 +1,12 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'deferred_lib1.dart' deferred as lib1;
import 'deferred_lib2.dart' deferred as lib2;
main() async {
await lib1.loadLibrary();
print(new lib1.Class1().method());
print(new lib2.Class2().method());
}

View file

@ -43,10 +43,24 @@ Future<D8Result> runWithD8(
entryPoint ??= Uri.parse('memory:main.dart');
Uri mainFile =
await createTemp(entryPoint, memorySourceFiles, printSteps: printSteps);
String output = uriPathToNative(mainFile.resolve('out.js').path);
Uri output = nativeToUri(mainFile.resolve('out.js').path);
CompilationResult compilationResult = await getCompilationResultsForD8(
mainFile, output,
options: options, printJs: printJs, printSteps: printSteps);
final d8Result = executeJsWithD8(output,
expectedOutput: expectedOutput, printSteps: printSteps);
return D8Result(compilationResult, d8Result, output.toFilePath());
}
Future<CompilationResult> getCompilationResultsForD8(
Uri mainFile, Uri outputFile,
{List<String> options = const <String>[],
bool printJs = false,
bool printSteps = false}) async {
List<String> dart2jsArgs = [
mainFile.toString(),
'-o$output',
'-o${outputFile.toFilePath()}',
if (Platform.packageConfig != null) '--packages=${Platform.packageConfig}',
...options,
if (options.contains(Flags.noSoundNullSafety))
@ -60,12 +74,16 @@ Future<D8Result> runWithD8(
Expect.isTrue(result.isSuccess);
if (printJs) {
print('dart2js output:');
print(new File(output).readAsStringSync());
print(new File(outputFile.toFilePath()).readAsStringSync());
}
return result;
}
ProcessResult executeJsWithD8(Uri jsFile,
{String? expectedOutput, bool printSteps = false}) {
List<String> d8Args = [
'$sdkPath/_internal/js_runtime/lib/preambles/d8.js',
output
jsFile.toFilePath()
];
if (printSteps) print('Running: d8 ${d8Args.join(' ')}');
ProcessResult runResult = Process.runSync(d8executable, d8Args);
@ -77,7 +95,7 @@ Future<D8Result> runWithD8(
Expect.stringEquals(expectedOutput.trim(),
runResult.stdout.replaceAll('\r\n', '\n').trim());
}
return D8Result(result, runResult, output);
return runResult;
}
class D8Result {