Skip to content

Commit d92dd12

Browse files
committed
Add test coverage around startup race and missing file on startup.
1 parent ecd7dd5 commit d92dd12

File tree

5 files changed

+86
-17
lines changed

5 files changed

+86
-17
lines changed

pkgs/watcher/test/file_watcher/shared.dart renamed to pkgs/watcher/test/file_watcher/file_tests.dart

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:io';
6+
57
import 'package:test/test.dart';
68

79
import '../utils.dart';
810

9-
void sharedTests() {
11+
void fileTests({required bool isNative}) {
12+
setUp(() {
13+
writeFile('file.txt');
14+
});
15+
1016
test("doesn't notify if the file isn't modified", () async {
1117
await startWatcher(path: 'file.txt');
12-
await pumpEventQueue();
13-
deleteFile('file.txt');
14-
await expectRemoveEvent('file.txt');
18+
await expectNoEvents();
1519
});
1620

1721
test('notifies when a file is modified', () async {
@@ -70,4 +74,22 @@ void sharedTests() {
7074
// startWatcher awaits 'ready'
7175
await startWatcher(path: 'foo/bar/baz');
7276
});
77+
78+
test('throws if file does not exist', () async {
79+
await startWatcher(path: 'other_file.txt');
80+
81+
// TODO(davidmorgan): reconcile differences.
82+
if (isNative && Platform.isLinux) {
83+
expect(expectNoEvents, throwsA(isA<PathNotFoundException>()));
84+
} else {
85+
// The polling watcher and the MacOS watcher do not throw on missing file
86+
// on watch. Instead, they report both creating and modification as
87+
// modifications.
88+
await expectNoEvents();
89+
writeFile('other_file.txt');
90+
await expectModifyEvent('other_file.txt');
91+
writeFile('other_file.txt');
92+
await expectModifyEvent('other_file.txt');
93+
}
94+
});
7395
}

pkgs/watcher/test/file_watcher/native_test.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ import 'package:test/test.dart';
99
import 'package:watcher/src/file_watcher/native.dart';
1010

1111
import '../utils.dart';
12-
import 'shared.dart';
12+
import 'file_tests.dart';
1313

1414
void main() {
1515
watcherFactory = NativeFileWatcher.new;
1616

17-
setUp(() {
18-
writeFile('file.txt');
19-
});
20-
21-
sharedTests();
17+
fileTests(isNative: true);
2218
}

pkgs/watcher/test/file_watcher/polling_test.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:test/test.dart';
65
import 'package:watcher/watcher.dart';
76

87
import '../utils.dart';
9-
import 'shared.dart';
8+
import 'file_tests.dart';
109

1110
void main() {
1211
watcherFactory = (file) =>
1312
PollingFileWatcher(file, pollingDelay: const Duration(milliseconds: 100));
1413

15-
setUp(() {
16-
writeFile('file.txt');
17-
});
18-
19-
sharedTests();
14+
fileTests(isNative: false);
2015
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2025, 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+
import 'dart:io';
6+
7+
import 'package:test/test.dart';
8+
9+
import '../utils.dart';
10+
11+
/// Tests for a startup race that affects MacOS.
12+
///
13+
/// As documented in `File.watch`, changes from shortly _before_ the `watch`
14+
/// method is called might be reported on MacOS. They should be ignored.
15+
void startupRaceTests() {
16+
test('ignores events from before watch starts', () async {
17+
// Write then immediately watch 100 times and count the events received.
18+
var events = 0;
19+
final futures = <Future<void>>[];
20+
for (var i = 0; i != 100; ++i) {
21+
writeFile('file$i.txt');
22+
await startWatcher(path: 'file$i.txt');
23+
futures.add(
24+
waitForEvent().then((event) {
25+
if (event != null) ++events;
26+
}),
27+
);
28+
}
29+
await Future.wait(futures);
30+
31+
// TODO(davidmorgan): the MacOS watcher currently does get unwanted events,
32+
// fix it.
33+
if (Platform.isMacOS) {
34+
expect(events, greaterThan(50));
35+
} else {
36+
expect(events, 0);
37+
}
38+
});
39+
}

pkgs/watcher/test/utils.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ Matcher isModifyEvent(String path) => isWatchEvent(ChangeType.MODIFY, path);
174174
/// [path].
175175
Matcher isRemoveEvent(String path) => isWatchEvent(ChangeType.REMOVE, path);
176176

177+
/// Takes the first event emitted during [duration], or returns `null` if there
178+
/// is none.
179+
Future<WatchEvent?> waitForEvent({
180+
Duration duration = const Duration(seconds: 1),
181+
}) async {
182+
final result = await _watcherEvents.peek
183+
.then<WatchEvent?>((e) => e)
184+
.timeout(duration, onTimeout: () => null);
185+
if (result != null) _watcherEvents.take(1).ignore();
186+
return result;
187+
}
188+
189+
/// Expects that no events are omitted for [duration].
190+
Future expectNoEvents({Duration duration = const Duration(seconds: 1)}) async {
191+
expect(await waitForEvent(duration: duration), isNull);
192+
}
193+
177194
/// Expects that the next event emitted will be for an add event for [path].
178195
Future expectAddEvent(String path) =>
179196
_expectOrCollect(isWatchEvent(ChangeType.ADD, path));

0 commit comments

Comments
 (0)