packageTestPathFromFilePath function

String packageTestPathFromFilePath(
  1. String filePath
)

Converts a file path to a package-root-relative path when possible.

Implementation

String packageTestPathFromFilePath(String filePath) {
  final absoluteFilePath = p.normalize(p.absolute(filePath));
  final cached = _packageTestPathByAbsoluteFilePath[absoluteFilePath];
  if (cached != null) {
    return cached;
  }

  final packageRoot = _findPackageRoot(absoluteFilePath);
  final result = packageRoot == null
      ? getRelativePath(filePath)
      : getPosixPath(p.relative(absoluteFilePath, from: packageRoot));
  _packageTestPathByAbsoluteFilePath[absoluteFilePath] = result;
  return result;
}