generateForWidget method
Future<void>
generateForWidget({
- required ClassDeclaration classDeclaration,
- required CompilationUnit compilationUnit,
- required String subjectFile,
- required TestRunInfo testRunInfo,
Implementation
Future<void> generateForWidget({
required ClassDeclaration classDeclaration,
required final CompilationUnit compilationUnit,
required final String subjectFile,
required TestRunInfo testRunInfo,
}) async {
final widgetName = classDeclaration.name.toString();
try {
final testRequestData = await widgetTestExecutor.prepareRequest(
classDeclaration: classDeclaration,
compilationUnit: compilationUnit,
);
final file = PathUtils.correspondingPermanentWidgetTestFile(
widgetName,
subjectFile,
);
final existStatus = await cacheManager.widgetTestExistStatus(
await testRequestData.codeInScrutity(excludeAnnotations: false),
subjectFile,
testRequestData.widgetName);
bool update = false;
late TestGenerationStatus testGenerationStatus;
switch (existStatus) {
case TestExistStatus.generationExists:
return;
case TestExistStatus.onlyRequestExists:
testRequestData.forceGenerate = true;
testGenerationStatus = TestGenerationStatus.forceUpdate;
break;
case TestExistStatus.outdatedCodeExists:
update = true;
testGenerationStatus = TestGenerationStatus.update;
break;
case TestExistStatus.doesNotExist:
testGenerationStatus = TestGenerationStatus.create;
break;
}
wtLog.log('🔍 Analyzing ${widgetName} for requirements', verbose: true);
final requirements =
await generationRepository.getRequirements(testRequestData);
wtLog.log(
"Requirements: \n${requirements.map((e) => '${e.name}:${e.operation}').join('\n')}",
verbose: true);
await testRequestData.addExtractioncode(requirements);
final widgetTests =
await generationRepository.buildWidgettests(testRequestData);
String? completion = widgetTests['fixed_completion'];
late File testFile;
if (completion != null) {
testFile = await writerExecutor.outputGeneration(file.path, completion);
} else {
completion = widgetTests['model_completion'];
if (completion == null) {
wtLog.warning(
"⛔️ Sorry, we can't process ${testRequestData.widgetName} at the moment. Please try again Later.");
return;
}
testFile = await writerExecutor.processGeneration(
file.path, completion, widgetName);
}
cacheManager.cacheWidgetTest(
code: await testRequestData.codeInScrutity(excludeAnnotations: false),
codeFilePath: subjectFile,
test: completion,
testFilePath: testFile.path,
widgetName: testRequestData.widgetName,
);
testRunInfo.incrementFromAction(testGenerationStatus, widgetName);
wtLog.log(
'✔️ Tests ${update ? 'updated' : 'generated'} for ${widgetName}');
} catch (e, stackTrace) {
testRunInfo.incrementFromAction(TestGenerationStatus.failed, widgetName);
wtTelemetry.trackError(
severity: Severity.error, error: e, stackTrace: stackTrace);
wtLog.warning(
"Error generating widget tests for widget: ${widgetName}");
wtLog.log(e.toString());
}
}