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.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'; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 136 |
137 UriResolver providePackageResolver(Folder folder) { | 137 UriResolver providePackageResolver(Folder folder) { |
138 return packageResolver; | 138 return packageResolver; |
139 } | 139 } |
140 | 140 |
141 void setUp() { | 141 void setUp() { |
142 resourceProvider = new MemoryResourceProvider(); | 142 resourceProvider = new MemoryResourceProvider(); |
143 packageMapProvider = new MockPackageMapProvider(); | 143 packageMapProvider = new MockPackageMapProvider(); |
144 manager = new ContextManagerImpl(resourceProvider, providePackageResolver, | 144 manager = new ContextManagerImpl(resourceProvider, providePackageResolver, |
145 packageMapProvider, InstrumentationService.NULL_SERVICE); | 145 packageMapProvider, InstrumentationService.NULL_SERVICE); |
146 callbacks = new TestContextManagerCallbacks(); | 146 callbacks = new TestContextManagerCallbacks(resourceProvider); |
147 manager.callbacks = callbacks; | 147 manager.callbacks = callbacks; |
148 resourceProvider.newFolder(projPath); | 148 resourceProvider.newFolder(projPath); |
149 ContextManagerImpl.ENABLE_PACKAGESPEC_SUPPORT = true; | 149 ContextManagerImpl.ENABLE_PACKAGESPEC_SUPPORT = true; |
150 } | 150 } |
151 | 151 |
152 void tearDown() { | 152 void tearDown() { |
153 ContextManagerImpl.ENABLE_PACKAGESPEC_SUPPORT = false; | 153 ContextManagerImpl.ENABLE_PACKAGESPEC_SUPPORT = false; |
154 } | 154 } |
155 | 155 |
156 void test_contextsInAnalysisRoot_nestedContext() { | 156 void test_contextsInAnalysisRoot_nestedContext() { |
(...skipping 1264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1421 String filePath = '$projPath/bin/main.dart'; | 1421 String filePath = '$projPath/bin/main.dart'; |
1422 | 1422 |
1423 resourceProvider.newFile(packagesPath, ''); | 1423 resourceProvider.newFile(packagesPath, ''); |
1424 resourceProvider.newFile(filePath, 'library main;'); | 1424 resourceProvider.newFile(filePath, 'library main;'); |
1425 | 1425 |
1426 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | 1426 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); |
1427 | 1427 |
1428 Map<String, int> filePaths = callbacks.currentContextFilePaths[projPath]; | 1428 Map<String, int> filePaths = callbacks.currentContextFilePaths[projPath]; |
1429 expect(filePaths, hasLength(1)); | 1429 expect(filePaths, hasLength(1)); |
1430 expect(filePaths, contains(filePath)); | 1430 expect(filePaths, contains(filePath)); |
1431 Packages packages = callbacks.currentContextPackagespecs[projPath]; | 1431 Packages packages = callbacks.currentContextDispositions[projPath].packages; |
1432 expect(packages.packages, isEmpty); | 1432 expect(packages.packages, isEmpty); |
1433 | 1433 |
1434 // update .packages | 1434 // update .packages |
1435 callbacks.now++; | 1435 callbacks.now++; |
1436 resourceProvider.modifyFile(packagesPath, 'main:./lib/'); | 1436 resourceProvider.modifyFile(packagesPath, 'main:./lib/'); |
1437 return pumpEventQueue().then((_) { | 1437 return pumpEventQueue().then((_) { |
1438 // verify new package info | 1438 // verify new package info |
1439 packages = callbacks.currentContextPackagespecs[projPath]; | 1439 packages = callbacks.currentContextDispositions[projPath].packages; |
1440 expect(packages.packages, unorderedEquals(['main'])); | 1440 expect(packages.packages, unorderedEquals(['main'])); |
1441 }); | 1441 }); |
1442 } | 1442 } |
1443 | 1443 |
1444 /** | 1444 /** |
1445 * Verify that package URI's for source files in [path] will be resolved | 1445 * Verify that package URI's for source files in [path] will be resolved |
1446 * using a package map matching [expectation]. | 1446 * using a package map matching [expectation]. |
1447 */ | 1447 */ |
1448 void _checkPackageMap(String path, expectation) { | 1448 void _checkPackageMap(String path, expectation) { |
1449 UriResolver resolver = callbacks.currentContextPackageUriResolvers[path]; | 1449 FolderDisposition disposition = callbacks.currentContextDispositions[path]; |
1450 Map<String, List<Folder>> packageMap = | 1450 Map<String, List<Folder>> packageMap = |
1451 resolver is PackageMapUriResolver ? resolver.packageMap : null; | 1451 disposition is PackageMapDisposition ? disposition.packageMap : null; |
1452 expect(packageMap, expectation); | 1452 expect(packageMap, expectation); |
1453 } | 1453 } |
1454 | 1454 |
1455 /** | 1455 /** |
1456 * Verify that package URI's for source files in [path] will be resolved | 1456 * Verify that package URI's for source files in [path] will be resolved |
1457 * using a package root maching [expectation]. | 1457 * using a package root maching [expectation]. |
1458 */ | 1458 */ |
1459 void _checkPackageRoot(String path, expectation) { | 1459 void _checkPackageRoot(String path, expectation) { |
1460 UriResolver resolver = callbacks.currentContextPackageUriResolvers[path]; | 1460 // TODO(paulberry): this code needs to be reworked, since the context |
1461 expect(resolver, new isInstanceOf<PackageUriResolver>()); | 1461 // manager inspects the contents of the package root directory and converts |
1462 PackageUriResolver packageUriResolver = resolver; | 1462 // it to a package map. The only reason this code used to work was due to |
1463 expect(packageUriResolver.packagesDirectory_forTesting, expectation); | 1463 // dartbug.com/23909. |
| 1464 fail('Cannot verify package root'); |
1464 } | 1465 } |
1465 } | 1466 } |
1466 | 1467 |
1467 class TestContextManagerCallbacks extends ContextManagerCallbacks { | 1468 class TestContextManagerCallbacks extends ContextManagerCallbacks { |
1468 /** | 1469 /** |
1469 * Source of timestamps stored in [currentContextFilePaths]. | 1470 * Source of timestamps stored in [currentContextFilePaths]. |
1470 */ | 1471 */ |
1471 int now = 0; | 1472 int now = 0; |
1472 | 1473 |
1473 /** | 1474 /** |
(...skipping 13 matching lines...) Expand all Loading... |
1487 <String, Map<String, int>>{}; | 1488 <String, Map<String, int>>{}; |
1488 | 1489 |
1489 /** | 1490 /** |
1490 * A map from the paths of contexts to a set of the sources that should be | 1491 * A map from the paths of contexts to a set of the sources that should be |
1491 * explicitly analyzed in those contexts. | 1492 * explicitly analyzed in those contexts. |
1492 */ | 1493 */ |
1493 final Map<String, Set<Source>> currentContextSources = <String, Set<Source>>{ | 1494 final Map<String, Set<Source>> currentContextSources = <String, Set<Source>>{ |
1494 }; | 1495 }; |
1495 | 1496 |
1496 /** | 1497 /** |
1497 * Map from context to package URI resolver. | 1498 * Map from context to folder disposition. |
1498 */ | 1499 */ |
1499 final Map<String, UriResolver> currentContextPackageUriResolvers = | 1500 final Map<String, FolderDisposition> currentContextDispositions = |
1500 <String, UriResolver>{}; | 1501 <String, FolderDisposition>{}; |
1501 | 1502 |
1502 /** | 1503 /** |
1503 * Map from context to packages object. | 1504 * Resource provider used for this test. |
1504 */ | 1505 */ |
1505 final Map<String, Packages> currentContextPackagespecs = <String, Packages>{}; | 1506 final ResourceProvider resourceProvider; |
| 1507 |
| 1508 TestContextManagerCallbacks(this.resourceProvider); |
1506 | 1509 |
1507 /** | 1510 /** |
1508 * Iterable of the paths to contexts that currently exist. | 1511 * Iterable of the paths to contexts that currently exist. |
1509 */ | 1512 */ |
1510 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; | 1513 Iterable<String> get currentContextPaths => currentContextTimestamps.keys; |
1511 | 1514 |
1512 @override | 1515 @override |
1513 AnalysisContext addContext( | 1516 AnalysisContext addContext(Folder folder, FolderDisposition disposition) { |
1514 Folder folder, UriResolver packageUriResolver, Packages packages) { | |
1515 String path = folder.path; | 1517 String path = folder.path; |
1516 expect(currentContextPaths, isNot(contains(path))); | 1518 expect(currentContextPaths, isNot(contains(path))); |
1517 currentContextTimestamps[path] = now; | 1519 currentContextTimestamps[path] = now; |
1518 currentContextFilePaths[path] = <String, int>{}; | 1520 currentContextFilePaths[path] = <String, int>{}; |
1519 currentContextSources[path] = new HashSet<Source>(); | 1521 currentContextSources[path] = new HashSet<Source>(); |
1520 currentContextPackageUriResolvers[path] = packageUriResolver; | 1522 currentContextDispositions[path] = disposition; |
1521 currentContextPackagespecs[path] = packages; | |
1522 currentContext = AnalysisEngine.instance.createAnalysisContext(); | 1523 currentContext = AnalysisEngine.instance.createAnalysisContext(); |
1523 List<UriResolver> resolvers = [new FileUriResolver()]; | 1524 List<UriResolver> resolvers = [new FileUriResolver()]; |
1524 if (packageUriResolver != null) { | 1525 resolvers.addAll(disposition.createPackageUriResolvers(resourceProvider)); |
1525 resolvers.add(packageUriResolver); | 1526 currentContext.sourceFactory = |
1526 } | 1527 new SourceFactory(resolvers, disposition.packages); |
1527 currentContext.sourceFactory = new SourceFactory(resolvers, packages); | |
1528 return currentContext; | 1528 return currentContext; |
1529 } | 1529 } |
1530 | 1530 |
1531 @override | 1531 @override |
1532 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { | 1532 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet) { |
1533 Map<String, int> filePaths = currentContextFilePaths[contextFolder.path]; | 1533 Map<String, int> filePaths = currentContextFilePaths[contextFolder.path]; |
1534 Set<Source> sources = currentContextSources[contextFolder.path]; | 1534 Set<Source> sources = currentContextSources[contextFolder.path]; |
1535 | 1535 |
1536 for (Source source in changeSet.addedSources) { | 1536 for (Source source in changeSet.addedSources) { |
1537 expect(filePaths, isNot(contains(source.fullName))); | 1537 expect(filePaths, isNot(contains(source.fullName))); |
(...skipping 22 matching lines...) Expand all Loading... |
1560 expect(currentContextPaths, unorderedEquals(expected)); | 1560 expect(currentContextPaths, unorderedEquals(expected)); |
1561 } | 1561 } |
1562 | 1562 |
1563 @override | 1563 @override |
1564 void removeContext(Folder folder, List<String> flushedFiles) { | 1564 void removeContext(Folder folder, List<String> flushedFiles) { |
1565 String path = folder.path; | 1565 String path = folder.path; |
1566 expect(currentContextPaths, contains(path)); | 1566 expect(currentContextPaths, contains(path)); |
1567 currentContextTimestamps.remove(path); | 1567 currentContextTimestamps.remove(path); |
1568 currentContextFilePaths.remove(path); | 1568 currentContextFilePaths.remove(path); |
1569 currentContextSources.remove(path); | 1569 currentContextSources.remove(path); |
1570 currentContextPackageUriResolvers.remove(path); | 1570 currentContextDispositions.remove(path); |
1571 } | 1571 } |
1572 | 1572 |
1573 @override | 1573 @override |
1574 bool shouldFileBeAnalyzed(File file) { | 1574 bool shouldFileBeAnalyzed(File file) { |
1575 if (!(AnalysisEngine.isDartFileName(file.path) || | 1575 if (!(AnalysisEngine.isDartFileName(file.path) || |
1576 AnalysisEngine.isHtmlFileName(file.path))) { | 1576 AnalysisEngine.isHtmlFileName(file.path))) { |
1577 return false; | 1577 return false; |
1578 } | 1578 } |
1579 // Emacs creates dummy links to track the fact that a file is open for | 1579 // Emacs creates dummy links to track the fact that a file is open for |
1580 // editing and has unsaved changes (e.g. having unsaved changes to | 1580 // editing and has unsaved changes (e.g. having unsaved changes to |
1581 // 'foo.dart' causes a link '.#foo.dart' to be created, which points to the | 1581 // 'foo.dart' causes a link '.#foo.dart' to be created, which points to the |
1582 // non-existent file 'username@hostname.pid'. To avoid these dummy links | 1582 // non-existent file 'username@hostname.pid'. To avoid these dummy links |
1583 // causing the analyzer to thrash, just ignore links to non-existent files. | 1583 // causing the analyzer to thrash, just ignore links to non-existent files. |
1584 return file.exists; | 1584 return file.exists; |
1585 } | 1585 } |
1586 | 1586 |
1587 @override | 1587 @override |
1588 void updateContextPackageUriResolver( | 1588 void updateContextPackageUriResolver( |
1589 Folder contextFolder, UriResolver packageUriResolver, Packages packages) { | 1589 Folder contextFolder, FolderDisposition disposition) { |
1590 currentContextPackageUriResolvers[contextFolder.path] = packageUriResolver; | 1590 currentContextDispositions[contextFolder.path] = disposition; |
1591 currentContextPackagespecs[contextFolder.path] = packages; | |
1592 } | 1591 } |
1593 } | 1592 } |
1594 | 1593 |
1595 /** | 1594 /** |
1596 * A [Source] that knows it's [fullName]. | 1595 * A [Source] that knows it's [fullName]. |
1597 */ | 1596 */ |
1598 class TestSource implements Source { | 1597 class TestSource implements Source { |
1599 TestSource(); | 1598 TestSource(); |
1600 | 1599 |
1601 @override | 1600 @override |
1602 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | 1601 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
1603 } | 1602 } |
1604 | 1603 |
1605 class TestUriResolver extends UriResolver { | 1604 class TestUriResolver extends UriResolver { |
1606 Map<Uri, Source> uriMap; | 1605 Map<Uri, Source> uriMap; |
1607 | 1606 |
1608 TestUriResolver(this.uriMap); | 1607 TestUriResolver(this.uriMap); |
1609 | 1608 |
1610 @override | 1609 @override |
1611 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | 1610 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
1612 return uriMap[uri]; | 1611 return uriMap[uri]; |
1613 } | 1612 } |
1614 } | 1613 } |
OLD | NEW |