runUnit method

Future<void> runUnit(
  1. FileSystemEntity file,
  2. AnalysisContextCollection collection,
  3. PubspecData pubspecData,
  4. DartServerRepository dartServerRepository,
  5. TestRunInfo testRunInfo, {
  6. required List<String> classNames,
  7. required List<String> methodNames,
  8. required bool dryrun,
  9. required bool testcases,
  10. required bool testability,
  11. required FlagGenerationRunInfo flagGenerationRunInfo,
})

Implementation

Future<void> runUnit(
    FileSystemEntity file,
    AnalysisContextCollection collection,
    PubspecData pubspecData,
    DartServerRepository dartServerRepository,
    TestRunInfo testRunInfo,
    {required List<String> classNames,
    required List<String> methodNames,
    required bool dryrun,
    required bool testcases,
    required bool testability,
    required FlagGenerationRunInfo flagGenerationRunInfo}) async {
  try {
    final resolvedUnit = await file.resolveFile(collection);
    CompilationUnit unit = resolvedUnit.unit;

    final unitGenerator = UnitGenerator(
        dartServerRepository: dartServerRepository,
        cacheManager: CacheManager());
    unitGenerator.init();

    List<Future> generationFutures = [];
    for (final declaration
        in unit.declarations.whereType<ClassDeclaration>()) {
      final element = declaration.declaredElement!;
      //selection conditions
      if (!isAnnotationPresent(element)) continue;
      if (classNames.isNotEmpty) {
        final className = declaration.name.toString().toLowerCase();
        if (!classNames.contains(className)) continue;

        flagGenerationRunInfo.popFromQueue(FlagType.className, className);
      }

      if (element.checkIfAWidgetOrState)
        wtLog.warning(Warnings.annotationOnWidget(element.displayName));

      generationFutures.add(unitGenerator.generateForClass(
          classElement: element,
          annotation: element.metadata.firstWhere((element) =>
              element.element!.displayName ==
              Welltested().runtimeType.toString()),
          pubspecData: pubspecData,
          classAstNode: declaration,
          compilationUnit: unit,
          testRunInfo: testRunInfo,
          classFile: file as File,
          requestedFunctions: methodNames,
          dryrun: dryrun,
          testcases: testcases,
          testability: testability,
          flagGenerationRunInfo: flagGenerationRunInfo));
    }
    for (final declaration
        in unit.declarations.whereType<ExtensionDeclaration>()) {
      final element = declaration.declaredElement!;

      if (!isAnnotationPresent(element)) continue;
      if (classNames.isNotEmpty) {
        final className = declaration.name.toString().toLowerCase();
        if (!classNames.contains(className)) continue;

        flagGenerationRunInfo.popFromQueue(FlagType.className, className);
      }

      generationFutures.add(unitGenerator.generateForExtension(
          compilationUnit: unit,
          annotation: element.metadata.firstWhere((element) =>
              element.element!.displayName ==
              Welltested().runtimeType.toString()),
          extAstNode: declaration,
          extElement: element,
          pubspecData: pubspecData,
          extFile: file as File,
          testRunInfo: testRunInfo,
          requestedFunctions: methodNames,
          dryrun: dryrun,
          testcases: testcases,
          testability: testability,
          flagGenerationRunInfo: flagGenerationRunInfo));
    }
    for (final declaration
        in unit.declarations.whereType<FunctionDeclaration>()) {
      if (!isAnnotationPresent(declaration.declaredElement as Element))
        continue;

      if (methodNames.isNotEmpty) {
        final methodName = declaration.name.toString().toLowerCase();
        if (!methodNames.contains(methodName)) continue;

        flagGenerationRunInfo.popFromQueue(FlagType.methodName, methodName);
      }

      if (!(declaration.declaredElement != null &&
          declaration.declaredElement is FunctionElement)) {
        generationFutures
            .add(unitGenerator.generateForGlobalExeculatbleElement(
          methodElement: declaration.declaredElement!,
          methodAstNode: declaration,
          compilationUnit: unit,
          annotation: declaration.declaredElement!.metadata.firstWhere(
              (element) =>
                  element.element!.displayName ==
                  Welltested().runtimeType.toString()),
          pubspecData: pubspecData,
          methodFile: file as File,
          testRunInfo: testRunInfo,
          dryrun: dryrun,
          testcases: testcases,
          testability: testability,
        ));
        continue;
      }
      final element = declaration.declaredElement! as FunctionElement;
      generationFutures.add(unitGenerator.generateForGlobalMethod(
        methodElement: element,
        methodAstNode: declaration,
        compilationUnit: unit,
        annotation: element.metadata.firstWhere((element) =>
            element.element!.displayName ==
            Welltested().runtimeType.toString()),
        pubspecData: pubspecData,
        methodFile: file as File,
        testRunInfo: testRunInfo,
        dryrun: dryrun,
        testcases: testcases,
        testability: testability,
      ));
    }
    generationsCount += generationFutures.length;
    await Future.wait(generationFutures);
  } catch (e, stackTrace) {
    wtTelemetry.trackError(
        severity: Severity.error, error: e, stackTrace: stackTrace);
    wtLog.error('Unable to resolve file ${file.path}', verbose: true);
  }
}