OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
Brian Wilkerson
2016/04/25 15:31:06
"2014" --> "2016"
| |
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 library test.context.directory.manager.single; | |
6 | |
7 import 'dart:core' hide Resource; | |
8 | |
9 import 'package:analysis_server/src/single_context_manager.dart'; | |
10 import 'package:analyzer/file_system/file_system.dart'; | |
11 import 'package:analyzer/file_system/memory_file_system.dart'; | |
12 import 'package:analyzer/src/generated/engine.dart'; | |
13 import 'package:analyzer/src/generated/java_io.dart'; | |
14 import 'package:analyzer/src/generated/sdk.dart'; | |
15 import 'package:analyzer/src/generated/source.dart'; | |
16 import 'package:analyzer/src/generated/source_io.dart'; | |
17 import 'package:analyzer/src/util/glob.dart'; | |
18 import 'package:linter/src/plugin/linter_plugin.dart'; | |
19 import 'package:path/path.dart'; | |
20 import 'package:plugin/manager.dart'; | |
21 import 'package:plugin/plugin.dart'; | |
22 import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
23 import 'package:unittest/unittest.dart'; | |
24 | |
25 import 'context_manager_test.dart' show TestContextManagerCallbacks; | |
26 import 'mock_sdk.dart'; | |
27 import 'utils.dart'; | |
28 | |
29 main() { | |
30 initializeTestEnvironment(); | |
31 defineReflectiveTests(SingleContextManagerTest); | |
32 } | |
33 | |
34 @reflectiveTest | |
35 class SingleContextManagerTest { | |
36 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); | |
37 | |
38 TestUriResolver packageResolver; | |
39 TestContextManagerCallbacks callbacks; | |
40 SingleContextManager manager; | |
41 | |
42 /** | |
43 * TODO(scheglov) rename after copying tests! | |
44 */ | |
45 String projPath = '/my/project'; | |
46 Folder rootFolder; | |
47 | |
48 List<Glob> get analysisFilesGlobs { | |
49 List<String> patterns = <String>[ | |
50 '**/*.${AnalysisEngine.SUFFIX_DART}', | |
51 '**/*.${AnalysisEngine.SUFFIX_HTML}', | |
52 ]; | |
53 return patterns | |
54 .map((pattern) => new Glob(JavaFile.pathContext.separator, pattern)) | |
55 .toList(); | |
56 } | |
57 | |
58 String newFile(List<String> pathComponents, [String content = '']) { | |
59 String filePath = posix.joinAll(pathComponents); | |
60 resourceProvider.newFile(filePath, content); | |
61 return filePath; | |
62 } | |
63 | |
64 String newFolder(List<String> pathComponents) { | |
65 String folderPath = posix.joinAll(pathComponents); | |
66 resourceProvider.newFolder(folderPath); | |
67 return folderPath; | |
68 } | |
69 | |
70 void setUp() { | |
71 rootFolder = resourceProvider.newFolder(projPath); | |
72 packageResolver = new TestUriResolver(resourceProvider, rootFolder); | |
73 | |
74 _processRequiredPlugins(); | |
75 DartSdkManager sdkManager = new DartSdkManager((_) { | |
76 return new MockSdk(); | |
77 }); | |
78 manager = new SingleContextManager(resourceProvider, sdkManager, | |
79 _providePackageResolver, analysisFilesGlobs); | |
80 callbacks = new TestContextManagerCallbacks(resourceProvider); | |
81 manager.callbacks = callbacks; | |
82 resourceProvider.newFolder(projPath); | |
83 } | |
84 | |
85 void test_setRoots_addFolderWithDartFile() { | |
86 String filePath = posix.join(projPath, 'lib', 'foo.dart'); | |
87 resourceProvider.newFile(filePath, 'contents'); | |
88 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | |
89 // verify | |
90 Map<String, int> filePaths = callbacks.currentContextFilePaths[projPath]; | |
91 expect(filePaths, hasLength(1)); | |
92 expect(filePaths, contains(filePath)); | |
93 // There is an analysis context. | |
94 List<AnalysisContext> contextsInAnalysisRoot = | |
95 manager.contextsInAnalysisRoot(rootFolder); | |
96 expect(contextsInAnalysisRoot, hasLength(1)); | |
97 AnalysisContext context = contextsInAnalysisRoot[0]; | |
98 expect(context, isNotNull); | |
99 // Files in lib/ have package: URIs. | |
100 Source result = context.sourceFactory.forUri('package:foo/foo.dart'); | |
101 expect(result, isNotNull); | |
102 expect(result.exists(), isTrue); | |
103 } | |
104 | |
105 void test_setRoots_addFolderWithDartFileInSubfolder() { | |
106 String filePath = posix.join(projPath, 'foo', 'bar.dart'); | |
107 resourceProvider.newFile(filePath, 'contents'); | |
108 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | |
109 // verify | |
110 Map<String, int> filePaths = callbacks.currentContextFilePaths[projPath]; | |
111 expect(filePaths, hasLength(1)); | |
112 expect(filePaths, contains(filePath)); | |
113 } | |
114 | |
115 void test_setRoots_addFolderWithDummyLink() { | |
116 String filePath = posix.join(projPath, 'foo.dart'); | |
117 resourceProvider.newDummyLink(filePath); | |
118 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | |
119 // verify | |
120 Map<String, int> filePaths = callbacks.currentContextFilePaths[projPath]; | |
121 expect(filePaths, isEmpty); | |
122 } | |
123 | |
124 void test_setRoots_addFolderWithNestedPackageSpec() { | |
125 newFile([projPath, 'aaa', 'pubspec.yaml']); | |
126 newFile([projPath, 'bbb', 'pubspec.yaml']); | |
127 manager.setRoots(<String>[projPath], <String>[], <String, String>{}); | |
128 // We don't care about pubspec.yaml files - still just one context. | |
129 callbacks.assertContextPaths([projPath]); | |
130 expect(manager.contextsInAnalysisRoot(rootFolder), hasLength(1)); | |
131 } | |
132 | |
133 void test_setRoots_exclude_newRoot_withExcludedFile() { | |
134 String project = '/project'; | |
135 String file1 = '$project/file1.dart'; | |
136 String file2 = '$project/file2.dart'; | |
137 // create files | |
138 resourceProvider.newFile(file1, '// 1'); | |
139 resourceProvider.newFile(file2, '// 2'); | |
140 // set roots | |
141 manager.setRoots(<String>[project], <String>[file1], <String, String>{}); | |
142 callbacks.assertContextPaths([project]); | |
143 callbacks.assertContextFiles(project, [file2]); | |
144 } | |
145 | |
146 void test_setRoots_exclude_newRoot_withExcludedFolder() { | |
147 String project = '/project'; | |
148 String folderA = '$project/aaa'; | |
149 String folderB = '$project/bbb'; | |
150 String fileA = '$folderA/a.dart'; | |
151 String fileB = '$folderB/b.dart'; | |
152 // create files | |
153 resourceProvider.newFile(fileA, 'library a;'); | |
154 resourceProvider.newFile(fileB, 'library b;'); | |
155 // set roots | |
156 manager.setRoots(<String>[project], <String>[folderB], <String, String>{}); | |
157 callbacks.assertContextPaths([project]); | |
158 callbacks.assertContextFiles(project, [fileA]); | |
159 } | |
160 | |
161 void test_setRoots_exclude_sameRoot_addExcludedFile() { | |
162 String project = '/project'; | |
163 String file1 = '$project/file1.dart'; | |
164 String file2 = '$project/file2.dart'; | |
165 // create files | |
166 resourceProvider.newFile(file1, '// 1'); | |
167 resourceProvider.newFile(file2, '// 2'); | |
168 // set roots | |
169 manager.setRoots(<String>[project], <String>[], <String, String>{}); | |
170 callbacks.assertContextPaths([project]); | |
171 callbacks.assertContextFiles(project, [file1, file2]); | |
172 // exclude "2" | |
173 manager.setRoots(<String>[project], <String>[file2], <String, String>{}); | |
174 callbacks.assertContextPaths([project]); | |
175 callbacks.assertContextFiles(project, [file1]); | |
176 } | |
177 | |
178 void test_setRoots_exclude_sameRoot_addExcludedFolder() { | |
179 String project = '/project'; | |
180 String folderA = '$project/aaa'; | |
181 String folderB = '$project/bbb'; | |
182 String fileA = '$folderA/a.dart'; | |
183 String fileB = '$folderB/b.dart'; | |
184 // create files | |
185 resourceProvider.newFile(fileA, 'library a;'); | |
186 resourceProvider.newFile(fileB, 'library b;'); | |
187 // initially both "aaa/a" and "bbb/b" are included | |
188 manager.setRoots(<String>[project], <String>[], <String, String>{}); | |
189 callbacks.assertContextPaths([project]); | |
190 callbacks.assertContextFiles(project, [fileA, fileB]); | |
191 // exclude "bbb/" | |
192 manager.setRoots(<String>[project], <String>[folderB], <String, String>{}); | |
193 callbacks.assertContextPaths([project]); | |
194 callbacks.assertContextFiles(project, [fileA]); | |
195 } | |
196 | |
197 void test_setRoots_exclude_sameRoot_removeExcludedFile() { | |
198 String project = '/project'; | |
199 String file1 = '$project/file1.dart'; | |
200 String file2 = '$project/file2.dart'; | |
201 // create files | |
202 resourceProvider.newFile(file1, '// 1'); | |
203 resourceProvider.newFile(file2, '// 2'); | |
204 // set roots | |
205 manager.setRoots(<String>[project], <String>[file2], <String, String>{}); | |
206 callbacks.assertContextPaths([project]); | |
207 callbacks.assertContextFiles(project, [file1]); | |
208 // stop excluding "2" | |
209 manager.setRoots(<String>[project], <String>[], <String, String>{}); | |
210 callbacks.assertContextPaths([project]); | |
211 callbacks.assertContextFiles(project, [file1, file2]); | |
212 } | |
213 | |
214 void test_setRoots_exclude_sameRoot_removeExcludedFile_inFolder() { | |
215 String project = '/project'; | |
216 String file1 = '$project/bin/file1.dart'; | |
217 String file2 = '$project/bin/file2.dart'; | |
218 // create files | |
219 resourceProvider.newFile(file1, '// 1'); | |
220 resourceProvider.newFile(file2, '// 2'); | |
221 // set roots | |
222 manager.setRoots(<String>[project], <String>[file2], <String, String>{}); | |
223 callbacks.assertContextPaths([project]); | |
224 callbacks.assertContextFiles(project, [file1]); | |
225 // stop excluding "2" | |
226 manager.setRoots(<String>[project], <String>[], <String, String>{}); | |
227 callbacks.assertContextPaths([project]); | |
228 callbacks.assertContextFiles(project, [file1, file2]); | |
229 } | |
230 | |
231 void test_setRoots_exclude_sameRoot_removeExcludedFolder() { | |
232 String project = '/project'; | |
233 String folderA = '$project/aaa'; | |
234 String folderB = '$project/bbb'; | |
235 String fileA = '$folderA/a.dart'; | |
236 String fileB = '$folderB/b.dart'; | |
237 // create files | |
238 resourceProvider.newFile(fileA, 'library a;'); | |
239 resourceProvider.newFile(fileB, 'library b;'); | |
240 // exclude "bbb/" | |
241 manager.setRoots(<String>[project], <String>[folderB], <String, String>{}); | |
242 callbacks.assertContextPaths([project]); | |
243 callbacks.assertContextFiles(project, [fileA]); | |
244 // stop excluding "bbb/" | |
245 manager.setRoots(<String>[project], <String>[], <String, String>{}); | |
246 callbacks.assertContextPaths([project]); | |
247 callbacks.assertContextFiles(project, [fileA, fileB]); | |
248 } | |
249 | |
250 void test_setRoots_pathContainsDotFile() { | |
251 // If the path to a file (relative to the context root) contains a folder | |
252 // whose name begins with '.', then the file is ignored. | |
253 String project = '/project'; | |
254 String fileA = '$project/foo.dart'; | |
255 String fileB = '$project/.pub/bar.dart'; | |
256 resourceProvider.newFile(fileA, ''); | |
257 resourceProvider.newFile(fileB, ''); | |
258 manager.setRoots(<String>[project], <String>[], <String, String>{}); | |
259 callbacks.assertContextPaths([project]); | |
260 callbacks.assertContextFiles(project, [fileA]); | |
261 } | |
262 | |
263 void test_setRoots_rootPathContainsDotFile() { | |
264 // If the path to the context root itself contains a folder whose name | |
265 // begins with '.', then that is not sufficient to cause any files in the | |
266 // context to be ignored. | |
267 String project = '/.pub/project'; | |
268 String fileA = '$project/foo.dart'; | |
269 resourceProvider.newFile(fileA, ''); | |
270 manager.setRoots(<String>[project], <String>[], <String, String>{}); | |
271 callbacks.assertContextPaths([project]); | |
272 callbacks.assertContextFiles(project, [fileA]); | |
273 } | |
274 | |
275 void _processRequiredPlugins() { | |
276 List<Plugin> plugins = <Plugin>[]; | |
277 plugins.addAll(AnalysisEngine.instance.requiredPlugins); | |
278 plugins.add(AnalysisEngine.instance.commandLinePlugin); | |
279 plugins.add(AnalysisEngine.instance.optionsPlugin); | |
280 plugins.add(linterPlugin); | |
281 ExtensionManager manager = new ExtensionManager(); | |
282 manager.processPlugins(plugins); | |
283 } | |
284 | |
285 UriResolver _providePackageResolver(Folder folder) => packageResolver; | |
286 } | |
287 | |
288 class TestUriResolver extends UriResolver { | |
289 final ResourceProvider resourceProvider; | |
290 final Folder rootFolder; | |
291 | |
292 TestUriResolver(this.resourceProvider, this.rootFolder); | |
293 | |
294 @override | |
295 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | |
296 if (uri.scheme == 'package') { | |
297 List<String> segments = uri.pathSegments; | |
298 if (segments.length >= 2) { | |
299 List<String> relSegments = <String>['lib']..addAll(segments.skip(1)); | |
300 String relPath = resourceProvider.pathContext.joinAll(relSegments); | |
301 Resource file = rootFolder.getChild(relPath); | |
302 if (file is File && file.exists) { | |
303 return file.createSource(uri); | |
304 } | |
305 } | |
306 } | |
307 return null; | |
308 } | |
309 } | |
OLD | NEW |