prepareBridges method
Generate bridges once for a project without running any script.
This method:
- Deletes the existing binary to verify regeneration
- Generates bridges using the in-memory generator
- Compiles the test runner to a native binary
Use this in setUpAll to prepare once, then call runScriptOnly
for each test script.
Returns true if generation and compilation succeeded, false otherwise.
On failure, the errors are stored and can be retrieved from subsequent
runScriptOnly calls (which will return a failed result immediately).
Example:
setUpAll(() async {
final ok = await tester.prepareBridges(config);
expect(ok, isTrue, reason: 'Bridge generation failed');
});
test('feature X', () async {
final result = await tester.runScriptOnly(config, 'test/x.dart');
_expectSuccess(result, 'feature X');
});
Implementation
Future<bool> prepareBridges(BridgeConfig config) async {
// Step 0: Ensure dependencies are resolved (package_config.json must exist
// for the generator to resolve package: URIs in barrel files)
final packageConfig = File(
p.join(projectPath, '.dart_tool', 'package_config.json'),
);
if (!packageConfig.existsSync()) {
final pubGetResult = await Process.run('dart', [
'pub',
'get',
], workingDirectory: projectPath);
if (pubGetResult.exitCode != 0) {
_lastGenerationErrors = [
'dart pub get failed in $projectPath:',
pubGetResult.stderr.toString(),
];
return false;
}
}
// Step 1: Delete existing binary to verify it gets regenerated
final binaryFile = File(_binaryPath);
if (binaryFile.existsSync()) {
binaryFile.deleteSync();
}
// Step 2: Generate bridges
final genResult = await _generateBridges(config);
if (!genResult.isSuccess) {
_lastGenerationErrors = genResult.errors;
return false;
}
// Step 3: Compile the test runner to a native binary
final runnerPath = _resolveRunnerPath(config);
final compileResult = await Process.run('dart', [
'compile',
'exe',
runnerPath,
'-o',
_binaryPath,
], workingDirectory: projectPath);
if (compileResult.exitCode != 0) {
_lastGenerationErrors = [
'Failed to compile test runner:',
compileResult.stderr.toString(),
];
return false;
}
// Verify binary was created
if (!File(_binaryPath).existsSync()) {
_lastGenerationErrors = ['Compiled binary not found at $_binaryPath'];
return false;
}
_lastGenerationErrors = null;
return true;
}