testReader method

Future<List<PendingTest>> testReader(
  1. BuildContext? context, {
  2. String? suiteName,
})

Implementation of the TestReader functional interface that can read test data from GitHub.

Implementation

Future<List<PendingTest>> testReader(
  BuildContext? context, {
  String? suiteName,
}) async {
  await _loadData();
  final results = <PendingTest>[];

  try {
    final actualTestsPath =
        testsPath.startsWith('/') ? testsPath.substring(1) : testsPath;

    final tests = _tree.entries!.where(
      (entry) =>
          (actualTestsPath.isEmpty ||
              entry.path?.startsWith(actualTestsPath) == true) &&
          entry.path!.endsWith('.json'),
    );

    final batchSize = 10;
    final batch = <Future>[];

    for (var test in tests) {
      batch.add(
        Future.microtask(() async {
          final encodedPath = _encodeGitPath(test.path!);
          try {
            final contents = await _github.repositories.getContents(
              slug,
              encodedPath,
              ref: branch,
            );

            final body = utf8.decode(
              base64.decode(
                contents.file!.content!.replaceAll('\n', ''),
              ),
            );
            final fullTest = Test.fromDynamic(json.decode(body));
            results.add(
              PendingTest.memory(fullTest),
            );
          } catch (e, stack) {
            _logger.warning(
              '[TEST LOAD FAILED]: Failed attempting to load test at path: [${test.path}]',
              e,
              stack,
            );
          }
        }),
      );
      if (batch.length >= batchSize) {
        await Future.wait(batch);
        batch.clear();
      }
    }

    if (batch.isNotEmpty) {
      await Future.wait(batch);
      batch.clear();
    }

    results.sort(
      (a, b) => a.name.toLowerCase().compareTo(b.name.toLowerCase()),
    );
  } catch (e, stack) {
    _logger.severe('Error loading tests', e, stack);
  }

  return results;
}