| OLD | NEW |
| 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.analysis_server; | 5 library test.analysis_server; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/analysis_server.dart'; | 9 import 'package:analysis_server/src/analysis_server.dart'; |
| 10 import 'package:analysis_server/src/constants.dart'; | 10 import 'package:analysis_server/src/constants.dart'; |
| 11 import 'package:analysis_server/src/domain_server.dart'; | 11 import 'package:analysis_server/src/domain_server.dart'; |
| 12 import 'package:analysis_server/src/operation/operation.dart'; | 12 import 'package:analysis_server/src/operation/operation.dart'; |
| 13 import 'package:analysis_server/src/protocol.dart'; | 13 import 'package:analysis_server/src/protocol.dart'; |
| 14 import 'package:analyzer/file_system/file_system.dart'; | 14 import 'package:analyzer/file_system/file_system.dart'; |
| 15 import 'package:analyzer/file_system/memory_file_system.dart'; | 15 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 16 import 'package:analyzer/instrumentation/instrumentation.dart'; | 16 import 'package:analyzer/instrumentation/instrumentation.dart'; |
| 17 import 'package:analyzer/source/package_map_resolver.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; | 18 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/java_engine.dart'; | 19 import 'package:analyzer/src/generated/java_engine.dart'; |
| 19 import 'package:analyzer/src/generated/source.dart'; | 20 import 'package:analyzer/src/generated/source.dart'; |
| 20 import 'package:typed_mock/typed_mock.dart'; | 21 import 'package:typed_mock/typed_mock.dart'; |
| 21 import 'package:unittest/unittest.dart'; | 22 import 'package:unittest/unittest.dart'; |
| 22 | 23 |
| 23 import 'mock_sdk.dart'; | 24 import 'mock_sdk.dart'; |
| 24 import 'mocks.dart'; | 25 import 'mocks.dart'; |
| 25 import 'reflective_tests.dart'; | 26 import 'reflective_tests.dart'; |
| 26 | 27 |
| 27 main() { | 28 main() { |
| 28 groupSep = ' | '; | 29 groupSep = ' | '; |
| 29 runReflectiveTests(AnalysisServerTest); | 30 runReflectiveTests(AnalysisServerTest); |
| 30 } | 31 } |
| 31 | 32 |
| 32 @reflectiveTest | 33 @reflectiveTest |
| 33 class AnalysisServerTest { | 34 class AnalysisServerTest { |
| 34 MockServerChannel channel; | 35 MockServerChannel channel; |
| 35 AnalysisServer server; | 36 AnalysisServer server; |
| 36 MemoryResourceProvider resourceProvider; | 37 MemoryResourceProvider resourceProvider; |
| 38 MockPackageMapProvider packageMapProvider; |
| 37 | 39 |
| 38 /** | 40 /** |
| 39 * Verify that getAnalysisContextForSource returns the correct contexts even | 41 * Verify that getAnalysisContextForSource returns the correct contexts even |
| 40 * for sources that are included by multiple contexts. | 42 * for sources that are included by multiple contexts. |
| 41 * | 43 * |
| 42 * See dartbug.com/21898 | 44 * See dartbug.com/21898 |
| 43 */ | 45 */ |
| 44 Future fail_getAnalysisContextForSource_crossImports() { | 46 Future fail_getAnalysisContextForSource_crossImports() { |
| 45 // Subscribe to STATUS so we'll know when analysis is done. | 47 // Subscribe to STATUS so we'll know when analysis is done. |
| 46 server.serverServices = [ServerService.STATUS].toSet(); | 48 server.serverServices = [ServerService.STATUS].toSet(); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 expect(fooContext.getKindOf(fooSource), SourceKind.LIBRARY); | 107 expect(fooContext.getKindOf(fooSource), SourceKind.LIBRARY); |
| 106 expect(fooContext.getKindOf(barSource), SourceKind.UNKNOWN); | 108 expect(fooContext.getKindOf(barSource), SourceKind.UNKNOWN); |
| 107 expect(barContext.getKindOf(fooSource), SourceKind.UNKNOWN); | 109 expect(barContext.getKindOf(fooSource), SourceKind.UNKNOWN); |
| 108 expect(barContext.getKindOf(barSource), SourceKind.LIBRARY); | 110 expect(barContext.getKindOf(barSource), SourceKind.LIBRARY); |
| 109 }); | 111 }); |
| 110 } | 112 } |
| 111 | 113 |
| 112 void setUp() { | 114 void setUp() { |
| 113 channel = new MockServerChannel(); | 115 channel = new MockServerChannel(); |
| 114 resourceProvider = new MemoryResourceProvider(); | 116 resourceProvider = new MemoryResourceProvider(); |
| 115 server = new AnalysisServer(channel, resourceProvider, | 117 packageMapProvider = new MockPackageMapProvider(); |
| 116 new MockPackageMapProvider(), null, new AnalysisServerOptions(), | 118 server = new AnalysisServer(channel, resourceProvider, packageMapProvider, |
| 117 new MockSdk(), InstrumentationService.NULL_SERVICE, | 119 null, new AnalysisServerOptions(), new MockSdk(), |
| 118 rethrowExceptions: true); | 120 InstrumentationService.NULL_SERVICE, rethrowExceptions: true); |
| 119 } | 121 } |
| 120 | 122 |
| 121 Future test_contextDisposed() { | 123 Future test_contextDisposed() { |
| 122 resourceProvider.newFolder('/foo'); | 124 resourceProvider.newFolder('/foo'); |
| 123 resourceProvider.newFile('/foo/bar.dart', 'library lib;'); | 125 resourceProvider.newFile('/foo/bar.dart', 'library lib;'); |
| 124 server.setAnalysisRoots('0', ['/foo'], [], {}); | 126 server.setAnalysisRoots('0', ['/foo'], [], {}); |
| 125 AnalysisContext context; | 127 AnalysisContext context; |
| 126 return pumpEventQueue().then((_) { | 128 return pumpEventQueue().then((_) { |
| 127 context = server.getAnalysisContext('/foo/bar.dart'); | 129 context = server.getAnalysisContext('/foo/bar.dart'); |
| 128 server.setAnalysisRoots('1', [], [], {}); | 130 server.setAnalysisRoots('1', [], [], {}); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 184 |
| 183 Future test_echo() { | 185 Future test_echo() { |
| 184 server.handlers = [new EchoHandler()]; | 186 server.handlers = [new EchoHandler()]; |
| 185 var request = new Request('my22', 'echo'); | 187 var request = new Request('my22', 'echo'); |
| 186 return channel.sendRequest(request).then((Response response) { | 188 return channel.sendRequest(request).then((Response response) { |
| 187 expect(response.id, equals('my22')); | 189 expect(response.id, equals('my22')); |
| 188 expect(response.error, isNull); | 190 expect(response.error, isNull); |
| 189 }); | 191 }); |
| 190 } | 192 } |
| 191 | 193 |
| 192 test_getAnalysisContext_nested() { | |
| 193 String dir1Path = '/dir1'; | |
| 194 String dir2Path = dir1Path + '/dir2'; | |
| 195 String filePath = dir2Path + '/file.dart'; | |
| 196 Folder dir1 = resourceProvider.newFolder(dir1Path); | |
| 197 Folder dir2 = resourceProvider.newFolder(dir2Path); | |
| 198 resourceProvider.newFile(filePath, 'library lib;'); | |
| 199 | |
| 200 AnalysisContext context1 = AnalysisEngine.instance.createAnalysisContext(); | |
| 201 AnalysisContext context2 = AnalysisEngine.instance.createAnalysisContext(); | |
| 202 server.folderMap[dir1] = context1; | |
| 203 server.folderMap[dir2] = context2; | |
| 204 | |
| 205 expect(server.getAnalysisContext(filePath), context2); | |
| 206 } | |
| 207 | |
| 208 test_getAnalysisContext_simple() { | |
| 209 String dirPath = '/dir'; | |
| 210 String filePath = dirPath + '/file.dart'; | |
| 211 Folder dir = resourceProvider.newFolder(dirPath); | |
| 212 resourceProvider.newFile(filePath, 'library lib;'); | |
| 213 | |
| 214 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); | |
| 215 server.folderMap[dir] = context; | |
| 216 | |
| 217 expect(server.getAnalysisContext(filePath), context); | |
| 218 } | |
| 219 | |
| 220 Future test_getAnalysisContextForSource() { | 194 Future test_getAnalysisContextForSource() { |
| 221 // Subscribe to STATUS so we'll know when analysis is done. | 195 // Subscribe to STATUS so we'll know when analysis is done. |
| 222 server.serverServices = [ServerService.STATUS].toSet(); | 196 server.serverServices = [ServerService.STATUS].toSet(); |
| 223 // Analyze project foo containing foo.dart and project bar containing | 197 // Analyze project foo containing foo.dart and project bar containing |
| 224 // bar.dart. | 198 // bar.dart. |
| 225 resourceProvider.newFolder('/foo'); | 199 resourceProvider.newFolder('/foo'); |
| 226 resourceProvider.newFolder('/bar'); | 200 resourceProvider.newFolder('/bar'); |
| 227 File foo = resourceProvider.newFile('/foo/foo.dart', 'library lib;'); | 201 File foo = resourceProvider.newFile('/foo/foo.dart', 'library lib;'); |
| 228 Source fooSource = foo.createSource(); | 202 Source fooSource = foo.createSource(); |
| 229 File bar = resourceProvider.newFile('/bar/bar.dart', 'library lib;'); | 203 File bar = resourceProvider.newFile('/bar/bar.dart', 'library lib;'); |
| 230 Source barSource = bar.createSource(); | 204 Source barSource = bar.createSource(); |
| 231 server.setAnalysisRoots('0', ['/foo', '/bar'], [], {}); | 205 server.setAnalysisRoots('0', ['/foo', '/bar'], [], {}); |
| 232 return pumpEventQueue(40).then((_) { | 206 return pumpEventQueue(40).then((_) { |
| 233 expect(server.statusAnalyzing, isFalse); | 207 expect(server.statusAnalyzing, isFalse); |
| 234 // Make sure getAnalysisContext returns the proper context for each. | 208 // Make sure getAnalysisContext returns the proper context for each. |
| 235 AnalysisContext fooContext = | 209 AnalysisContext fooContext = |
| 236 server.getAnalysisContextForSource(fooSource); | 210 server.getAnalysisContextForSource(fooSource); |
| 237 expect(fooContext, isNotNull); | 211 expect(fooContext, isNotNull); |
| 238 AnalysisContext barContext = | 212 AnalysisContext barContext = |
| 239 server.getAnalysisContextForSource(barSource); | 213 server.getAnalysisContextForSource(barSource); |
| 240 expect(barContext, isNotNull); | 214 expect(barContext, isNotNull); |
| 241 expect(fooContext, isNot(same(barContext))); | 215 expect(fooContext, isNot(same(barContext))); |
| 242 expect(fooContext.getKindOf(fooSource), SourceKind.LIBRARY); | 216 expect(fooContext.getKindOf(fooSource), SourceKind.LIBRARY); |
| 243 expect(fooContext.getKindOf(barSource), SourceKind.UNKNOWN); | 217 expect(fooContext.getKindOf(barSource), SourceKind.UNKNOWN); |
| 244 expect(barContext.getKindOf(fooSource), SourceKind.UNKNOWN); | 218 expect(barContext.getKindOf(fooSource), SourceKind.UNKNOWN); |
| 245 expect(barContext.getKindOf(barSource), SourceKind.LIBRARY); | 219 expect(barContext.getKindOf(barSource), SourceKind.LIBRARY); |
| 246 }); | 220 }); |
| 247 } | 221 } |
| 248 | 222 |
| 223 test_getContextSourcePair_nested() { |
| 224 String dir1Path = '/dir1'; |
| 225 String dir2Path = dir1Path + '/dir2'; |
| 226 String filePath = dir2Path + '/file.dart'; |
| 227 Folder dir1 = resourceProvider.newFolder(dir1Path); |
| 228 Folder dir2 = resourceProvider.newFolder(dir2Path); |
| 229 resourceProvider.newFile(filePath, 'library lib;'); |
| 230 |
| 231 AnalysisContext context1 = AnalysisEngine.instance.createAnalysisContext(); |
| 232 AnalysisContext context2 = AnalysisEngine.instance.createAnalysisContext(); |
| 233 _configureSourceFactory(context1); |
| 234 _configureSourceFactory(context2); |
| 235 server.folderMap[dir1] = context1; |
| 236 server.folderMap[dir2] = context2; |
| 237 |
| 238 ContextSourcePair pair = server.getContextSourcePair(filePath); |
| 239 Source source = pair.source; |
| 240 expect(pair.context, same(context2)); |
| 241 expect(source, isNotNull); |
| 242 expect(source.uri.scheme, 'file'); |
| 243 expect(source.fullName, filePath); |
| 244 } |
| 245 |
| 246 test_getContextSourcePair_package_inRoot() { |
| 247 String rootPath = '/my_package'; |
| 248 String filePath = rootPath + '/lib/file.dart'; |
| 249 Folder rootFolder = resourceProvider.newFolder(rootPath); |
| 250 resourceProvider.newFile(filePath, 'library lib;'); |
| 251 |
| 252 packageMapProvider.packageMap = <String, List<Folder>>{ |
| 253 'my_package': <Folder>[rootFolder] |
| 254 }; |
| 255 |
| 256 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); |
| 257 _configureSourceFactory(context); |
| 258 server.folderMap[rootFolder] = context; |
| 259 |
| 260 ContextSourcePair pair = server.getContextSourcePair(filePath); |
| 261 Source source = pair.source; |
| 262 expect(pair.context, same(context)); |
| 263 expect(source, isNotNull); |
| 264 expect(source.uri.scheme, 'package'); |
| 265 expect(source.fullName, filePath); |
| 266 } |
| 267 |
| 268 test_getContextSourcePair_simple() { |
| 269 String dirPath = '/dir'; |
| 270 String filePath = dirPath + '/file.dart'; |
| 271 Folder dir = resourceProvider.newFolder(dirPath); |
| 272 resourceProvider.newFile(filePath, 'library lib;'); |
| 273 |
| 274 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); |
| 275 _configureSourceFactory(context); |
| 276 server.folderMap[dir] = context; |
| 277 |
| 278 ContextSourcePair pair = server.getContextSourcePair(filePath); |
| 279 Source source = pair.source; |
| 280 expect(pair.context, same(context)); |
| 281 expect(source, isNotNull); |
| 282 expect(source.uri.scheme, 'file'); |
| 283 expect(source.fullName, filePath); |
| 284 } |
| 285 |
| 249 /** | 286 /** |
| 250 * Test that having multiple analysis contexts analyze the same file doesn't | 287 * Test that having multiple analysis contexts analyze the same file doesn't |
| 251 * cause that file to receive duplicate notifications when it's modified. | 288 * cause that file to receive duplicate notifications when it's modified. |
| 252 */ | 289 */ |
| 253 Future test_no_duplicate_notifications() async { | 290 Future test_no_duplicate_notifications() async { |
| 254 // Subscribe to STATUS so we'll know when analysis is done. | 291 // Subscribe to STATUS so we'll know when analysis is done. |
| 255 server.serverServices = [ServerService.STATUS].toSet(); | 292 server.serverServices = [ServerService.STATUS].toSet(); |
| 256 resourceProvider.newFolder('/foo'); | 293 resourceProvider.newFolder('/foo'); |
| 257 resourceProvider.newFolder('/bar'); | 294 resourceProvider.newFolder('/bar'); |
| 258 resourceProvider.newFile('/foo/foo.dart', 'import "../bar/bar.dart";'); | 295 resourceProvider.newFile('/foo/foo.dart', 'import "../bar/bar.dart";'); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 } | 443 } |
| 407 | 444 |
| 408 Future test_unknownRequest() { | 445 Future test_unknownRequest() { |
| 409 server.handlers = [new EchoHandler()]; | 446 server.handlers = [new EchoHandler()]; |
| 410 var request = new Request('my22', 'randomRequest'); | 447 var request = new Request('my22', 'randomRequest'); |
| 411 return channel.sendRequest(request).then((Response response) { | 448 return channel.sendRequest(request).then((Response response) { |
| 412 expect(response.id, equals('my22')); | 449 expect(response.id, equals('my22')); |
| 413 expect(response.error, isNotNull); | 450 expect(response.error, isNotNull); |
| 414 }); | 451 }); |
| 415 } | 452 } |
| 453 |
| 454 void _configureSourceFactory(AnalysisContext context) { |
| 455 var resourceUriResolver = new ResourceUriResolver(resourceProvider); |
| 456 var packageUriResolver = new PackageMapUriResolver( |
| 457 resourceProvider, packageMapProvider.packageMap); |
| 458 context.sourceFactory = |
| 459 new SourceFactory([packageUriResolver, resourceUriResolver]); |
| 460 } |
| 416 } | 461 } |
| 417 | 462 |
| 418 class EchoHandler implements RequestHandler { | 463 class EchoHandler implements RequestHandler { |
| 419 @override | 464 @override |
| 420 Response handleRequest(Request request) { | 465 Response handleRequest(Request request) { |
| 421 if (request.method == 'echo') { | 466 if (request.method == 'echo') { |
| 422 return new Response(request.id, result: {'echo': true}); | 467 return new Response(request.id, result: {'echo': true}); |
| 423 } | 468 } |
| 424 return null; | 469 return null; |
| 425 } | 470 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 436 _MockServerOperation(this.context); | 481 _MockServerOperation(this.context); |
| 437 | 482 |
| 438 @override | 483 @override |
| 439 ServerOperationPriority get priority => ServerOperationPriority.ANALYSIS; | 484 ServerOperationPriority get priority => ServerOperationPriority.ANALYSIS; |
| 440 | 485 |
| 441 @override | 486 @override |
| 442 void perform(AnalysisServer server) { | 487 void perform(AnalysisServer server) { |
| 443 isComplete = true; | 488 isComplete = true; |
| 444 } | 489 } |
| 445 } | 490 } |
| OLD | NEW |