Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:io'; | |
| 6 | |
| 7 import 'dart:async'; // temp | |
|
nweiz
2013/07/11 00:25:01
Remove temp import
Bob Nystrom
2013/07/11 18:33:20
Done.
| |
| 8 | |
| 9 import 'package:pathos/path.dart' as pathos; | |
| 10 import 'package:scheduled_test/scheduled_test.dart'; | |
| 11 import 'package:watcher/watcher.dart'; | |
| 12 | |
| 13 import 'utils.dart'; | |
| 14 | |
| 15 main() { | |
| 16 initConfig(); | |
| 17 | |
| 18 test('does not notify for files that already exist when started', () { | |
| 19 // Make some pre-existing files. | |
| 20 writeFile("a.txt"); | |
| 21 writeFile("b.txt"); | |
| 22 | |
| 23 createWatcher(); | |
| 24 | |
| 25 // Change one after the watcher is running. | |
| 26 schedule(() { | |
| 27 writeFile("b.txt", contents: "modified"); | |
| 28 }); | |
| 29 | |
| 30 // We should get a modify event for the changed file, but no add events | |
| 31 // for them before this. | |
| 32 expectModifyEvent("b.txt"); | |
| 33 }); | |
| 34 | |
| 35 test('notifies when a file is added', () { | |
| 36 createWatcher(); | |
| 37 | |
| 38 schedule(() { | |
| 39 writeFile("file.txt"); | |
| 40 }); | |
| 41 | |
| 42 expectAddEvent("file.txt"); | |
| 43 }); | |
| 44 | |
| 45 test('notifies when a file is modified', () { | |
| 46 writeFile("file.txt"); | |
| 47 createWatcher(); | |
| 48 | |
| 49 schedule(() { | |
| 50 writeFile("file.txt", contents: "modified"); | |
| 51 }); | |
| 52 | |
| 53 expectModifyEvent("file.txt"); | |
| 54 }); | |
| 55 | |
| 56 test('notifies when a file is removed', () { | |
| 57 writeFile("file.txt"); | |
| 58 createWatcher(); | |
| 59 | |
| 60 schedule(() { | |
| 61 deleteFile("file.txt"); | |
| 62 }); | |
| 63 | |
| 64 expectRemoveEvent("file.txt"); | |
| 65 }); | |
| 66 | |
| 67 test('notifies when a file is moved', () { | |
| 68 writeFile("old.txt"); | |
| 69 createWatcher(); | |
| 70 | |
| 71 schedule(() { | |
| 72 renameFile("old.txt", "new.txt"); | |
| 73 }); | |
| 74 | |
| 75 expectAddEvent("new.txt"); | |
| 76 expectRemoveEvent("old.txt"); | |
| 77 }); | |
| 78 | |
| 79 test('does not notify if the file contents are unchanged', () { | |
| 80 writeFile("a.txt", contents: "same"); | |
| 81 writeFile("b.txt", contents: "before"); | |
| 82 createWatcher(); | |
| 83 | |
| 84 schedule(() { | |
| 85 writeFile("a.txt", contents: "same"); | |
| 86 writeFile("b.txt", contents: "after"); | |
| 87 }); | |
| 88 | |
| 89 expectModifyEvent("b.txt"); | |
| 90 }); | |
| 91 | |
| 92 test('does not notify if the modification time did not change', () { | |
| 93 writeFile("a.txt", contents: "before"); | |
| 94 writeFile("b.txt", contents: "before"); | |
| 95 createWatcher(); | |
| 96 | |
| 97 schedule(() { | |
| 98 writeFile("a.txt", contents: "after", updateModified: false); | |
| 99 writeFile("b.txt", contents: "after"); | |
| 100 }); | |
| 101 | |
| 102 expectModifyEvent("b.txt"); | |
| 103 }); | |
| 104 | |
| 105 test('watches files in subdirectories', () { | |
| 106 createWatcher(); | |
| 107 | |
| 108 schedule(() { | |
| 109 writeFile("a/b/c/d/file.txt"); | |
| 110 }); | |
| 111 | |
| 112 expectAddEvent("a/b/c/d/file.txt"); | |
| 113 }); | |
| 114 | |
| 115 test('does not notify for changes when there were no subscribers', () { | |
| 116 // Note that this test doesn't rely as heavily on the test functions in | |
| 117 // utils.dart because it needs to be very explicit about when the event | |
| 118 // stream is and is not subscribed. | |
| 119 var watcher = createWatcher(); | |
| 120 | |
| 121 // Subscribe to the events. | |
| 122 var completer = new Completer(); | |
| 123 var subscription = watcher.events.listen((event) { | |
| 124 expect(event.type, equals(ChangeType.ADD)); | |
| 125 expect(event.path, endsWith("file.txt")); | |
| 126 completer.complete(); | |
| 127 }); | |
| 128 | |
| 129 // Write a file. | |
| 130 schedule(() { | |
| 131 writeFile("file.txt"); | |
| 132 }); | |
| 133 | |
| 134 // Then wait until we get an event for it. | |
| 135 schedule(() => completer.future); | |
| 136 | |
| 137 // Unsubscribe. | |
| 138 schedule(() { | |
| 139 subscription.cancel(); | |
| 140 }); | |
| 141 | |
| 142 // Now write a file while we aren't listening. | |
| 143 schedule(() { | |
| 144 writeFile("unwatched.txt"); | |
| 145 }); | |
| 146 | |
| 147 // Then start listening again. | |
| 148 schedule(() { | |
| 149 completer = new Completer(); | |
| 150 subscription = watcher.events.listen((event) { | |
| 151 // We should get an event for the third file, not the one added while | |
| 152 // we weren't subscribed. | |
| 153 expect(event.type, equals(ChangeType.ADD)); | |
| 154 expect(event.path, endsWith("added.txt")); | |
| 155 completer.complete(); | |
| 156 }); | |
| 157 }); | |
| 158 | |
| 159 // The watcher will have been cancelled and then resumed in the middle of | |
| 160 // its pause between polling loops. That means the second scan to skip | |
| 161 // what changed while we were unsubscribed won't happen until after that | |
| 162 // delay is done. Wait long enough for that to happen. | |
| 163 schedule(() => new Future.delayed(new Duration(seconds: 2))); | |
| 164 | |
| 165 // And add a third file. | |
| 166 schedule(() { | |
| 167 writeFile("added.txt"); | |
| 168 }); | |
| 169 | |
| 170 // Wait until we get an event for the third file. | |
| 171 schedule(() => completer.future); | |
| 172 | |
| 173 schedule(() { | |
| 174 subscription.cancel(); | |
| 175 }); | |
| 176 }); | |
| 177 } | |
| OLD | NEW |