generateUnit method
Future<void>
generateUnit(
{ - required PubspecData pubspecData,
- required List<String> filePaths,
- required List<String> classNames,
- required List<String> methodNames,
- required FlagGenerationRunInfo flagGenerationRunInfo,
- bool dryrun = false,
- bool testcases = false,
- bool testability = false,
})
Implementation
Future<void> generateUnit({
required PubspecData pubspecData,
required List<String> filePaths,
required List<String> classNames,
required List<String> methodNames,
required FlagGenerationRunInfo flagGenerationRunInfo,
bool dryrun = false,
bool testcases = false,
bool testability = false,
}) async {
wtLog.startSpinner("Getting ready...");
final userRepository = UserRepository();
final dartServerRepository = DartServerRepository();
await dartServerRepository.initialize();
final libDirectory = Directory(PathUtils.libPath);
final testDirectory = Directory(PathUtils.testPath);
await dartServerRepository.addFilesToRoot(
[libDirectory.absolute.path, testDirectory.absolute.path]);
Iterable<FileSystemEntity> fileList = filePaths.isNotEmpty
? filePaths.map((filePath) => File(filePath)).toList()
: (libDirectory.listSync(recursive: true))
.where((element) => element.path.endsWith(".dart"));
AnalysisContextCollection collection = AnalysisContextCollection(
includedPaths: [PathUtils.libPath],
resourceProvider: PhysicalResourceProvider.INSTANCE,
);
List<Future> generationFutures = [];
final testRunInfo = GenerateRunInfo.newRun();
for (var file in fileList) {
generationFutures.add(
runUnit(
file, collection, pubspecData, dartServerRepository, testRunInfo,
classNames: classNames,
methodNames: methodNames,
dryrun: dryrun,
testcases: testcases,
testability: testability,
flagGenerationRunInfo: flagGenerationRunInfo),
);
}
wtLog.startSpinner(dryrun
? 'Running in dryrun mode for ${methodNames.humanPreferredString(3)}'
: testcases
? 'Generating testcases for ${methodNames.humanPreferredString(3)}'
: testability
? 'Analyzing testability for ${methodNames.humanPreferredString(3)}'
: 'Generating unit tests');
await Future.wait(generationFutures);
if (generationsCount == 0 ||
flagGenerationRunInfo.classQueue.isNotEmpty ||
flagGenerationRunInfo.functionQueue.isNotEmpty) {
final classGenerationFailure = flagGenerationRunInfo.classQueue;
final methodGenerationFailure = flagGenerationRunInfo.functionQueue;
if (classGenerationFailure.isEmpty && methodGenerationFailure.isEmpty) {
wtLog.warning("No class or method found with `@Welltested` annotation");
} else {
wtLog.warning(
"The following classes/methods not found with `@Welltested` annotation:");
if (classGenerationFailure.isNotEmpty)
wtLog.warning(
'Classes: ${classGenerationFailure.humanPreferredString(-1)}');
if (methodGenerationFailure.isNotEmpty)
wtLog.warning(
'Methods: ${methodGenerationFailure.humanPreferredString(-1)}');
}
}
if (!dryrun && !testcases && !testability) {
String? sessionInfo;
Map<String, dynamic>? cliVersionInfo;
try {
//Session Info Failure shouln't abrupt the user experience
final sessionResult = await userRepository.getSessionInfo('generate');
sessionInfo = sessionResult.info;
cliVersionInfo = sessionResult.additionalInfo['cli_version'];
} catch (_) {}
if (cliVersionInfo != null && cliVersionInfo['is_critical'] == true) {
wtLog.stopSpinner(
message:
"🚨 ${cliVersionInfo['info'] ?? 'Attention: Welltested CLI package is outdated! Please update your CLI.'}",
severity: MessageSeverity.Error);
} else {
wtLog.stopSpinner(
message:
'✅ Succeeded. Please find the generated tests in `/test` folder.\nCreated: ${testRunInfo.created}\nUpdated:${testRunInfo.updated}\nFailed:${testRunInfo.failed}' +
(sessionInfo != null ? '\n\n${sessionInfo}' : ''));
if (cliVersionInfo != null)
wtLog.info(cliVersionInfo['info'] ??
'Note: New version of CLI available for download.');
}
}
if (wtLog.isBusy) {
wtLog.stopSpinner();
}
dartServerRepository.dispose();
if (!dryrun && !testcases && !testability) await runMockitoCommand();
}