OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 analyzer.test.source.embedder_test; | 5 library analyzer.test.source.embedder_test; |
6 | 6 |
7 import 'dart:core' hide Resource; | 7 import 'dart:core' hide Resource; |
8 | 8 |
9 import 'package:analyzer/file_system/file_system.dart'; | 9 import 'package:analyzer/file_system/file_system.dart'; |
10 import 'package:analyzer/file_system/memory_file_system.dart'; | 10 import 'package:analyzer/file_system/memory_file_system.dart'; |
11 import 'package:analyzer/source/embedder.dart'; | 11 import 'package:analyzer/source/embedder.dart'; |
12 import 'package:analyzer/src/generated/source.dart'; | 12 import 'package:analyzer/src/generated/source.dart'; |
13 import 'package:analyzer/src/util/absolute_path.dart'; | |
14 import 'package:path/path.dart' as path; | 13 import 'package:path/path.dart' as path; |
15 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
16 | 15 |
| 16 import '../resource_utils.dart'; |
17 import '../utils.dart'; | 17 import '../utils.dart'; |
18 | 18 |
19 main() { | 19 main() { |
20 group('EmbedderUriResolverTest', () { | 20 group('EmbedderUriResolverTest', () { |
21 setUp(() { | 21 setUp(() { |
22 initializeTestEnvironment(path.context); | 22 initializeTestEnvironment(path.context); |
23 buildResourceProvider(); | 23 buildResourceProvider(); |
24 }); | 24 }); |
25 tearDown(() { | 25 tearDown(() { |
26 initializeTestEnvironment(); | 26 initializeTestEnvironment(); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 "dart:relative": "../relative.dart" | 156 "dart:relative": "../relative.dart" |
157 "dart:deep": "deep/directory/file.dart" | 157 "dart:deep": "deep/directory/file.dart" |
158 "fart:loudly": "nomatter.dart" | 158 "fart:loudly": "nomatter.dart" |
159 '''); | 159 '''); |
160 } | 160 } |
161 | 161 |
162 clearResourceProvider() { | 162 clearResourceProvider() { |
163 resourceProvider = null; | 163 resourceProvider = null; |
164 pathTranslator = null; | 164 pathTranslator = null; |
165 } | 165 } |
166 | |
167 // TODO(danrubel) if this approach works well for running tests | |
168 // in a platform specific way, then move all of the following functionality | |
169 // into a separate test utility library. | |
170 | |
171 bool get isWindows => path.Style.platform == path.Style.windows; | |
172 | |
173 /** | |
174 * Assert that the given path is posix. | |
175 */ | |
176 void expectAbsolutePosixPath(String posixPath) { | |
177 expect(posixPath, startsWith('/'), | |
178 reason: 'Expected absolute posix path, but found $posixPath'); | |
179 } | |
180 | |
181 /** | |
182 * Translate the given posixPath to a path appropriate for the | |
183 * platform on which the tests are executing. | |
184 */ | |
185 String posixToOSPath(String posixPath) { | |
186 expectAbsolutePosixPath(posixPath); | |
187 if (isWindows) { | |
188 String windowsPath = posixPath.replaceAll('/', '\\'); | |
189 if (posixPath.startsWith('/')) { | |
190 return 'C:$windowsPath'; | |
191 } | |
192 return windowsPath; | |
193 } | |
194 return posixPath; | |
195 } | |
196 | |
197 /** | |
198 * Translate the given posixPath to a file URI appropriate for the | |
199 * platform on which the tests are executing. | |
200 */ | |
201 String posixToOSFileUri(String posixPath) { | |
202 expectAbsolutePosixPath(posixPath); | |
203 return isWindows ? 'file:///C:$posixPath' : 'file://$posixPath'; | |
204 } | |
205 | |
206 /** | |
207 * A convenience utility for setting up a test [MemoryResourceProvider]. | |
208 * All supplied paths are assumed to be in [path.posix] format | |
209 * and are automatically translated to [path.context]. | |
210 * | |
211 * This class intentionally does not implement [ResourceProvider] | |
212 * directly or indirectly so that it cannot be used as a resource provider. | |
213 * We do not want functionality under test to interact with a resource provider | |
214 * that automatically translates paths. | |
215 */ | |
216 class TestPathTranslator { | |
217 final MemoryResourceProvider _provider; | |
218 | |
219 TestPathTranslator(this._provider); | |
220 | |
221 Resource getResource(String posixPath) => | |
222 _provider.getResource(posixToOSPath(posixPath)); | |
223 | |
224 File newFile(String posixPath, String content) => | |
225 _provider.newFile(posixToOSPath(posixPath), content); | |
226 | |
227 Folder newFolder(String posixPath) => | |
228 _provider.newFolder(posixToOSPath(posixPath)); | |
229 } | |
230 | |
231 /** | |
232 * A resource provider for testing that asserts that any supplied paths | |
233 * are appropriate for the OS platform on which the tests are running. | |
234 */ | |
235 class TestResourceProvider implements ResourceProvider { | |
236 final ResourceProvider _provider; | |
237 | |
238 TestResourceProvider(this._provider) { | |
239 expect(_provider.absolutePathContext.separator, isWindows ? '\\' : '/'); | |
240 } | |
241 | |
242 @override | |
243 AbsolutePathContext get absolutePathContext => _provider.absolutePathContext; | |
244 | |
245 @override | |
246 File getFile(String path) => _provider.getFile(_assertPath(path)); | |
247 | |
248 @override | |
249 Folder getFolder(String path) => _provider.getFolder(_assertPath(path)); | |
250 | |
251 @override | |
252 Resource getResource(String path) => _provider.getResource(_assertPath(path)); | |
253 | |
254 @override | |
255 Folder getStateLocation(String pluginId) => | |
256 _provider.getStateLocation(pluginId); | |
257 | |
258 @override | |
259 path.Context get pathContext => _provider.pathContext; | |
260 | |
261 /** | |
262 * Assert that the given path is valid for the OS platform on which the | |
263 * tests are running. | |
264 */ | |
265 String _assertPath(String path) { | |
266 if (isWindows) { | |
267 if (path.contains('/')) { | |
268 fail('Expected windows path, but found: $path'); | |
269 } | |
270 } else { | |
271 if (path.contains('\\')) { | |
272 fail('Expected posix path, but found: $path'); | |
273 } | |
274 } | |
275 return path; | |
276 } | |
277 } | |
OLD | NEW |