| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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 // SharedOptions=--supermixin | 4 // SharedOptions=--supermixin |
| 5 | 5 |
| 6 library front_end.test.memory_file_system_test; | 6 library front_end.test.memory_file_system_test; |
| 7 | 7 |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io' as io; | 9 import 'dart:io' as io; |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 } | 23 } |
| 24 | 24 |
| 25 @reflectiveTest | 25 @reflectiveTest |
| 26 class FileTest extends _BaseTestNative { | 26 class FileTest extends _BaseTestNative { |
| 27 String path; | 27 String path; |
| 28 MemoryFileSystemEntity file; | 28 MemoryFileSystemEntity file; |
| 29 | 29 |
| 30 setUp() { | 30 setUp() { |
| 31 super.setUp(); | 31 super.setUp(); |
| 32 path = join(tempPath, 'file.txt'); | 32 path = join(tempPath, 'file.txt'); |
| 33 file = fileSystem.entityForPath(path); | 33 file = entityForPath(path); |
| 34 } | 34 } |
| 35 | 35 |
| 36 test_equals_differentPaths() { | 36 test_equals_differentPaths() { |
| 37 expect( | 37 expect(file == entityForPath(join(tempPath, 'file2.txt')), isFalse); |
| 38 file == fileSystem.entityForPath(join(tempPath, 'file2.txt')), isFalse); | |
| 39 } | 38 } |
| 40 | 39 |
| 41 test_equals_samePath() { | 40 test_equals_samePath() { |
| 42 expect( | 41 expect(file == entityForPath(join(tempPath, 'file.txt')), isTrue); |
| 43 file == fileSystem.entityForPath(join(tempPath, 'file.txt')), isTrue); | |
| 44 } | 42 } |
| 45 | 43 |
| 46 test_hashCode_samePath() { | 44 test_hashCode_samePath() { |
| 47 expect(file.hashCode, | 45 expect(file.hashCode, entityForPath(join(tempPath, 'file.txt')).hashCode); |
| 48 fileSystem.entityForPath(join(tempPath, 'file.txt')).hashCode); | |
| 49 } | 46 } |
| 50 | 47 |
| 51 test_path() { | 48 test_path() { |
| 52 expect(file.path, path); | 49 expect(file.uri, fileSystem.context.toUri(path)); |
| 53 } | 50 } |
| 54 | 51 |
| 55 test_readAsBytes_badUtf8() async { | 52 test_readAsBytes_badUtf8() async { |
| 56 // A file containing invalid UTF-8 can still be read as raw bytes. | 53 // A file containing invalid UTF-8 can still be read as raw bytes. |
| 57 List<int> bytes = [0xc0, 0x40]; // Invalid UTF-8 | 54 List<int> bytes = [0xc0, 0x40]; // Invalid UTF-8 |
| 58 file.writeAsBytesSync(bytes); | 55 file.writeAsBytesSync(bytes); |
| 59 expect(await file.readAsBytes(), bytes); | 56 expect(await file.readAsBytes(), bytes); |
| 60 } | 57 } |
| 61 | 58 |
| 62 test_readAsBytes_doesNotExist() { | 59 test_readAsBytes_doesNotExist() { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 } | 118 } |
| 122 | 119 |
| 123 abstract class MemoryFileSystemTestMixin extends _BaseTest { | 120 abstract class MemoryFileSystemTestMixin extends _BaseTest { |
| 124 Uri tempUri; | 121 Uri tempUri; |
| 125 | 122 |
| 126 setUp() { | 123 setUp() { |
| 127 super.setUp(); | 124 super.setUp(); |
| 128 tempUri = fileSystem.context.toUri(tempPath); | 125 tempUri = fileSystem.context.toUri(tempPath); |
| 129 } | 126 } |
| 130 | 127 |
| 131 test_entityForPath() { | 128 test_currentDirectory_trailingSlash() { |
| 132 var path = join(tempPath, 'file.txt'); | 129 // The currentDirectory should already end in a trailing slash. |
| 133 expect(fileSystem.entityForPath(path).path, path); | 130 expect(fileSystem.currentDirectory.path, endsWith('/')); |
| 131 // A trailing slash should automatically be appended when creating a |
| 132 // MemoryFileSystem. |
| 133 var path = fileSystem.currentDirectory.path; |
| 134 var currentDirectoryWithoutSlash = fileSystem.currentDirectory |
| 135 .replace(path: path.substring(0, path.length - 1)); |
| 136 expect( |
| 137 new MemoryFileSystem(fileSystem.context, currentDirectoryWithoutSlash) |
| 138 .currentDirectory, |
| 139 fileSystem.currentDirectory); |
| 140 // If the currentDirectory supplied to the MemoryFileSystem constructor |
| 141 // already has a trailing slash, no further trailing slash should be added. |
| 142 expect( |
| 143 new MemoryFileSystem(fileSystem.context, fileSystem.currentDirectory) |
| 144 .currentDirectory, |
| 145 fileSystem.currentDirectory); |
| 134 } | 146 } |
| 135 | 147 |
| 136 test_entityForPath_absolutize() { | 148 test_entityForPath_absolutize() { |
| 137 expect(fileSystem.entityForPath('file.txt').path, | 149 expect(entityForPath('file.txt').uri, |
| 138 join(fileSystem.currentDirectory, 'file.txt')); | 150 fileSystem.currentDirectory.resolve('file.txt')); |
| 139 } | 151 } |
| 140 | 152 |
| 141 test_entityForPath_normalize_dot() { | 153 test_entityForPath_normalize_dot() { |
| 142 expect(fileSystem.entityForPath(join(tempPath, '.', 'file.txt')).path, | 154 expect(entityForPath(join(tempPath, '.', 'file.txt')).uri, |
| 143 join(tempPath, 'file.txt')); | 155 Uri.parse('$tempUri/file.txt')); |
| 144 } | 156 } |
| 145 | 157 |
| 146 test_entityForPath_normalize_dotDot() { | 158 test_entityForPath_normalize_dotDot() { |
| 147 expect( | 159 expect(entityForPath(join(tempPath, 'foo', '..', 'file.txt')).uri, |
| 148 fileSystem.entityForPath(join(tempPath, 'foo', '..', 'file.txt')).path, | 160 Uri.parse('$tempUri/file.txt')); |
| 149 join(tempPath, 'file.txt')); | |
| 150 } | 161 } |
| 151 | 162 |
| 152 test_entityForUri() { | 163 test_entityForUri() { |
| 153 expect(fileSystem.entityForUri(Uri.parse('$tempUri/file.txt')).path, | 164 expect(fileSystem.entityForUri(Uri.parse('$tempUri/file.txt')).uri, |
| 154 join(tempPath, 'file.txt')); | 165 Uri.parse('$tempUri/file.txt')); |
| 155 } | |
| 156 | |
| 157 test_entityForUri_bareUri_absolute() { | |
| 158 expect(() => fileSystem.entityForUri(Uri.parse('/file.txt')), | |
| 159 throwsA(new isInstanceOf<Error>())); | |
| 160 } | |
| 161 | |
| 162 test_entityForUri_bareUri_relative() { | |
| 163 expect(() => fileSystem.entityForUri(Uri.parse('file.txt')), | |
| 164 throwsA(new isInstanceOf<Error>())); | |
| 165 } | 166 } |
| 166 | 167 |
| 167 test_entityForUri_fileUri_relative() { | 168 test_entityForUri_fileUri_relative() { |
| 168 // A weird quirk of the Uri class is that it doesn't seem possible to create | 169 // A weird quirk of the Uri class is that it doesn't seem possible to create |
| 169 // a `file:` uri with a relative path, no matter how many slashes you use or | 170 // a `file:` uri with a relative path, no matter how many slashes you use or |
| 170 // if you populate the fields directly. But just to be certain, try to do | 171 // if you populate the fields directly. But just to be certain, try to do |
| 171 // so, and make that `file:` uris with relative paths are rejected. | 172 // so, and make that `file:` uris with relative paths are rejected. |
| 172 for (var uri in <Uri>[ | 173 for (var uri in <Uri>[ |
| 173 new Uri(scheme: 'file', path: 'file.txt'), | 174 new Uri(scheme: 'file', path: 'file.txt'), |
| 174 Uri.parse('file:file.txt'), | 175 Uri.parse('file:file.txt'), |
| 175 Uri.parse('file:/file.txt'), | 176 Uri.parse('file:/file.txt'), |
| 176 Uri.parse('file://file.txt'), | 177 Uri.parse('file://file.txt'), |
| 177 Uri.parse('file:///file.txt') | 178 Uri.parse('file:///file.txt') |
| 178 ]) { | 179 ]) { |
| 179 if (!uri.path.startsWith('/')) { | 180 if (!uri.path.startsWith('/')) { |
| 180 expect(() => fileSystem.entityForUri(uri), | 181 expect(() => fileSystem.entityForUri(uri), |
| 181 throwsA(new isInstanceOf<Error>())); | 182 throwsA(new isInstanceOf<Error>())); |
| 182 } | 183 } |
| 183 } | 184 } |
| 184 } | 185 } |
| 185 | 186 |
| 186 test_entityForUri_nonFileUri() { | 187 test_entityForUri_nonFileUri() { |
| 187 expect(() => fileSystem.entityForUri(Uri.parse('package:foo/bar.dart')), | 188 var uri = Uri.parse('package:foo/bar.dart'); |
| 188 throwsA(new isInstanceOf<Error>())); | 189 expect(fileSystem.entityForUri(uri).uri, uri); |
| 189 } | 190 } |
| 190 | 191 |
| 191 test_entityForUri_normalize_dot() { | 192 test_entityForUri_normalize_dot() { |
| 192 expect(fileSystem.entityForUri(Uri.parse('$tempUri/./file.txt')).path, | 193 expect(fileSystem.entityForUri(Uri.parse('$tempUri/./file.txt')).uri, |
| 193 join(tempPath, 'file.txt')); | 194 Uri.parse('$tempUri/file.txt')); |
| 194 } | 195 } |
| 195 | 196 |
| 196 test_entityForUri_normalize_dotDot() { | 197 test_entityForUri_normalize_dotDot() { |
| 197 expect(fileSystem.entityForUri(Uri.parse('$tempUri/foo/../file.txt')).path, | 198 expect(fileSystem.entityForUri(Uri.parse('$tempUri/foo/../file.txt')).uri, |
| 198 join(tempPath, 'file.txt')); | 199 Uri.parse('$tempUri/file.txt')); |
| 199 } | 200 } |
| 200 } | 201 } |
| 201 | 202 |
| 202 @reflectiveTest | 203 @reflectiveTest |
| 203 class MemoryFileSystemTestNative extends _BaseTestNative | 204 class MemoryFileSystemTestNative extends _BaseTestNative |
| 204 with MemoryFileSystemTestMixin {} | 205 with MemoryFileSystemTestMixin {} |
| 205 | 206 |
| 206 @reflectiveTest | 207 @reflectiveTest |
| 207 class MemoryFileSystemTestPosix extends _BaseTestPosix | 208 class MemoryFileSystemTestPosix extends _BaseTestPosix |
| 208 with MemoryFileSystemTestMixin {} | 209 with MemoryFileSystemTestMixin {} |
| 209 | 210 |
| 210 @reflectiveTest | 211 @reflectiveTest |
| 211 class MemoryFileSystemTestWindows extends _BaseTestWindows | 212 class MemoryFileSystemTestWindows extends _BaseTestWindows |
| 212 with MemoryFileSystemTestMixin {} | 213 with MemoryFileSystemTestMixin {} |
| 213 | 214 |
| 214 abstract class _BaseTest { | 215 abstract class _BaseTest { |
| 215 MemoryFileSystem get fileSystem; | 216 MemoryFileSystem get fileSystem; |
| 217 |
| 216 String get tempPath; | 218 String get tempPath; |
| 219 |
| 220 MemoryFileSystemEntity entityForPath(String path) => |
| 221 fileSystem.entityForUri(fileSystem.context.toUri(path)); |
| 222 |
| 217 String join(String path1, String path2, [String path3, String path4]); | 223 String join(String path1, String path2, [String path3, String path4]); |
| 224 |
| 218 void setUp(); | 225 void setUp(); |
| 219 } | 226 } |
| 220 | 227 |
| 221 class _BaseTestNative extends _BaseTest { | 228 class _BaseTestNative extends _BaseTest { |
| 222 MemoryFileSystem fileSystem; | 229 MemoryFileSystem fileSystem; |
| 223 String tempPath; | 230 String tempPath; |
| 224 | 231 |
| 225 String join(String path1, String path2, [String path3, String path4]) => | 232 String join(String path1, String path2, [String path3, String path4]) => |
| 226 pathos.join(path1, path2, path3, path4); | 233 pathos.join(path1, path2, path3, path4); |
| 227 | 234 |
| 228 setUp() { | 235 setUp() { |
| 229 tempPath = pathos.join(io.Directory.systemTemp.path, 'test_file_system'); | 236 tempPath = pathos.join(io.Directory.systemTemp.path, 'test_file_system'); |
| 230 fileSystem = | 237 fileSystem = new MemoryFileSystem( |
| 231 new MemoryFileSystem(pathos.context, io.Directory.current.path); | 238 pathos.context, pathos.toUri(io.Directory.current.path)); |
| 232 } | 239 } |
| 233 } | 240 } |
| 234 | 241 |
| 235 class _BaseTestPosix extends _BaseTest { | 242 class _BaseTestPosix extends _BaseTest { |
| 236 MemoryFileSystem fileSystem; | 243 MemoryFileSystem fileSystem; |
| 237 String tempPath; | 244 String tempPath; |
| 238 | 245 |
| 239 String join(String path1, String path2, [String path3, String path4]) => | 246 String join(String path1, String path2, [String path3, String path4]) => |
| 240 pathos.posix.join(path1, path2, path3, path4); | 247 pathos.posix.join(path1, path2, path3, path4); |
| 241 | 248 |
| 242 void setUp() { | 249 void setUp() { |
| 243 tempPath = '/test_file_system'; | 250 tempPath = '/test_file_system'; |
| 244 fileSystem = new MemoryFileSystem(pathos.posix, '/cwd'); | 251 fileSystem = new MemoryFileSystem(pathos.posix, Uri.parse('file:///cwd')); |
| 245 } | 252 } |
| 246 } | 253 } |
| 247 | 254 |
| 248 class _BaseTestWindows extends _BaseTest { | 255 class _BaseTestWindows extends _BaseTest { |
| 249 MemoryFileSystem fileSystem; | 256 MemoryFileSystem fileSystem; |
| 250 String tempPath; | 257 String tempPath; |
| 251 | 258 |
| 252 String join(String path1, String path2, [String path3, String path4]) => | 259 String join(String path1, String path2, [String path3, String path4]) => |
| 253 pathos.windows.join(path1, path2, path3, path4); | 260 pathos.windows.join(path1, path2, path3, path4); |
| 254 | 261 |
| 255 void setUp() { | 262 void setUp() { |
| 256 tempPath = r'c:\test_file_system'; | 263 tempPath = r'c:\test_file_system'; |
| 257 fileSystem = new MemoryFileSystem(pathos.windows, r'c:\cwd'); | 264 fileSystem = |
| 265 new MemoryFileSystem(pathos.windows, Uri.parse('file:///c:/cwd')); |
| 258 } | 266 } |
| 259 } | 267 } |
| OLD | NEW |