Skip to content

Commit 49dc347

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer plugins: Document how to add a stub package in tests
Fixes #61915 I add documentation to a few APIs, and primarily document `newPackage` and `PackageBuilder.addFile`, which already have sufficient doc comments. Change-Id: Ice088c2d76604ccb991d895a1e4bb408c492b963 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/467240 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent dadbd93 commit 49dc347

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed

pkg/analysis_server_plugin/doc/testing_rules.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,57 @@ running `dart test` or `dart --enable-asserts test/my_rule_test.dart`.
148148

149149
[`analyzer_testing`]: https://pub.dev/packages/analyzer_testing
150150
[writing rules]: https://github.com/dart-lang/sdk/blob/main/pkg/analysis_server_plugin/doc/writing_rules.md
151-
[`test_reflective_loader`]: https://pub.dev/packages/test_reflective_loader
151+
[`test_reflective_loader`]: https://pub.dev/packages/test_reflective_loader
152+
153+
## Writing stub package sources
154+
155+
Often an analysis rule needs to understand if a type or an element (like a class
156+
or a method) is a specific type/element from a specific library in a package.
157+
For example, a rule might be concerned with the use of the `test` function
158+
declared in the `test_core` package. In order to write tests for such a rule,
159+
the test code needs to import something like
160+
`'package:test_core/test_core.dart'`. In order to make such an import
161+
meaningful, some stub code needs to be written so that, in fact, a `test`
162+
function is made available by that import.
163+
164+
The [`AnalysisRuleTest`][] class offers a [`newPackage`][] method which supports
165+
writing code in other packages. `newPackage` returns a `PackageBuilder`, which
166+
is used to add individual library sources via its `addFile` method. For example,
167+
to write the sources for a stub `test` function in a package named `test_core`,
168+
you can:
169+
170+
```dart
171+
class MyRuleTest extends AnalysisRuleTest {
172+
@override
173+
void setUp() {
174+
newPackage('test_core')..addFile('lib/test_core.dart', r'''
175+
void test(
176+
Object? description,
177+
dynamic body(), {
178+
String? testOn,
179+
Object? /*Timeout?*/ timeout,
180+
Object? skip,
181+
Object? tags,
182+
Map<String, dynamic>? onPlatform,
183+
int? retry,
184+
Object? /*TestLocation?*/ location,
185+
bool solo = false,
186+
}) {}
187+
''');
188+
super.setUp();
189+
}
190+
}
191+
```
192+
193+
Here are a few tips for writing stub package sources:
194+
195+
* `newPackage` needs to be called in `setUp`, before the call to `super.setUp`.
196+
* For the static analysis purposes of testing analysis rules, it is unnecessary
197+
to include function bodies (see the empty `test` body above).
198+
* It is often not necessary to include all of the types which are needed to
199+
write a type or an element, like a function signature (see the `location`
200+
parameter above, which is typed as an `Object?` instead of a `TestLocation?`).
201+
This can greatly simplify the stubs.
202+
203+
[`AnalysisRuleTest`]: https://pub.dev/documentation/analyzer_testing/latest/analysis_rule_analysis_rule/AnalysisRuleTest-class.html
204+
[`newPackage`]: https://pub.dev/documentation/analyzer_testing/latest/analysis_rule_analysis_rule/AnalysisRuleTest/newPackage.html

pkg/analyzer/lib/utilities/package_config_file_builder.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ import 'package:path/path.dart' as path;
77
/// Helper for building `.dart_tool/package_config.json` files.
88
///
99
/// See accepted/future-releases/language-versioning/package-config-file-v2.md
10-
/// in https://github.com/dart-lang/language/
10+
/// in https://github.com/dart-lang/language/.
11+
///
12+
/// Use the [add] method to add package configurations. These configurations
13+
/// will accumulate into one package config file with the [toContent] method.
1114
class PackageConfigFileBuilder {
1215
final List<_PackageDescription> _packages = [];
1316

1417
/// The [rootPath] will be given to `toUriStr` of [toContent] to produce
1518
/// the corresponding `file://` URI, normally a POSIX path.
1619
///
17-
/// The [packageUri] is optional, a URI reference, resolved against the
18-
/// file URI of the [rootPath]. The result must be inside the [rootPath].
20+
/// The [packageUri] is optional (defaults to `'lib/'`), a relative path
21+
/// resolved against the file URI of the [rootPath]. The result must be inside
22+
/// the [rootPath].
23+
///
24+
/// The [languageVersion] specifies the package's Dart language version, in
25+
/// the form of 'X.Y', such as '3.9'.
1926
void add({
2027
required String name,
2128
required String rootPath,

pkg/analyzer_testing/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package:analyzer_testing/analysis_rule/analysis_rule.dart:
1212
lint (method: ExpectedDiagnostic Function(int, int, {List<ExpectedContextMessage>? contextMessages, Pattern? correctionContains, deprecated Pattern? messageContains, List<Pattern> messageContainsAll, String? name}))
1313
setUp (method: void Function())
1414
unexpectedMessage (method: String Function(List<Diagnostic>))
15+
PackageBuilder (non-public)
1516
package:analyzer_testing/experiments/experiments.dart:
1617
experimentsForTests (static getter: List<String>)
1718
experimentsForTests= (static setter: List<String>)

pkg/analyzer_testing/lib/analysis_rule/analysis_rule.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import 'package:analyzer_testing/src/analysis_rule/pub_package_resolution.dart';
2020
import 'package:analyzer_testing/utilities/utilities.dart';
2121
import 'package:meta/meta.dart';
2222

23+
export 'package:analyzer_testing/src/analysis_rule/pub_package_resolution.dart'
24+
show PackageBuilder;
25+
2326
ExpectedContextMessage contextMessage(
2427
File file,
2528
int offset,

pkg/analyzer_testing/lib/resource_provider_mixin.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ mixin ResourceProviderMixin {
138138
return newFile(path, content);
139139
}
140140

141-
/// Writes a `.dart_tool/package_config.json` file at [directoryPath].
141+
/// Writes a `.dart_tool/package_config.json` file at [directoryPath],
142+
/// returning the [File] object.
143+
///
144+
/// See the documentation for [PackageConfigFileBuilder] for information on
145+
/// how to add package configurations.
142146
File newPackageConfigJsonFileFromBuilder(
143147
String directoryPath,
144148
PackageConfigFileBuilder builder,

0 commit comments

Comments
 (0)