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