@@ -130,7 +130,21 @@ abstract class LintRuleTest extends PubPackageResolutionTest {
130
130
);
131
131
}
132
132
133
- class PubPackageResolutionTest extends _ContextResolutionTest {
133
+ class PubPackageResolutionTest with MockPackagesMixin , ResourceProviderMixin {
134
+ static bool _lintRulesAreRegistered = false ;
135
+
136
+ /// The byte store that is reused between tests. This allows reusing all
137
+ /// unlinked and linked summaries for SDK, so that tests run much faster.
138
+ /// However nothing is preserved between Dart VM runs, so changes to the
139
+ /// implementation are still fully verified.
140
+ static final MemoryByteStore _sharedByteStore = MemoryByteStore ();
141
+
142
+ final MemoryByteStore _byteStore = _sharedByteStore;
143
+
144
+ AnalysisContextCollectionImpl ? _analysisContextCollection;
145
+
146
+ late ResolvedUnitResult result;
147
+
134
148
final List <String > _lintRules = const [];
135
149
136
150
/// Adds the 'fixnum' package as a dependency to the package-under-test.
@@ -170,10 +184,17 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
170
184
/// The list of language experiments to be enabled for these tests.
171
185
List <String > get experiments => experimentsForTests;
172
186
173
- String get testFileName => 'test.dart' ;
187
+ /// Error codes that by default should be ignored in test expectations.
188
+ List <DiagnosticCode > get ignoredErrorCodes => [
189
+ WarningCode .UNUSED_LOCAL_VARIABLE ,
190
+ ];
174
191
192
+ /// The path to the root of the external packages.
175
193
@override
176
- String get testFilePath => '$testPackageLibPath /$testFileName ' ;
194
+ String get packagesRootPath => '/packages' ;
195
+
196
+ /// The name of the test file.
197
+ String get testFileName => 'test.dart' ;
177
198
178
199
/// The language version for the package-under-test.
179
200
///
@@ -189,9 +210,18 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
189
210
190
211
String get workspaceRootPath => '/home' ;
191
212
192
- @override
193
213
List <String > get _collectionIncludedPaths => [workspaceRootPath];
194
214
215
+ /// The diagnostics that were computed during analysis.
216
+ List <Diagnostic > get _diagnostics =>
217
+ result.errors
218
+ .whereNot ((e) => ignoredErrorCodes.any ((c) => e.errorCode == c))
219
+ .toList ();
220
+
221
+ Folder get _sdkRoot => newFolder ('/sdk' );
222
+
223
+ String get _testFilePath => '$testPackageLibPath /$testFileName ' ;
224
+
195
225
/// Asserts that the number of diagnostics reported in [content] matches the
196
226
/// number of [expectedDiagnostics] and that they have the expected error
197
227
/// descriptions and locations.
@@ -201,9 +231,9 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
201
231
String content,
202
232
List <ExpectedDiagnostic > expectedDiagnostics,
203
233
) async {
204
- addTestFile (content);
205
- await resolveTestFile ();
206
- await _assertDiagnosticsIn (_diagnostics, expectedDiagnostics);
234
+ _addTestFile (content);
235
+ await _resolveTestFile ();
236
+ _assertDiagnosticsIn (_diagnostics, expectedDiagnostics);
207
237
}
208
238
209
239
/// Asserts that the number of diagnostics that have been gathered at [path]
@@ -216,7 +246,7 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
216
246
List <ExpectedDiagnostic > expectedDiagnostics,
217
247
) async {
218
248
await _resolveFile (path);
219
- await _assertDiagnosticsIn (_diagnostics, expectedDiagnostics);
249
+ _assertDiagnosticsIn (_diagnostics, expectedDiagnostics);
220
250
}
221
251
222
252
/// Asserts that the diagnostics for each `path` match those in the paired
@@ -230,7 +260,7 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
230
260
) async {
231
261
for (var (path, expectedDiagnostics) in unitsAndDiagnostics) {
232
262
result = await resolveFile (convertPath (path));
233
- await _assertDiagnosticsIn (result.errors, expectedDiagnostics);
263
+ _assertDiagnosticsIn (result.errors, expectedDiagnostics);
234
264
}
235
265
}
236
266
@@ -246,7 +276,7 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
246
276
Future <void > assertNoPubspecDiagnostics (String content) async {
247
277
newFile (testPackagePubspecPath, content);
248
278
var errors = await _resolvePubspecFile (content);
249
- await _assertDiagnosticsIn (errors, []);
279
+ _assertDiagnosticsIn (errors, []);
250
280
}
251
281
252
282
/// Asserts that [expectedDiagnostics] are reported when resolving [content] .
@@ -256,13 +286,36 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
256
286
) async {
257
287
newFile (testPackagePubspecPath, content);
258
288
var errors = await _resolvePubspecFile (content);
259
- await _assertDiagnosticsIn (errors, expectedDiagnostics);
289
+ _assertDiagnosticsIn (errors, expectedDiagnostics);
260
290
}
261
291
262
292
@override
293
+ File newFile (String path, String content) {
294
+ if (_analysisContextCollection != null && ! path.endsWith ('.dart' )) {
295
+ throw StateError ('Only dart files can be changed after analysis.' );
296
+ }
297
+
298
+ return super .newFile (path, content);
299
+ }
300
+
301
+ /// Resolves a Dart source file at [path] .
302
+ ///
303
+ /// [path] must be converted for this file system.
304
+ Future <ResolvedUnitResult > resolveFile (String path) async {
305
+ var analysisContext = _contextFor (path);
306
+ var session = analysisContext.currentSession;
307
+ return await session.getResolvedUnit (path) as ResolvedUnitResult ;
308
+ }
309
+
263
310
@mustCallSuper
264
311
void setUp () {
265
- super .setUp ();
312
+ if (! _lintRulesAreRegistered) {
313
+ registerLintRules ();
314
+ _lintRulesAreRegistered = true ;
315
+ }
316
+
317
+ createMockSdk (resourceProvider: resourceProvider, root: _sdkRoot);
318
+
266
319
// Check for any needlessly enabled experiments.
267
320
for (var experiment in experiments) {
268
321
var feature = ExperimentStatus .knownFeatures[experiment];
@@ -282,6 +335,12 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
282
335
_writeTestPackagePubspecYamlFile (pubspecYamlContent (name: 'test' ));
283
336
}
284
337
338
+ @mustCallSuper
339
+ Future <void > tearDown () async {
340
+ await _analysisContextCollection? .dispose ();
341
+ _analysisContextCollection = null ;
342
+ }
343
+
285
344
void writePackageConfig (String path, PackageConfigFileBuilder config) {
286
345
newFile (path, config.toContent (toUriStr: toUriStr));
287
346
}
@@ -335,11 +394,15 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
335
394
writePackageConfig (path, configCopy);
336
395
}
337
396
397
+ void _addTestFile (String content) {
398
+ newFile (_testFilePath, content);
399
+ }
400
+
338
401
/// Asserts that the diagnostics in [diagnostics] match [expectedDiagnostics] .
339
- Future < void > _assertDiagnosticsIn (
402
+ void _assertDiagnosticsIn (
340
403
List <Diagnostic > diagnostics,
341
404
List <ExpectedDiagnostic > expectedDiagnostics,
342
- ) async {
405
+ ) {
343
406
//
344
407
// Match actual diagnostics to expected diagnostics.
345
408
//
@@ -464,6 +527,36 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
464
527
}
465
528
}
466
529
530
+ DriverBasedAnalysisContext _contextFor (String path) {
531
+ _createAnalysisContexts ();
532
+
533
+ var convertedPath = convertPath (path);
534
+ return _analysisContextCollection! .contextFor (convertedPath);
535
+ }
536
+
537
+ /// Creates all analysis contexts in [_collectionIncludedPaths] .
538
+ void _createAnalysisContexts () {
539
+ if (_analysisContextCollection != null ) {
540
+ return ;
541
+ }
542
+
543
+ _analysisContextCollection = AnalysisContextCollectionImpl (
544
+ byteStore: _byteStore,
545
+ declaredVariables: {},
546
+ enableIndex: true ,
547
+ includedPaths: _collectionIncludedPaths.map (convertPath).toList (),
548
+ resourceProvider: resourceProvider,
549
+ sdkPath: _sdkRoot.path,
550
+ );
551
+ }
552
+
553
+ /// Resolves the file with the [path] into [result] .
554
+ Future <void > _resolveFile (String path) async {
555
+ var convertedPath = convertPath (path);
556
+
557
+ result = await resolveFile (convertedPath);
558
+ }
559
+
467
560
Future <List <Diagnostic >> _resolvePubspecFile (String content) async {
468
561
var path = convertPath (testPackagePubspecPath);
469
562
var pubspecRules = < AbstractAnalysisRule , PubspecVisitor <Object ?>> {};
@@ -499,119 +592,13 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
499
592
return [...listener.errors];
500
593
}
501
594
595
+ Future <void > _resolveTestFile () => _resolveFile (_testFilePath);
596
+
502
597
void _writeTestPackagePubspecYamlFile (String content) {
503
598
newPubspecYamlFile (testPackageRootPath, content);
504
599
}
505
600
}
506
601
507
- abstract class _ContextResolutionTest
508
- with MockPackagesMixin , ResourceProviderMixin {
509
- static bool _lintRulesAreRegistered = false ;
510
-
511
- /// The byte store that is reused between tests. This allows reusing all
512
- /// unlinked and linked summaries for SDK, so that tests run much faster.
513
- /// However nothing is preserved between Dart VM runs, so changes to the
514
- /// implementation are still fully verified.
515
- static final MemoryByteStore _sharedByteStore = MemoryByteStore ();
516
-
517
- final MemoryByteStore _byteStore = _sharedByteStore;
518
-
519
- AnalysisContextCollectionImpl ? _analysisContextCollection;
520
-
521
- late ResolvedUnitResult result;
522
-
523
- /// Error codes that by default should be ignored in test expectations.
524
- List <DiagnosticCode > get ignoredErrorCodes => [
525
- WarningCode .UNUSED_LOCAL_VARIABLE ,
526
- ];
527
-
528
- /// The path to the root of the external packages.
529
- @override
530
- String get packagesRootPath => '/packages' ;
531
-
532
- String get testFilePath;
533
-
534
- List <String > get _collectionIncludedPaths;
535
-
536
- /// The diagnostics that were computed during analysis.
537
- List <Diagnostic > get _diagnostics =>
538
- result.errors
539
- .whereNot ((e) => ignoredErrorCodes.any ((c) => e.errorCode == c))
540
- .toList ();
541
-
542
- Folder get _sdkRoot => newFolder ('/sdk' );
543
-
544
- void addTestFile (String content) {
545
- newFile (testFilePath, content);
546
- }
547
-
548
- @override
549
- File newFile (String path, String content) {
550
- if (_analysisContextCollection != null && ! path.endsWith ('.dart' )) {
551
- throw StateError ('Only dart files can be changed after analysis.' );
552
- }
553
-
554
- return super .newFile (path, content);
555
- }
556
-
557
- /// Resolves a Dart source file at [path] .
558
- ///
559
- /// [path] must be converted for this file system.
560
- Future <ResolvedUnitResult > resolveFile (String path) async {
561
- var analysisContext = _contextFor (path);
562
- var session = analysisContext.currentSession;
563
- return await session.getResolvedUnit (path) as ResolvedUnitResult ;
564
- }
565
-
566
- Future <void > resolveTestFile () => _resolveFile (testFilePath);
567
-
568
- @mustCallSuper
569
- void setUp () {
570
- if (! _lintRulesAreRegistered) {
571
- registerLintRules ();
572
- _lintRulesAreRegistered = true ;
573
- }
574
-
575
- createMockSdk (resourceProvider: resourceProvider, root: _sdkRoot);
576
- }
577
-
578
- @mustCallSuper
579
- Future <void > tearDown () async {
580
- await _analysisContextCollection? .dispose ();
581
- _analysisContextCollection = null ;
582
- }
583
-
584
- DriverBasedAnalysisContext _contextFor (String path) {
585
- _createAnalysisContexts ();
586
-
587
- var convertedPath = convertPath (path);
588
- return _analysisContextCollection! .contextFor (convertedPath);
589
- }
590
-
591
- /// Creates all analysis contexts in [_collectionIncludedPaths] .
592
- void _createAnalysisContexts () {
593
- if (_analysisContextCollection != null ) {
594
- return ;
595
- }
596
-
597
- _analysisContextCollection = AnalysisContextCollectionImpl (
598
- byteStore: _byteStore,
599
- declaredVariables: {},
600
- enableIndex: true ,
601
- includedPaths: _collectionIncludedPaths.map (convertPath).toList (),
602
- resourceProvider: resourceProvider,
603
- sdkPath: _sdkRoot.path,
604
- );
605
- }
606
-
607
- /// Resolves the file with the [path] into [result] .
608
- Future <void > _resolveFile (String path) async {
609
- var convertedPath = convertPath (path);
610
-
611
- result = await resolveFile (convertedPath);
612
- }
613
- }
614
-
615
602
/// A description of an expected error.
616
603
final class _ExpectedError extends ExpectedDiagnostic {
617
604
final DiagnosticCode _code;
0 commit comments