create static method

Future<CliAppFixture> create(
  1. String appScriptPath
)

Implementation

static Future<CliAppFixture> create(String appScriptPath) async {
  final dartVmServicePrefix =
      RegExp('(Observatory|The Dart VM service is) listening on ');

  final process = await Process.start(
    Platform.resolvedExecutable,
    <String>['--observe=0', '--pause-isolates-on-start', appScriptPath],
  );

  final Stream<String> lines =
      process.stdout.transform(utf8.decoder).transform(const LineSplitter());
  final lineController = StreamController<String>.broadcast();
  final completer = Completer<String>();

  final linesSubscription = lines.listen((String line) {
    if (completer.isCompleted) {
      lineController.add(line);
    } else if (line.contains(dartVmServicePrefix)) {
      completer.complete(line);
    } else {
      // Often something like:
      // "Waiting for another flutter command to release the startup lock...".
      print(line);
    }
  });

  // Observatory listening on http://127.0.0.1:9595/(token)
  final observatoryText = await completer.future;
  final observatoryUri = observatoryText.replaceAll(dartVmServicePrefix, '');
  var uri = Uri.parse(observatoryUri);

  if (!uri.isAbsolute) {
    throw 'Could not parse VM Service URI: "$observatoryText"';
  }

  // Map to WS URI.
  uri = convertToWebSocketUrl(serviceProtocolUrl: uri);

  final serviceConnection = await vmServiceConnectUri(uri.toString());

  final vm = await serviceConnection.getVM();

  final isolate = await _waitForIsolate(serviceConnection, 'PauseStart');
  await serviceConnection.resume(isolate.id!);

  Future<void> onTeardown() async {
    await linesSubscription.cancel();
    await lineController.close();
  }

  return CliAppFixture._(
    appScriptPath,
    process,
    lineController.stream,
    uri,
    serviceConnection,
    vm.isolates!,
    onTeardown,
  );
}