| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library analyzer.test.resource_utils; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:core'; |
| 9 |
| 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/source.dart'; |
| 13 import 'package:analyzer/src/util/absolute_path.dart'; |
| 14 import 'package:path/path.dart' as path; |
| 15 import 'package:unittest/unittest.dart'; |
| 16 |
| 17 bool get isWindows => path.Style.platform == path.Style.windows; |
| 18 |
| 19 /** |
| 20 * Assert that the given path is posix and absolute. |
| 21 */ |
| 22 void expectAbsolutePosixPath(String posixPath) { |
| 23 expect(posixPath, startsWith('/'), |
| 24 reason: 'Expected absolute posix path, but found $posixPath'); |
| 25 } |
| 26 |
| 27 /** |
| 28 * Assert that the given path is posix. |
| 29 */ |
| 30 void expectPosixPath(String posixPath) { |
| 31 expect(posixPath.indexOf('\\'), -1, |
| 32 reason: 'Expected posix path, but found $posixPath'); |
| 33 } |
| 34 |
| 35 /** |
| 36 * Translate the given posixPath to a file URI appropriate for the |
| 37 * platform on which the tests are executing. |
| 38 */ |
| 39 String posixToOSFileUri(String posixPath) { |
| 40 expectPosixPath(posixPath); |
| 41 return isWindows ? 'file:///C:$posixPath' : 'file://$posixPath'; |
| 42 } |
| 43 |
| 44 /** |
| 45 * Translate the given posixPath to a path appropriate for the |
| 46 * platform on which the tests are executing. |
| 47 */ |
| 48 String posixToOSPath(String posixPath) { |
| 49 expectPosixPath(posixPath); |
| 50 if (isWindows) { |
| 51 String windowsPath = posixPath.replaceAll('/', '\\'); |
| 52 if (posixPath.startsWith('/')) { |
| 53 return 'C:$windowsPath'; |
| 54 } |
| 55 return windowsPath; |
| 56 } |
| 57 return posixPath; |
| 58 } |
| 59 |
| 60 /** |
| 61 * A convenience utility for setting up a test [MemoryResourceProvider]. |
| 62 * All supplied paths are assumed to be in [path.posix] format |
| 63 * and are automatically translated to [path.context]. |
| 64 * |
| 65 * This class intentionally does not implement [ResourceProvider] |
| 66 * directly or indirectly so that it cannot be used as a resource provider. |
| 67 * We do not want functionality under test to interact with a resource provider |
| 68 * that automatically translates paths. |
| 69 */ |
| 70 class TestPathTranslator { |
| 71 final MemoryResourceProvider _provider; |
| 72 |
| 73 TestPathTranslator(this._provider); |
| 74 |
| 75 Resource getResource(String posixPath) => |
| 76 _provider.getResource(posixToOSPath(posixPath)); |
| 77 |
| 78 File newFile(String posixPath, String content) => |
| 79 _provider.newFile(posixToOSPath(posixPath), content); |
| 80 |
| 81 File newFileWithBytes(String posixPath, List<int> bytes) => |
| 82 _provider.newFileWithBytes(posixToOSPath(posixPath), bytes); |
| 83 |
| 84 Folder newFolder(String posixPath) => |
| 85 _provider.newFolder(posixToOSPath(posixPath)); |
| 86 } |
| 87 |
| 88 /** |
| 89 * A resource provider for testing that asserts that any supplied paths |
| 90 * are appropriate for the OS platform on which the tests are running. |
| 91 */ |
| 92 class TestResourceProvider implements ResourceProvider { |
| 93 final ResourceProvider _provider; |
| 94 |
| 95 TestResourceProvider(this._provider) { |
| 96 expect(_provider.absolutePathContext.separator, isWindows ? '\\' : '/'); |
| 97 } |
| 98 |
| 99 @override |
| 100 AbsolutePathContext get absolutePathContext => _provider.absolutePathContext; |
| 101 |
| 102 @override |
| 103 path.Context get pathContext => _provider.pathContext; |
| 104 |
| 105 @override |
| 106 File getFile(String path) => _provider.getFile(_assertPath(path)); |
| 107 |
| 108 @override |
| 109 Folder getFolder(String path) => _provider.getFolder(_assertPath(path)); |
| 110 |
| 111 @override |
| 112 Future<List<int>> getModificationTimes(List<Source> sources) async { |
| 113 return sources.map((source) => 0).toList(); |
| 114 } |
| 115 |
| 116 @override |
| 117 Resource getResource(String path) => _provider.getResource(_assertPath(path)); |
| 118 |
| 119 @override |
| 120 Folder getStateLocation(String pluginId) => |
| 121 _provider.getStateLocation(pluginId); |
| 122 |
| 123 /** |
| 124 * Assert that the given path is valid for the OS platform on which the |
| 125 * tests are running. |
| 126 */ |
| 127 String _assertPath(String path) { |
| 128 if (isWindows) { |
| 129 if (path.contains('/')) { |
| 130 fail('Expected windows path, but found: $path'); |
| 131 } |
| 132 } else { |
| 133 if (path.contains('\\')) { |
| 134 fail('Expected posix path, but found: $path'); |
| 135 } |
| 136 } |
| 137 return path; |
| 138 } |
| 139 } |
| OLD | NEW |