Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 3eb34ef

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: programmatically generate package_config.json in unit tests.
Previously, we hard a hard-coded set that included all packages added by unit tests, which created confusion when writing new unit tests. Now, `addPackageFile` records a list of all packages and `package_config.json` is automatically generated just before analysis begins. Change-Id: I3393c0266a54d051ffa7601e74753a75857635e2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/170024 Reviewed-by: Samuel Rawlins <srawlins@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
1 parent 5918aa8 commit 3eb34ef

File tree

2 files changed

+47
-146
lines changed

2 files changed

+47
-146
lines changed

pkg/nnbd_migration/test/abstract_context.dart

Lines changed: 47 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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+
57
import 'package:analyzer/dart/analysis/analysis_context.dart';
68
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
79
import '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
}

pkg/nnbd_migration/test/abstract_single_unit.dart

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ class AbstractSingleUnitTest extends AbstractContextTest {
3030
FindNode findNode;
3131
FindElement findElement;
3232

33-
/// Whether the test should perform analysis with NNBD enabled.
34-
///
35-
/// `false` by default. May be overridden in derived test classes.
36-
///
37-
/// TODO(paulberry): once NNBD ships, this should no longer be needed.
38-
bool get analyzeWithNnbd => false;
39-
4033
void addTestSource(String code, [Uri uri]) {
4134
testCode = code;
4235
testSource = addSource(testFile, code, uri);
@@ -80,28 +73,6 @@ name: tests
8073
version: 1.0.0
8174
environment:
8275
sdk: '>=2.12.0 <3.0.0'
83-
''');
84-
newFile('$testRoot/.dart_tool/package_config.json', content: '''
85-
{
86-
"configVersion": 2,
87-
"packages": [
88-
{
89-
"name": "meta",
90-
"rootUri": "file:///.pub-cache/meta",
91-
"packageUri": "lib/",
92-
"languageVersion": "2.12"
93-
},
94-
{
95-
"name": "tests",
96-
"rootUri": "../",
97-
"packageUri": "lib/",
98-
"languageVersion": "2.12"
99-
}
100-
],
101-
"generated": "2020-10-21T21:13:05.186004Z",
102-
"generator": "pub",
103-
"generatorVersion": "2.12.0"
104-
}
10576
''');
10677
}
10778
super.setUp();

0 commit comments

Comments
 (0)