Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add package config URI into VM test entrypoints #2245

Merged
merged 8 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 12 additions & 31 deletions pkgs/test_core/lib/src/runner/vm/platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,12 @@ stderr: ${processResult.stderr}''');
if (!file.existsSync()) {
file
..createSync(recursive: true)
..writeAsStringSync(_bootstrapIsolateTestContents(
await absoluteUri(testPath), languageVersionComment));
..writeAsStringSync(testBootstrapContents(
testUri: await absoluteUri(testPath),
languageVersionComment: languageVersionComment,
packageConfigUri: await packageConfigUri,
testType: VmTestType.isolate,
));
}
return file.uri;
}
Expand All @@ -316,40 +320,17 @@ stderr: ${processResult.stderr}''');
if (!file.existsSync()) {
file
..createSync(recursive: true)
..writeAsStringSync(_bootstrapNativeTestContents(
await absoluteUri(testPath), languageVersionComment));
..writeAsStringSync(testBootstrapContents(
testUri: await absoluteUri(testPath),
languageVersionComment: languageVersionComment,
packageConfigUri: await packageConfigUri,
testType: VmTestType.process,
));
}
return file.path;
}
}

/// Creates bootstrap file contents for running [testUri] in a VM isolate.
String _bootstrapIsolateTestContents(
Uri testUri, String languageVersionComment) =>
'''
$languageVersionComment
import "dart:isolate";
import "package:test_core/src/bootstrap/vm.dart";
import "$testUri" as test;
void main(_, SendPort sendPort) {
internalBootstrapVmTest(() => test.main, sendPort);
}
''';

/// Creates bootstrap file contents for running [testUri] as a native
/// executable.
String _bootstrapNativeTestContents(
Uri testUri, String languageVersionComment) =>
'''
$languageVersionComment
import "dart:isolate";
import "package:test_core/src/bootstrap/vm.dart";
import "$testUri" as test;
void main(List<String> args) {
internalBootstrapNativeTest(() => test.main, args);
}
''';

Future<Map<String, dynamic>> _gatherCoverage(Environment environment) async {
final isolateId = Uri.parse(environment.observatoryUrl!.fragment)
.queryParameters['isolateId'];
Expand Down
57 changes: 41 additions & 16 deletions pkgs/test_core/lib/src/runner/vm/test_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,6 @@ class _TestCompilerForLanguageVersion {
: _dillCachePath = '$dillCachePrefix.'
'${_dillCacheSuffix(_languageVersionComment, enabledExperiments)}';

String _generateEntrypoint(Uri testUri) {
return '''
$_languageVersionComment
import "dart:isolate";

import "package:test_core/src/bootstrap/vm.dart";

import "$testUri" as test;

void main(_, SendPort sendPort) {
internalBootstrapVmTest(() => test.main, sendPort);
}
''';
}

Future<CompilationResponse> compile(Uri mainUri) =>
_compilePool.withResource(() => _compile(mainUri));

Expand All @@ -108,7 +93,12 @@ class _TestCompilerForLanguageVersion {
if (_closeMemo.hasRun) return CompilationResponse._wasShutdown;
CompileResult? compilerOutput;
final tempFile = File(p.join(_outputDillDirectory.path, 'test.dart'))
..writeAsStringSync(_generateEntrypoint(mainUri));
..writeAsStringSync(testBootstrapContents(
testUri: mainUri,
packageConfigUri: await packageConfigUri,
languageVersionComment: _languageVersionComment,
testType: VmTestType.isolate,
));
final testCache = File(_dillCachePath);

try {
Expand Down Expand Up @@ -211,3 +201,38 @@ String _dillCacheSuffix(
}
return base64.encode(utf8.encode(identifierString.toString()));
}

/// Creates bootstrap file contents for running [testUri].
///
/// The [bootstrapType] argument should be either 'Vm' or 'Native' depending on
/// which `internalBootstrap*Test` function should be used.
String testBootstrapContents({
required Uri testUri,
required String languageVersionComment,
required Uri packageConfigUri,
required VmTestType testType,
}) {
final (mainArgs, forwardedArgName, bootstrapType) = switch (testType) {
VmTestType.isolate => ('_, SendPort sendPort', 'sendPort', 'Vm'),
VmTestType.process => ('List<String> args', 'args', 'Native'),
};
return '''
$languageVersionComment

import 'dart:isolate';

import 'package:test_core/src/bootstrap/vm.dart';

import '$testUri' as test;

// This variable is read at runtime through the VM service and is unsafe to
// remove.
const packageConfigLocation = '$packageConfigUri';

void main($mainArgs) {
internalBootstrap${bootstrapType}Test(() => test.main, $forwardedArgName);
}
''';
}

enum VmTestType { isolate, process }
Loading