Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: pkg/analyzer/test/src/summary/bazel_summary_test.dart

Issue 2386463002: Add BazelResultProvider for on-demand packages loading. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'package:analyzer/dart/ast/ast.dart'; 5 import 'package:analyzer/dart/ast/ast.dart';
6 import 'package:analyzer/dart/ast/token.dart'; 6 import 'package:analyzer/dart/ast/token.dart';
7 import 'package:analyzer/dart/element/element.dart';
7 import 'package:analyzer/error/listener.dart'; 8 import 'package:analyzer/error/listener.dart';
8 import 'package:analyzer/file_system/file_system.dart'; 9 import 'package:analyzer/file_system/file_system.dart';
10 import 'package:analyzer/src/context/cache.dart';
9 import 'package:analyzer/src/dart/scanner/reader.dart'; 11 import 'package:analyzer/src/dart/scanner/reader.dart';
10 import 'package:analyzer/src/dart/scanner/scanner.dart'; 12 import 'package:analyzer/src/dart/scanner/scanner.dart';
11 import 'package:analyzer/src/generated/engine.dart'; 13 import 'package:analyzer/src/generated/engine.dart';
12 import 'package:analyzer/src/generated/parser.dart'; 14 import 'package:analyzer/src/generated/parser.dart';
13 import 'package:analyzer/src/generated/source.dart'; 15 import 'package:analyzer/src/generated/source.dart';
14 import 'package:analyzer/src/summary/bazel_summary.dart'; 16 import 'package:analyzer/src/summary/bazel_summary.dart';
15 import 'package:analyzer/src/summary/format.dart'; 17 import 'package:analyzer/src/summary/format.dart';
16 import 'package:analyzer/src/summary/summarize_ast.dart'; 18 import 'package:analyzer/src/summary/summarize_ast.dart';
17 import 'package:analyzer/src/summary/summarize_elements.dart'; 19 import 'package:analyzer/src/summary/summarize_elements.dart';
18 import 'package:analyzer/src/util/fast_uri.dart'; 20 import 'package:analyzer/src/util/fast_uri.dart';
21 import 'package:analyzer/task/dart.dart';
19 import 'package:path/path.dart' as pathos; 22 import 'package:path/path.dart' as pathos;
20 import 'package:test_reflective_loader/test_reflective_loader.dart'; 23 import 'package:test_reflective_loader/test_reflective_loader.dart';
21 import 'package:unittest/unittest.dart'; 24 import 'package:unittest/unittest.dart';
22 25
23 import '../../utils.dart'; 26 import '../../utils.dart';
24 import '../context/abstract_context.dart'; 27 import '../context/abstract_context.dart';
25 28
26 main() { 29 main() {
27 initializeTestEnvironment(); 30 initializeTestEnvironment();
31 defineReflectiveTests(BazelResultProviderTest);
28 defineReflectiveTests(SummaryProviderTest); 32 defineReflectiveTests(SummaryProviderTest);
29 } 33 }
30 34
31 const OUT_ROOT = '$SRC_ROOT/bazel-bin'; 35 const OUT_ROOT = '$SRC_ROOT/bazel-bin';
32 const SRC_ROOT = '/company/src/user/project/root'; 36 const SRC_ROOT = '/company/src/user/project/root';
33 37
34 @reflectiveTest 38 @reflectiveTest
35 class SummaryProviderTest extends AbstractContextTest { 39 class BazelResultProviderTest extends _BaseTest {
40 BazelResultProvider provider;
41
42 @override
43 void setUp() {
44 super.setUp();
45 provider = new BazelResultProvider(
46 new SummaryProvider(resourceProvider, _getOutputFolder, context));
47 }
48
49 test_failure_inconsistent_directDependency() {
50 _setComponentFile('aaa', 'a.dart', 'class A {}');
51 _setComponentFile(
52 'bbb',
53 'b.dart',
54 r'''
55 import 'package:components.aaa/a.dart';
56 class B extends A {}
57 ''');
58 _writeUnlinkedBundle('components.aaa');
59 _writeUnlinkedBundle('components.bbb');
60 _setComponentFile('aaa', 'a.dart', 'class A2 {}');
61 // The 'aaa' unlinked bundle in inconsistent, so 'bbb' linking fails.
62 Source source = _resolveUri('package:components.bbb/b.dart');
63 CacheEntry entry = context.getCacheEntry(source);
64 expect(provider.compute(entry, LIBRARY_ELEMENT), isFalse);
65 }
66
67 test_failure_missingDirectDependency() {
68 _setComponentFile('aaa', 'a.dart', 'class A {}');
69 _setComponentFile(
70 'bbb',
71 'b.dart',
72 r'''
73 import 'package:components.aaa/a.dart';
74 class B extends A {}
75 ''');
76 _writeUnlinkedBundle('components.bbb');
77 // We cannot find 'aaa' bundle, so 'bbb' linking fails.
78 Source source = _resolveUri('package:components.bbb/b.dart');
79 CacheEntry entry = context.getCacheEntry(source);
80 expect(provider.compute(entry, LIBRARY_ELEMENT), isFalse);
81 }
82
83 test_success_withoutDependencies() {
84 _setComponentFile('aaa', 'a.dart', 'class A {}');
85 _writeUnlinkedBundle('components.aaa');
86 // Resynthesize 'aaa' library.
87 Source source = _resolveUri('package:components.aaa/a.dart');
88 LibraryElement library = _resynthesizeLibrary(source);
89 List<ClassElement> types = library.definingCompilationUnit.types;
90 expect(types, hasLength(1));
91 expect(types.single.name, 'A');
92 }
93
94 test_withDependency_import() {
95 _setComponentFile('aaa', 'a.dart', 'class A {}');
96 _setComponentFile(
97 'bbb',
98 'b.dart',
99 r'''
100 import 'package:components.aaa/a.dart';
101 class B extends A {}
102 ''');
103 _writeUnlinkedBundle('components.aaa');
104 _writeUnlinkedBundle('components.bbb');
105 // Prepare sources.
106 Source sourceA = _resolveUri('package:components.aaa/a.dart');
107 Source sourceB = _resolveUri('package:components.bbb/b.dart');
108 // Resynthesize 'bbb' library.
109 LibraryElement libraryB = _resynthesizeLibrary(sourceB);
110 List<ClassElement> types = libraryB.definingCompilationUnit.types;
111 expect(types, hasLength(1));
112 ClassElement typeB = types.single;
113 expect(typeB.name, 'B');
114 expect(typeB.supertype.name, 'A');
115 // The LibraryElement for 'aaa' is not created at all.
116 expect(context.getResult(sourceA, LIBRARY_ELEMENT), isNull);
117 // But we can resynthesize it, and it's the same as from 'bbb'.
118 expect(provider.compute(context.getCacheEntry(sourceA), LIBRARY_ELEMENT),
119 isTrue);
120 LibraryElement libraryA = context.getResult(sourceA, LIBRARY_ELEMENT);
121 expect(libraryA, isNotNull);
122 expect(typeB.supertype.element.library, same(libraryA));
123 }
124
125 LibraryElement _resynthesizeLibrary(Source source) {
126 CacheEntry entry = context.getCacheEntry(source);
127 expect(provider.compute(entry, LIBRARY_ELEMENT), isTrue);
128 return context.getResult(source, LIBRARY_ELEMENT);
129 }
130 }
131
132 @reflectiveTest
133 class SummaryProviderTest extends _BaseTest {
36 SummaryProvider manager; 134 SummaryProvider manager;
37 135
38 @override 136 @override
39 void setUp() { 137 void setUp() {
40 super.setUp(); 138 super.setUp();
41 // Include a 'package' URI resolver.
42 sourceFactory = new SourceFactory(<UriResolver>[
43 sdkResolver,
44 resourceResolver,
45 new _TestPackageResolver(resourceProvider)
46 ], null, resourceProvider);
47 context.sourceFactory = sourceFactory;
48 // Create a new SummaryProvider instance.
49 manager = new SummaryProvider(resourceProvider, _getOutputFolder, context); 139 manager = new SummaryProvider(resourceProvider, _getOutputFolder, context);
50 } 140 }
51 141
142 test_getLinkedPackages_null_inconsistent_directDependency() {
143 _setComponentFile('aaa', 'a.dart', 'class A {}');
144 _setComponentFile(
145 'bbb',
146 'b.dart',
147 r'''
148 import 'package:components.aaa/a.dart';
149 class B extends A {}
150 ''');
151 _writeUnlinkedBundle('components.aaa');
152 _writeUnlinkedBundle('components.bbb');
153 _setComponentFile('aaa', 'a.dart', 'class A2 {}');
154 // The 'aaa' unlinked bundle in inconsistent, so 'bbb' linking fails.
155 Source source = _resolveUri('package:components.bbb/b.dart');
156 List<Package> packages = manager.getLinkedPackages(source);
157 expect(packages, isNull);
158 }
159
52 test_getLinkedPackages_null_missingBundle() { 160 test_getLinkedPackages_null_missingBundle() {
53 _setComponentFile('aaa', 'a.dart', 'class A {}'); 161 _setComponentFile('aaa', 'a.dart', 'class A {}');
54 // We don't write 'aaa', so we cannot get its package. 162 // We don't write 'aaa', so we cannot get its package.
55 // Ask the package for the URI. 163 // Ask the package for the URI.
56 Source source = _resolveUri('package:components.aaa/a.dart'); 164 Source source = _resolveUri('package:components.aaa/a.dart');
57 List<Package> packages = manager.getLinkedPackages(source); 165 List<Package> packages = manager.getLinkedPackages(source);
58 expect(packages, isNull); 166 expect(packages, isNull);
59 } 167 }
60 168
61 test_getLinkedPackages_null_missingDirectDependency() { 169 test_getLinkedPackages_null_missingDirectDependency() {
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 File file1 = _setComponentFile('aaa', 'a1.dart', 'class A1 {}'); 311 File file1 = _setComponentFile('aaa', 'a1.dart', 'class A1 {}');
204 _setComponentFile('aaa', 'a2.dart', 'class A2 {}'); 312 _setComponentFile('aaa', 'a2.dart', 'class A2 {}');
205 _writeUnlinkedBundle('components.aaa'); 313 _writeUnlinkedBundle('components.aaa');
206 // Update one of the files, so the bundle is not consistent. 314 // Update one of the files, so the bundle is not consistent.
207 file1.writeAsStringSync('\nclass A1 {}'); 315 file1.writeAsStringSync('\nclass A1 {}');
208 Source source1 = _resolveUri('package:components.aaa/a1.dart'); 316 Source source1 = _resolveUri('package:components.aaa/a1.dart');
209 Source source2 = _resolveUri('package:components.aaa/a2.dart'); 317 Source source2 = _resolveUri('package:components.aaa/a2.dart');
210 expect(manager.getUnlinkedForUri(source1.uri), isNull); 318 expect(manager.getUnlinkedForUri(source1.uri), isNull);
211 expect(manager.getUnlinkedForUri(source2.uri), isNull); 319 expect(manager.getUnlinkedForUri(source2.uri), isNull);
212 } 320 }
321 }
322
323 class _BaseTest extends AbstractContextTest {
324 @override
325 void setUp() {
326 super.setUp();
327 // Include a 'package' URI resolver.
328 sourceFactory = new SourceFactory(<UriResolver>[
329 sdkResolver,
330 resourceResolver,
331 new _TestPackageResolver(resourceProvider)
332 ], null, resourceProvider);
333 context.sourceFactory = sourceFactory;
334 }
213 335
214 Folder _getOutputFolder(Uri absoluteUri) { 336 Folder _getOutputFolder(Uri absoluteUri) {
215 if (absoluteUri.scheme == 'package') { 337 if (absoluteUri.scheme == 'package') {
216 List<String> segments = absoluteUri.pathSegments; 338 List<String> segments = absoluteUri.pathSegments;
217 if (segments.isNotEmpty) { 339 if (segments.isNotEmpty) {
218 String packageName = segments.first; 340 String packageName = segments.first;
219 String path = OUT_ROOT + '/' + packageName.replaceAll('.', '/'); 341 String path = OUT_ROOT + '/' + packageName.replaceAll('.', '/');
220 return resourceProvider.getFolder(path); 342 return resourceProvider.getFolder(path);
221 } 343 }
222 } 344 }
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 } 457 }
336 } 458 }
337 return null; 459 return null;
338 } 460 }
339 461
340 @override 462 @override
341 Uri restoreAbsolute(Source source) { 463 Uri restoreAbsolute(Source source) {
342 throw new UnimplementedError(); 464 throw new UnimplementedError();
343 } 465 }
344 } 466 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698