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