| 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 library descriptor_test; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:io'; | |
| 9 | |
| 10 import 'package:pathos/path.dart' as path; | |
| 11 import 'package:scheduled_test/descriptor.dart' as d; | |
| 12 import 'package:scheduled_test/scheduled_test.dart'; | |
| 13 | |
| 14 import 'metatest.dart'; | |
| 15 import 'utils.dart'; | |
| 16 | |
| 17 String sandbox; | |
| 18 | |
| 19 void main() { | |
| 20 metaSetUp(() { | |
| 21 // TODO(nweiz): We used to only increase the timeout to 10s for the Windows | |
| 22 // bots, but the Linux and Mac bots have started taking upwards of 5s when | |
| 23 // running pumpEventQueue, so we're increasing the timeout across the board | |
| 24 // (see issue 9248). | |
| 25 currentSchedule.timeout = new Duration(seconds: 10); | |
| 26 }); | |
| 27 | |
| 28 expectTestsPass('file().create() creates a file', () { | |
| 29 test('test', () { | |
| 30 scheduleSandbox(); | |
| 31 | |
| 32 d.file('name.txt', 'contents').create(); | |
| 33 | |
| 34 schedule(() { | |
| 35 expect(new File(path.join(sandbox, 'name.txt')).readAsString(), | |
| 36 completion(equals('contents'))); | |
| 37 }); | |
| 38 }); | |
| 39 }); | |
| 40 | |
| 41 expectTestsPass('file().create() overwrites an existing file', () { | |
| 42 test('test', () { | |
| 43 scheduleSandbox(); | |
| 44 | |
| 45 d.file('name.txt', 'contents1').create(); | |
| 46 | |
| 47 d.file('name.txt', 'contents2').create(); | |
| 48 | |
| 49 schedule(() { | |
| 50 expect(new File(path.join(sandbox, 'name.txt')).readAsString(), | |
| 51 completion(equals('contents2'))); | |
| 52 }); | |
| 53 }); | |
| 54 }); | |
| 55 | |
| 56 expectTestsPass('file().validate() completes successfully if the filesystem ' | |
| 57 'matches the descriptor', () { | |
| 58 test('test', () { | |
| 59 scheduleSandbox(); | |
| 60 | |
| 61 schedule(() { | |
| 62 return new File(path.join(sandbox, 'name.txt')) | |
| 63 .writeAsString('contents'); | |
| 64 }); | |
| 65 | |
| 66 d.file('name.txt', 'contents').validate(); | |
| 67 }); | |
| 68 }); | |
| 69 | |
| 70 expectTestsPass("file().validate() fails if there's a file with the wrong " | |
| 71 "contents", () { | |
| 72 var errors; | |
| 73 test('test 1', () { | |
| 74 scheduleSandbox(); | |
| 75 | |
| 76 currentSchedule.onException.schedule(() { | |
| 77 errors = currentSchedule.errors; | |
| 78 }); | |
| 79 | |
| 80 schedule(() { | |
| 81 return new File(path.join(sandbox, 'name.txt')) | |
| 82 .writeAsString('wrongtents'); | |
| 83 }); | |
| 84 | |
| 85 d.file('name.txt', 'contents').validate(); | |
| 86 }); | |
| 87 | |
| 88 test('test 2', () { | |
| 89 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 90 expect(errors.map((e) => e.error), equals([ | |
| 91 "File 'name.txt' should contain:\n" | |
| 92 "| contents\n" | |
| 93 "but actually contained:\n" | |
| 94 "X wrongtents" | |
| 95 ]), verbose: true); | |
| 96 }); | |
| 97 }, passing: ['test 2']); | |
| 98 | |
| 99 expectTestsPass("file().validate() fails if there's no file", () { | |
| 100 var errors; | |
| 101 test('test 1', () { | |
| 102 scheduleSandbox(); | |
| 103 | |
| 104 currentSchedule.onException.schedule(() { | |
| 105 errors = currentSchedule.errors; | |
| 106 }); | |
| 107 | |
| 108 d.file('name.txt', 'contents').validate(); | |
| 109 }); | |
| 110 | |
| 111 test('test 2', () { | |
| 112 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 113 expect(errors.length, equals(1)); | |
| 114 expect(errors.first.error, | |
| 115 matches(r"^File not found: '[^']+[\\/]name\.txt'\.$")); | |
| 116 }); | |
| 117 }, passing: ['test 2']); | |
| 118 | |
| 119 expectTestsPass("file().read() returns the contents of the file as a stream", | |
| 120 () { | |
| 121 test('test', () { | |
| 122 expect(byteStreamToString(d.file('name.txt', 'contents').read()), | |
| 123 completion(equals('contents'))); | |
| 124 }); | |
| 125 }); | |
| 126 | |
| 127 expectTestsPass("file().load() throws an error", () { | |
| 128 test('test', () { | |
| 129 expect(d.file('name.txt', 'contents').load('path').toList(), | |
| 130 throwsA(equals("Can't load 'path' from within 'name.txt': not a " | |
| 131 "directory."))); | |
| 132 }); | |
| 133 }); | |
| 134 | |
| 135 expectTestsPass("file().describe() returns the filename", () { | |
| 136 test('test', () { | |
| 137 expect(d.file('name.txt', 'contents').describe(), equals('name.txt')); | |
| 138 }); | |
| 139 }); | |
| 140 | |
| 141 expectTestsPass('binaryFile().create() creates a file', () { | |
| 142 test('test', () { | |
| 143 scheduleSandbox(); | |
| 144 | |
| 145 d.binaryFile('name.bin', [1, 2, 3, 4, 5]).create(); | |
| 146 | |
| 147 schedule(() { | |
| 148 expect(new File(path.join(sandbox, 'name.bin')).readAsBytes(), | |
| 149 completion(equals([1, 2, 3, 4, 5]))); | |
| 150 }); | |
| 151 }); | |
| 152 }); | |
| 153 | |
| 154 expectTestsPass('binaryFile().validate() completes successfully if the ' | |
| 155 'filesystem matches the descriptor', () { | |
| 156 test('test', () { | |
| 157 scheduleSandbox(); | |
| 158 | |
| 159 schedule(() { | |
| 160 return new File(path.join(sandbox, 'name.bin')) | |
| 161 .writeAsBytes([1, 2, 3, 4, 5]); | |
| 162 }); | |
| 163 | |
| 164 d.binaryFile('name.bin', [1, 2, 3, 4, 5]).validate(); | |
| 165 }); | |
| 166 }); | |
| 167 | |
| 168 expectTestsPass("binaryFile().validate() fails if there's a file with the " | |
| 169 "wrong contents", () { | |
| 170 var errors; | |
| 171 test('test 1', () { | |
| 172 scheduleSandbox(); | |
| 173 | |
| 174 currentSchedule.onException.schedule(() { | |
| 175 errors = currentSchedule.errors; | |
| 176 }); | |
| 177 | |
| 178 schedule(() { | |
| 179 return new File(path.join(sandbox, 'name.bin')) | |
| 180 .writeAsBytes([2, 4, 6, 8, 10]); | |
| 181 }); | |
| 182 | |
| 183 d.binaryFile('name.bin', [1, 2, 3, 4, 5]).validate(); | |
| 184 }); | |
| 185 | |
| 186 test('test 2', () { | |
| 187 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 188 expect(errors.map((e) => e.error), equals([ | |
| 189 "File 'name.bin' didn't contain the expected binary data." | |
| 190 ]), verbose: true); | |
| 191 }); | |
| 192 }, passing: ['test 2']); | |
| 193 | |
| 194 expectTestsPass("directory().create() creates a directory and its contents", | |
| 195 () { | |
| 196 test('test', () { | |
| 197 scheduleSandbox(); | |
| 198 | |
| 199 d.dir('dir', [ | |
| 200 d.dir('subdir', [ | |
| 201 d.file('subfile1.txt', 'subcontents1'), | |
| 202 d.file('subfile2.txt', 'subcontents2') | |
| 203 ]), | |
| 204 d.file('file1.txt', 'contents1'), | |
| 205 d.file('file2.txt', 'contents2') | |
| 206 ]).create(); | |
| 207 | |
| 208 schedule(() { | |
| 209 expect(new File(path.join(sandbox, 'dir', 'file1.txt')).readAsString(), | |
| 210 completion(equals('contents1'))); | |
| 211 expect(new File(path.join(sandbox, 'dir', 'file2.txt')).readAsString(), | |
| 212 completion(equals('contents2'))); | |
| 213 expect(new File(path.join(sandbox, 'dir', 'subdir', 'subfile1.txt')) | |
| 214 .readAsString(), | |
| 215 completion(equals('subcontents1'))); | |
| 216 expect(new File(path.join(sandbox, 'dir', 'subdir', 'subfile2.txt')) | |
| 217 .readAsString(), | |
| 218 completion(equals('subcontents2'))); | |
| 219 }); | |
| 220 }); | |
| 221 }); | |
| 222 | |
| 223 expectTestsPass("directory().create() works if the directory already exists", | |
| 224 () { | |
| 225 test('test', () { | |
| 226 scheduleSandbox(); | |
| 227 | |
| 228 d.dir('dir').create(); | |
| 229 d.dir('dir', [d.file('name.txt', 'contents')]).create(); | |
| 230 | |
| 231 schedule(() { | |
| 232 expect(new File(path.join(sandbox, 'dir', 'name.txt')).readAsString(), | |
| 233 completion(equals('contents'))); | |
| 234 }); | |
| 235 }); | |
| 236 }); | |
| 237 | |
| 238 expectTestsPass("directory().validate() completes successfully if the " | |
| 239 "filesystem matches the descriptor", () { | |
| 240 test('test', () { | |
| 241 scheduleSandbox(); | |
| 242 | |
| 243 schedule(() { | |
| 244 var dirPath = path.join(sandbox, 'dir'); | |
| 245 var subdirPath = path.join(dirPath, 'subdir'); | |
| 246 return new Directory(subdirPath).create(recursive: true).then((_) { | |
| 247 return Future.wait([ | |
| 248 new File(path.join(dirPath, 'file1.txt')) | |
| 249 .writeAsString('contents1'), | |
| 250 new File(path.join(dirPath, 'file2.txt')) | |
| 251 .writeAsString('contents2'), | |
| 252 new File(path.join(subdirPath, 'subfile1.txt')) | |
| 253 .writeAsString('subcontents1'), | |
| 254 new File(path.join(subdirPath, 'subfile2.txt')) | |
| 255 .writeAsString('subcontents2') | |
| 256 ]); | |
| 257 }); | |
| 258 }); | |
| 259 | |
| 260 d.dir('dir', [ | |
| 261 d.dir('subdir', [ | |
| 262 d.file('subfile1.txt', 'subcontents1'), | |
| 263 d.file('subfile2.txt', 'subcontents2') | |
| 264 ]), | |
| 265 d.file('file1.txt', 'contents1'), | |
| 266 d.file('file2.txt', 'contents2') | |
| 267 ]).validate(); | |
| 268 }); | |
| 269 }); | |
| 270 | |
| 271 expectTestsPass("directory().validate() fails if a directory isn't found" | |
| 272 , () { | |
| 273 var errors; | |
| 274 test('test 1', () { | |
| 275 scheduleSandbox(); | |
| 276 | |
| 277 currentSchedule.onException.schedule(() { | |
| 278 errors = currentSchedule.errors; | |
| 279 }); | |
| 280 | |
| 281 schedule(() { | |
| 282 var dirPath = path.join(sandbox, 'dir'); | |
| 283 return new Directory(dirPath).create().then((_) { | |
| 284 return Future.wait([ | |
| 285 new File(path.join(dirPath, 'file1.txt')) | |
| 286 .writeAsString('contents1'), | |
| 287 new File(path.join(dirPath, 'file2.txt')) | |
| 288 .writeAsString('contents2') | |
| 289 ]); | |
| 290 }); | |
| 291 }); | |
| 292 | |
| 293 d.dir('dir', [ | |
| 294 d.dir('subdir', [ | |
| 295 d.file('subfile1.txt', 'subcontents1'), | |
| 296 d.file('subfile2.txt', 'subcontents2') | |
| 297 ]), | |
| 298 d.file('file1.txt', 'contents1'), | |
| 299 d.file('file2.txt', 'contents2') | |
| 300 ]).validate(); | |
| 301 }); | |
| 302 | |
| 303 test('test 2', () { | |
| 304 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 305 expect(errors.length, equals(1)); | |
| 306 expect(errors.first.error.toString(), | |
| 307 matches(r"^Directory not found: '[^']+[\\/]dir[\\/]subdir'\.$")); | |
| 308 }); | |
| 309 }, passing: ['test 2']); | |
| 310 | |
| 311 expectTestsPass("directory().validate() fails if a file isn't found", () { | |
| 312 var errors; | |
| 313 test('test 1', () { | |
| 314 scheduleSandbox(); | |
| 315 | |
| 316 currentSchedule.onException.schedule(() { | |
| 317 errors = currentSchedule.errors; | |
| 318 }); | |
| 319 | |
| 320 schedule(() { | |
| 321 var dirPath = path.join(sandbox, 'dir'); | |
| 322 var subdirPath = path.join(dirPath, 'subdir'); | |
| 323 return new Directory(subdirPath).create(recursive: true).then((_) { | |
| 324 return Future.wait([ | |
| 325 new File(path.join(dirPath, 'file1.txt')) | |
| 326 .writeAsString('contents1'), | |
| 327 new File(path.join(subdirPath, 'subfile1.txt')) | |
| 328 .writeAsString('subcontents1'), | |
| 329 new File(path.join(subdirPath, 'subfile2.txt')) | |
| 330 .writeAsString('subcontents2') | |
| 331 ]); | |
| 332 }); | |
| 333 }); | |
| 334 | |
| 335 d.dir('dir', [ | |
| 336 d.dir('subdir', [ | |
| 337 d.file('subfile1.txt', 'subcontents1'), | |
| 338 d.file('subfile2.txt', 'subcontents2') | |
| 339 ]), | |
| 340 d.file('file1.txt', 'contents1'), | |
| 341 d.file('file2.txt', 'contents2') | |
| 342 ]).validate(); | |
| 343 }); | |
| 344 | |
| 345 test('test 2', () { | |
| 346 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 347 expect(errors.length, equals(1)); | |
| 348 expect(errors.first.error.toString(), | |
| 349 matches(r"^File not found: '[^']+[\\/]dir[\\/]file2\.txt'\.$")); | |
| 350 }); | |
| 351 }, passing: ['test 2']); | |
| 352 | |
| 353 expectTestsPass("directory().validate() fails if multiple children aren't " | |
| 354 "found or have the wrong contents", () { | |
| 355 var errors; | |
| 356 test('test 1', () { | |
| 357 scheduleSandbox(); | |
| 358 | |
| 359 currentSchedule.onException.schedule(() { | |
| 360 errors = currentSchedule.errors; | |
| 361 }); | |
| 362 | |
| 363 schedule(() { | |
| 364 var dirPath = path.join(sandbox, 'dir'); | |
| 365 var subdirPath = path.join(dirPath, 'subdir'); | |
| 366 return new Directory(subdirPath).create(recursive: true).then((_) { | |
| 367 return Future.wait([ | |
| 368 new File(path.join(dirPath, 'file1.txt')) | |
| 369 .writeAsString('contents1'), | |
| 370 new File(path.join(subdirPath, 'subfile2.txt')) | |
| 371 .writeAsString('subwrongtents2') | |
| 372 ]); | |
| 373 }); | |
| 374 }); | |
| 375 | |
| 376 d.dir('dir', [ | |
| 377 d.dir('subdir', [ | |
| 378 d.file('subfile1.txt', 'subcontents1'), | |
| 379 d.file('subfile2.txt', 'subcontents2') | |
| 380 ]), | |
| 381 d.file('file1.txt', 'contents1'), | |
| 382 d.file('file2.txt', 'contents2') | |
| 383 ]).validate(); | |
| 384 }); | |
| 385 | |
| 386 test('test 2', () { | |
| 387 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 388 expect(errors.length, equals(1)); | |
| 389 expect(errors.first.error.toString(), matches( | |
| 390 r"^\* File not found: '[^']+[\\/]dir[\\/]subdir[\\/]subfile1\.txt'\." | |
| 391 r"\n" | |
| 392 r"\* File 'subfile2\.txt' should contain:\n" | |
| 393 r" \| subcontents2\n" | |
| 394 r" but actually contained:\n" | |
| 395 r" X subwrongtents2\n" | |
| 396 r"\* File not found: '[^']+[\\/]dir[\\/]file2\.txt'\.$")); | |
| 397 }); | |
| 398 }, passing: ['test 2']); | |
| 399 | |
| 400 expectTestsPass("directory().validate() fails if a file has the wrong " | |
| 401 "contents", () { | |
| 402 var errors; | |
| 403 test('test 1', () { | |
| 404 scheduleSandbox(); | |
| 405 | |
| 406 currentSchedule.onException.schedule(() { | |
| 407 errors = currentSchedule.errors; | |
| 408 }); | |
| 409 | |
| 410 schedule(() { | |
| 411 var dirPath = path.join(sandbox, 'dir'); | |
| 412 var subdirPath = path.join(dirPath, 'subdir'); | |
| 413 return new Directory(subdirPath).create(recursive: true).then((_) { | |
| 414 return Future.wait([ | |
| 415 new File(path.join(dirPath, 'file1.txt')) | |
| 416 .writeAsString('contents1'), | |
| 417 new File(path.join(dirPath, 'file2.txt')) | |
| 418 .writeAsString('contents2'), | |
| 419 new File(path.join(subdirPath, 'subfile1.txt')) | |
| 420 .writeAsString('wrongtents1'), | |
| 421 new File(path.join(subdirPath, 'subfile2.txt')) | |
| 422 .writeAsString('subcontents2') | |
| 423 ]); | |
| 424 }); | |
| 425 }); | |
| 426 | |
| 427 d.dir('dir', [ | |
| 428 d.dir('subdir', [ | |
| 429 d.file('subfile1.txt', 'subcontents1'), | |
| 430 d.file('subfile2.txt', 'subcontents2') | |
| 431 ]), | |
| 432 d.file('file1.txt', 'contents1'), | |
| 433 d.file('file2.txt', 'contents2') | |
| 434 ]).validate(); | |
| 435 }); | |
| 436 | |
| 437 test('test 2', () { | |
| 438 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 439 expect(errors.map((e) => e.error.toString()), equals([ | |
| 440 "File 'subfile1.txt' should contain:\n" | |
| 441 "| subcontents1\n" | |
| 442 "but actually contained:\n" | |
| 443 "X wrongtents1" | |
| 444 ])); | |
| 445 }); | |
| 446 }, passing: ['test 2']); | |
| 447 | |
| 448 expectTestsPass("directory().load() loads a file", () { | |
| 449 test('test', () { | |
| 450 var dir = d.dir('dir', [d.file('name.txt', 'contents')]); | |
| 451 expect(byteStreamToString(dir.load('name.txt')), | |
| 452 completion(equals('contents'))); | |
| 453 }); | |
| 454 }); | |
| 455 | |
| 456 expectTestsPass("directory().load() loads a deeply-nested file", () { | |
| 457 test('test', () { | |
| 458 var dir = d.dir('dir', [ | |
| 459 d.dir('subdir', [ | |
| 460 d.file('name.txt', 'subcontents') | |
| 461 ]), | |
| 462 d.file('name.txt', 'contents') | |
| 463 ]); | |
| 464 | |
| 465 expect(byteStreamToString(dir.load('subdir/name.txt')), | |
| 466 completion(equals('subcontents'))); | |
| 467 }); | |
| 468 }); | |
| 469 | |
| 470 expectTestsPass("directory().read() fails", () { | |
| 471 test('test', () { | |
| 472 var dir = d.dir('dir', [d.file('name.txt', 'contents')]); | |
| 473 expect(dir.read().toList(), | |
| 474 throwsA(equals("Can't read the contents of 'dir': is a directory."))); | |
| 475 }); | |
| 476 }); | |
| 477 | |
| 478 expectTestsPass("directory().load() fails to load a nested directory", () { | |
| 479 test('test', () { | |
| 480 var dir = d.dir('dir', [ | |
| 481 d.dir('subdir', [ | |
| 482 d.file('name.txt', 'subcontents') | |
| 483 ]), | |
| 484 d.file('name.txt', 'contents') | |
| 485 ]); | |
| 486 | |
| 487 expect(dir.load('subdir').toList(), | |
| 488 throwsA(equals("Can't read the contents of 'subdir': is a " | |
| 489 "directory."))); | |
| 490 }); | |
| 491 }); | |
| 492 | |
| 493 expectTestsPass("directory().load() fails to load an absolute path", () { | |
| 494 test('test', () { | |
| 495 var dir = d.dir('dir', [d.file('name.txt', 'contents')]); | |
| 496 | |
| 497 expect(dir.load('/name.txt').toList(), | |
| 498 throwsA(equals("Can't load absolute path '/name.txt'."))); | |
| 499 }); | |
| 500 }); | |
| 501 | |
| 502 expectTestsPass("directory().load() fails to load '.', '..', or ''", () { | |
| 503 test('test', () { | |
| 504 var dir = d.dir('dir', [d.file('name.txt', 'contents')]); | |
| 505 | |
| 506 expect(dir.load('.').toList(), | |
| 507 throwsA(equals("Can't load '.' from within 'dir'."))); | |
| 508 | |
| 509 expect(dir.load('..').toList(), | |
| 510 throwsA(equals("Can't load '..' from within 'dir'."))); | |
| 511 | |
| 512 expect(dir.load('').toList(), | |
| 513 throwsA(equals("Can't load '' from within 'dir'."))); | |
| 514 }); | |
| 515 }); | |
| 516 | |
| 517 expectTestsPass("directory().load() fails to load a file that doesn't exist", | |
| 518 () { | |
| 519 test('test', () { | |
| 520 var dir = d.dir('dir', [d.file('name.txt', 'contents')]); | |
| 521 | |
| 522 expect(dir.load('not-name.txt').toList(), | |
| 523 throwsA(equals("Couldn't find an entry named 'not-name.txt' within " | |
| 524 "'dir'."))); | |
| 525 }); | |
| 526 }); | |
| 527 | |
| 528 expectTestsPass("directory().load() fails to load a file that exists " | |
| 529 "multiple times", () { | |
| 530 test('test', () { | |
| 531 var dir = d.dir('dir', [ | |
| 532 d.file('name.txt', 'contents'), | |
| 533 d.file('name.txt', 'contents') | |
| 534 ]); | |
| 535 | |
| 536 expect(dir.load('name.txt').toList(), | |
| 537 throwsA(equals("Found multiple entries named 'name.txt' within " | |
| 538 "'dir'."))); | |
| 539 }); | |
| 540 }); | |
| 541 | |
| 542 expectTestsPass("directory().describe() lists the contents of the directory", | |
| 543 () { | |
| 544 test('test', () { | |
| 545 var dir = d.dir('dir', [ | |
| 546 d.file('file1.txt', 'contents1'), | |
| 547 d.file('file2.txt', 'contents2') | |
| 548 ]); | |
| 549 | |
| 550 expect(dir.describe(), equals( | |
| 551 "dir\n" | |
| 552 "|-- file1.txt\n" | |
| 553 "'-- file2.txt")); | |
| 554 }); | |
| 555 }); | |
| 556 | |
| 557 expectTestsPass("directory().describe() lists the contents of nested " | |
| 558 "directories", () { | |
| 559 test('test', () { | |
| 560 var dir = d.dir('dir', [ | |
| 561 d.file('file1.txt', 'contents1'), | |
| 562 d.dir('subdir', [ | |
| 563 d.file('subfile1.txt', 'subcontents1'), | |
| 564 d.file('subfile2.txt', 'subcontents2'), | |
| 565 d.dir('subsubdir', [ | |
| 566 d.file('subsubfile.txt', 'subsubcontents') | |
| 567 ]) | |
| 568 ]), | |
| 569 d.file('file2.txt', 'contents2') | |
| 570 ]); | |
| 571 | |
| 572 expect(dir.describe(), equals( | |
| 573 "dir\n" | |
| 574 "|-- file1.txt\n" | |
| 575 "|-- subdir\n" | |
| 576 "| |-- subfile1.txt\n" | |
| 577 "| |-- subfile2.txt\n" | |
| 578 "| '-- subsubdir\n" | |
| 579 "| '-- subsubfile.txt\n" | |
| 580 "'-- file2.txt")); | |
| 581 }); | |
| 582 }); | |
| 583 | |
| 584 expectTestsPass("directory().describe() with no contents returns the " | |
| 585 "directory name", () { | |
| 586 test('test', () { | |
| 587 expect(d.dir('dir').describe(), equals('dir')); | |
| 588 }); | |
| 589 }); | |
| 590 | |
| 591 expectTestsPass("async().create() forwards to file().create", () { | |
| 592 test('test', () { | |
| 593 scheduleSandbox(); | |
| 594 | |
| 595 d.async(pumpEventQueue().then((_) { | |
| 596 return d.file('name.txt', 'contents'); | |
| 597 })).create(); | |
| 598 | |
| 599 d.file('name.txt', 'contents').validate(); | |
| 600 }); | |
| 601 }); | |
| 602 | |
| 603 expectTestsPass("async().create() forwards to directory().create", () { | |
| 604 test('test', () { | |
| 605 scheduleSandbox(); | |
| 606 | |
| 607 d.async(pumpEventQueue().then((_) { | |
| 608 return d.dir('dir', [ | |
| 609 d.file('file1.txt', 'contents1'), | |
| 610 d.file('file2.txt', 'contents2') | |
| 611 ]); | |
| 612 })).create(); | |
| 613 | |
| 614 d.dir('dir', [ | |
| 615 d.file('file1.txt', 'contents1'), | |
| 616 d.file('file2.txt', 'contents2') | |
| 617 ]).validate(); | |
| 618 }); | |
| 619 }); | |
| 620 | |
| 621 expectTestsPass("async().validate() forwards to file().validate", () { | |
| 622 test('test', () { | |
| 623 scheduleSandbox(); | |
| 624 | |
| 625 d.file('name.txt', 'contents').create(); | |
| 626 | |
| 627 d.async(pumpEventQueue().then((_) { | |
| 628 return d.file('name.txt', 'contents'); | |
| 629 })).validate(); | |
| 630 }); | |
| 631 }); | |
| 632 | |
| 633 expectTestsPass("async().validate() fails if file().validate fails", () { | |
| 634 var errors; | |
| 635 test('test 1', () { | |
| 636 scheduleSandbox(); | |
| 637 | |
| 638 currentSchedule.onException.schedule(() { | |
| 639 errors = currentSchedule.errors; | |
| 640 }); | |
| 641 | |
| 642 d.async(pumpEventQueue().then((_) { | |
| 643 return d.file('name.txt', 'contents'); | |
| 644 })).validate(); | |
| 645 }); | |
| 646 | |
| 647 test('test 2', () { | |
| 648 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 649 expect(errors.length, equals(1)); | |
| 650 expect(errors.first.error, | |
| 651 matches(r"^File not found: '[^']+[\\/]name\.txt'\.$")); | |
| 652 }); | |
| 653 }, passing: ['test 2']); | |
| 654 | |
| 655 expectTestsPass("async().validate() forwards to directory().validate", () { | |
| 656 test('test', () { | |
| 657 scheduleSandbox(); | |
| 658 | |
| 659 d.dir('dir', [ | |
| 660 d.file('file1.txt', 'contents1'), | |
| 661 d.file('file2.txt', 'contents2') | |
| 662 ]).create(); | |
| 663 | |
| 664 d.async(pumpEventQueue().then((_) { | |
| 665 return d.dir('dir', [ | |
| 666 d.file('file1.txt', 'contents1'), | |
| 667 d.file('file2.txt', 'contents2') | |
| 668 ]); | |
| 669 })).validate(); | |
| 670 }); | |
| 671 }); | |
| 672 | |
| 673 expectTestsPass("async().create() fails if directory().create fails", () { | |
| 674 var errors; | |
| 675 test('test 1', () { | |
| 676 scheduleSandbox(); | |
| 677 | |
| 678 currentSchedule.onException.schedule(() { | |
| 679 errors = currentSchedule.errors; | |
| 680 }); | |
| 681 | |
| 682 d.async(pumpEventQueue().then((_) { | |
| 683 return d.dir('dir', [ | |
| 684 d.file('file1.txt', 'contents1'), | |
| 685 d.file('file2.txt', 'contents2') | |
| 686 ]); | |
| 687 })).validate(); | |
| 688 }); | |
| 689 | |
| 690 test('test 2', () { | |
| 691 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 692 expect(errors.length, equals(1)); | |
| 693 expect(errors.first.error, | |
| 694 matches(r"^Directory not found: '[^']+[\\/]dir'\.$")); | |
| 695 }); | |
| 696 }, passing: ['test 2']); | |
| 697 | |
| 698 expectTestsPass("async().load() fails", () { | |
| 699 test('test', () { | |
| 700 scheduleSandbox(); | |
| 701 | |
| 702 expect(d.async(new Future.immediate(d.file('name.txt'))) | |
| 703 .load('path').toList(), | |
| 704 throwsA(equals("AsyncDescriptors don't support load()."))); | |
| 705 }); | |
| 706 }); | |
| 707 | |
| 708 expectTestsPass("async().read() fails", () { | |
| 709 test('test', () { | |
| 710 scheduleSandbox(); | |
| 711 | |
| 712 expect(d.async(new Future.immediate(d.file('name.txt'))).read().toList(), | |
| 713 throwsA(equals("AsyncDescriptors don't support read()."))); | |
| 714 }); | |
| 715 }); | |
| 716 | |
| 717 expectTestsPass("nothing().create() does nothing", () { | |
| 718 test('test', () { | |
| 719 scheduleSandbox(); | |
| 720 | |
| 721 d.nothing('foo').create(); | |
| 722 | |
| 723 schedule(() { | |
| 724 expect(new File(path.join(sandbox, 'foo')).exists(), | |
| 725 completion(isFalse)); | |
| 726 }); | |
| 727 | |
| 728 schedule(() { | |
| 729 expect(new Directory(path.join(sandbox, 'foo')).exists(), | |
| 730 completion(isFalse)); | |
| 731 }); | |
| 732 }); | |
| 733 }); | |
| 734 | |
| 735 expectTestsPass("nothing().validate() succeeds if nothing's there", () { | |
| 736 test('test', () { | |
| 737 scheduleSandbox(); | |
| 738 | |
| 739 d.nothing('foo').validate(); | |
| 740 }); | |
| 741 }); | |
| 742 | |
| 743 expectTestsPass("nothing().validate() fails if there's a file", () { | |
| 744 var errors; | |
| 745 test('test 1', () { | |
| 746 scheduleSandbox(); | |
| 747 | |
| 748 currentSchedule.onException.schedule(() { | |
| 749 errors = currentSchedule.errors; | |
| 750 }); | |
| 751 | |
| 752 d.file('name.txt', 'contents').create(); | |
| 753 d.nothing('name.txt').validate(); | |
| 754 }); | |
| 755 | |
| 756 test('test 2', () { | |
| 757 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 758 expect(errors.length, equals(1)); | |
| 759 expect(errors.first.error, | |
| 760 matches(r"^Expected nothing to exist at '[^']+[\\/]name.txt', but " | |
| 761 r"found a file\.$")); | |
| 762 }); | |
| 763 }, passing: ['test 2']); | |
| 764 | |
| 765 expectTestsPass("nothing().validate() fails if there's a directory", () { | |
| 766 var errors; | |
| 767 test('test 1', () { | |
| 768 scheduleSandbox(); | |
| 769 | |
| 770 currentSchedule.onException.schedule(() { | |
| 771 errors = currentSchedule.errors; | |
| 772 }); | |
| 773 | |
| 774 d.dir('dir').create(); | |
| 775 d.nothing('dir').validate(); | |
| 776 }); | |
| 777 | |
| 778 test('test 2', () { | |
| 779 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 780 expect(errors.length, equals(1)); | |
| 781 expect(errors.first.error, | |
| 782 matches(r"^Expected nothing to exist at '[^']+[\\/]dir', but found a " | |
| 783 r"directory\.$")); | |
| 784 }); | |
| 785 }, passing: ['test 2']); | |
| 786 | |
| 787 expectTestsPass("nothing().load() fails", () { | |
| 788 test('test', () { | |
| 789 scheduleSandbox(); | |
| 790 | |
| 791 expect(d.nothing('name.txt').load('path').toList(), | |
| 792 throwsA(equals("Nothing descriptors don't support load()."))); | |
| 793 }); | |
| 794 }); | |
| 795 | |
| 796 expectTestsPass("nothing().read() fails", () { | |
| 797 test('test', () { | |
| 798 scheduleSandbox(); | |
| 799 | |
| 800 expect(d.nothing('name.txt').read().toList(), | |
| 801 throwsA(equals("Nothing descriptors don't support read()."))); | |
| 802 }); | |
| 803 }); | |
| 804 | |
| 805 expectTestsPass("pattern().validate() succeeds if there's a file matching " | |
| 806 "the pattern and the child entry", () { | |
| 807 test('test', () { | |
| 808 scheduleSandbox(); | |
| 809 | |
| 810 d.file('foo', 'blap').create(); | |
| 811 | |
| 812 d.filePattern(new RegExp(r'f..'), 'blap').validate(); | |
| 813 }); | |
| 814 }); | |
| 815 | |
| 816 expectTestsPass("pattern().validate() succeeds if there's a dir matching " | |
| 817 "the pattern and the child entry", () { | |
| 818 test('test', () { | |
| 819 scheduleSandbox(); | |
| 820 | |
| 821 d.dir('foo', [ | |
| 822 d.file('bar', 'baz') | |
| 823 ]).create(); | |
| 824 | |
| 825 d.dirPattern(new RegExp(r'f..'), [ | |
| 826 d.file('bar', 'baz') | |
| 827 ]).validate(); | |
| 828 }); | |
| 829 }); | |
| 830 | |
| 831 expectTestsPass("pattern().validate() succeeds if there's multiple files " | |
| 832 "matching the pattern but only one matching the child entry", () { | |
| 833 test('test', () { | |
| 834 scheduleSandbox(); | |
| 835 | |
| 836 d.file('foo', 'blap').create(); | |
| 837 d.file('fee', 'blak').create(); | |
| 838 d.file('faa', 'blut').create(); | |
| 839 | |
| 840 d.filePattern(new RegExp(r'f..'), 'blap').validate(); | |
| 841 }); | |
| 842 }); | |
| 843 | |
| 844 expectTestsPass("pattern().validate() fails if there's no file matching the " | |
| 845 "pattern", () { | |
| 846 var errors; | |
| 847 test('test 1', () { | |
| 848 scheduleSandbox(); | |
| 849 | |
| 850 currentSchedule.onException.schedule(() { | |
| 851 errors = currentSchedule.errors; | |
| 852 }); | |
| 853 | |
| 854 d.filePattern(new RegExp(r'f..'), 'bar').validate(); | |
| 855 }); | |
| 856 | |
| 857 test('test 2', () { | |
| 858 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 859 expect(errors.length, equals(1)); | |
| 860 expect(errors.first.error, | |
| 861 matches(r"^No entry found in '[^']+' matching /f\.\./\.$")); | |
| 862 }); | |
| 863 }, passing: ['test 2']); | |
| 864 | |
| 865 expectTestsPass("pattern().validate() fails if there's a file matching the " | |
| 866 "pattern but not the entry", () { | |
| 867 var errors; | |
| 868 test('test 1', () { | |
| 869 scheduleSandbox(); | |
| 870 | |
| 871 currentSchedule.onException.schedule(() { | |
| 872 errors = currentSchedule.errors; | |
| 873 }); | |
| 874 | |
| 875 d.file('foo', 'bap').create(); | |
| 876 d.filePattern(new RegExp(r'f..'), 'bar').validate(); | |
| 877 }); | |
| 878 | |
| 879 test('test 2', () { | |
| 880 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 881 expect(errors.length, equals(1)); | |
| 882 expect(errors.first.error, | |
| 883 matches(r"^Caught error\n" | |
| 884 r"| File 'foo' should contain:\n" | |
| 885 r"| | bar\n" | |
| 886 r"| but actually contained:\n" | |
| 887 r"| X bap\n" | |
| 888 r"while validating\n" | |
| 889 r"| foo$")); | |
| 890 }); | |
| 891 }, passing: ['test 2']); | |
| 892 | |
| 893 expectTestsPass("pattern().validate() fails if there's a dir matching the " | |
| 894 "pattern but not the entry", () { | |
| 895 var errors; | |
| 896 test('test 1', () { | |
| 897 scheduleSandbox(); | |
| 898 | |
| 899 currentSchedule.onException.schedule(() { | |
| 900 errors = currentSchedule.errors; | |
| 901 }); | |
| 902 | |
| 903 d.dir('foo', [ | |
| 904 d.file('bar', 'bap') | |
| 905 ]).create(); | |
| 906 | |
| 907 d.dirPattern(new RegExp(r'f..'), [ | |
| 908 d.file('bar', 'baz') | |
| 909 ]).validate(); | |
| 910 }); | |
| 911 | |
| 912 test('test 2', () { | |
| 913 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 914 expect(errors.length, equals(1)); | |
| 915 expect(errors.first.error, | |
| 916 matches(r"^Caught error\n" | |
| 917 r"| File 'bar' should contain:\n" | |
| 918 r"| | baz\n" | |
| 919 r"| but actually contained:\n" | |
| 920 r"| X bap" | |
| 921 r"while validating\n" | |
| 922 r"| foo\n" | |
| 923 r"| '-- bar$")); | |
| 924 }); | |
| 925 }, passing: ['test 2']); | |
| 926 | |
| 927 expectTestsPass("pattern().validate() fails if there's multiple files " | |
| 928 "matching the pattern and the child entry", () { | |
| 929 var errors; | |
| 930 test('test 1', () { | |
| 931 scheduleSandbox(); | |
| 932 | |
| 933 currentSchedule.onException.schedule(() { | |
| 934 errors = currentSchedule.errors; | |
| 935 }); | |
| 936 | |
| 937 d.file('foo', 'bar').create(); | |
| 938 d.file('fee', 'bar').create(); | |
| 939 d.file('faa', 'bar').create(); | |
| 940 d.filePattern(new RegExp(r'f..'), 'bar').validate(); | |
| 941 }); | |
| 942 | |
| 943 test('test 2', () { | |
| 944 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 945 expect(errors.length, equals(1)); | |
| 946 expect(errors.first.error, matches( | |
| 947 r"^Multiple valid entries found in '[^']+' matching " | |
| 948 r"\/f\.\./:\n" | |
| 949 r"\* faa\n" | |
| 950 r"\* fee\n" | |
| 951 r"\* foo$")); | |
| 952 }); | |
| 953 }, passing: ['test 2']); | |
| 954 } | |
| 955 | |
| 956 void scheduleSandbox() { | |
| 957 schedule(() { | |
| 958 return new Directory('').createTemp().then((dir) { | |
| 959 sandbox = dir.path; | |
| 960 d.defaultRoot = sandbox; | |
| 961 }); | |
| 962 }); | |
| 963 | |
| 964 currentSchedule.onComplete.schedule(() { | |
| 965 d.defaultRoot = null; | |
| 966 if (sandbox == null) return; | |
| 967 var oldSandbox = sandbox; | |
| 968 sandbox = null; | |
| 969 return new Directory(oldSandbox).delete(recursive: true); | |
| 970 }); | |
| 971 } | |
| 972 | |
| 973 Future<List<int>> byteStreamToList(Stream<List<int>> stream) { | |
| 974 return stream.reduce(<int>[], (buffer, chunk) { | |
| 975 buffer.addAll(chunk); | |
| 976 return buffer; | |
| 977 }); | |
| 978 } | |
| 979 | |
| 980 Future<String> byteStreamToString(Stream<List<int>> stream) => | |
| 981 byteStreamToList(stream).then((bytes) => new String.fromCharCodes(bytes)); | |
| OLD | NEW |