Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1225)

Unified Diff: pkg/watcher/test/utils.dart

Issue 22999008: Handle watching a non-existent directory. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/watcher/test/directory_watcher_test.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/watcher/test/utils.dart
diff --git a/pkg/watcher/test/utils.dart b/pkg/watcher/test/utils.dart
index 65d47197d7afa1e387944a5e4ae92e2866cbd766..18bec9728d5ac9e6f25720c3925c9d71c77c4b1e 100644
--- a/pkg/watcher/test/utils.dart
+++ b/pkg/watcher/test/utils.dart
@@ -76,9 +76,17 @@ void createSandbox() {
/// Normally, this will pause the schedule until the watcher is done scanning
/// and is polling for changes. If you pass `false` for [waitForReady], it will
/// not schedule this delay.
-DirectoryWatcher createWatcher({bool waitForReady}) {
+///
+/// If [dir] is provided, watches a subdirectory in the sandbox with that name.
+DirectoryWatcher createWatcher({String dir, bool waitForReady}) {
+ if (dir == null) {
+ dir = _sandboxDir;
+ } else {
+ dir = p.join(_sandboxDir, dir);
+ }
+
// Use a short delay to make the tests run quickly.
- _watcher = new DirectoryWatcher(_sandboxDir,
+ _watcher = new DirectoryWatcher(dir,
pollingDelay: new Duration(milliseconds: 100));
// Wait until the scan is finished so that we don't miss changes to files
@@ -95,31 +103,45 @@ DirectoryWatcher createWatcher({bool waitForReady}) {
return _watcher;
}
-void expectEvent(ChangeType type, String path) {
- // Immediately create the future. This ensures we don't register too late and
- // drop the event before we receive it.
- var future = _watcher.events.elementAt(_nextEvent++).then((event) {
- expect(event, new _ChangeMatcher(type, path));
- });
+/// Expects that the next set of events will all be changes of [type] on
+/// [paths].
+///
+/// Validates that events are delivered for all paths in [paths], but allows
+/// them in any order.
+void expectEvents(ChangeType type, Iterable<String> paths) {
+ var pathSet = paths.map((path) => p.join(_sandboxDir, path)).toSet();
+
+ // Create an expectation for as many paths as we have.
+ var futures = [];
+
+ for (var i = 0; i < paths.length; i++) {
+ // Immediately create the futures. This ensures we don't register too
+ // late and drop the event before we receive it.
+ var future = _watcher.events.elementAt(_nextEvent++).then((event) {
+ expect(event.type, equals(type));
+ expect(pathSet, contains(event.path));
+
+ pathSet.remove(event.path);
+ });
- // Make sure the schedule is watching it in case it fails.
- currentSchedule.wrapFuture(future);
+ // Make sure the schedule is watching it in case it fails.
+ currentSchedule.wrapFuture(future);
+
+ futures.add(future);
+ }
// Schedule it so that later file modifications don't occur until after this
// event is received.
- schedule(() => future, "wait for $type event");
-}
-
-void expectAddEvent(String path) {
- expectEvent(ChangeType.ADD, p.join(_sandboxDir, path));
+ schedule(() => Future.wait(futures),
+ "wait for $type events on ${paths.join(', ')}");
}
-void expectModifyEvent(String path) {
- expectEvent(ChangeType.MODIFY, p.join(_sandboxDir, path));
-}
+void expectAddEvent(String path) => expectEvents(ChangeType.ADD, [path]);
+void expectModifyEvent(String path) => expectEvents(ChangeType.MODIFY, [path]);
+void expectRemoveEvent(String path) => expectEvents(ChangeType.REMOVE, [path]);
-void expectRemoveEvent(String path) {
- expectEvent(ChangeType.REMOVE, p.join(_sandboxDir, path));
+void expectRemoveEvents(Iterable<String> paths) {
+ expectEvents(ChangeType.REMOVE, paths);
}
/// Schedules writing a file in the sandbox at [path] with [contents].
@@ -175,6 +197,13 @@ void renameFile(String from, String to) {
}, "rename file $from to $to");
}
+/// Schedules deleting a directory in the sandbox at [path].
+void deleteDir(String path) {
+ schedule(() {
+ new Directory(p.join(_sandboxDir, path)).deleteSync(recursive: true);
+ }, "delete directory $path");
+}
+
/// A [Matcher] for [WatchEvent]s.
class _ChangeMatcher extends Matcher {
/// The expected change.
« no previous file with comments | « pkg/watcher/test/directory_watcher_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698