getPackagesToTest method

(Iterable<PackageToTest>?, ExitCode?) getPackagesToTest(
  1. List<String> testDirs,
  2. Map<String, DetermineFlutterOrDart> dirTools, {
  3. required bool optimize,
})

Implementation

(
  Iterable<PackageToTest>? filesToTest,
  ExitCode? exitCode,
) getPackagesToTest(
  List<String> testDirs,
  Map<String, DetermineFlutterOrDart> dirTools, {
  required bool optimize,
}) {
  final dirsWithTests = <String>[];
  final glob = Glob('**_test.dart');
  for (final MapEntry(key: dir, value: _) in dirTools.entries) {
    final result = glob.listFileSystemSync(fs, followLinks: false, root: dir);

    if (result.any((e) => e is File)) {
      dirsWithTests.add(dir);
    }
  }

  if (dirsWithTests.isEmpty) {
    logger.err('No tests found');
    return (null, ExitCode.unavailable);
  }

  logger
    ..detail('Found ${dirsWithTests.length} directories with tests')
    ..detail('  - ${dirsWithTests.join('\n  - ')}')
    ..detail(
      '${optimize ? '' : 'NOT '}Optimizing '
      '${dirsWithTests.length} test files',
    );

  if (optimize) {
    final done = logger.progress('Optimizing test files');
    final result = prepareOptimizedFilesFromDirs(dirsWithTests, dirTools);

    done.complete();

    if (result.isEmpty) {
      logger.err('No tests found');
      return (null, ExitCode.unavailable);
    }

    return (result, null);
  }

  logger.warn('Running tests without optimization');

  final dirs = [
    for (final dir in dirsWithTests)
      if (dirTools[dir] case final tool?)
        PackageToTest(
          tool: tool,
          packagePath: dir,
        ),
  ];

  return (dirs, null);
}