Chromium Code Reviews| Index: pkg/watcher/test/utils.dart |
| diff --git a/pkg/watcher/test/utils.dart b/pkg/watcher/test/utils.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6bf9f7eb7640f5562d4ea4b38b978fea4c302529 |
| --- /dev/null |
| +++ b/pkg/watcher/test/utils.dart |
| @@ -0,0 +1,199 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library watcher.test.utils; |
| + |
| +import 'dart:async'; |
| +import 'dart:io'; |
| + |
| +import 'package:pathos/path.dart' as pathos; |
| +import 'package:scheduled_test/scheduled_test.dart'; |
| +import 'package:watcher/watcher.dart'; |
| +import 'package:watcher/src/stat.dart'; |
| + |
| +// TODO(rnystrom): Find a better way to use this. |
| +import '../../../sdk/lib/_internal/pub/test/command_line_config.dart'; |
| + |
| +var _configured = false; |
| + |
| +/// The path to the temporary sandbox created for each test. All file |
| +/// operations are implicitly relative to this directory. |
| +String _sandboxDir; |
| + |
| +/// The [DirectoryWatcher] being used for the current scheduled test. |
| +DirectoryWatcher _watcher; |
| + |
| +/// The index in [_watcher]'s event stream for the next event. When event |
|
nweiz
2013/07/11 22:29:01
Paragraph break.
|
| +/// expectations are set using [expectEvent] (et. al.), they use this to |
| +/// expect a series of events in order. |
| +var _nextEvent = 0; |
| + |
| +/// The mock modification times (in milliseconds since epoch) for each file. |
| +/// |
| +/// The actual file system has pretty coarse granularity for file modification |
| +/// times. This means using the real file system requires us to put delays in |
| +/// the tests to ensure we wait long enough between operations for the mod time |
| +/// to be different. |
| +/// |
| +/// Instead, we'll just mock that out. Each time a file is written, we manually |
| +/// increment the mod time for that file instantly. |
| +Map<String, int> _mockFileModificationTimes; |
| + |
| +void initConfig() { |
| + if (_configured) return; |
| + _configured = true; |
| + unittestConfiguration = new CommandLineConfiguration(); |
| +} |
| + |
| +/// Creates a new [DirectoryWatcher] that watches a temporary directory. |
| +/// |
| +/// When the current schedule completes, the directory is deleted. |
| +DirectoryWatcher createWatcher() { |
| + _ensureSandbox(); |
| + _watcher = new DirectoryWatcher(_sandboxDir); |
| + |
| + // When a listener is first registered on the watcher, it scans the directory |
| + // to see the set of pre-existing files. This way, it doesn't report |
| + // notifications for files that were there before the watcher started. |
| + // |
| + // The scan is done asynchronously so that creating a watcher is fast, and |
| + // we don't have an exposed API to know when that initial scan is done. |
| + // Since many tests need to make changes after that scan, we need to ensure |
| + // that we don't start modifying things until its had time to complete. |
| + // |
| + // This waits some tuned amount of time to ensure that's happened. |
| + schedule(() => new Future.delayed(new Duration(milliseconds: 50))); |
| + |
| + currentSchedule.onComplete.schedule(() { |
| + _nextEvent = 0; |
| + _watcher = null; |
| + }, "reset watcher"); |
| + |
| + return _watcher; |
| +} |
| + |
| +void expectEvent(ChangeType type, String path) { |
| + // Immediately create the future. This ensures we don't register too late and |
| + // drop the event before we receive it. |
| + var future = _watcher.events.elementAt(_nextEvent++).then((event) { |
| + expect(event, new _ChangeMatcher(type, path)); |
| + }); |
| + |
| + // Make sure the schedule is watching it in case it fails. |
| + currentSchedule.wrapFuture(future); |
|
nweiz
2013/07/11 22:29:01
This is meant to wrap a Future; `future` should be
|
| + |
| + // Schedule it so that later file modifications don't occur until after this |
| + // event is received. |
| + schedule(() => future); |
| +} |
| + |
| +void expectAddEvent(String path) { |
| + expectEvent(ChangeType.ADD, pathos.join(_sandboxDir, path)); |
| +} |
| + |
| +void expectModifyEvent(String path) { |
| + expectEvent(ChangeType.MODIFY, pathos.join(_sandboxDir, path)); |
| +} |
| + |
| +void expectRemoveEvent(String path) { |
| + expectEvent(ChangeType.REMOVE, pathos.join(_sandboxDir, path)); |
| +} |
| + |
| +/// Schedules writing a file in the sandbox at [path] with [contents]. |
| +/// |
| +/// If [contents] is omitted, creates an empty file. If [updatedModified] is |
| +/// `false`, the mock file modification time is not changed. |
| +void writeFile(String path, {String contents, bool updateModified}) { |
| + if (contents == null) contents = ""; |
| + if (updateModified == null) updateModified = true; |
| + |
| + _ensureSandbox(); |
| + schedule(() { |
| + var fullPath = pathos.join(_sandboxDir, path); |
| + |
| + // Create any needed subdirectories. |
| + var dir = new Directory(pathos.dirname(fullPath)); |
| + if (!dir.existsSync()) { |
| + dir.createSync(recursive: true); |
| + } |
| + |
| + new File(fullPath).writeAsStringSync(contents); |
| + |
| + // Manually update the mock modification time for the file. |
| + if (updateModified) { |
| + var milliseconds = _mockFileModificationTimes.putIfAbsent(path, () => 0); |
| + _mockFileModificationTimes[path]++; |
| + } |
| + }); |
| +} |
| + |
| +/// Schedules deleting a file in the sandbox at [path]. |
| +void deleteFile(String path) { |
| + _ensureSandbox(); |
| + schedule(() { |
| + new File(pathos.join(_sandboxDir, path)).deleteSync(); |
| + }); |
| +} |
| + |
| +/// Schedules renaming a file in the sandbox from [from] to [to]. |
| +/// |
| +/// If [contents] is omitted, creates an empty file. |
| +void renameFile(String from, String to) { |
| + _ensureSandbox(); |
| + schedule(() { |
| + new File(pathos.join(_sandboxDir, from)).renameSync( |
| + pathos.join(_sandboxDir, to)); |
| + |
| + // Manually update the mock modification time for the file. |
| + var milliseconds = _mockFileModificationTimes.putIfAbsent(to, () => 0); |
| + _mockFileModificationTimes[to]++; |
| + }); |
| +} |
| + |
| +/// Makes sure the sandbox directory has been created for this schedule. |
| +void _ensureSandbox() { |
| + if (_sandboxDir != null) return; |
| + |
| + var dir = new Directory("").createTempSync(); |
| + _sandboxDir = dir.path; |
| + |
| + _mockFileModificationTimes = new Map<String, int>(); |
| + mockGetModificationTime((path) { |
| + path = pathos.relative(path, from: _sandboxDir); |
| + |
| + // Make sure we got a path in the sandbox. |
| + assert(pathos.isRelative(path) && !path.startsWith("..")); |
| + |
| + return new DateTime.fromMillisecondsSinceEpoch( |
| + _mockFileModificationTimes[path]); |
| + }); |
| + |
| + currentSchedule.onComplete.schedule(() { |
| + if (_sandboxDir != null) { |
| + new Directory(_sandboxDir).deleteSync(recursive: true); |
| + _sandboxDir = null; |
| + } |
| + |
| + _mockFileModificationTimes = null; |
| + mockGetModificationTime(null); |
| + }, "delete sandbox"); |
| +} |
| + |
| +/// A [Matcher] for [WatchEvent]s. |
| +class _ChangeMatcher extends BaseMatcher { |
| + /// The expected change. |
| + final ChangeType type; |
| + |
| + /// The expected path. |
| + final String path; |
| + |
| + _ChangeMatcher(this.type, this.path); |
| + |
| + Description describe(Description description) { |
| + description.add("$type $path"); |
| + } |
| + |
| + bool matches(item, Map matchState) => |
| + item is WatchEvent && item.type == type && item.path == path; |
| +} |