addAppTests function

List<String> addAppTests(
  1. List<MatrixAppTest> tests, {
  2. required MatrixApp app,
  3. required String directory,
  4. required String packageName,
  5. FileSystem fileSystem = const LocalFileSystem(),
})

Copies the files of tests into the app of the matrix app, generated in directory with the package packageName, with the placeholders of the files filled, and returns the paths of the files in the app; see MatrixAppTest.directory.

Throws a MatrixAppTestException, before it copies anything, if a file keeps a placeholder that no value fills, or if two of the tests have a file at the same path.

Implementation

List<String> addAppTests(
  List<MatrixAppTest> tests, {
  required MatrixApp app,
  required String directory,
  required String packageName,
  FileSystem fileSystem = const LocalFileSystem(),
}) {
  final context = fileSystem.path;
  final texts = <String, String>{};
  final owners = <String, String>{};
  for (final test in tests) {
    final values = {'app_name': packageName, ...?test.values?.call(app)};
    final root = fileSystem.directory(test.directory);
    final files = [
      for (final entity in root.listSync(recursive: true))
        if (entity is File &&
            !context
                .split(context.relative(entity.path, from: root.path))
                .any((part) => part.startsWith('.')))
          entity,
    ]..sort((a, b) => a.path.compareTo(b.path));
    for (final file in files) {
      final path = context.relative(file.path, from: root.path);
      if (owners[path] case final other?) {
        throw MatrixAppTestException(
          'The tests of $other and ${test.directory} both have $path.',
        );
      }
      owners[path] = test.directory;
      texts[path] = _filled(file.readAsStringSync(), values, test, path);
    }
  }
  for (final MapEntry(key: path, value: text) in texts.entries) {
    fileSystem.file(context.join(directory, path))
      ..createSync(recursive: true)
      ..writeAsStringSync(text);
  }
  return [...texts.keys];
}