saveTestFlow method

Future<void> saveTestFlow({
  1. required IntegrationTestInput testInput,
  2. required List<IntegrationTestInput> allTests,
  3. required int index,
  4. required PubspecData pubspecData,
  5. required AnalysisContextCollection collection,
})

Implementation

Future<void> saveTestFlow(
    {required IntegrationTestInput testInput,
    required List<IntegrationTestInput> allTests,
    required int index,
    required PubspecData pubspecData,
    required AnalysisContextCollection collection}) async {
  try {
    final file =
        PathUtils.correspondingIntegrationTestFile(testInput.moduleName);
    final resolvedUnit = await file.resolveFile(collection);
    CompilationUnit unit = resolvedUnit.unit;
    String? fileContent = await file.getContent();
    if (fileContent == null) return;
    final IntegrationTestVisitor integrationTestVisitor =
        IntegrationTestVisitor(
            flow: testInput.testFlows[index], sourceCode: fileContent);
    unit.visitChildren(integrationTestVisitor);
    integrationTestVisitor.main?.functionExpression.body
        .visitChildren(integrationTestVisitor);
    final existingFlow = integrationTestVisitor.getFlow;
    if (existingFlow == null)
      return; //This particular test hasn't been saved yet
    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;
    }

    //remove all other flows
    List<String> codeToReplace = [];
    //remove all other flows
    for (final flow in integrationTestVisitor.allFlows) {
      if (flow.flowDescription !=
          integrationTestVisitor.getFlow?.flowDescription) {
        codeToReplace.add(fileContent.getPrettyCodeForNode(flow) + ';');
      }
    }
    codeToReplace.forEach((element) {
      fileContent = fileContent!.replaceAll(element, '');
    });

    //TODO: Remove Test Flows
    IntegrationUpdateRequestData integrationUpdateRequestData =
        await IntegrationExecutor().prepareUpdateRequest(
      testInput,
      allTests,
      index,
      pubspecData,
      integrationTestVisitor.helperFunctions,
    );
    wtLog.startSpinner("Updating Test for ${testInput.moduleName}");

    await generationRepository.updateIntegrationTests(
        integrationUpdateRequestData, fileContent!);

    wtLog.stopSpinner(message: '✅ Succeeded. The test was saved.');
  } catch (e, stackTrace) {
    wtTelemetry.trackError(
        severity: Severity.error, error: e, stackTrace: stackTrace);
    wtLog.warning(
        "Error saving integration tests module: ${testInput.moduleName}.");
    wtLog.log(e.toString());
  } finally {
    wtLog.stopSpinner();
  }
}