collect function

Future<Map<String, dynamic>> collect(
  1. Uri serviceUri,
  2. bool resume,
  3. bool waitPaused,
  4. bool includeDart,
  5. Set<String>? scopedOutput, {
  6. Set<String>? isolateIds,
  7. Duration? timeout,
  8. bool functionCoverage = false,
  9. bool branchCoverage = false,
  10. Map<String, Set<int>>? coverableLineCache,
  11. VmService? serviceOverrideForTesting,
})

Collects coverage for all isolates in the running VM.

Collects a hit-map containing merged coverage for all isolates in the Dart VM associated with the specified serviceUri. Returns a map suitable for input to the coverage formatters that ship with this package.

serviceUri must specify the http/https URI of the service port of a running Dart VM and must not be null.

If resume is true, all isolates will be resumed once coverage collection is complete.

If waitPaused is true, collection will not begin until all isolates are in the paused state.

If includeDart is true, code coverage for core dart:* libraries will be collected.

If functionCoverage is true, function coverage information will be collected.

If branchCoverage is true, branch coverage information will be collected. This will only work correctly if the target VM was run with the --branch-coverage flag.

If scopedOutput is non-empty, coverage will be restricted so that only scripts that start with any of the provided paths are considered.

If isolateIds is set, the coverage gathering will be restricted to only those VM isolates.

If coverableLineCache is set, the collector will avoid recompiling libraries it has already seen (see VmService.getSourceReport's librariesAlreadyCompiled parameter). This is only useful when doing more than one collect call over the same libraries. Pass an empty map to the first call, and then pass the same map to all subsequent calls.

serviceOverrideForTesting is for internal testing only, and should not be set by users.

Implementation

Future<Map<String, dynamic>> collect(Uri serviceUri, bool resume,
    bool waitPaused, bool includeDart, Set<String>? scopedOutput,
    {Set<String>? isolateIds,
    Duration? timeout,
    bool functionCoverage = false,
    bool branchCoverage = false,
    Map<String, Set<int>>? coverableLineCache,
    VmService? serviceOverrideForTesting}) async {
  scopedOutput ??= <String>{};

  late VmService service;
  if (serviceOverrideForTesting != null) {
    service = serviceOverrideForTesting;
  } else {
    // Create websocket URI. Handle any trailing slashes.
    final pathSegments =
        serviceUri.pathSegments.where((c) => c.isNotEmpty).toList()..add('ws');
    final uri = serviceUri.replace(scheme: 'ws', pathSegments: pathSegments);

    await retry(() async {
      try {
        final options = const CompressionOptions(enabled: false);
        final socket = await WebSocket.connect('$uri', compression: options);
        final controller = StreamController<String>();
        socket.listen((data) => controller.add(data as String), onDone: () {
          controller.close();
          service.dispose();
        });
        service = VmService(controller.stream, socket.add,
            log: StdoutLog(), disposeHandler: socket.close);
        await service.getVM().timeout(_retryInterval);
      } on TimeoutException {
        // The signature changed in vm_service version 6.0.0.
        // ignore: await_only_futures
        await service.dispose();
        rethrow;
      }
    }, _retryInterval, timeout: timeout);
  }

  try {
    if (waitPaused) {
      await _waitIsolatesPaused(service, timeout: timeout);
    }

    return await _getAllCoverage(service, includeDart, functionCoverage,
        branchCoverage, scopedOutput, isolateIds, coverableLineCache);
  } finally {
    if (resume) {
      await _resumeIsolates(service);
    }
    // The signature changed in vm_service version 6.0.0.
    // ignore: await_only_futures
    await service.dispose();
  }
}