| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, 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 'dart:async'; | |
| 6 import 'dart:io'; | |
| 7 | |
| 8 import 'package:path/path.dart' as path; | |
| 9 import 'package:scheduled_test/descriptor.dart' as d; | |
| 10 import 'package:scheduled_test/scheduled_test.dart'; | |
| 11 | |
| 12 import 'package:metatest/metatest.dart'; | |
| 13 import 'utils.dart'; | |
| 14 | |
| 15 void main() => initTests(_test); | |
| 16 | |
| 17 void _test(message) { | |
| 18 initMetatest(message); | |
| 19 | |
| 20 setUpTimeout(); | |
| 21 | |
| 22 expectTestsPass("directory().create() creates a directory and its contents", | |
| 23 () { | |
| 24 test('test', () { | |
| 25 scheduleSandbox(); | |
| 26 | |
| 27 d.dir('dir', [ | |
| 28 d.dir('subdir', [ | |
| 29 d.file('subfile1.txt', 'subcontents1'), | |
| 30 d.file('subfile2.txt', 'subcontents2') | |
| 31 ]), | |
| 32 d.file('file1.txt', 'contents1'), | |
| 33 d.file('file2.txt', 'contents2') | |
| 34 ]).create(); | |
| 35 | |
| 36 schedule(() { | |
| 37 expect(new File(path.join(sandbox, 'dir', 'file1.txt')).readAsString(), | |
| 38 completion(equals('contents1'))); | |
| 39 expect(new File(path.join(sandbox, 'dir', 'file2.txt')).readAsString(), | |
| 40 completion(equals('contents2'))); | |
| 41 expect(new File(path.join(sandbox, 'dir', 'subdir', 'subfile1.txt')) | |
| 42 .readAsString(), | |
| 43 completion(equals('subcontents1'))); | |
| 44 expect(new File(path.join(sandbox, 'dir', 'subdir', 'subfile2.txt')) | |
| 45 .readAsString(), | |
| 46 completion(equals('subcontents2'))); | |
| 47 }); | |
| 48 }); | |
| 49 }); | |
| 50 | |
| 51 expectTestsPass("directory().create() works if the directory already exists", | |
| 52 () { | |
| 53 test('test', () { | |
| 54 scheduleSandbox(); | |
| 55 | |
| 56 d.dir('dir').create(); | |
| 57 d.dir('dir', [d.file('name.txt', 'contents')]).create(); | |
| 58 | |
| 59 schedule(() { | |
| 60 expect(new File(path.join(sandbox, 'dir', 'name.txt')).readAsString(), | |
| 61 completion(equals('contents'))); | |
| 62 }); | |
| 63 }); | |
| 64 }); | |
| 65 | |
| 66 expectTestsPass("directory().validate() completes successfully if the " | |
| 67 "filesystem matches the descriptor", () { | |
| 68 test('test', () { | |
| 69 scheduleSandbox(); | |
| 70 | |
| 71 schedule(() { | |
| 72 var dirPath = path.join(sandbox, 'dir'); | |
| 73 var subdirPath = path.join(dirPath, 'subdir'); | |
| 74 return new Directory(subdirPath).create(recursive: true).then((_) { | |
| 75 return Future.wait([ | |
| 76 new File(path.join(dirPath, 'file1.txt')) | |
| 77 .writeAsString('contents1'), | |
| 78 new File(path.join(dirPath, 'file2.txt')) | |
| 79 .writeAsString('contents2'), | |
| 80 new File(path.join(subdirPath, 'subfile1.txt')) | |
| 81 .writeAsString('subcontents1'), | |
| 82 new File(path.join(subdirPath, 'subfile2.txt')) | |
| 83 .writeAsString('subcontents2') | |
| 84 ]); | |
| 85 }); | |
| 86 }); | |
| 87 | |
| 88 d.dir('dir', [ | |
| 89 d.dir('subdir', [ | |
| 90 d.file('subfile1.txt', 'subcontents1'), | |
| 91 d.file('subfile2.txt', 'subcontents2') | |
| 92 ]), | |
| 93 d.file('file1.txt', 'contents1'), | |
| 94 d.file('file2.txt', 'contents2') | |
| 95 ]).validate(); | |
| 96 }); | |
| 97 }); | |
| 98 | |
| 99 expectTestsPass("directory().validate() fails if a directory isn't found" | |
| 100 , () { | |
| 101 var errors; | |
| 102 test('test 1', () { | |
| 103 scheduleSandbox(); | |
| 104 | |
| 105 currentSchedule.onException.schedule(() { | |
| 106 errors = currentSchedule.errors; | |
| 107 }); | |
| 108 | |
| 109 schedule(() { | |
| 110 var dirPath = path.join(sandbox, 'dir'); | |
| 111 return new Directory(dirPath).create().then((_) { | |
| 112 return Future.wait([ | |
| 113 new File(path.join(dirPath, 'file1.txt')) | |
| 114 .writeAsString('contents1'), | |
| 115 new File(path.join(dirPath, 'file2.txt')) | |
| 116 .writeAsString('contents2') | |
| 117 ]); | |
| 118 }); | |
| 119 }); | |
| 120 | |
| 121 d.dir('dir', [ | |
| 122 d.dir('subdir', [ | |
| 123 d.file('subfile1.txt', 'subcontents1'), | |
| 124 d.file('subfile2.txt', 'subcontents2') | |
| 125 ]), | |
| 126 d.file('file1.txt', 'contents1'), | |
| 127 d.file('file2.txt', 'contents2') | |
| 128 ]).validate(); | |
| 129 }); | |
| 130 | |
| 131 test('test 2', () { | |
| 132 expect(errors.single, new isInstanceOf<ScheduleError>()); | |
| 133 expect(errors.single.error.toString(), | |
| 134 matches(r"^Directory not found: '[^']+[\\/]dir[\\/]subdir'\.$")); | |
| 135 }); | |
| 136 }, passing: ['test 2']); | |
| 137 | |
| 138 expectTestsPass("directory().validate() fails if a file isn't found", () { | |
| 139 var errors; | |
| 140 test('test 1', () { | |
| 141 scheduleSandbox(); | |
| 142 | |
| 143 currentSchedule.onException.schedule(() { | |
| 144 errors = currentSchedule.errors; | |
| 145 }); | |
| 146 | |
| 147 schedule(() { | |
| 148 var dirPath = path.join(sandbox, 'dir'); | |
| 149 var subdirPath = path.join(dirPath, 'subdir'); | |
| 150 return new Directory(subdirPath).create(recursive: true).then((_) { | |
| 151 return Future.wait([ | |
| 152 new File(path.join(dirPath, 'file1.txt')) | |
| 153 .writeAsString('contents1'), | |
| 154 new File(path.join(subdirPath, 'subfile1.txt')) | |
| 155 .writeAsString('subcontents1'), | |
| 156 new File(path.join(subdirPath, 'subfile2.txt')) | |
| 157 .writeAsString('subcontents2') | |
| 158 ]); | |
| 159 }); | |
| 160 }); | |
| 161 | |
| 162 d.dir('dir', [ | |
| 163 d.dir('subdir', [ | |
| 164 d.file('subfile1.txt', 'subcontents1'), | |
| 165 d.file('subfile2.txt', 'subcontents2') | |
| 166 ]), | |
| 167 d.file('file1.txt', 'contents1'), | |
| 168 d.file('file2.txt', 'contents2') | |
| 169 ]).validate(); | |
| 170 }); | |
| 171 | |
| 172 test('test 2', () { | |
| 173 expect(errors.single, new isInstanceOf<ScheduleError>()); | |
| 174 expect(errors.single.error.toString(), | |
| 175 matches(r"^File not found: '[^']+[\\/]dir[\\/]file2\.txt'\.$")); | |
| 176 }); | |
| 177 }, passing: ['test 2']); | |
| 178 | |
| 179 expectTestsPass("directory().validate() fails if multiple children aren't " | |
| 180 "found or have the wrong contents", () { | |
| 181 var errors; | |
| 182 test('test 1', () { | |
| 183 scheduleSandbox(); | |
| 184 | |
| 185 currentSchedule.onException.schedule(() { | |
| 186 errors = currentSchedule.errors; | |
| 187 }); | |
| 188 | |
| 189 schedule(() { | |
| 190 var dirPath = path.join(sandbox, 'dir'); | |
| 191 var subdirPath = path.join(dirPath, 'subdir'); | |
| 192 return new Directory(subdirPath).create(recursive: true).then((_) { | |
| 193 return Future.wait([ | |
| 194 new File(path.join(dirPath, 'file1.txt')) | |
| 195 .writeAsString('contents1'), | |
| 196 new File(path.join(subdirPath, 'subfile2.txt')) | |
| 197 .writeAsString('subwrongtents2') | |
| 198 ]); | |
| 199 }); | |
| 200 }); | |
| 201 | |
| 202 d.dir('dir', [ | |
| 203 d.dir('subdir', [ | |
| 204 d.file('subfile1.txt', 'subcontents1'), | |
| 205 d.file('subfile2.txt', 'subcontents2') | |
| 206 ]), | |
| 207 d.file('file1.txt', 'contents1'), | |
| 208 d.file('file2.txt', 'contents2') | |
| 209 ]).validate(); | |
| 210 }); | |
| 211 | |
| 212 test('test 2', () { | |
| 213 expect(errors.single, new isInstanceOf<ScheduleError>()); | |
| 214 expect(errors.single.error.toString(), matches( | |
| 215 r"^\* File not found: '[^']+[\\/]dir[\\/]subdir[\\/]subfile1\.txt'\." | |
| 216 r"\n" | |
| 217 r"\* File 'subfile2\.txt' should contain:\n" | |
| 218 r" \| subcontents2\n" | |
| 219 r" but actually contained:\n" | |
| 220 r" X subwrongtents2\n" | |
| 221 r"\* File not found: '[^']+[\\/]dir[\\/]file2\.txt'\.$")); | |
| 222 }); | |
| 223 }, passing: ['test 2']); | |
| 224 | |
| 225 expectTestsPass("directory().validate() fails if a file has the wrong " | |
| 226 "contents", () { | |
| 227 var errors; | |
| 228 test('test 1', () { | |
| 229 scheduleSandbox(); | |
| 230 | |
| 231 currentSchedule.onException.schedule(() { | |
| 232 errors = currentSchedule.errors; | |
| 233 }); | |
| 234 | |
| 235 schedule(() { | |
| 236 var dirPath = path.join(sandbox, 'dir'); | |
| 237 var subdirPath = path.join(dirPath, 'subdir'); | |
| 238 return new Directory(subdirPath).create(recursive: true).then((_) { | |
| 239 return Future.wait([ | |
| 240 new File(path.join(dirPath, 'file1.txt')) | |
| 241 .writeAsString('contents1'), | |
| 242 new File(path.join(dirPath, 'file2.txt')) | |
| 243 .writeAsString('contents2'), | |
| 244 new File(path.join(subdirPath, 'subfile1.txt')) | |
| 245 .writeAsString('wrongtents1'), | |
| 246 new File(path.join(subdirPath, 'subfile2.txt')) | |
| 247 .writeAsString('subcontents2') | |
| 248 ]); | |
| 249 }); | |
| 250 }); | |
| 251 | |
| 252 d.dir('dir', [ | |
| 253 d.dir('subdir', [ | |
| 254 d.file('subfile1.txt', 'subcontents1'), | |
| 255 d.file('subfile2.txt', 'subcontents2') | |
| 256 ]), | |
| 257 d.file('file1.txt', 'contents1'), | |
| 258 d.file('file2.txt', 'contents2') | |
| 259 ]).validate(); | |
| 260 }); | |
| 261 | |
| 262 test('test 2', () { | |
| 263 expect(errors.single, new isInstanceOf<ScheduleError>()); | |
| 264 expect(errors.single.error.toString(), equals( | |
| 265 "File 'subfile1.txt' should contain:\n" | |
| 266 "| subcontents1\n" | |
| 267 "but actually contained:\n" | |
| 268 "X wrongtents1")); | |
| 269 }); | |
| 270 }, passing: ['test 2']); | |
| 271 | |
| 272 expectTestsPass("directory().load() loads a file", () { | |
| 273 test('test', () { | |
| 274 var dir = d.dir('dir', [d.file('name.txt', 'contents')]); | |
| 275 expect(byteStreamToString(dir.load('name.txt')), | |
| 276 completion(equals('contents'))); | |
| 277 }); | |
| 278 }); | |
| 279 | |
| 280 expectTestsPass("directory().load() loads a deeply-nested file", () { | |
| 281 test('test', () { | |
| 282 var dir = d.dir('dir', [ | |
| 283 d.dir('subdir', [ | |
| 284 d.file('name.txt', 'subcontents') | |
| 285 ]), | |
| 286 d.file('name.txt', 'contents') | |
| 287 ]); | |
| 288 | |
| 289 expect(byteStreamToString(dir.load('subdir/name.txt')), | |
| 290 completion(equals('subcontents'))); | |
| 291 }); | |
| 292 }); | |
| 293 | |
| 294 expectTestsPass("directory().load() fails to load a nested directory", () { | |
| 295 test('test', () { | |
| 296 var dir = d.dir('dir', [ | |
| 297 d.dir('subdir', [ | |
| 298 d.file('name.txt', 'subcontents') | |
| 299 ]), | |
| 300 d.file('name.txt', 'contents') | |
| 301 ]); | |
| 302 | |
| 303 expect(dir.load('subdir').toList(), throwsA(predicate( | |
| 304 (x) => x.toString() == "Couldn't find a readable entry named " | |
| 305 "'subdir' within 'dir'."))); | |
| 306 }); | |
| 307 }); | |
| 308 | |
| 309 expectTestsPass("directory().load() fails to load an absolute path", () { | |
| 310 test('test', () { | |
| 311 var dir = d.dir('dir', [d.file('name.txt', 'contents')]); | |
| 312 | |
| 313 expect(dir.load('/name.txt').toList(), throwsArgumentError); | |
| 314 }); | |
| 315 }); | |
| 316 | |
| 317 expectTestsPass("directory().load() fails to load '.', '..', or ''", () { | |
| 318 test('test', () { | |
| 319 var dir = d.dir('dir', [d.file('name.txt', 'contents')]); | |
| 320 | |
| 321 expect(dir.load('.').toList(), throwsArgumentError); | |
| 322 | |
| 323 expect(dir.load('..').toList(), throwsArgumentError); | |
| 324 | |
| 325 expect(dir.load('').toList(), throwsArgumentError); | |
| 326 }); | |
| 327 }); | |
| 328 | |
| 329 expectTestsPass("directory().load() fails to load a file that doesn't exist", | |
| 330 () { | |
| 331 test('test', () { | |
| 332 var dir = d.dir('dir', [d.file('name.txt', 'contents')]); | |
| 333 | |
| 334 expect(dir.load('not-name.txt').toList(), throwsA(predicate( | |
| 335 (x) => x.toString() == "Couldn't find a readable entry named " | |
| 336 "'not-name.txt' within 'dir'."))); | |
| 337 }); | |
| 338 }); | |
| 339 | |
| 340 expectTestsPass("directory().load() fails to load a file that exists " | |
| 341 "multiple times", () { | |
| 342 test('test', () { | |
| 343 var dir = d.dir('dir', [ | |
| 344 d.file('name.txt', 'contents'), | |
| 345 d.file('name.txt', 'contents') | |
| 346 ]); | |
| 347 | |
| 348 expect(dir.load('name.txt').toList(), throwsA(predicate( | |
| 349 (x) => x.toString() == "Found multiple readable entries named " | |
| 350 "'name.txt' within 'dir'."))); | |
| 351 }); | |
| 352 }); | |
| 353 | |
| 354 expectTestsPass("directory().load() loads a file next to a subdirectory with " | |
| 355 "the same name", () { | |
| 356 test('test', () { | |
| 357 var dir = d.dir('dir', [ | |
| 358 d.file('name', 'contents'), | |
| 359 d.dir('name', [d.file('subfile', 'contents')]) | |
| 360 ]); | |
| 361 | |
| 362 expect(byteStreamToString(dir.load('name')), | |
| 363 completion(equals('contents'))); | |
| 364 }); | |
| 365 }); | |
| 366 | |
| 367 expectTestsPass("directory().describe() lists the contents of the directory", | |
| 368 () { | |
| 369 test('test', () { | |
| 370 var dir = d.dir('dir', [ | |
| 371 d.file('file1.txt', 'contents1'), | |
| 372 d.file('file2.txt', 'contents2') | |
| 373 ]); | |
| 374 | |
| 375 expect(dir.describe(), equals( | |
| 376 "dir\n" | |
| 377 "|-- file1.txt\n" | |
| 378 "'-- file2.txt")); | |
| 379 }); | |
| 380 }); | |
| 381 | |
| 382 expectTestsPass("directory().describe() lists the contents of nested " | |
| 383 "directories", () { | |
| 384 test('test', () { | |
| 385 var dir = d.dir('dir', [ | |
| 386 d.file('file1.txt', 'contents1'), | |
| 387 d.dir('subdir', [ | |
| 388 d.file('subfile1.txt', 'subcontents1'), | |
| 389 d.file('subfile2.txt', 'subcontents2'), | |
| 390 d.dir('subsubdir', [ | |
| 391 d.file('subsubfile.txt', 'subsubcontents') | |
| 392 ]) | |
| 393 ]), | |
| 394 d.file('file2.txt', 'contents2') | |
| 395 ]); | |
| 396 | |
| 397 expect(dir.describe(), equals( | |
| 398 "dir\n" | |
| 399 "|-- file1.txt\n" | |
| 400 "|-- subdir\n" | |
| 401 "| |-- subfile1.txt\n" | |
| 402 "| |-- subfile2.txt\n" | |
| 403 "| '-- subsubdir\n" | |
| 404 "| '-- subsubfile.txt\n" | |
| 405 "'-- file2.txt")); | |
| 406 }); | |
| 407 }); | |
| 408 | |
| 409 expectTestsPass("directory().describe() with no contents returns the " | |
| 410 "directory name", () { | |
| 411 test('test', () { | |
| 412 expect(d.dir('dir').describe(), equals('dir')); | |
| 413 }); | |
| 414 }); | |
| 415 | |
| 416 expectTestPasses("new DirectoryDescriptor().fromFilesystem creates a " | |
| 417 "descriptor based on the physical filesystem", () { | |
| 418 scheduleSandbox(); | |
| 419 | |
| 420 d.dir('dir', [ | |
| 421 d.dir('subdir', [ | |
| 422 d.file('subfile1.txt', 'subcontents1'), | |
| 423 d.file('subfile2.txt', 'subcontents2') | |
| 424 ]), | |
| 425 d.file('file1.txt', 'contents1'), | |
| 426 d.file('file2.txt', 'contents2') | |
| 427 ]).create(); | |
| 428 | |
| 429 schedule(() { | |
| 430 var descriptor = new d.DirectoryDescriptor.fromFilesystem( | |
| 431 "descriptor", path.join(sandbox, 'dir')); | |
| 432 expect(descriptor, isDirectoryDescriptor('descriptor', [ | |
| 433 isDirectoryDescriptor('subdir', [ | |
| 434 isFileDescriptor('subfile1.txt', 'subcontents1'), | |
| 435 isFileDescriptor('subfile2.txt', 'subcontents2') | |
| 436 ]), | |
| 437 isFileDescriptor('file1.txt', 'contents1'), | |
| 438 isFileDescriptor('file2.txt', 'contents2') | |
| 439 ])); | |
| 440 }); | |
| 441 }); | |
| 442 | |
| 443 expectTestPasses("new DirectoryDescriptor().fromFilesystem ignores hidden " | |
| 444 "files", () { | |
| 445 scheduleSandbox(); | |
| 446 | |
| 447 d.dir('dir', [ | |
| 448 d.dir('subdir', [ | |
| 449 d.file('subfile1.txt', 'subcontents1'), | |
| 450 d.file('.hidden', 'subcontents2') | |
| 451 ]), | |
| 452 d.file('file1.txt', 'contents1'), | |
| 453 d.file('.DS_Store', 'contents2') | |
| 454 ]).create(); | |
| 455 | |
| 456 schedule(() { | |
| 457 var descriptor = new d.DirectoryDescriptor.fromFilesystem( | |
| 458 "descriptor", path.join(sandbox, 'dir')); | |
| 459 expect(descriptor, isDirectoryDescriptor('descriptor', [ | |
| 460 isDirectoryDescriptor('subdir', [ | |
| 461 isFileDescriptor('subfile1.txt', 'subcontents1') | |
| 462 ]), | |
| 463 isFileDescriptor('file1.txt', 'contents1') | |
| 464 ])); | |
| 465 }); | |
| 466 }); | |
| 467 } | |
| OLD | NEW |