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