prepareBridges method

Future<bool> prepareBridges(
  1. BridgeConfig config
)

Generate bridges once for a project without running any script.

This method:

  1. Deletes the existing binary to verify regeneration
  2. Generates bridges using the in-memory generator
  3. 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: Make sure the project still resolves. Checking only that a
  // package config exists is not enough: a project that stopped resolving
  // keeps its last config, and generation then fails on a downstream symptom
  // — once an analyzer "API break" that was really a config naming a version
  // the pub cache no longer had. Pub's own message names the cause.
  final unresolved = await resolveIfUnresolved(projectPath);
  if (unresolved != null) {
    _lastGenerationErrors = [unresolved];
    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();
  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;
}