Skip to content

Commit 1188d01

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer_testing: improve error message when trying to use built-in mocks
Work towards #61597 The analyzer_testing package's mock packages are "data" or "resource" files which are notoriously hard to find on disk. No dart tooling I know of can give me "the root directory of a package which is referenced in a Dart program via "package:". Konstantin notes that a program may be compiled ahead of time, and so the running VM itself does not know where source files originally came from. When users of the analyzer_testing package try to use `addFlutterPackageDep` in a test, which attempts to locate the Dart SDK's `pkg/` root directory, they get a StateError. This CL adds more information and context to that error. Change-Id: I3ae55cd580c8fa16ca86d66e6f7b655b8f9bcc42 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/452532 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 9ec5b0b commit 1188d01

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

pkg/analyzer_testing/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.1.4-dev
2+
3+
- Require version `8.3.0` of the `analyzer` package.
4+
- Improve error message when trying to use any of the built-in mock libraries
5+
from a test outside of the Dart SDK.
6+
17
## 0.1.3
28

39
- Require version `8.2.0` of the `analyzer` package.

pkg/analyzer_testing/lib/mock_packages/mock_packages.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,16 @@ mixin MockPackagesMixin {
151151
Folder _addFiles(String packageName) {
152152
var cachedFiles = _cachedFiles;
153153
if (cachedFiles == null) {
154-
cachedFiles = {};
155-
_cacheFiles(cachedFiles);
156-
_cachedFiles = cachedFiles;
154+
try {
155+
cachedFiles = {};
156+
_cacheFiles(cachedFiles);
157+
_cachedFiles = cachedFiles;
158+
} on StateError catch (e) {
159+
throw StateError(
160+
'${e.message}\nAdding built-in mock library for "$packageName" is '
161+
'not supported when writing a test outside of the Dart SDK source repository.',
162+
);
163+
}
157164
}
158165

159166
for (var entry in cachedFiles.entries) {

pkg/analyzer_testing/lib/package_root.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ String get packageRoot {
4141
return pathFromCwd;
4242
}
4343

44-
throw StateError('Unable to find sdk/pkg/ in $scriptPath');
44+
var exceptionMessage =
45+
'Unable to find the Dart SDK package root directory ("sdk/pkg/") in ';
46+
if (pkgRootVar != null) {
47+
exceptionMessage += '"$pkgRootVar" (specified via `-DpkgRoot=`), or ';
48+
}
49+
exceptionMessage +=
50+
'in an ancestor of either "$pathFromScript", or "$pathFromCwd".';
51+
52+
throw StateError(exceptionMessage);
4553
}
4654

4755
/// Tries to find the path to the 'pkg' folder from [searchPath].

pkg/analyzer_testing/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: analyzer_testing
22
description: Testing utilities related to the analyzer and analysis_server_plugin packages.
3-
version: 0.1.3
3+
version: 0.1.4-dev
44
repository: https://github.com/dart-lang/sdk/tree/main/pkg/analyzer_testing
55

66
environment:

0 commit comments

Comments
 (0)