| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library watcher.test.utils; | 5 library watcher.test.utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 /// The stream of events from the watcher started with [startWatcher]. | 109 /// The stream of events from the watcher started with [startWatcher]. |
| 110 ScheduledStream<WatchEvent> _watcherEvents; | 110 ScheduledStream<WatchEvent> _watcherEvents; |
| 111 | 111 |
| 112 /// Creates a new [DirectoryWatcher] that watches a temporary directory and | 112 /// Creates a new [DirectoryWatcher] that watches a temporary directory and |
| 113 /// starts monitoring it for events. | 113 /// starts monitoring it for events. |
| 114 /// | 114 /// |
| 115 /// If [dir] is provided, watches a subdirectory in the sandbox with that name. | 115 /// If [dir] is provided, watches a subdirectory in the sandbox with that name. |
| 116 void startWatcher({String dir}) { | 116 void startWatcher({String dir}) { |
| 117 var testCase = currentTestCase.description; | 117 var testCase = currentTestCase.description; |
| 118 if (MacOSDirectoryWatcher.logDebugInfo) { | 118 if (MacOSDirectoryWatcher.logDebugInfo) { |
| 119 print("starting watcher for $testCase"); | 119 print("starting watcher for $testCase (${new DateTime.now()})"); |
| 120 } | 120 } |
| 121 | 121 |
| 122 // We want to wait until we're ready *after* we subscribe to the watcher's | 122 // We want to wait until we're ready *after* we subscribe to the watcher's |
| 123 // events. | 123 // events. |
| 124 _watcher = createWatcher(dir: dir, waitForReady: false); | 124 _watcher = createWatcher(dir: dir, waitForReady: false); |
| 125 | 125 |
| 126 // Schedule [_watcher.events.listen] so that the watcher doesn't start | 126 // Schedule [_watcher.events.listen] so that the watcher doesn't start |
| 127 // watching [dir] before it exists. Expose [_watcherEvents] immediately so | 127 // watching [dir] before it exists. Expose [_watcherEvents] immediately so |
| 128 // that it can be accessed synchronously after this. | 128 // that it can be accessed synchronously after this. |
| 129 _watcherEvents = new ScheduledStream(futureStream(schedule(() { | 129 _watcherEvents = new ScheduledStream(futureStream(schedule(() { |
| 130 currentSchedule.onComplete.schedule(() { | 130 currentSchedule.onComplete.schedule(() { |
| 131 if (MacOSDirectoryWatcher.logDebugInfo) { | 131 if (MacOSDirectoryWatcher.logDebugInfo) { |
| 132 print("stopping watcher for $testCase"); | 132 print("stopping watcher for $testCase (${new DateTime.now()})"); |
| 133 } | 133 } |
| 134 | 134 |
| 135 _watcher = null; | 135 _watcher = null; |
| 136 if (!_closePending) _watcherEvents.close(); | 136 if (!_closePending) _watcherEvents.close(); |
| 137 | 137 |
| 138 // If there are already errors, don't add this to the output and make | 138 // If there are already errors, don't add this to the output and make |
| 139 // people think it might be the root cause. | 139 // people think it might be the root cause. |
| 140 if (currentSchedule.errors.isEmpty) { | 140 if (currentSchedule.errors.isEmpty) { |
| 141 _watcherEvents.expect(isDone); | 141 _watcherEvents.expect(isDone); |
| 142 } | 142 } |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 | 291 |
| 292 schedule(() { | 292 schedule(() { |
| 293 var fullPath = p.join(_sandboxDir, path); | 293 var fullPath = p.join(_sandboxDir, path); |
| 294 | 294 |
| 295 // Create any needed subdirectories. | 295 // Create any needed subdirectories. |
| 296 var dir = new Directory(p.dirname(fullPath)); | 296 var dir = new Directory(p.dirname(fullPath)); |
| 297 if (!dir.existsSync()) { | 297 if (!dir.existsSync()) { |
| 298 dir.createSync(recursive: true); | 298 dir.createSync(recursive: true); |
| 299 } | 299 } |
| 300 | 300 |
| 301 if (MacOSDirectoryWatcher.logDebugInfo) { |
| 302 print("[test] writing file $path"); |
| 303 } |
| 301 new File(fullPath).writeAsStringSync(contents); | 304 new File(fullPath).writeAsStringSync(contents); |
| 302 | 305 |
| 303 // Manually update the mock modification time for the file. | 306 // Manually update the mock modification time for the file. |
| 304 if (updateModified) { | 307 if (updateModified) { |
| 305 // Make sure we always use the same separator on Windows. | 308 // Make sure we always use the same separator on Windows. |
| 306 path = p.normalize(path); | 309 path = p.normalize(path); |
| 307 | 310 |
| 308 var milliseconds = _mockFileModificationTimes.putIfAbsent(path, () => 0); | 311 var milliseconds = _mockFileModificationTimes.putIfAbsent(path, () => 0); |
| 309 _mockFileModificationTimes[path]++; | 312 _mockFileModificationTimes[path]++; |
| 310 } | 313 } |
| 311 }, "write file $path"); | 314 }, "write file $path"); |
| 312 } | 315 } |
| 313 | 316 |
| 314 /// Schedules deleting a file in the sandbox at [path]. | 317 /// Schedules deleting a file in the sandbox at [path]. |
| 315 void deleteFile(String path) { | 318 void deleteFile(String path) { |
| 316 schedule(() { | 319 schedule(() { |
| 320 if (MacOSDirectoryWatcher.logDebugInfo) { |
| 321 print("[test] deleting file $path"); |
| 322 } |
| 317 new File(p.join(_sandboxDir, path)).deleteSync(); | 323 new File(p.join(_sandboxDir, path)).deleteSync(); |
| 318 }, "delete file $path"); | 324 }, "delete file $path"); |
| 319 } | 325 } |
| 320 | 326 |
| 321 /// Schedules renaming a file in the sandbox from [from] to [to]. | 327 /// Schedules renaming a file in the sandbox from [from] to [to]. |
| 322 /// | 328 /// |
| 323 /// If [contents] is omitted, creates an empty file. | 329 /// If [contents] is omitted, creates an empty file. |
| 324 void renameFile(String from, String to) { | 330 void renameFile(String from, String to) { |
| 325 schedule(() { | 331 schedule(() { |
| 332 if (MacOSDirectoryWatcher.logDebugInfo) { |
| 333 print("[test] renaming file $from to $to"); |
| 334 } |
| 335 |
| 326 new File(p.join(_sandboxDir, from)).renameSync(p.join(_sandboxDir, to)); | 336 new File(p.join(_sandboxDir, from)).renameSync(p.join(_sandboxDir, to)); |
| 327 | 337 |
| 328 // Make sure we always use the same separator on Windows. | 338 // Make sure we always use the same separator on Windows. |
| 329 to = p.normalize(to); | 339 to = p.normalize(to); |
| 330 | 340 |
| 331 // Manually update the mock modification time for the file. | 341 // Manually update the mock modification time for the file. |
| 332 var milliseconds = _mockFileModificationTimes.putIfAbsent(to, () => 0); | 342 var milliseconds = _mockFileModificationTimes.putIfAbsent(to, () => 0); |
| 333 _mockFileModificationTimes[to]++; | 343 _mockFileModificationTimes[to]++; |
| 334 }, "rename file $from to $to"); | 344 }, "rename file $from to $to"); |
| 335 } | 345 } |
| 336 | 346 |
| 337 /// Schedules creating a directory in the sandbox at [path]. | 347 /// Schedules creating a directory in the sandbox at [path]. |
| 338 void createDir(String path) { | 348 void createDir(String path) { |
| 339 schedule(() { | 349 schedule(() { |
| 350 if (MacOSDirectoryWatcher.logDebugInfo) { |
| 351 print("[test] creating directory $path"); |
| 352 } |
| 340 new Directory(p.join(_sandboxDir, path)).createSync(); | 353 new Directory(p.join(_sandboxDir, path)).createSync(); |
| 341 }, "create directory $path"); | 354 }, "create directory $path"); |
| 342 } | 355 } |
| 343 | 356 |
| 344 /// Schedules renaming a directory in the sandbox from [from] to [to]. | 357 /// Schedules renaming a directory in the sandbox from [from] to [to]. |
| 345 void renameDir(String from, String to) { | 358 void renameDir(String from, String to) { |
| 346 schedule(() { | 359 schedule(() { |
| 360 if (MacOSDirectoryWatcher.logDebugInfo) { |
| 361 print("[test] renaming directory $from to $to"); |
| 362 } |
| 347 new Directory(p.join(_sandboxDir, from)) | 363 new Directory(p.join(_sandboxDir, from)) |
| 348 .renameSync(p.join(_sandboxDir, to)); | 364 .renameSync(p.join(_sandboxDir, to)); |
| 349 }, "rename directory $from to $to"); | 365 }, "rename directory $from to $to"); |
| 350 } | 366 } |
| 351 | 367 |
| 352 /// Schedules deleting a directory in the sandbox at [path]. | 368 /// Schedules deleting a directory in the sandbox at [path]. |
| 353 void deleteDir(String path) { | 369 void deleteDir(String path) { |
| 354 schedule(() { | 370 schedule(() { |
| 371 if (MacOSDirectoryWatcher.logDebugInfo) { |
| 372 print("[test] deleting directory $path"); |
| 373 } |
| 355 new Directory(p.join(_sandboxDir, path)).deleteSync(recursive: true); | 374 new Directory(p.join(_sandboxDir, path)).deleteSync(recursive: true); |
| 356 }, "delete directory $path"); | 375 }, "delete directory $path"); |
| 357 } | 376 } |
| 358 | 377 |
| 359 /// Runs [callback] with every permutation of non-negative [i], [j], and [k] | 378 /// Runs [callback] with every permutation of non-negative [i], [j], and [k] |
| 360 /// less than [limit]. | 379 /// less than [limit]. |
| 361 /// | 380 /// |
| 362 /// Returns a set of all values returns by [callback]. | 381 /// Returns a set of all values returns by [callback]. |
| 363 /// | 382 /// |
| 364 /// [limit] defaults to 3. | 383 /// [limit] defaults to 3. |
| 365 Set withPermutations(callback(int i, int j, int k), {int limit}) { | 384 Set withPermutations(callback(int i, int j, int k), {int limit}) { |
| 366 if (limit == null) limit = 3; | 385 if (limit == null) limit = 3; |
| 367 var results = new Set(); | 386 var results = new Set(); |
| 368 for (var i = 0; i < limit; i++) { | 387 for (var i = 0; i < limit; i++) { |
| 369 for (var j = 0; j < limit; j++) { | 388 for (var j = 0; j < limit; j++) { |
| 370 for (var k = 0; k < limit; k++) { | 389 for (var k = 0; k < limit; k++) { |
| 371 results.add(callback(i, j, k)); | 390 results.add(callback(i, j, k)); |
| 372 } | 391 } |
| 373 } | 392 } |
| 374 } | 393 } |
| 375 return results; | 394 return results; |
| 376 } | 395 } |
| OLD | NEW |