OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library watcher.test.utils; | 5 library watcher.test.utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
(...skipping 30 matching lines...) Expand all Loading... |
41 } | 41 } |
42 | 42 |
43 /// Creates the sandbox directory the other functions in this library use and | 43 /// Creates the sandbox directory the other functions in this library use and |
44 /// ensures it's deleted when the test ends. | 44 /// ensures it's deleted when the test ends. |
45 /// | 45 /// |
46 /// This should usually be called by [setUp]. | 46 /// This should usually be called by [setUp]. |
47 void createSandbox() { | 47 void createSandbox() { |
48 var dir = new Directory("").createTempSync(); | 48 var dir = new Directory("").createTempSync(); |
49 _sandboxDir = dir.path; | 49 _sandboxDir = dir.path; |
50 | 50 |
51 // TODO(rnystrom): Temporary while debugging the Windows bot. | |
52 print("create mock modtime map for $_sandboxDir"); | |
53 | |
54 _mockFileModificationTimes = new Map<String, int>(); | 51 _mockFileModificationTimes = new Map<String, int>(); |
55 mockGetModificationTime((path) { | 52 mockGetModificationTime((path) { |
56 path = p.relative(path, from: _sandboxDir); | 53 path = p.normalize(p.relative(path, from: _sandboxDir)); |
57 | 54 |
58 // Make sure we got a path in the sandbox. | 55 // Make sure we got a path in the sandbox. |
59 assert(p.isRelative(path) && !path.startsWith("..")); | 56 assert(p.isRelative(path) && !path.startsWith("..")); |
60 | 57 |
61 // TODO(rnystrom): Temporary while debugging the Windows bot. | |
62 print("get mock modtime for $path = ${_mockFileModificationTimes[path]}"); | |
63 return new DateTime.fromMillisecondsSinceEpoch( | 58 return new DateTime.fromMillisecondsSinceEpoch( |
64 _mockFileModificationTimes[path]); | 59 _mockFileModificationTimes[path]); |
65 }); | 60 }); |
66 | 61 |
67 // Delete the sandbox when done. | 62 // Delete the sandbox when done. |
68 currentSchedule.onComplete.schedule(() { | 63 currentSchedule.onComplete.schedule(() { |
69 // TODO(rnystrom): Temporary while debugging the Windows bot. | |
70 print("delete mock modtime map for $_sandboxDir"); | |
71 | |
72 if (_sandboxDir != null) { | 64 if (_sandboxDir != null) { |
73 new Directory(_sandboxDir).deleteSync(recursive: true); | 65 new Directory(_sandboxDir).deleteSync(recursive: true); |
74 _sandboxDir = null; | 66 _sandboxDir = null; |
75 } | 67 } |
76 | 68 |
77 _mockFileModificationTimes = null; | 69 _mockFileModificationTimes = null; |
78 mockGetModificationTime(null); | 70 mockGetModificationTime(null); |
79 }, "delete sandbox"); | 71 }, "delete sandbox"); |
80 } | 72 } |
81 | 73 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 132 |
141 schedule(() { | 133 schedule(() { |
142 var fullPath = p.join(_sandboxDir, path); | 134 var fullPath = p.join(_sandboxDir, path); |
143 | 135 |
144 // Create any needed subdirectories. | 136 // Create any needed subdirectories. |
145 var dir = new Directory(p.dirname(fullPath)); | 137 var dir = new Directory(p.dirname(fullPath)); |
146 if (!dir.existsSync()) { | 138 if (!dir.existsSync()) { |
147 dir.createSync(recursive: true); | 139 dir.createSync(recursive: true); |
148 } | 140 } |
149 | 141 |
150 // TODO(rnystrom): Temporary while debugging the Windows bot. | |
151 print("write $path"); | |
152 | |
153 new File(fullPath).writeAsStringSync(contents); | 142 new File(fullPath).writeAsStringSync(contents); |
154 | 143 |
155 // Manually update the mock modification time for the file. | 144 // Manually update the mock modification time for the file. |
156 if (updateModified) { | 145 if (updateModified) { |
| 146 // Make sure we always use the same separator on Windows. |
| 147 path = p.normalize(path); |
| 148 |
157 var milliseconds = _mockFileModificationTimes.putIfAbsent(path, () => 0); | 149 var milliseconds = _mockFileModificationTimes.putIfAbsent(path, () => 0); |
158 _mockFileModificationTimes[path]++; | 150 _mockFileModificationTimes[path]++; |
159 // TODO(rnystrom): Temporary while debugging the Windows bot. | |
160 print(" update modtime to ${_mockFileModificationTimes[path]}"); | |
161 } | 151 } |
162 }); | 152 }); |
163 } | 153 } |
164 | 154 |
165 /// Schedules deleting a file in the sandbox at [path]. | 155 /// Schedules deleting a file in the sandbox at [path]. |
166 void deleteFile(String path) { | 156 void deleteFile(String path) { |
167 schedule(() { | 157 schedule(() { |
168 new File(p.join(_sandboxDir, path)).deleteSync(); | 158 new File(p.join(_sandboxDir, path)).deleteSync(); |
169 }); | 159 }); |
170 } | 160 } |
171 | 161 |
172 /// Schedules renaming a file in the sandbox from [from] to [to]. | 162 /// Schedules renaming a file in the sandbox from [from] to [to]. |
173 /// | 163 /// |
174 /// If [contents] is omitted, creates an empty file. | 164 /// If [contents] is omitted, creates an empty file. |
175 void renameFile(String from, String to) { | 165 void renameFile(String from, String to) { |
176 schedule(() { | 166 schedule(() { |
177 new File(p.join(_sandboxDir, from)).renameSync(p.join(_sandboxDir, to)); | 167 new File(p.join(_sandboxDir, from)).renameSync(p.join(_sandboxDir, to)); |
178 | 168 |
179 // TODO(rnystrom): Temporary while debugging the Windows bot. | 169 // Make sure we always use the same separator on Windows. |
180 print("rename $from -> $to"); | 170 to = p.normalize(to); |
181 | 171 |
182 // Manually update the mock modification time for the file. | 172 // Manually update the mock modification time for the file. |
183 var milliseconds = _mockFileModificationTimes.putIfAbsent(to, () => 0); | 173 var milliseconds = _mockFileModificationTimes.putIfAbsent(to, () => 0); |
184 _mockFileModificationTimes[to]++; | 174 _mockFileModificationTimes[to]++; |
185 // TODO(rnystrom): Temporary while debugging the Windows bot. | |
186 print(" update modtime to ${_mockFileModificationTimes[to]}"); | |
187 }); | 175 }); |
188 } | 176 } |
189 | 177 |
190 /// A [Matcher] for [WatchEvent]s. | 178 /// A [Matcher] for [WatchEvent]s. |
191 class _ChangeMatcher extends BaseMatcher { | 179 class _ChangeMatcher extends BaseMatcher { |
192 /// The expected change. | 180 /// The expected change. |
193 final ChangeType type; | 181 final ChangeType type; |
194 | 182 |
195 /// The expected path. | 183 /// The expected path. |
196 final String path; | 184 final String path; |
197 | 185 |
198 _ChangeMatcher(this.type, this.path); | 186 _ChangeMatcher(this.type, this.path); |
199 | 187 |
200 Description describe(Description description) { | 188 Description describe(Description description) { |
201 description.add("$type $path"); | 189 description.add("$type $path"); |
202 } | 190 } |
203 | 191 |
204 bool matches(item, Map matchState) => | 192 bool matches(item, Map matchState) => |
205 item is WatchEvent && item.type == type && item.path == path; | 193 item is WatchEvent && item.type == type && item.path == path; |
206 } | 194 } |
OLD | NEW |