createCoverageHelperTest method

Future<void> createCoverageHelperTest(
  1. String path,
  2. String packageName
)

Implementation

Future<void> createCoverageHelperTest(String path, String packageName) async {
  final cwd = Directory(path).uri;
  final libDir = Directory.fromUri(cwd.resolve('lib'));
  final testDir = Directory.fromUri(cwd.resolve('test'));
  final buffer = StringBuffer();

  DirectoryHelper.createDir(testDir.path);

  var files = libDir
      .listSync(recursive: true)
      .whereType<File>()
      .where((file) =>
          file.path.endsWith('.dart') &&
          !file.path.contains('.freezed.') &&
          !file.path.contains('.g.') &&
          !file.path.endsWith('generated_plugin_registrant.dart') &&
          !file.readAsLinesSync().any((line) => line.startsWith('part of')))
      .toList();

  buffer.writeln('// GENERATED MORPHEME COVERAGE HELPER TEST');
  buffer.writeln();
  buffer.writeln('// ignore_for_file: unused_import');
  buffer.writeln(
      "import 'package:dev_dependency_manager/dev_dependency_manager.dart';");

  for (var file in files) {
    final fileLibPath =
        file.uri.toFilePath().substring(libDir.uri.toFilePath().length);
    buffer.writeln('import \'package:$packageName/$fileLibPath\';');
  }

  buffer.writeln();
  buffer.writeln('''void main() {
test('generated helper test', () {
  expect(1, 1);
});
  }''');
  buffer.writeln();

  final output = File(cwd.resolve(pathCoverageHelper).toFilePath());
  await output.writeAsString(buffer.toString());
}