Chromium Code Reviews| 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. | |
|
Siggi Cherem (dart-lang)
2013/07/10 22:53:53
move the config to unittest?
For now, make sure t
Bob Nystrom
2013/07/10 23:04:39
It depends on stack_trace and, I think, some other
Siggi Cherem (dart-lang)
2013/07/10 23:25:31
Eventually yes, I believe Nathan was already worki
nweiz
2013/07/11 00:25:01
It caused a VM crash. asiva@ is working on it.
Bob Nystrom
2013/07/11 18:33:20
Yup, I'll remove this and switch it out to use com
| |
| 16 import '../../../sdk/lib/_internal/pub/test/command_line_config.dart'; | |
| 17 | |
| 18 var _configured = false; | |
| 19 | |
| 20 String _sandboxDir; | |
| 21 DirectoryWatcher _watcher; | |
| 22 var _nextEvent = 0; | |
|
nweiz
2013/07/11 00:25:01
It would be nice to document all of these variable
Bob Nystrom
2013/07/11 18:33:20
Done.
| |
| 23 | |
| 24 /// The mock modification times (in milliseconds since epoch) for each file. | |
| 25 /// | |
| 26 /// The actual file system has pretty high granularity for file modification | |
|
nweiz
2013/07/11 00:25:01
Do you mean "low granularity"?
Bob Nystrom
2013/07/11 18:33:20
Damn ambiguity of directional metaphors! Changed t
| |
| 27 /// times. This means using the real file system requires us to put delays in | |
| 28 /// the tests to ensure we wait long enough between operations for the mod time | |
| 29 /// to be different. | |
| 30 /// | |
| 31 /// Instead, we'll just mock that out. Each time a file is written, we manually | |
| 32 /// increment the mod time for that file instantly. | |
| 33 Map<String, int> _mockFileModificationTimes; | |
| 34 | |
| 35 void initConfig() { | |
| 36 if (_configured) return; | |
| 37 _configured = true; | |
| 38 unittestConfiguration = new CommandLineConfiguration(); | |
| 39 } | |
| 40 | |
| 41 /// Creates a new [DirectoryWatcher] that watches a temporary directory. | |
| 42 /// | |
| 43 /// When the current schedule completes, the directory is deleted. | |
| 44 DirectoryWatcher createWatcher() { | |
| 45 _ensureSandbox(); | |
|
nweiz
2013/07/11 00:25:01
Is calling this everywhere really cleaner than cre
Bob Nystrom
2013/07/11 18:33:20
I kind of like it. Wrapping test() is a bit tediou
nweiz
2013/07/11 22:29:00
You should just call [setUp] explicitly. You only
Bob Nystrom
2013/07/12 00:31:09
I'm certain more tests will exist and the current
nweiz
2013/07/12 01:04:28
Why will more tests exist? Are you planning on add
Bob Nystrom
2013/07/12 17:46:16
Probably. At the very least, I'm thinking of expan
| |
| 46 _watcher = new DirectoryWatcher(_sandboxDir); | |
| 47 | |
| 48 // When a listener is first registered on the watcher, it scans the directory | |
| 49 // to see the set of pre-existing files. This way, it doesn't report | |
| 50 // notifications for files that were there before the watcher started. | |
| 51 // | |
| 52 // The scan is done asynchronously so that creating a watcher is fast, and | |
| 53 // we don't have an exposed API to know when that initial scan is done. | |
| 54 // Since many tests need to make changes after that scan, we need to ensure | |
| 55 // that we don't start modifying things until its had time to complete. | |
| 56 // | |
| 57 // This waits some tuned amount of time to ensure that's happened. | |
| 58 schedule(() => new Future.delayed(new Duration(milliseconds: 50))); | |
|
nweiz
2013/07/11 00:25:01
Explicit timeouts are very dangerous when dealing
Bob Nystrom
2013/07/11 18:33:20
I know. My plan was to tune it upwards until the b
nweiz
2013/07/11 22:29:00
Given the state of the bots, I think pretty much a
Bob Nystrom
2013/07/12 00:31:09
Done.
Calling it "ready" is nice and ambiguous an
| |
| 59 | |
| 60 currentSchedule.onComplete.schedule(() { | |
| 61 _nextEvent = 0; | |
| 62 _watcher = null; | |
| 63 }, "reset watcher"); | |
| 64 | |
| 65 return _watcher; | |
| 66 } | |
| 67 | |
| 68 void expectEvent(ChangeType type, String path) { | |
| 69 // Immediately create the future. This ensures we don't register too late and | |
| 70 // the event gets dropped before we receive it. | |
|
nweiz
2013/07/11 00:25:01
"the event gets dropped" -> "drop the event"
Bob Nystrom
2013/07/11 18:33:20
Done.
| |
| 71 var future = expect(_watcher.events.elementAt(_nextEvent++).then((event) { | |
| 72 expect(event, new _ChangeMatcher(type, path)); | |
| 73 }), completes); | |
|
nweiz
2013/07/11 00:25:01
expect() doesn't return a Future. I think you want
Bob Nystrom
2013/07/11 18:33:20
Done.
| |
| 74 | |
| 75 // Then schedule the expectation on the future. This ensures that later | |
| 76 // scheduled stuff doesn't happen until after this event is received. | |
| 77 schedule(() => future, "wait for $type $path event"); | |
| 78 } | |
| 79 | |
| 80 void expectAddEvent(String path) { | |
| 81 _ensureSandbox(); | |
|
nweiz
2013/07/11 00:25:01
This seems redundant, since [createWatcher] must b
Bob Nystrom
2013/07/11 18:33:20
Removed.
| |
| 82 expectEvent(ChangeType.ADD, pathos.join(_sandboxDir, path)); | |
| 83 } | |
| 84 | |
| 85 void expectModifyEvent(String path) { | |
| 86 _ensureSandbox(); | |
| 87 expectEvent(ChangeType.MODIFY, pathos.join(_sandboxDir, path)); | |
| 88 } | |
| 89 | |
| 90 void expectRemoveEvent(String path) { | |
| 91 _ensureSandbox(); | |
| 92 expectEvent(ChangeType.REMOVE, pathos.join(_sandboxDir, path)); | |
| 93 } | |
| 94 | |
| 95 /// Writes a file in the sandbox at [path] with [contents]. | |
| 96 /// | |
| 97 /// If [contents] is omitted, creates an empty file. If [updatedModified] is | |
| 98 /// `false`, the mock file modification time is not changed. | |
| 99 void writeFile(String path, {String contents, bool updateModified}) { | |
|
nweiz
2013/07/11 00:25:01
This should be scheduled, as should removeFile and
Bob Nystrom
2013/07/11 18:33:20
Done.
| |
| 100 if (contents == null) contents = ""; | |
| 101 if (updateModified == null) updateModified = true; | |
| 102 | |
| 103 _ensureSandbox(); | |
| 104 | |
| 105 var fullPath = pathos.join(_sandboxDir, path); | |
| 106 | |
| 107 // Create any needed subdirectories. | |
| 108 var dir = new Directory(pathos.dirname(fullPath)); | |
| 109 if (!dir.existsSync()) { | |
| 110 dir.createSync(recursive: true); | |
| 111 } | |
| 112 | |
| 113 new File(fullPath).writeAsStringSync(contents); | |
| 114 | |
| 115 // Manually update the mock modification time for the file. | |
| 116 if (updateModified) { | |
| 117 var milliseconds = _mockFileModificationTimes.putIfAbsent(path, () => 0); | |
| 118 _mockFileModificationTimes[path]++; | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 /// Deletes a file in the sandbox at [path]. | |
| 123 void deleteFile(String path) { | |
| 124 _ensureSandbox(); | |
| 125 new File(pathos.join(_sandboxDir, path)).deleteSync(); | |
| 126 } | |
| 127 | |
| 128 /// Renames a file in the sandbox from [from] to [to]. | |
| 129 /// | |
| 130 /// If [contents] is omitted, creates an empty file. | |
| 131 void renameFile(String from, String to) { | |
| 132 _ensureSandbox(); | |
| 133 | |
| 134 new File(pathos.join(_sandboxDir, from)).renameSync( | |
| 135 pathos.join(_sandboxDir, to)); | |
| 136 | |
| 137 // Manually update the mock modification time for the file. | |
| 138 var milliseconds = _mockFileModificationTimes.putIfAbsent(to, () => 0); | |
| 139 _mockFileModificationTimes[to]++; | |
| 140 } | |
| 141 | |
| 142 /// Wait an appropriate amount of time to ensure that the watcher has had time | |
| 143 /// to walk the entire directory contents. | |
| 144 /// | |
| 145 /// The watcher repeately lists the contents of the directory and notifies | |
| 146 /// based on the differences it finds between successive runs. That means for | |
| 147 /// some tests, we need to ensure that we don't start modifying things until | |
| 148 /// a listing has had time to complete. | |
| 149 /// | |
| 150 /// This waits some tuned amount of time to ensure that's happened. | |
| 151 waitForListing() { | |
| 152 schedule(() => new Future.delayed(new Duration(milliseconds: 500))); | |
| 153 } | |
|
nweiz
2013/07/11 00:25:01
This function isn't called anywhere.
Bob Nystrom
2013/07/11 18:33:20
Oops. Old code. Removed.
| |
| 154 | |
| 155 /// Makes sure the sandbox directory has been created for this schedule. | |
| 156 void _ensureSandbox() { | |
| 157 if (_sandboxDir == null) { | |
|
nweiz
2013/07/11 00:25:01
Feels like this should short-circuit.
Bob Nystrom
2013/07/11 18:33:20
Done.
| |
| 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); | |
|
nweiz
2013/07/11 00:25:01
Add an assert that this works.
Bob Nystrom
2013/07/11 18:33:20
Done.
| |
| 164 return new DateTime.fromMillisecondsSinceEpoch( | |
| 165 _mockFileModificationTimes[path]); | |
| 166 }); | |
| 167 | |
| 168 currentSchedule.onComplete.schedule(() { | |
| 169 if (_sandboxDir != null) { | |
| 170 new Directory(_sandboxDir).deleteSync(recursive: true); | |
| 171 _sandboxDir = null; | |
| 172 } | |
| 173 | |
| 174 _mockFileModificationTimes = null; | |
| 175 mockGetModificationTime(null); | |
| 176 }, "delete sandbox"); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 /// A [Matcher] for [WatchEvent]s. | |
| 181 class _ChangeMatcher extends BaseMatcher { | |
| 182 /// The expected change. | |
| 183 final ChangeType type; | |
| 184 | |
| 185 /// The expected path. | |
| 186 final String path; | |
| 187 | |
| 188 _ChangeMatcher(this.type, this.path); | |
| 189 | |
| 190 Description describe(Description description) { | |
| 191 description.add("$type $path"); | |
| 192 } | |
| 193 | |
| 194 bool matches(item, Map matchState) => | |
| 195 item is WatchEvent && item.type == type && item.path == path; | |
| 196 } | |
| OLD | NEW |