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

Side by Side Diff: pkg/analysis_server/test/context_manager_test.dart

Issue 1503353002: Embedded option processing fixes (#25115). (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years 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) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 library test.context.directory.manager; 5 library test.context.directory.manager;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analysis_server/src/context_manager.dart'; 9 import 'package:analysis_server/src/context_manager.dart';
10 import 'package:analyzer/file_system/file_system.dart'; 10 import 'package:analyzer/file_system/file_system.dart';
11 import 'package:analyzer/file_system/memory_file_system.dart'; 11 import 'package:analyzer/file_system/memory_file_system.dart';
12 import 'package:analyzer/instrumentation/instrumentation.dart'; 12 import 'package:analyzer/instrumentation/instrumentation.dart';
13 import 'package:analyzer/source/embedder.dart'; 13 import 'package:analyzer/source/embedder.dart';
14 import 'package:analyzer/src/generated/engine.dart'; 14 import 'package:analyzer/src/generated/engine.dart';
15 import 'package:analyzer/src/generated/error.dart'; 15 import 'package:analyzer/src/generated/error.dart';
16 import 'package:analyzer/src/generated/source.dart'; 16 import 'package:analyzer/src/generated/source.dart';
17 import 'package:analyzer/src/generated/source_io.dart'; 17 import 'package:analyzer/src/generated/source_io.dart';
18 import 'package:analyzer/src/services/lint.dart';
19 import 'package:linter/src/plugin/linter_plugin.dart';
20 import 'package:linter/src/rules/avoid_as.dart';
18 import 'package:package_config/packages.dart'; 21 import 'package:package_config/packages.dart';
19 import 'package:path/path.dart'; 22 import 'package:path/path.dart';
23 import 'package:plugin/plugin.dart';
20 import 'package:test_reflective_loader/test_reflective_loader.dart'; 24 import 'package:test_reflective_loader/test_reflective_loader.dart';
21 import 'package:unittest/unittest.dart'; 25 import 'package:unittest/unittest.dart';
22 26
23 import 'mocks.dart'; 27 import 'mocks.dart';
24 import 'utils.dart'; 28 import 'utils.dart';
25 29
26 main() { 30 main() {
27 initializeTestEnvironment(); 31 initializeTestEnvironment();
28 defineReflectiveTests(AbstractContextManagerTest); 32 defineReflectiveTests(AbstractContextManagerTest);
29 } 33 }
(...skipping 30 matching lines...) Expand all
60 TestContextManagerCallbacks callbacks; 64 TestContextManagerCallbacks callbacks;
61 65
62 MemoryResourceProvider resourceProvider; 66 MemoryResourceProvider resourceProvider;
63 67
64 MockPackageMapProvider packageMapProvider; 68 MockPackageMapProvider packageMapProvider;
65 69
66 UriResolver packageResolver = null; 70 UriResolver packageResolver = null;
67 71
68 String projPath = '/my/proj'; 72 String projPath = '/my/proj';
69 73
74 AnalysisError missing_return =
75 new AnalysisError(new TestSource(), 0, 1, HintCode.MISSING_RETURN, [
76 ['x']
77 ]);
78
79 AnalysisError invalid_assignment_error =
80 new AnalysisError(new TestSource(), 0, 1, HintCode.INVALID_ASSIGNMENT, [
81 ['x'],
82 ['y']
83 ]);
84
85 AnalysisError unused_local_variable = new AnalysisError(
86 new TestSource(), 0, 1, HintCode.UNUSED_LOCAL_VARIABLE, [
87 ['x']
88 ]);
89
90 List<ErrorFilter> get errorFilters =>
91 callbacks.currentContext.getConfigurationData(CONFIGURED_ERROR_FILTERS);
92
93 List<Linter> get lints => getLints(callbacks.currentContext);
94
95 AnalysisOptions get options => callbacks.currentContext.analysisOptions;
96
97 void deleteFile(List<String> pathComponents) {
98 String filePath = posix.joinAll(pathComponents);
99 resourceProvider.deleteFile(filePath);
100 }
101
70 String newFile(List<String> pathComponents, [String content = '']) { 102 String newFile(List<String> pathComponents, [String content = '']) {
71 String filePath = posix.joinAll(pathComponents); 103 String filePath = posix.joinAll(pathComponents);
72 resourceProvider.newFile(filePath, content); 104 resourceProvider.newFile(filePath, content);
73 return filePath; 105 return filePath;
74 } 106 }
75 107
76 String newFolder(List<String> pathComponents) { 108 String newFolder(List<String> pathComponents) {
77 String folderPath = posix.joinAll(pathComponents); 109 String folderPath = posix.joinAll(pathComponents);
78 resourceProvider.newFolder(folderPath); 110 resourceProvider.newFolder(folderPath);
79 return folderPath; 111 return folderPath;
80 } 112 }
81 113
82 UriResolver providePackageResolver(Folder folder) { 114 UriResolver providePackageResolver(Folder folder) {
83 return packageResolver; 115 return packageResolver;
84 } 116 }
85 117
86 void setUp() { 118 void setUp() {
119 List<Plugin> plugins = <Plugin>[];
120 plugins.add(linterPlugin);
121
122 // Defer to the extension manager in AE for plugin registration.
123 AnalysisEngine.instance.userDefinedPlugins = plugins;
124 // Force registration.
125 AnalysisEngine.instance.taskManager;
126
87 resourceProvider = new MemoryResourceProvider(); 127 resourceProvider = new MemoryResourceProvider();
88 packageMapProvider = new MockPackageMapProvider(); 128 packageMapProvider = new MockPackageMapProvider();
89 manager = new ContextManagerImpl(resourceProvider, providePackageResolver, 129 manager = new ContextManagerImpl(resourceProvider, providePackageResolver,
90 packageMapProvider, InstrumentationService.NULL_SERVICE); 130 packageMapProvider, InstrumentationService.NULL_SERVICE);
91 callbacks = new TestContextManagerCallbacks(resourceProvider); 131 callbacks = new TestContextManagerCallbacks(resourceProvider);
92 manager.callbacks = callbacks; 132 manager.callbacks = callbacks;
93 resourceProvider.newFolder(projPath); 133 resourceProvider.newFolder(projPath);
94 } 134 }
95 135
136 test_analysis_options_file_delete() async {
137 // Setup .analysis_options
138 newFile(
139 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE],
140 r'''
141 embedder_libs:
142 "dart:foobar": "../sdk_ext/entry.dart"
143 analyzer:
144 language:
145 enableGenericMethods: true
146 errors:
147 unused_local_variable: false
148 linter:
149 rules:
150 - camel_case_types
151 ''');
152
153 // Setup context.
154 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
155 await pumpEventQueue();
156
157 // Verify options were set.
158 expect(errorFilters, hasLength(1));
159 expect(lints, hasLength(1));
160 expect(options.enableGenericMethods, isTrue);
161
162 // Remove options.
163 deleteFile([projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE]);
164 await pumpEventQueue();
165
166 // Verify defaults restored.
167 expect(errorFilters, isEmpty);
168 expect(lints, isEmpty);
169 expect(options.enableGenericMethods, isFalse);
170 }
171
172 test_analysis_options_file_delete_with_embedder() async {
173 // Setup _embedder.yaml.
174 String libPath = newFolder([projPath, LIB_NAME]);
175 newFile(
176 [libPath, '_embedder.yaml'],
177 r'''
178 analyzer:
179 strong-mode: true
180 errors:
181 missing_return: false
182 linter:
183 rules:
184 - avoid_as
185 ''');
186
187 // Setup .packages file
188 newFile(
189 [projPath, '.packages'],
190 r'''
191 test_pack:lib/''');
192
193 // Setup .analysis_options
194 newFile(
195 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE],
196 r'''
197 analyzer:
198 language:
199 enableGenericMethods: true
200 errors:
201 unused_local_variable: false
202 linter:
203 rules:
204 - camel_case_types
205 ''');
206
207 // Setup context.
208 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
209 await pumpEventQueue();
210
211 // Verify options were set.
212 expect(options.enableGenericMethods, isTrue);
213 expect(options.strongMode, isTrue);
214 expect(errorFilters, hasLength(2));
215 expect(lints, hasLength(2));
216
217 // Remove options.
218 deleteFile([projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE]);
219 await pumpEventQueue();
220
221 // Verify defaults restored.
222 expect(options.enableGenericMethods, isFalse);
223 expect(lints, hasLength(1));
224 expect(lints.first, new isInstanceOf<AvoidAs>());
225 expect(errorFilters, hasLength(1));
226 expect(errorFilters.first(missing_return), isTrue);
227 }
228
96 test_analysis_options_parse_failure() async { 229 test_analysis_options_parse_failure() async {
97 // Create files. 230 // Create files.
98 String libPath = newFolder([projPath, LIB_NAME]); 231 String libPath = newFolder([projPath, LIB_NAME]);
99 newFile([libPath, 'main.dart']); 232 newFile([libPath, 'main.dart']);
100 String sdkExtPath = newFolder([projPath, 'sdk_ext']); 233 String sdkExtPath = newFolder([projPath, 'sdk_ext']);
101 newFile([sdkExtPath, 'entry.dart']); 234 newFile([sdkExtPath, 'entry.dart']);
102 String sdkExtSrcPath = newFolder([projPath, 'sdk_ext', 'src']); 235 String sdkExtSrcPath = newFolder([projPath, 'sdk_ext', 'src']);
103 newFile([sdkExtSrcPath, 'part.dart']); 236 newFile([sdkExtSrcPath, 'part.dart']);
104 // Setup analysis options file with ignore list. 237 // Setup analysis options file with ignore list.
105 newFile( 238 newFile(
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 // Setup _embedder.yaml. 280 // Setup _embedder.yaml.
148 newFile( 281 newFile(
149 [libPath, '_embedder.yaml'], 282 [libPath, '_embedder.yaml'],
150 r''' 283 r'''
151 embedder_libs: 284 embedder_libs:
152 "dart:foobar": "../sdk_ext/entry.dart" 285 "dart:foobar": "../sdk_ext/entry.dart"
153 analyzer: 286 analyzer:
154 strong-mode: true 287 strong-mode: true
155 language: 288 language:
156 enableSuperMixins: true 289 enableSuperMixins: true
290 errors:
291 missing_return: false
292 linter:
293 rules:
294 - avoid_as
157 '''); 295 ''');
158 // Setup .packages file 296 // Setup .packages file
159 newFile( 297 newFile(
160 [projPath, '.packages'], 298 [projPath, '.packages'],
161 r''' 299 r'''
162 test_pack:lib/'''); 300 test_pack:lib/''');
163 301
164 // Setup .analysis_options 302 // Setup .analysis_options
165 newFile( 303 newFile(
166 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE], 304 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE],
167 r''' 305 r'''
168 analyzer: 306 analyzer:
169 exclude: 307 exclude:
170 - 'test/**' 308 - 'test/**'
171 language: 309 language:
172 enableGenericMethods: true 310 enableGenericMethods: true
173 errors: 311 errors:
174 unused_local_variable: false 312 unused_local_variable: false
313 linter:
314 rules:
315 - camel_case_types
175 '''); 316 ''');
176 317
177 // Setup context. 318 // Setup context.
178 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 319 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
179 await pumpEventQueue(); 320 await pumpEventQueue();
180 321
181 // Confirm that one context was created. 322 // Confirm that one context was created.
182 var contexts = 323 var contexts =
183 manager.contextsInAnalysisRoot(resourceProvider.newFolder(projPath)); 324 manager.contextsInAnalysisRoot(resourceProvider.newFolder(projPath));
184 expect(contexts, isNotNull); 325 expect(contexts, isNotNull);
185 expect(contexts, hasLength(1)); 326 expect(contexts, hasLength(1));
186 var context = contexts[0]; 327 var context = contexts[0];
187 328
188 // Verify options. 329 // Verify options.
189 // * from `_embedder.yaml`: 330 // * from `_embedder.yaml`:
190 expect(context.analysisOptions.strongMode, isTrue); 331 expect(context.analysisOptions.strongMode, isTrue);
191 expect(context.analysisOptions.enableSuperMixins, isTrue); 332 expect(context.analysisOptions.enableSuperMixins, isTrue);
192 // * from `.analysis_options`: 333 // * from `.analysis_options`:
193 expect(context.analysisOptions.enableGenericMethods, isTrue); 334 expect(context.analysisOptions.enableGenericMethods, isTrue);
194 // verify tests are excluded 335 // * verify tests are excluded
195 expect(callbacks.currentContextFilePaths[projPath].keys, 336 expect(callbacks.currentContextFilePaths[projPath].keys,
196 ['/my/proj/sdk_ext/entry.dart']); 337 ['/my/proj/sdk_ext/entry.dart']);
197 338
198 // Verify filter setup. 339 // Verify filter setup.
199 List<ErrorFilter> filters = 340 expect(errorFilters, hasLength(2));
200 callbacks.currentContext.getConfigurationData(CONFIGURED_ERROR_FILTERS); 341
201 expect(filters, hasLength(1)); 342 // * (embedder.)
343 expect(errorFilters.any((f) => f(missing_return)), isTrue);
344
345 // * (options.)
346 expect(errorFilters.any((f) => f(unused_local_variable)), isTrue);
347
348 // Verify lints.
349 var lintNames = lints.map((lint) => lint.name);
350
202 expect( 351 expect(
203 filters.first(new AnalysisError( 352 lintNames,
204 new TestSource(), 0, 1, HintCode.UNUSED_LOCAL_VARIABLE, [ 353 unorderedEquals(
205 ['x'] 354 ['avoid_as' /* embedder */, 'camel_case_types' /* options */]));
206 ])),
207 isTrue);
208 355
209 // Sanity check embedder libs. 356 // Sanity check embedder libs.
210 var source = context.sourceFactory.forUri('dart:foobar'); 357 var source = context.sourceFactory.forUri('dart:foobar');
211 expect(source, isNotNull); 358 expect(source, isNotNull);
212 expect(source.fullName, '/my/proj/sdk_ext/entry.dart'); 359 expect(source.fullName, '/my/proj/sdk_ext/entry.dart');
213 } 360 }
214 361
215 test_embedder_packagespec() async { 362 test_embedder_packagespec() async {
216 // Create files. 363 // Create files.
217 String libPath = newFolder([projPath, LIB_NAME]); 364 String libPath = newFolder([projPath, LIB_NAME]);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE], 408 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE],
262 r''' 409 r'''
263 analyzer: 410 analyzer:
264 errors: 411 errors:
265 unused_local_variable: ignore 412 unused_local_variable: ignore
266 '''); 413 ''');
267 // Setup context. 414 // Setup context.
268 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 415 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
269 416
270 // Verify filter setup. 417 // Verify filter setup.
271 List<ErrorFilter> filters = 418 expect(errorFilters, isNotNull);
272 callbacks.currentContext.getConfigurationData(CONFIGURED_ERROR_FILTERS); 419 expect(errorFilters, hasLength(1));
273 expect(filters, isNotNull); 420 expect(errorFilters.first(unused_local_variable), isTrue);
274 expect(filters, hasLength(1));
275 expect(
276 filters.first(new AnalysisError(
277 new TestSource(), 0, 1, HintCode.UNUSED_LOCAL_VARIABLE, [
278 ['x']
279 ])),
280 isTrue);
281 } 421 }
282 422
283 test_error_filter_analysis_option_multiple_filters() async { 423 test_error_filter_analysis_option_multiple_filters() async {
284 // Create files. 424 // Create files.
285 newFile( 425 newFile(
286 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE], 426 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE],
287 r''' 427 r'''
288 analyzer: 428 analyzer:
289 errors: 429 errors:
290 invalid_assignment: ignore 430 invalid_assignment: ignore
291 unused_local_variable: ignore 431 unused_local_variable: ignore
292 '''); 432 ''');
293 // Setup context. 433 // Setup context.
294 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 434 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
295 435
296 // Verify filter setup. 436 // Verify filter setup.
297 List<ErrorFilter> filters = 437 expect(errorFilters, isNotNull);
298 callbacks.currentContext.getConfigurationData(CONFIGURED_ERROR_FILTERS); 438 expect(errorFilters, hasLength(2));
299 expect(filters, isNotNull);
300 expect(filters, hasLength(2));
301 439
302 var unused_error = new AnalysisError( 440 expect(errorFilters.any((filter) => filter(unused_local_variable)), isTrue);
303 new TestSource(), 0, 1, HintCode.UNUSED_LOCAL_VARIABLE, [ 441 expect(
304 ['x'] 442 errorFilters.any((filter) => filter(invalid_assignment_error)), isTrue);
305 ]);
306
307 var invalid_assignment_error =
308 new AnalysisError(new TestSource(), 0, 1, HintCode.INVALID_ASSIGNMENT, [
309 ['x'],
310 ['y']
311 ]);
312
313 expect(filters.any((filter) => filter(unused_error)), isTrue);
314 expect(filters.any((filter) => filter(invalid_assignment_error)), isTrue);
315 } 443 }
316 444
317 test_error_filter_analysis_option_synonyms() async { 445 test_error_filter_analysis_option_synonyms() async {
318 // Create files. 446 // Create files.
319 newFile( 447 newFile(
320 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE], 448 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE],
321 r''' 449 r'''
322 analyzer: 450 analyzer:
323 errors: 451 errors:
324 unused_local_variable: ignore 452 unused_local_variable: ignore
325 ambiguous_import: false 453 ambiguous_import: false
326 '''); 454 ''');
327 // Setup context. 455 // Setup context.
328 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 456 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
329 457
330 // Verify filter setup. 458 // Verify filter setup.
331 List<ErrorFilter> filters = 459 expect(errorFilters, isNotNull);
332 callbacks.currentContext.getConfigurationData(CONFIGURED_ERROR_FILTERS); 460 expect(errorFilters, hasLength(2));
333 expect(filters, isNotNull);
334 expect(filters, hasLength(2));
335 } 461 }
336 462
337 test_error_filter_analysis_option_unpsecified() async { 463 test_error_filter_analysis_option_unpsecified() async {
338 // Create files. 464 // Create files.
339 newFile( 465 newFile(
340 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE], 466 [projPath, AnalysisEngine.ANALYSIS_OPTIONS_FILE],
341 r''' 467 r'''
342 analyzer: 468 analyzer:
343 # errors: 469 # errors:
344 # unused_local_variable: ignore 470 # unused_local_variable: ignore
345 '''); 471 ''');
346 // Setup context. 472 // Setup context.
347 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); 473 manager.setRoots(<String>[projPath], <String>[], <String, String>{});
348 474
349 // Verify filter setup. 475 // Verify filter setup.
350 List<ErrorFilter> filters = 476 expect(errorFilters, isEmpty);
351 callbacks.currentContext.getConfigurationData(CONFIGURED_ERROR_FILTERS);
352 expect(filters, isEmpty);
353 } 477 }
354 478
355 test_ignoreFilesInPackagesFolder() { 479 test_ignoreFilesInPackagesFolder() {
356 // create a context with a pubspec.yaml file 480 // create a context with a pubspec.yaml file
357 String pubspecPath = posix.join(projPath, 'pubspec.yaml'); 481 String pubspecPath = posix.join(projPath, 'pubspec.yaml');
358 resourceProvider.newFile(pubspecPath, 'pubspec'); 482 resourceProvider.newFile(pubspecPath, 'pubspec');
359 // create a file in the "packages" folder 483 // create a file in the "packages" folder
360 String filePath1 = posix.join(projPath, 'packages', 'file1.dart'); 484 String filePath1 = posix.join(projPath, 'packages', 'file1.dart');
361 resourceProvider.newFile(filePath1, 'contents'); 485 resourceProvider.newFile(filePath1, 'contents');
362 // "packages" files are ignored initially 486 // "packages" files are ignored initially
(...skipping 1924 matching lines...) Expand 10 before | Expand all | Expand 10 after
2287 class TestUriResolver extends UriResolver { 2411 class TestUriResolver extends UriResolver {
2288 Map<Uri, Source> uriMap; 2412 Map<Uri, Source> uriMap;
2289 2413
2290 TestUriResolver(this.uriMap); 2414 TestUriResolver(this.uriMap);
2291 2415
2292 @override 2416 @override
2293 Source resolveAbsolute(Uri uri, [Uri actualUri]) { 2417 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
2294 return uriMap[uri]; 2418 return uriMap[uri];
2295 } 2419 }
2296 } 2420 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/lib/src/context_manager.dart ('k') | pkg/analyzer/lib/src/task/options.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698