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:async'; |
| 6 import 'dart:io'; |
| 7 |
| 8 import 'package:scheduled_test/scheduled_test.dart'; |
| 9 import 'package:watcher/watcher.dart'; |
| 10 |
| 11 import 'utils.dart'; |
| 12 |
| 13 main() { |
| 14 initConfig(); |
| 15 |
| 16 setUp(createSandbox); |
| 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 writeFile("b.txt", contents: "modified"); |
| 27 |
| 28 // We should get a modify event for the changed file, but no add events |
| 29 // for them before this. |
| 30 expectModifyEvent("b.txt"); |
| 31 }); |
| 32 |
| 33 test('notifies when a file is added', () { |
| 34 createWatcher(); |
| 35 writeFile("file.txt"); |
| 36 expectAddEvent("file.txt"); |
| 37 }); |
| 38 |
| 39 test('notifies when a file is modified', () { |
| 40 writeFile("file.txt"); |
| 41 createWatcher(); |
| 42 writeFile("file.txt", contents: "modified"); |
| 43 expectModifyEvent("file.txt"); |
| 44 }); |
| 45 |
| 46 test('notifies when a file is removed', () { |
| 47 writeFile("file.txt"); |
| 48 createWatcher(); |
| 49 deleteFile("file.txt"); |
| 50 expectRemoveEvent("file.txt"); |
| 51 }); |
| 52 |
| 53 test('notifies when a file is moved', () { |
| 54 writeFile("old.txt"); |
| 55 createWatcher(); |
| 56 renameFile("old.txt", "new.txt"); |
| 57 expectAddEvent("new.txt"); |
| 58 expectRemoveEvent("old.txt"); |
| 59 }); |
| 60 |
| 61 test('notifies when a file is modified multiple times', () { |
| 62 writeFile("file.txt"); |
| 63 createWatcher(); |
| 64 writeFile("file.txt", contents: "modified"); |
| 65 expectModifyEvent("file.txt"); |
| 66 writeFile("file.txt", contents: "modified again"); |
| 67 expectModifyEvent("file.txt"); |
| 68 }); |
| 69 |
| 70 test('does not notify if the file contents are unchanged', () { |
| 71 writeFile("a.txt", contents: "same"); |
| 72 writeFile("b.txt", contents: "before"); |
| 73 createWatcher(); |
| 74 writeFile("a.txt", contents: "same"); |
| 75 writeFile("b.txt", contents: "after"); |
| 76 expectModifyEvent("b.txt"); |
| 77 }); |
| 78 |
| 79 test('does not notify if the modification time did not change', () { |
| 80 writeFile("a.txt", contents: "before"); |
| 81 writeFile("b.txt", contents: "before"); |
| 82 createWatcher(); |
| 83 writeFile("a.txt", contents: "after", updateModified: false); |
| 84 writeFile("b.txt", contents: "after"); |
| 85 expectModifyEvent("b.txt"); |
| 86 }); |
| 87 |
| 88 test('watches files in subdirectories', () { |
| 89 createWatcher(); |
| 90 writeFile("a/b/c/d/file.txt"); |
| 91 expectAddEvent("a/b/c/d/file.txt"); |
| 92 }); |
| 93 |
| 94 test('does not notify for changes when there were no subscribers', () { |
| 95 // Note that this test doesn't rely as heavily on the test functions in |
| 96 // utils.dart because it needs to be very explicit about when the event |
| 97 // stream is and is not subscribed. |
| 98 var watcher = createWatcher(); |
| 99 |
| 100 // Subscribe to the events. |
| 101 var completer = new Completer(); |
| 102 var subscription = watcher.events.listen((event) { |
| 103 expect(event.type, equals(ChangeType.ADD)); |
| 104 expect(event.path, endsWith("file.txt")); |
| 105 completer.complete(); |
| 106 }); |
| 107 |
| 108 writeFile("file.txt"); |
| 109 |
| 110 // Then wait until we get an event for it. |
| 111 schedule(() => completer.future); |
| 112 |
| 113 // Unsubscribe. |
| 114 schedule(() { |
| 115 subscription.cancel(); |
| 116 }); |
| 117 |
| 118 // Now write a file while we aren't listening. |
| 119 writeFile("unwatched.txt"); |
| 120 |
| 121 // Then start listening again. |
| 122 schedule(() { |
| 123 completer = new Completer(); |
| 124 subscription = watcher.events.listen((event) { |
| 125 // We should get an event for the third file, not the one added while |
| 126 // we weren't subscribed. |
| 127 expect(event.type, equals(ChangeType.ADD)); |
| 128 expect(event.path, endsWith("added.txt")); |
| 129 completer.complete(); |
| 130 }); |
| 131 }); |
| 132 |
| 133 // The watcher will have been cancelled and then resumed in the middle of |
| 134 // its pause between polling loops. That means the second scan to skip |
| 135 // what changed while we were unsubscribed won't happen until after that |
| 136 // delay is done. Wait long enough for that to happen. |
| 137 schedule(() => new Future.delayed(new Duration(seconds: 1))); |
| 138 |
| 139 // And add a third file. |
| 140 writeFile("added.txt"); |
| 141 |
| 142 // Wait until we get an event for the third file. |
| 143 schedule(() => completer.future); |
| 144 |
| 145 schedule(() { |
| 146 subscription.cancel(); |
| 147 }); |
| 148 }); |
| 149 |
| 150 |
| 151 test('ready does not complete until after subscription', () { |
| 152 var watcher = createWatcher(waitForReady: false); |
| 153 |
| 154 var ready = false; |
| 155 watcher.ready.then((_) { |
| 156 ready = true; |
| 157 }); |
| 158 |
| 159 // Should not be ready yet. |
| 160 schedule(() { |
| 161 expect(ready, isFalse); |
| 162 }); |
| 163 |
| 164 // Subscribe to the events. |
| 165 schedule(() { |
| 166 var subscription = watcher.events.listen((event) {}); |
| 167 |
| 168 currentSchedule.onComplete.schedule(() { |
| 169 subscription.cancel(); |
| 170 }); |
| 171 }); |
| 172 |
| 173 // Should eventually be ready. |
| 174 schedule(() => watcher.ready); |
| 175 |
| 176 schedule(() { |
| 177 expect(ready, isTrue); |
| 178 }); |
| 179 }); |
| 180 |
| 181 test('ready completes immediately when already ready', () { |
| 182 var watcher = createWatcher(waitForReady: false); |
| 183 |
| 184 // Subscribe to the events. |
| 185 schedule(() { |
| 186 var subscription = watcher.events.listen((event) {}); |
| 187 |
| 188 currentSchedule.onComplete.schedule(() { |
| 189 subscription.cancel(); |
| 190 }); |
| 191 }); |
| 192 |
| 193 // Should eventually be ready. |
| 194 schedule(() => watcher.ready); |
| 195 |
| 196 // Now ready should be a future that immediately completes. |
| 197 var ready = false; |
| 198 schedule(() { |
| 199 watcher.ready.then((_) { |
| 200 ready = true; |
| 201 }); |
| 202 }); |
| 203 |
| 204 schedule(() { |
| 205 expect(ready, isTrue); |
| 206 }); |
| 207 }); |
| 208 |
| 209 test('ready returns a future that does not complete after unsubscribing', () { |
| 210 var watcher = createWatcher(waitForReady: false); |
| 211 |
| 212 // Subscribe to the events. |
| 213 var subscription; |
| 214 schedule(() { |
| 215 subscription = watcher.events.listen((event) {}); |
| 216 }); |
| 217 |
| 218 var ready = false; |
| 219 |
| 220 // Wait until ready. |
| 221 schedule(() => watcher.ready); |
| 222 |
| 223 // Now unsubscribe. |
| 224 schedule(() { |
| 225 subscription.cancel(); |
| 226 |
| 227 // Track when it's ready again. |
| 228 ready = false; |
| 229 watcher.ready.then((_) { |
| 230 ready = true; |
| 231 }); |
| 232 }); |
| 233 |
| 234 // Should be back to not ready. |
| 235 schedule(() { |
| 236 expect(ready, isFalse); |
| 237 }); |
| 238 }); |
| 239 } |
OLD | NEW |