buildCliShardPlan function

Future<CliShardPlan> buildCliShardPlan(
  1. String appDir,
  2. String? testsDir, {
  3. List<String> arguments = const [],
})

Implementation

Future<CliShardPlan> buildCliShardPlan(
  String appDir,
  String? testsDir, {
  List<String> arguments = const [],
}) async {
  if (testsDir == null) return CliShardPlan();
  final historicalDurations =
      await EnsembleTestHistoryStore.loadPlannerDurations(appDir);
  final parallel = <CliShardFile>[];
  final serial = <String>[];
  final config = _readTestsConfig(testsDir);
  final effectiveConfig = config == null
      ? null
      : applyDeviceFilter(
          config,
          CliTestSelection.valueSet(arguments, '--device'),
        );
  final files = Directory(testsDir)
      .listSync(recursive: true)
      .whereType<File>()
      .where((file) => file.path.endsWith('.test.yaml'))
      .toList()
    ..sort((a, b) => a.path.compareTo(b.path));
  final docsByPath = <String, YamlMap?>{};
  final contentsByPath = <String, String>{};
  final pathById = <String, String>{};
  final referencedSessionIds = <String>{};
  final hasShardSelection = CliTestSelection.hasShardSelection(arguments);
  for (final file in files) {
    final relative = p.relative(file.path, from: appDir).replaceAll('\\', '/');
    final content = file.readAsStringSync();
    contentsByPath[relative] = content;
    final doc = _readShardDocumentContent(content);
    docsByPath[relative] = doc;
    final id = doc?['id']?.toString();
    if (id != null && id.isNotEmpty) {
      pathById[id] = relative;
    }
    final session = doc?['session']?.toString();
    if (session != null && session.isNotEmpty) {
      referencedSessionIds.add(session);
    }
  }

  final sessionProducerPaths = <String>{
    for (final id in referencedSessionIds)
      if (pathById[id] != null) pathById[id]!,
  };

  for (final file in files) {
    final relative = p.relative(file.path, from: appDir).replaceAll('\\', '/');
    final doc = docsByPath[relative];
    if (!CliTestSelection.matchesShardSelection(
      relative,
      doc,
      arguments,
      config: effectiveConfig,
    )) {
      continue;
    }
    if (_isParallelShardFile(doc: doc) &&
        (hasShardSelection || !sessionProducerPaths.contains(relative))) {
      final definitions = _expandedShardDefinitionsForFile(
        doc: doc,
        relativePath: relative,
        config: effectiveConfig,
        arguments: arguments,
      );
      final fileEstimate = _estimateShardFileDurationMs(
        file,
        relative,
        historicalDurations,
      );
      final fallbackEstimate =
          (fileEstimate / definitions.length.clamp(1, 1000000)).round();
      for (final definition in definitions) {
        parallel.add(
          CliShardFile(
            path: relative,
            paths: _shardPathsForDefinition(
              definition,
              relative,
              pathById,
            ),
            id: definition.id,
            estimatedDurationMs:
                historicalDurations.testDuration(definition.id) ??
                    fallbackEstimate,
          ),
        );
      }
    } else if (!hasShardSelection &&
        sessionProducerPaths.contains(relative) &&
        _isParallelShardFile(doc: doc)) {
      // Session producers are selected automatically by each shard that needs
      // them. Running them as standalone files would waste a worker lane.
      continue;
    } else {
      serial.add(relative);
    }
  }
  return CliShardPlan(
    parallel: parallel,
    serial: serial,
    assetContents: contentsByPath,
    decodedDocuments: docsByPath,
  );
}