scenarioDeclaringFile function
The test file a scenario() call was made from, /-separated and relative
to the package when it sits under it, the absolute path as the platform
spells it when it does not, or null when the frame says nothing.
Read off the declaring stack because nothing else can say it: a bare
flutter test tells a test nothing about which file it is, and a scenario
must not have to repeat its own path to get its screenshots filed under it.
The runner's lane never asks — the harness generated the group and knows.
Implementation
@visibleForTesting
String? scenarioDeclaringFile(StackTrace stack) {
for (var line in stack.toString().split('\n')) {
var location = _frameLocation.firstMatch(line)?.group(1);
if (location == null) continue;
// This library's own frames sit above the caller — `scenario()` itself,
// and anything it is reached through — whether the runtime spells them as
// a package uri or as a path into a checkout.
if (location.endsWith('flutterware/src/scenarios/scenario.dart') ||
location.endsWith('flutterware/lib/src/scenarios/scenario.dart')) {
continue;
}
var uri = Uri.tryParse(location);
if (uri == null || uri.scheme != 'file') return null;
var path = uri.toFilePath();
var root = Directory.current.path;
// Joined with `/`, an absolute path kept its root as the platform spells
// it and gained a slash after it: `C:\/elsewhere/…` on Windows.
if (!p.isWithin(root, path)) return path;
return p.url.joinAll(p.split(p.relative(path, from: root)));
}
return null;
}