OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'package:analyzer/dart/ast/ast.dart'; |
| 6 import 'package:analyzer/dart/ast/token.dart'; |
| 7 import 'package:analyzer/error/listener.dart'; |
| 8 import 'package:analyzer/file_system/file_system.dart'; |
| 9 import 'package:analyzer/src/dart/scanner/reader.dart'; |
| 10 import 'package:analyzer/src/dart/scanner/scanner.dart'; |
| 11 import 'package:analyzer/src/generated/engine.dart'; |
| 12 import 'package:analyzer/src/generated/parser.dart'; |
| 13 import 'package:analyzer/src/generated/source.dart'; |
| 14 import 'package:analyzer/src/summary/bazel_summary.dart'; |
| 15 import 'package:analyzer/src/summary/format.dart'; |
| 16 import 'package:analyzer/src/summary/summarize_ast.dart'; |
| 17 import 'package:analyzer/src/summary/summarize_elements.dart'; |
| 18 import 'package:analyzer/src/util/fast_uri.dart'; |
| 19 import 'package:path/path.dart' as pathos; |
| 20 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 21 import 'package:unittest/unittest.dart'; |
| 22 |
| 23 import '../../utils.dart'; |
| 24 import '../context/abstract_context.dart'; |
| 25 |
| 26 main() { |
| 27 initializeTestEnvironment(); |
| 28 defineReflectiveTests(SummaryProviderTest); |
| 29 } |
| 30 |
| 31 const OUT_ROOT = '$SRC_ROOT/bazel-bin'; |
| 32 const SRC_ROOT = '/company/src/user/project/root'; |
| 33 |
| 34 @reflectiveTest |
| 35 class SummaryProviderTest extends AbstractContextTest { |
| 36 SummaryProvider manager; |
| 37 |
| 38 @override |
| 39 void setUp() { |
| 40 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, _getOutputPath, context); |
| 50 } |
| 51 |
| 52 test_getPackageForUri() { |
| 53 String pathA = '$SRC_ROOT/components/aaa/lib'; |
| 54 resourceProvider.newFile( |
| 55 '$pathA/a1.dart', |
| 56 r''' |
| 57 class A1 {} |
| 58 '''); |
| 59 resourceProvider.newFile( |
| 60 '$pathA/a2.dart', |
| 61 r''' |
| 62 class A2 {} |
| 63 '''); |
| 64 _writeUnlinkedBundle('components.aaa'); |
| 65 // Ask the package for the URI. |
| 66 Source source1 = _resolveUri('package:components.aaa/a1.dart'); |
| 67 Source source2 = _resolveUri('package:components.aaa/a2.dart'); |
| 68 Package package = manager.getPackageForUri(source1.uri); |
| 69 expect(package, isNotNull); |
| 70 // The same instance is returned to another URI in the same package. |
| 71 expect(manager.getPackageForUri(source2.uri), same(package)); |
| 72 } |
| 73 |
| 74 test_getPackageForUri_inconsistent() { |
| 75 String pathA = '$SRC_ROOT/components/aaa/lib'; |
| 76 File fileA1 = resourceProvider.newFile( |
| 77 '$pathA/a1.dart', |
| 78 r''' |
| 79 class A1 {} |
| 80 '''); |
| 81 resourceProvider.newFile( |
| 82 '$pathA/a2.dart', |
| 83 r''' |
| 84 class A2 {} |
| 85 '''); |
| 86 _writeUnlinkedBundle('components.aaa'); |
| 87 // Update one of the files file, so the bundle is not consistent. |
| 88 fileA1.writeAsStringSync('// different'); |
| 89 Source source1 = _resolveUri('package:components.aaa/a1.dart'); |
| 90 Source source2 = _resolveUri('package:components.aaa/a2.dart'); |
| 91 expect(manager.getPackageForUri(source1.uri), isNull); |
| 92 expect(manager.getPackageForUri(source2.uri), isNull); |
| 93 } |
| 94 |
| 95 Source _resolveUri(String uri) { |
| 96 return context.sourceFactory.resolveUri(null, uri); |
| 97 } |
| 98 |
| 99 void _writeUnlinkedBundle(String packageName) { |
| 100 String packagePath = packageName.replaceAll('.', '/'); |
| 101 var unlinkedBundle = _computeUnlinkedBundle( |
| 102 resourceProvider, |
| 103 packageName, |
| 104 resourceProvider.getFolder(SRC_ROOT + '/' + packagePath + '/lib'), |
| 105 true); |
| 106 String shortName = packageName.substring(packageName.lastIndexOf('.') + 1); |
| 107 resourceProvider.newFileWithBytes( |
| 108 '$OUT_ROOT/$packagePath/$shortName.full.ds', unlinkedBundle.toBuffer()); |
| 109 } |
| 110 |
| 111 static PackageBundleBuilder _computeUnlinkedBundle(ResourceProvider provider, |
| 112 String packageName, Folder libFolder, bool strong) { |
| 113 var pathContext = provider.pathContext; |
| 114 String libPath = libFolder.path + pathContext.separator; |
| 115 PackageBundleAssembler assembler = new PackageBundleAssembler(); |
| 116 |
| 117 /** |
| 118 * Return the `package` [Uri] for the given [path] in the `lib` folder |
| 119 * of the current package. |
| 120 */ |
| 121 Uri getUri(String path) { |
| 122 String pathInLib = path.substring(libPath.length); |
| 123 String uriPath = pathos.posix.joinAll(pathContext.split(pathInLib)); |
| 124 String uriStr = 'package:$packageName/$uriPath'; |
| 125 return FastUri.parse(uriStr); |
| 126 } |
| 127 |
| 128 /** |
| 129 * If the given [file] is a Dart file, add its unlinked unit. |
| 130 */ |
| 131 void addDartFile(File file) { |
| 132 String path = file.path; |
| 133 if (AnalysisEngine.isDartFileName(path)) { |
| 134 Uri uri = getUri(path); |
| 135 Source source = file.createSource(uri); |
| 136 CompilationUnit unit = _parse(source, strong); |
| 137 UnlinkedUnitBuilder unlinkedUnit = serializeAstUnlinked(unit); |
| 138 assembler.addUnlinkedUnit(source, unlinkedUnit); |
| 139 } |
| 140 } |
| 141 |
| 142 /** |
| 143 * Visit the [folder] recursively. |
| 144 */ |
| 145 void addDartFiles(Folder folder) { |
| 146 List<Resource> children = folder.getChildren(); |
| 147 for (Resource child in children) { |
| 148 if (child is File) { |
| 149 addDartFile(child); |
| 150 } |
| 151 } |
| 152 for (Resource child in children) { |
| 153 if (child is Folder) { |
| 154 addDartFiles(child); |
| 155 } |
| 156 } |
| 157 } |
| 158 |
| 159 addDartFiles(libFolder); |
| 160 return assembler.assemble(); |
| 161 } |
| 162 |
| 163 static String _getOutputPath(ResourceProvider provider, Uri uri) { |
| 164 if (uri.scheme == 'package') { |
| 165 List<String> segments = uri.pathSegments; |
| 166 if (segments.isNotEmpty) { |
| 167 String packageName = segments.first; |
| 168 return OUT_ROOT + '/' + packageName.replaceAll('.', '/'); |
| 169 } |
| 170 } |
| 171 return null; |
| 172 } |
| 173 |
| 174 /** |
| 175 * Parse the given [source] into AST. |
| 176 */ |
| 177 static CompilationUnit _parse(Source source, bool strong) { |
| 178 String code = source.contents.data; |
| 179 AnalysisErrorListener errorListener = AnalysisErrorListener.NULL_LISTENER; |
| 180 CharSequenceReader reader = new CharSequenceReader(code); |
| 181 Scanner scanner = new Scanner(source, reader, errorListener); |
| 182 scanner.scanGenericMethodComments = strong; |
| 183 Token token = scanner.tokenize(); |
| 184 LineInfo lineInfo = new LineInfo(scanner.lineStarts); |
| 185 Parser parser = new Parser(source, errorListener); |
| 186 parser.parseGenericMethodComments = strong; |
| 187 CompilationUnit unit = parser.parseCompilationUnit(token); |
| 188 unit.lineInfo = lineInfo; |
| 189 return unit; |
| 190 } |
| 191 } |
| 192 |
| 193 class _TestPackageResolver implements UriResolver { |
| 194 final ResourceProvider resourceProvider; |
| 195 |
| 196 _TestPackageResolver(this.resourceProvider); |
| 197 |
| 198 @override |
| 199 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 200 if (uri.scheme == 'package') { |
| 201 List<String> segments = uri.pathSegments; |
| 202 if (segments.isNotEmpty) { |
| 203 pathos.Context pathContext = resourceProvider.pathContext; |
| 204 String packageName = segments.first; |
| 205 String folderPath = pathContext.join( |
| 206 SRC_ROOT, packageName.replaceAll('.', pathContext.separator)); |
| 207 String path = pathContext.join( |
| 208 folderPath, 'lib', pathContext.joinAll(segments.skip(1))); |
| 209 return resourceProvider.getFile(path).createSource(uri); |
| 210 } |
| 211 } |
| 212 return null; |
| 213 } |
| 214 |
| 215 @override |
| 216 Uri restoreAbsolute(Source source) { |
| 217 throw new UnimplementedError(); |
| 218 } |
| 219 } |
OLD | NEW |