inUnitTest property

  1. @visibleForTesting
bool inUnitTest

Returns true if we are running in a unit test. We do this by inspecting the stack looking for the test_api package so this method has very limited use and is intended for internal dcli testing.

Implementation

@visibleForTesting
bool get inUnitTest {
  Stackframe? scriptFrame;
  for (final frame in StackTraceImpl().frames) {
    if (frame.sourceType == FrameSourceType.package &&
        frame.sourceFile.path.startsWith('test_api')) {
      if (scriptFrame != null) {
        _unitTestPath = truepath(scriptFrame.sourceFile.path);
      }
      return true;
    }
    scriptFrame = frame;
  }

  return false;
  // p.extension(Platform.script.toFilePath()) == '.dill' &&
  //     p.basenameWithoutExtension(Platform.script.toFilePath()) ==
  //         'test.dart_1';
}