findSnapshotFile function

File? findSnapshotFile(
  1. String source, {
  2. String root = '.',
})

The snapshot file source names, or null when nothing matches.

A path is taken as written. A bare name also matches <name>.json, and is looked for in snapshotFolders under root, so import demo finds snapshots/demo.json.

Implementation

File? findSnapshotFile(String source, {String root = '.'}) {
  final List<String> names = [
    source,
    if (!source.toLowerCase().endsWith('.json')) '$source.json',
  ];
  for (final String name in names) {
    final File file = File(name);
    if (file.existsSync()) return file;
  }
  // Only a bare name is looked for elsewhere; a path means where it says.
  if (source.contains('/') || source.contains(r'\')) return null;
  for (final String folder in snapshotFolders) {
    for (final String name in names) {
      final File file = File('$root/$folder/$name');
      if (file.existsSync()) return file;
    }
  }
  return null;
}