saveTestsForWidget method

Future<void> saveTestsForWidget({
  1. required ClassDeclaration widget,
  2. required String widgetName,
  3. required CompilationUnit compilationUnit,
  4. required String fileImport,
  5. required File subjectFile,
  6. required TestRunInfo testRunInfo,
  7. ClassDeclaration? classAstNode,
  8. ExtensionDeclaration? extAstNode,
  9. String? className,
  10. Map<String, MethodDeclaration>? methodAstNodes,
})

Implementation

Future<void> saveTestsForWidget({
  required ClassDeclaration widget,
  required String widgetName,
  required CompilationUnit compilationUnit,
  required String fileImport,
  required File subjectFile,
  required TestRunInfo testRunInfo,
  ClassDeclaration? classAstNode,
  ExtensionDeclaration? extAstNode,
  String? className,
  Map<String, MethodDeclaration>? methodAstNodes,
}) async {
  try {
    final testRequestData = await widgetTestExecutor.prepareRequest(
        classDeclaration: widget, compilationUnit: compilationUnit);

    final file = PathUtils.correspondingPermanentWidgetTestFile(
        widgetName, subjectFile.path);
    if (!(await file.exists())) return;
    final testIsSaved = await cacheManager.checkIfWidgetTestSaved(
      await testRequestData.codeInScrutity(excludeAnnotations: false),
      subjectFile.path,
      testRequestData.widgetName,
    );
    if (testIsSaved) return; //this test is already saved.

    var hasErrors =
        await fixerExecutor.hasCompilationErrors(file.absolute.path);
    if (hasErrors) {
      wtLog.log(
          "⚠️ File ${PathUtils.relatetiveLibFilePath(file.path)} has errors and will not be updated");
      return;
    }
    final fixedTest = await file.readAsString();
    await generationRepository.updateWidgetTest(testRequestData, fixedTest);
    await cacheManager.cacheWidgetTest(
        code: await testRequestData.codeInScrutity(excludeAnnotations: false),
        codeFilePath: subjectFile.path,
        test: fixedTest,
        testFilePath: file.path,
        widgetName: widgetName,
        saved: true);

    testRunInfo.incrementFromAction(TestGenerationStatus.update, widgetName);
  } catch (e, stackTrace) {
    wtTelemetry.trackError(
        severity: Severity.error, error: e, stackTrace: stackTrace);
    wtLog.warning("Error saving widget tests for widget: ${widgetName}");
    wtLog.log(e.toString());
  }
}