22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5+ import 'dart:convert' ;
6+
57import 'package:analyzer/dart/analysis/analysis_context.dart' ;
68import 'package:analyzer/dart/analysis/analysis_context_collection.dart' ;
79import 'package:analyzer/dart/analysis/session.dart' ;
@@ -23,7 +25,19 @@ class AbstractContextTest with ResourceProviderMixin {
2325 AnalysisContextCollection _analysisContextCollection;
2426 AnalysisDriver _driver;
2527
26- AnalysisDriver get driver => _driver;
28+ final Set <String > knownPackages = {};
29+
30+ /// Whether the test should perform analysis with NNBD enabled.
31+ ///
32+ /// `false` by default. May be overridden in derived test classes.
33+ bool get analyzeWithNnbd => false ;
34+
35+ AnalysisDriver get driver {
36+ if (_driver == null ) {
37+ _createAnalysisContexts ();
38+ }
39+ return _driver;
40+ }
2741
2842 String get homePath => '/home' ;
2943
@@ -49,7 +63,7 @@ class Required {
4963 /// package.
5064 File addPackageFile (String packageName, String pathInLib, String content) {
5165 var packagePath = '/.pub-cache/$packageName ' ;
52- _addTestPackageDependency (packageName, packagePath );
66+ knownPackages. add (packageName);
5367 return newFile ('$packagePath /lib/$pathInLib ' , content: content);
5468 }
5569
@@ -91,22 +105,13 @@ export 'package:test_core/test_core.dart';
91105''' );
92106 }
93107
94- /// Create all analysis contexts in [_homePath] .
95- void createAnalysisContexts () {
96- _analysisContextCollection = AnalysisContextCollectionImpl (
97- includedPaths: [convertPath (homePath)],
98- enableIndex: true ,
99- resourceProvider: overlayResourceProvider,
100- sdkPath: convertPath ('/sdk' ),
101- );
102-
103- _driver = getDriver (convertPath (testsPath));
104- }
105-
106108 /// Return the existing analysis context that should be used to analyze the
107109 /// given [path] , or throw [StateError] if the [path] is not analyzed in any
108110 /// of the created analysis contexts.
109111 AnalysisContext getContext (String path) {
112+ if (_analysisContextCollection == null ) {
113+ _createAnalysisContexts ();
114+ }
110115 path = convertPath (path);
111116 return _analysisContextCollection.contextFor (path);
112117 }
@@ -142,86 +147,6 @@ environment:
142147 sdk: '>=2.9.0 <3.0.0'
143148''' );
144149 }
145- var packageConfigPath = '$testsPath /.dart_tool/package_config.json' ;
146- // Subclasses may write out a different file first.
147- if (! getFile (packageConfigPath).exists) {
148- // TODO(srawlins): This is a rough hack to allow for the "null safe by
149- // default" flag flip. We need to _opt out_ all packages at the onset.
150- // A better solution likely involves the package config-editing code in
151- // analyzer's [context_collection_resolution.dart].
152- newFile (packageConfigPath, content: '''
153- {
154- "configVersion": 2,
155- "packages": [
156- {
157- "name": "args",
158- "rootUri": "${toUriStr ('/.pub-cache/args' )}",
159- "packageUri": "lib/",
160- "languageVersion": "2.9"
161- },
162- {
163- "name": "collection",
164- "rootUri": "${toUriStr ('/.pub-cache/collection' )}",
165- "packageUri": "lib/",
166- "languageVersion": "2.9"
167- },
168- {
169- "name": "fixnum",
170- "rootUri": "${toUriStr ('/.pub-cache/fixnum' )}",
171- "packageUri": "lib/",
172- "languageVersion": "2.9"
173- },
174- {
175- "name": "foo",
176- "rootUri": "${toUriStr ('/.pub-cache/foo' )}",
177- "packageUri": "lib/",
178- "languageVersion": "2.9"
179- },
180- {
181- "name": "http",
182- "rootUri": "${toUriStr ('/.pub-cache/http' )}",
183- "packageUri": "lib/",
184- "languageVersion": "2.9"
185- },
186- {
187- "name": "meta",
188- "rootUri": "${toUriStr ('/.pub-cache/meta' )}",
189- "packageUri": "lib/",
190- "languageVersion": "2.9"
191- },
192- {
193- "name": "quiver",
194- "rootUri": "${toUriStr ('/.pub-cache/quiver' )}",
195- "packageUri": "lib/",
196- "languageVersion": "2.9"
197- },
198- {
199- "name": "test",
200- "rootUri": "${toUriStr ('/.pub-cache/test' )}",
201- "packageUri": "lib/",
202- "languageVersion": "2.9"
203- },
204- {
205- "name": "test_core",
206- "rootUri": "${toUriStr ('/.pub-cache/test_core' )}",
207- "packageUri": "lib/",
208- "languageVersion": "2.9"
209- },
210- {
211- "name": "tests",
212- "rootUri": "../",
213- "packageUri": "lib/",
214- "languageVersion": "2.9"
215- }
216- ],
217- "generated": "2020-10-21T21:13:05.186004Z",
218- "generator": "pub",
219- "generatorVersion": "2.10.0"
220- }
221- ''' );
222- }
223-
224- createAnalysisContexts ();
225150 }
226151
227152 void setupResourceProvider () {}
@@ -230,33 +155,38 @@ environment:
230155 AnalysisEngine .instance.clearCaches ();
231156 }
232157
233- void _addTestPackageDependency (String name, String rootPath) {
234- var packagesFile = getFile ('$testsPath /.packages' );
235- var packagesContent = packagesFile.readAsStringSync ();
236-
237- // Ignore if there is already the same package dependency.
238- if (packagesContent.contains ('$name :file://' )) {
239- return ;
240- }
241-
242- packagesContent += '$name :${toUri ('$rootPath /lib' )}\n ' ;
243-
244- packagesFile.writeAsStringSync (packagesContent);
245-
246- _createDriver ();
247- }
248-
249- void _createDriver () {
250- var collection = AnalysisContextCollectionImpl (
158+ /// Create all analysis contexts in [_homePath] .
159+ void _createAnalysisContexts () {
160+ var packageConfigJson = {
161+ 'configVersion' : 2 ,
162+ 'packages' : [
163+ for (var packageName in knownPackages)
164+ {
165+ 'name' : packageName,
166+ 'rootUri' : toUriStr ('/.pub-cache/$packageName ' ),
167+ 'packageUri' : 'lib/' ,
168+ 'languageVersion' : '2.9'
169+ },
170+ {
171+ 'name' : 'tests' ,
172+ 'rootUri' : '../' ,
173+ 'packageUri' : 'lib/' ,
174+ 'languageVersion' : analyzeWithNnbd ? '2.12' : '2.9'
175+ }
176+ ],
177+ 'generated' : '2020-10-21T21:13:05.186004Z' ,
178+ 'generator' : 'pub' ,
179+ 'generatorVersion' : '2.10.0'
180+ };
181+ newFile ('$testsPath /.dart_tool/package_config.json' ,
182+ content: JsonEncoder .withIndent (' ' ).convert (packageConfigJson));
183+ _analysisContextCollection = AnalysisContextCollectionImpl (
251184 includedPaths: [convertPath (homePath)],
252185 enableIndex: true ,
253- resourceProvider: resourceProvider ,
186+ resourceProvider: overlayResourceProvider ,
254187 sdkPath: convertPath ('/sdk' ),
255188 );
256189
257- var testPath = convertPath (testsPath);
258- var context = collection.contextFor (testPath) as DriverBasedAnalysisContext ;
259-
260- _driver = context.driver;
190+ _driver = getDriver (convertPath (testsPath));
261191 }
262192}
0 commit comments