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 library watcher.test.utils; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io'; | |
9 | |
10 import 'package:pathos/path.dart' as pathos; | |
11 import 'package:scheduled_test/scheduled_test.dart'; | |
12 import 'package:watcher/watcher.dart'; | |
13 import 'package:watcher/src/stat.dart'; | |
14 | |
15 // TODO(rnystrom): Find a better way to use this. | |
16 import '../../../sdk/lib/_internal/pub/test/command_line_config.dart'; | |
17 | |
18 var _configured = false; | |
19 | |
20 /// The path to the temporary sandbox created for each test. All file | |
21 /// operations are implicitly relative to this directory. | |
22 String _sandboxDir; | |
23 | |
24 /// The [DirectoryWatcher] being used for the current scheduled test. | |
25 DirectoryWatcher _watcher; | |
26 | |
27 /// The index in [_watcher]'s event stream for the next event. When event | |
nweiz
2013/07/11 22:29:01
Paragraph break.
| |
28 /// expectations are set using [expectEvent] (et. al.), they use this to | |
29 /// expect a series of events in order. | |
30 var _nextEvent = 0; | |
31 | |
32 /// The mock modification times (in milliseconds since epoch) for each file. | |
33 /// | |
34 /// The actual file system has pretty coarse granularity for file modification | |
35 /// times. This means using the real file system requires us to put delays in | |
36 /// the tests to ensure we wait long enough between operations for the mod time | |
37 /// to be different. | |
38 /// | |
39 /// Instead, we'll just mock that out. Each time a file is written, we manually | |
40 /// increment the mod time for that file instantly. | |
41 Map<String, int> _mockFileModificationTimes; | |
42 | |
43 void initConfig() { | |
44 if (_configured) return; | |
45 _configured = true; | |
46 unittestConfiguration = new CommandLineConfiguration(); | |
47 } | |
48 | |
49 /// Creates a new [DirectoryWatcher] that watches a temporary directory. | |
50 /// | |
51 /// When the current schedule completes, the directory is deleted. | |
52 DirectoryWatcher createWatcher() { | |
53 _ensureSandbox(); | |
54 _watcher = new DirectoryWatcher(_sandboxDir); | |
55 | |
56 // When a listener is first registered on the watcher, it scans the directory | |
57 // to see the set of pre-existing files. This way, it doesn't report | |
58 // notifications for files that were there before the watcher started. | |
59 // | |
60 // The scan is done asynchronously so that creating a watcher is fast, and | |
61 // we don't have an exposed API to know when that initial scan is done. | |
62 // Since many tests need to make changes after that scan, we need to ensure | |
63 // that we don't start modifying things until its had time to complete. | |
64 // | |
65 // This waits some tuned amount of time to ensure that's happened. | |
66 schedule(() => new Future.delayed(new Duration(milliseconds: 50))); | |
67 | |
68 currentSchedule.onComplete.schedule(() { | |
69 _nextEvent = 0; | |
70 _watcher = null; | |
71 }, "reset watcher"); | |
72 | |
73 return _watcher; | |
74 } | |
75 | |
76 void expectEvent(ChangeType type, String path) { | |
77 // Immediately create the future. This ensures we don't register too late and | |
78 // drop the event before we receive it. | |
79 var future = _watcher.events.elementAt(_nextEvent++).then((event) { | |
80 expect(event, new _ChangeMatcher(type, path)); | |
81 }); | |
82 | |
83 // Make sure the schedule is watching it in case it fails. | |
84 currentSchedule.wrapFuture(future); | |
nweiz
2013/07/11 22:29:01
This is meant to wrap a Future; `future` should be
| |
85 | |
86 // Schedule it so that later file modifications don't occur until after this | |
87 // event is received. | |
88 schedule(() => future); | |
89 } | |
90 | |
91 void expectAddEvent(String path) { | |
92 expectEvent(ChangeType.ADD, pathos.join(_sandboxDir, path)); | |
93 } | |
94 | |
95 void expectModifyEvent(String path) { | |
96 expectEvent(ChangeType.MODIFY, pathos.join(_sandboxDir, path)); | |
97 } | |
98 | |
99 void expectRemoveEvent(String path) { | |
100 expectEvent(ChangeType.REMOVE, pathos.join(_sandboxDir, path)); | |
101 } | |
102 | |
103 /// Schedules writing a file in the sandbox at [path] with [contents]. | |
104 /// | |
105 /// If [contents] is omitted, creates an empty file. If [updatedModified] is | |
106 /// `false`, the mock file modification time is not changed. | |
107 void writeFile(String path, {String contents, bool updateModified}) { | |
108 if (contents == null) contents = ""; | |
109 if (updateModified == null) updateModified = true; | |
110 | |
111 _ensureSandbox(); | |
112 schedule(() { | |
113 var fullPath = pathos.join(_sandboxDir, path); | |
114 | |
115 // Create any needed subdirectories. | |
116 var dir = new Directory(pathos.dirname(fullPath)); | |
117 if (!dir.existsSync()) { | |
118 dir.createSync(recursive: true); | |
119 } | |
120 | |
121 new File(fullPath).writeAsStringSync(contents); | |
122 | |
123 // Manually update the mock modification time for the file. | |
124 if (updateModified) { | |
125 var milliseconds = _mockFileModificationTimes.putIfAbsent(path, () => 0); | |
126 _mockFileModificationTimes[path]++; | |
127 } | |
128 }); | |
129 } | |
130 | |
131 /// Schedules deleting a file in the sandbox at [path]. | |
132 void deleteFile(String path) { | |
133 _ensureSandbox(); | |
134 schedule(() { | |
135 new File(pathos.join(_sandboxDir, path)).deleteSync(); | |
136 }); | |
137 } | |
138 | |
139 /// Schedules renaming a file in the sandbox from [from] to [to]. | |
140 /// | |
141 /// If [contents] is omitted, creates an empty file. | |
142 void renameFile(String from, String to) { | |
143 _ensureSandbox(); | |
144 schedule(() { | |
145 new File(pathos.join(_sandboxDir, from)).renameSync( | |
146 pathos.join(_sandboxDir, to)); | |
147 | |
148 // Manually update the mock modification time for the file. | |
149 var milliseconds = _mockFileModificationTimes.putIfAbsent(to, () => 0); | |
150 _mockFileModificationTimes[to]++; | |
151 }); | |
152 } | |
153 | |
154 /// Makes sure the sandbox directory has been created for this schedule. | |
155 void _ensureSandbox() { | |
156 if (_sandboxDir != null) return; | |
157 | |
158 var dir = new Directory("").createTempSync(); | |
159 _sandboxDir = dir.path; | |
160 | |
161 _mockFileModificationTimes = new Map<String, int>(); | |
162 mockGetModificationTime((path) { | |
163 path = pathos.relative(path, from: _sandboxDir); | |
164 | |
165 // Make sure we got a path in the sandbox. | |
166 assert(pathos.isRelative(path) && !path.startsWith("..")); | |
167 | |
168 return new DateTime.fromMillisecondsSinceEpoch( | |
169 _mockFileModificationTimes[path]); | |
170 }); | |
171 | |
172 currentSchedule.onComplete.schedule(() { | |
173 if (_sandboxDir != null) { | |
174 new Directory(_sandboxDir).deleteSync(recursive: true); | |
175 _sandboxDir = null; | |
176 } | |
177 | |
178 _mockFileModificationTimes = null; | |
179 mockGetModificationTime(null); | |
180 }, "delete sandbox"); | |
181 } | |
182 | |
183 /// A [Matcher] for [WatchEvent]s. | |
184 class _ChangeMatcher extends BaseMatcher { | |
185 /// The expected change. | |
186 final ChangeType type; | |
187 | |
188 /// The expected path. | |
189 final String path; | |
190 | |
191 _ChangeMatcher(this.type, this.path); | |
192 | |
193 Description describe(Description description) { | |
194 description.add("$type $path"); | |
195 } | |
196 | |
197 bool matches(item, Map matchState) => | |
198 item is WatchEvent && item.type == type && item.path == path; | |
199 } | |
OLD | NEW |