Skip to content

Commit fa978cd

Browse files
authored
Remove backup expectations (#2209)
1 parent f5920a2 commit fa978cd

File tree

7 files changed

+45
-120
lines changed

7 files changed

+45
-120
lines changed

pkgs/watcher/test/directory_watcher/file_tests.dart

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import 'package:watcher/src/utils.dart';
1212
import '../utils.dart';
1313

1414
void fileTests() {
15+
for (var i = 0; i != runsPerTest; ++i) {
16+
_fileTests();
17+
}
18+
}
19+
20+
void _fileTests() {
1521
test('does not notify for files that already exist when started', () async {
1622
// Make some pre-existing files.
1723
writeFile('a.txt');
@@ -141,27 +147,13 @@ void fileTests() {
141147
});
142148
});
143149

144-
// Most of the time, when multiple filesystem actions happen in sequence,
145-
// they'll be batched together and the watcher will see them all at once.
146-
// These tests verify that the watcher normalizes and combine these events
147-
// properly. However, very occasionally the events will be reported in
148-
// separate batches, and the watcher will report them as though they occurred
149-
// far apart in time, so each of these tests has a "backup case" to allow for
150-
// that as well.
151150
group('clustered changes', () {
152151
test("doesn't notify when a file is created and then immediately removed",
153152
() async {
154153
writeFile('test.txt');
155154
await startWatcher();
156155
writeFile('file.txt');
157156
deleteFile('file.txt');
158-
159-
// Backup case.
160-
startClosingEventStream();
161-
await allowEvents(() {
162-
expectAddEvent('file.txt');
163-
expectRemoveEvent('file.txt');
164-
});
165157
});
166158

167159
test(
@@ -173,13 +165,7 @@ void fileTests() {
173165
deleteFile('file.txt');
174166
writeFile('file.txt', contents: 're-created');
175167

176-
await allowEither(() {
177-
expectModifyEvent('file.txt');
178-
}, () {
179-
// Backup case.
180-
expectRemoveEvent('file.txt');
181-
expectAddEvent('file.txt');
182-
});
168+
await expectModifyEvent('file.txt');
183169
});
184170

185171
test(
@@ -191,14 +177,7 @@ void fileTests() {
191177
renameFile('old.txt', 'new.txt');
192178
writeFile('old.txt', contents: 're-created');
193179

194-
await allowEither(() {
195-
inAnyOrder([isModifyEvent('old.txt'), isAddEvent('new.txt')]);
196-
}, () {
197-
// Backup case.
198-
expectRemoveEvent('old.txt');
199-
expectAddEvent('new.txt');
200-
expectAddEvent('old.txt');
201-
});
180+
await inAnyOrder([isModifyEvent('old.txt'), isAddEvent('new.txt')]);
202181
});
203182

204183
test(
@@ -210,9 +189,6 @@ void fileTests() {
210189
writeFile('file.txt', contents: 'modified');
211190
deleteFile('file.txt');
212191

213-
// Backup case.
214-
await allowModifyEvent('file.txt');
215-
216192
await expectRemoveEvent('file.txt');
217193
});
218194

@@ -224,10 +200,6 @@ void fileTests() {
224200
writeFile('file.txt', contents: 'modified');
225201

226202
await expectAddEvent('file.txt');
227-
228-
// Backup case.
229-
startClosingEventStream();
230-
await allowModifyEvent('file.txt');
231203
});
232204
});
233205

pkgs/watcher/test/directory_watcher/link_tests.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import 'package:test/test.dart';
99
import '../utils.dart';
1010

1111
void linkTests({required bool isNative}) {
12+
for (var i = 0; i != runsPerTest; ++i) {
13+
_linkTests(isNative: isNative);
14+
}
15+
}
16+
17+
void _linkTests({required bool isNative}) {
1218
test('notifies when a link is added', () async {
1319
createDir('targets');
1420
createDir('links');

pkgs/watcher/test/directory_watcher/linux_test.dart

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,9 @@ void main() {
3232
renameDir('dir/sub', 'sub');
3333
renameDir('sub', 'dir/sub');
3434

35-
await allowEither(() {
36-
inAnyOrder(withPermutations(
37-
(i, j, k) => isRemoveEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
38-
39-
inAnyOrder(withPermutations(
40-
(i, j, k) => isAddEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
41-
}, () {
42-
inAnyOrder(withPermutations(
43-
(i, j, k) => isModifyEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
44-
});
35+
await inAnyOrder(withPermutations(
36+
(i, j, k) => isRemoveEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
37+
await inAnyOrder(withPermutations(
38+
(i, j, k) => isAddEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
4539
});
4640
}

pkgs/watcher/test/directory_watcher/mac_os_test.dart

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,10 @@ void main() {
4646
renameDir('dir/sub', 'sub');
4747
renameDir('sub', 'dir/sub');
4848

49-
await allowEither(() {
50-
inAnyOrder(withPermutations(
51-
(i, j, k) => isRemoveEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
52-
53-
inAnyOrder(withPermutations(
54-
(i, j, k) => isAddEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
55-
}, () {
56-
inAnyOrder(withPermutations(
57-
(i, j, k) => isModifyEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
58-
});
49+
await inAnyOrder(withPermutations(
50+
(i, j, k) => isRemoveEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
51+
await inAnyOrder(withPermutations(
52+
(i, j, k) => isAddEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
5953
});
6054
test('does not suppress files with the same prefix as a directory', () async {
6155
// Regression test for https://github.com/dart-lang/watcher/issues/83

pkgs/watcher/test/file_watcher/file_tests.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ void fileTests({required bool isNative}) {
1313
writeFile('file.txt');
1414
});
1515

16+
for (var i = 0; i != runsPerTest; ++i) {
17+
_fileTests(isNative: isNative);
18+
}
19+
}
20+
21+
void _fileTests({required bool isNative}) {
1622
test("doesn't notify if the file isn't modified", () async {
17-
// TODO(davidmorgan): fix startup race on MacOS.
18-
if (isNative && Platform.isMacOS) {
19-
await Future<void>.delayed(const Duration(milliseconds: 100));
20-
}
2123
await startWatcher(path: 'file.txt');
2224
await expectNoEvents();
2325
});

pkgs/watcher/test/file_watcher/link_tests.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ void linkTests({required bool isNative}) {
1212
writeLink(link: 'link.txt', target: 'target.txt');
1313
});
1414

15+
for (var i = 0; i != runsPerTest; ++i) {
16+
_linkTests(isNative: isNative);
17+
}
18+
}
19+
20+
void _linkTests({required bool isNative}) {
1521
test("doesn't notify if nothing is modified", () async {
1622
await startWatcher(path: 'link.txt');
1723
await expectNoEvents();

pkgs/watcher/test/utils.dart

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import 'package:test_descriptor/test_descriptor.dart' as d;
1212
import 'package:watcher/src/stat.dart';
1313
import 'package:watcher/watcher.dart';
1414

15+
/// Edit this to run fast-running tests many times.
16+
int runsPerTest = 1;
17+
1518
typedef WatcherFactory = Watcher Function(String directory);
1619

1720
/// Sets the function used to create the watcher.
@@ -164,62 +167,18 @@ void startClosingEventStream() async {
164167
await _watcherEvents.cancel(immediate: true);
165168
}
166169

167-
/// A list of [StreamMatcher]s that have been collected using
168-
/// [_collectStreamMatcher].
169-
List<StreamMatcher>? _collectedStreamMatchers;
170-
171-
/// Collects all stream matchers that are registered within [block] into a
172-
/// single stream matcher.
173-
///
174-
/// The returned matcher will match each of the collected matchers in order.
175-
StreamMatcher _collectStreamMatcher(void Function() block) {
176-
var oldStreamMatchers = _collectedStreamMatchers;
177-
var collectedStreamMatchers = _collectedStreamMatchers = <StreamMatcher>[];
178-
try {
179-
block();
180-
return emitsInOrder(collectedStreamMatchers);
181-
} finally {
182-
_collectedStreamMatchers = oldStreamMatchers;
183-
}
184-
}
185-
186-
/// Either add [streamMatcher] as an expectation to [_watcherEvents], or collect
187-
/// it with [_collectStreamMatcher].
170+
/// Add [streamMatcher] as an expectation to [_watcherEvents].
188171
///
189172
/// [streamMatcher] can be a [StreamMatcher], a [Matcher], or a value.
190-
Future _expectOrCollect(Matcher streamMatcher) {
191-
var collectedStreamMatchers = _collectedStreamMatchers;
192-
if (collectedStreamMatchers != null) {
193-
collectedStreamMatchers.add(emits(streamMatcher));
194-
return Future.sync(() {});
195-
} else {
196-
return expectLater(_watcherEvents, emits(streamMatcher));
197-
}
173+
Future _expect(Matcher streamMatcher) {
174+
return expectLater(_watcherEvents, emits(streamMatcher));
198175
}
199176

200177
/// Expects that [matchers] will match emitted events in any order.
201178
///
202179
/// [matchers] may be [Matcher]s or values, but not [StreamMatcher]s.
203-
Future inAnyOrder(Iterable matchers) {
204-
matchers = matchers.toSet();
205-
return _expectOrCollect(emitsInAnyOrder(matchers));
206-
}
207-
208-
/// Expects that the expectations established in either [block1] or [block2]
209-
/// will match the emitted events.
210-
///
211-
/// If both blocks match, the one that consumed more events will be used.
212-
Future allowEither(void Function() block1, void Function() block2) =>
213-
_expectOrCollect(emitsAnyOf(
214-
[_collectStreamMatcher(block1), _collectStreamMatcher(block2)]));
215-
216-
/// Allows the expectations established in [block] to match the emitted events.
217-
///
218-
/// If the expectations in [block] don't match, no error will be raised and no
219-
/// events will be consumed. If this is used at the end of a test,
220-
/// [startClosingEventStream] should be called before it.
221-
Future allowEvents(void Function() block) =>
222-
_expectOrCollect(mayEmit(_collectStreamMatcher(block)));
180+
Future inAnyOrder(Iterable matchers) =>
181+
_expect(emitsInAnyOrder(matchers.toSet()));
223182

224183
/// Returns a StreamMatcher that matches a [WatchEvent] with the given [type]
225184
/// and [path].
@@ -274,24 +233,16 @@ Future<List<WatchEvent>> takeEvents({required Duration duration}) async {
274233

275234
/// Expects that the next event emitted will be for an add event for [path].
276235
Future expectAddEvent(String path) =>
277-
_expectOrCollect(isWatchEvent(ChangeType.ADD, path));
236+
_expect(isWatchEvent(ChangeType.ADD, path));
278237

279238
/// Expects that the next event emitted will be for a modification event for
280239
/// [path].
281240
Future expectModifyEvent(String path) =>
282-
_expectOrCollect(isWatchEvent(ChangeType.MODIFY, path));
241+
_expect(isWatchEvent(ChangeType.MODIFY, path));
283242

284243
/// Expects that the next event emitted will be for a removal event for [path].
285244
Future expectRemoveEvent(String path) =>
286-
_expectOrCollect(isWatchEvent(ChangeType.REMOVE, path));
287-
288-
/// Consumes a modification event for [path] if one is emitted at this point in
289-
/// the schedule, but doesn't throw an error if it isn't.
290-
///
291-
/// If this is used at the end of a test, [startClosingEventStream] should be
292-
/// called before it.
293-
Future allowModifyEvent(String path) =>
294-
_expectOrCollect(mayEmit(isWatchEvent(ChangeType.MODIFY, path)));
245+
_expect(isWatchEvent(ChangeType.REMOVE, path));
295246

296247
/// Track a fake timestamp to be used when writing files. This always increases
297248
/// so that files that are deleted and re-created do not have their timestamp

0 commit comments

Comments
 (0)