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