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

Side by Side Diff: pkg/watcher/test/directory_watcher_test.dart

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

Powered by Google App Engine
This is Rietveld 408576698