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; | |
6 | |
7 import 'dart:io'; | 5 import 'dart:io'; |
8 | 6 |
9 import 'package:path/path.dart' as p; | 7 import 'package:path/path.dart' as p; |
10 import 'package:scheduled_test/scheduled_stream.dart'; | 8 import 'package:scheduled_test/scheduled_stream.dart'; |
11 import 'package:scheduled_test/scheduled_test.dart'; | 9 import 'package:scheduled_test/scheduled_test.dart'; |
12 import 'package:watcher/watcher.dart'; | 10 import 'package:watcher/watcher.dart'; |
13 import 'package:watcher/src/stat.dart'; | 11 import 'package:watcher/src/stat.dart'; |
14 import 'package:watcher/src/utils.dart'; | 12 import 'package:watcher/src/utils.dart'; |
15 | 13 |
16 // TODO(nweiz): remove this when issue 15042 is fixed. | |
17 import 'package:watcher/src/directory_watcher/mac_os.dart'; | |
18 | |
19 /// The path to the temporary sandbox created for each test. All file | 14 /// The path to the temporary sandbox created for each test. All file |
20 /// operations are implicitly relative to this directory. | 15 /// operations are implicitly relative to this directory. |
21 String _sandboxDir; | 16 String _sandboxDir; |
22 | 17 |
23 /// The [Watcher] being used for the current scheduled test. | 18 /// The [Watcher] being used for the current scheduled test. |
24 Watcher _watcher; | 19 Watcher _watcher; |
25 | 20 |
26 /// The mock modification times (in milliseconds since epoch) for each file. | 21 /// The mock modification times (in milliseconds since epoch) for each file. |
27 /// | 22 /// |
28 /// The actual file system has pretty coarse granularity for file modification | 23 /// The actual file system has pretty coarse granularity for file modification |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 dir.createSync(recursive: true); | 280 dir.createSync(recursive: true); |
286 } | 281 } |
287 | 282 |
288 new File(fullPath).writeAsStringSync(contents); | 283 new File(fullPath).writeAsStringSync(contents); |
289 | 284 |
290 // Manually update the mock modification time for the file. | 285 // Manually update the mock modification time for the file. |
291 if (updateModified) { | 286 if (updateModified) { |
292 // Make sure we always use the same separator on Windows. | 287 // Make sure we always use the same separator on Windows. |
293 path = p.normalize(path); | 288 path = p.normalize(path); |
294 | 289 |
295 var milliseconds = _mockFileModificationTimes.putIfAbsent(path, () => 0); | 290 _mockFileModificationTimes.putIfAbsent(path, () => 0); |
296 _mockFileModificationTimes[path]++; | 291 _mockFileModificationTimes[path]++; |
297 } | 292 } |
298 }, "write file $path"); | 293 }, "write file $path"); |
299 } | 294 } |
300 | 295 |
301 /// Schedules deleting a file in the sandbox at [path]. | 296 /// Schedules deleting a file in the sandbox at [path]. |
302 void deleteFile(String path) { | 297 void deleteFile(String path) { |
303 schedule(() { | 298 schedule(() { |
304 new File(p.join(_sandboxDir, path)).deleteSync(); | 299 new File(p.join(_sandboxDir, path)).deleteSync(); |
305 }, "delete file $path"); | 300 }, "delete file $path"); |
306 } | 301 } |
307 | 302 |
308 /// Schedules renaming a file in the sandbox from [from] to [to]. | 303 /// Schedules renaming a file in the sandbox from [from] to [to]. |
309 /// | 304 /// |
310 /// If [contents] is omitted, creates an empty file. | 305 /// If [contents] is omitted, creates an empty file. |
311 void renameFile(String from, String to) { | 306 void renameFile(String from, String to) { |
312 schedule(() { | 307 schedule(() { |
313 new File(p.join(_sandboxDir, from)).renameSync(p.join(_sandboxDir, to)); | 308 new File(p.join(_sandboxDir, from)).renameSync(p.join(_sandboxDir, to)); |
314 | 309 |
315 // Make sure we always use the same separator on Windows. | 310 // Make sure we always use the same separator on Windows. |
316 to = p.normalize(to); | 311 to = p.normalize(to); |
317 | 312 |
318 // Manually update the mock modification time for the file. | 313 // Manually update the mock modification time for the file. |
319 var milliseconds = _mockFileModificationTimes.putIfAbsent(to, () => 0); | 314 _mockFileModificationTimes.putIfAbsent(to, () => 0); |
320 _mockFileModificationTimes[to]++; | 315 _mockFileModificationTimes[to]++; |
321 }, "rename file $from to $to"); | 316 }, "rename file $from to $to"); |
322 } | 317 } |
323 | 318 |
324 /// Schedules creating a directory in the sandbox at [path]. | 319 /// Schedules creating a directory in the sandbox at [path]. |
325 void createDir(String path) { | 320 void createDir(String path) { |
326 schedule(() { | 321 schedule(() { |
327 new Directory(p.join(_sandboxDir, path)).createSync(); | 322 new Directory(p.join(_sandboxDir, path)).createSync(); |
328 }, "create directory $path"); | 323 }, "create directory $path"); |
329 } | 324 } |
(...skipping 12 matching lines...) Expand all Loading... |
342 new Directory(p.join(_sandboxDir, path)).deleteSync(recursive: true); | 337 new Directory(p.join(_sandboxDir, path)).deleteSync(recursive: true); |
343 }, "delete directory $path"); | 338 }, "delete directory $path"); |
344 } | 339 } |
345 | 340 |
346 /// Runs [callback] with every permutation of non-negative [i], [j], and [k] | 341 /// Runs [callback] with every permutation of non-negative [i], [j], and [k] |
347 /// less than [limit]. | 342 /// less than [limit]. |
348 /// | 343 /// |
349 /// Returns a set of all values returns by [callback]. | 344 /// Returns a set of all values returns by [callback]. |
350 /// | 345 /// |
351 /// [limit] defaults to 3. | 346 /// [limit] defaults to 3. |
352 Set withPermutations(callback(int i, int j, int k), {int limit}) { | 347 Set/*<S>*/ withPermutations/*<S>*/(/*=S*/ callback(int i, int j, int k), |
| 348 {int limit}) { |
353 if (limit == null) limit = 3; | 349 if (limit == null) limit = 3; |
354 var results = new Set(); | 350 var results = new Set/*<S>*/(); |
355 for (var i = 0; i < limit; i++) { | 351 for (var i = 0; i < limit; i++) { |
356 for (var j = 0; j < limit; j++) { | 352 for (var j = 0; j < limit; j++) { |
357 for (var k = 0; k < limit; k++) { | 353 for (var k = 0; k < limit; k++) { |
358 results.add(callback(i, j, k)); | 354 results.add(callback(i, j, k)); |
359 } | 355 } |
360 } | 356 } |
361 } | 357 } |
362 return results; | 358 return results; |
363 } | 359 } |
OLD | NEW |