| 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:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| 11 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
| 12 import 'package:scheduled_test/scheduled_test.dart'; | 12 import 'package:scheduled_test/scheduled_test.dart'; |
| 13 import 'package:unittest/compact_vm_config.dart'; | 13 import 'package:unittest/compact_vm_config.dart'; |
| 14 import 'package:watcher/watcher.dart'; | 14 import 'package:watcher/watcher.dart'; |
| 15 import 'package:watcher/src/stat.dart'; | 15 import 'package:watcher/src/stat.dart'; |
| 16 import 'package:watcher/src/utils.dart'; | 16 import 'package:watcher/src/utils.dart'; |
| 17 | 17 |
| 18 // TODO(nweiz): remove this when issue 15042 is fixed. |
| 19 import 'package:watcher/src/directory_watcher/mac_os.dart'; |
| 20 |
| 18 /// The path to the temporary sandbox created for each test. All file | 21 /// The path to the temporary sandbox created for each test. All file |
| 19 /// operations are implicitly relative to this directory. | 22 /// operations are implicitly relative to this directory. |
| 20 String _sandboxDir; | 23 String _sandboxDir; |
| 21 | 24 |
| 22 /// The [DirectoryWatcher] being used for the current scheduled test. | 25 /// The [DirectoryWatcher] being used for the current scheduled test. |
| 23 DirectoryWatcher _watcher; | 26 DirectoryWatcher _watcher; |
| 24 | 27 |
| 25 /// The index in [_watcher]'s event stream for the next event. When event | 28 /// The index in [_watcher]'s event stream for the next event. When event |
| 26 /// expectations are set using [expectEvent] (et. al.), they use this to | 29 /// expectations are set using [expectEvent] (et. al.), they use this to |
| 27 /// expect a series of events in order. | 30 /// expect a series of events in order. |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 } | 111 } |
| 109 | 112 |
| 110 /// The stream of events from the watcher started with [startWatcher]. | 113 /// The stream of events from the watcher started with [startWatcher]. |
| 111 Stream _watcherEvents; | 114 Stream _watcherEvents; |
| 112 | 115 |
| 113 /// Creates a new [DirectoryWatcher] that watches a temporary directory and | 116 /// Creates a new [DirectoryWatcher] that watches a temporary directory and |
| 114 /// starts monitoring it for events. | 117 /// starts monitoring it for events. |
| 115 /// | 118 /// |
| 116 /// If [dir] is provided, watches a subdirectory in the sandbox with that name. | 119 /// If [dir] is provided, watches a subdirectory in the sandbox with that name. |
| 117 void startWatcher({String dir}) { | 120 void startWatcher({String dir}) { |
| 121 var testCase = currentTestCase.description; |
| 122 if (MacOSDirectoryWatcher.logDebugInfo) { |
| 123 print("starting watcher for $testCase"); |
| 124 } |
| 125 |
| 118 // We want to wait until we're ready *after* we subscribe to the watcher's | 126 // We want to wait until we're ready *after* we subscribe to the watcher's |
| 119 // events. | 127 // events. |
| 120 _watcher = createWatcher(dir: dir, waitForReady: false); | 128 _watcher = createWatcher(dir: dir, waitForReady: false); |
| 121 | 129 |
| 122 // Schedule [_watcher.events.listen] so that the watcher doesn't start | 130 // Schedule [_watcher.events.listen] so that the watcher doesn't start |
| 123 // watching [dir] before it exists. Expose [_watcherEvents] immediately so | 131 // watching [dir] before it exists. Expose [_watcherEvents] immediately so |
| 124 // that it can be accessed synchronously after this. | 132 // that it can be accessed synchronously after this. |
| 125 _watcherEvents = futureStream(schedule(() { | 133 _watcherEvents = futureStream(schedule(() { |
| 126 var allEvents = new Queue(); | 134 var allEvents = new Queue(); |
| 127 var subscription = _watcher.events.listen(allEvents.add, | 135 var subscription = _watcher.events.listen(allEvents.add, |
| 128 onError: currentSchedule.signalError); | 136 onError: currentSchedule.signalError); |
| 129 | 137 |
| 130 currentSchedule.onComplete.schedule(() { | 138 currentSchedule.onComplete.schedule(() { |
| 139 if (MacOSDirectoryWatcher.logDebugInfo) { |
| 140 print("stopping watcher for $testCase"); |
| 141 } |
| 142 |
| 131 var numEvents = _nextEvent; | 143 var numEvents = _nextEvent; |
| 132 subscription.cancel(); | 144 subscription.cancel(); |
| 133 _nextEvent = 0; | 145 _nextEvent = 0; |
| 134 _watcher = null; | 146 _watcher = null; |
| 135 | 147 |
| 136 // If there are already errors, don't add this to the output and make | 148 // If there are already errors, don't add this to the output and make |
| 137 // people think it might be the root cause. | 149 // people think it might be the root cause. |
| 138 if (currentSchedule.errors.isEmpty) { | 150 if (currentSchedule.errors.isEmpty) { |
| 139 expect(allEvents, hasLength(numEvents)); | 151 expect(allEvents, hasLength(numEvents)); |
| 140 } else { | 152 } else { |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 void withPermutations(callback(int i, int j, int k), {int limit}) { | 303 void withPermutations(callback(int i, int j, int k), {int limit}) { |
| 292 if (limit == null) limit = 3; | 304 if (limit == null) limit = 3; |
| 293 for (var i = 0; i < limit; i++) { | 305 for (var i = 0; i < limit; i++) { |
| 294 for (var j = 0; j < limit; j++) { | 306 for (var j = 0; j < limit; j++) { |
| 295 for (var k = 0; k < limit; k++) { | 307 for (var k = 0; k < limit; k++) { |
| 296 callback(i, j, k); | 308 callback(i, j, k); |
| 297 } | 309 } |
| 298 } | 310 } |
| 299 } | 311 } |
| 300 } | 312 } |
| OLD | NEW |