Skip to content

Commit 2d41632

Browse files
committed
Add tests for file watcher behavior with links.
1 parent ecd7dd5 commit 2d41632

File tree

5 files changed

+242
-25
lines changed

5 files changed

+242
-25
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import 'package:test/test.dart';
66

77
import '../utils.dart';
88

9-
void sharedTests() {
9+
void fileTests() {
10+
setUp(() {
11+
writeFile('file.txt');
12+
});
13+
1014
test("doesn't notify if the file isn't modified", () async {
1115
await startWatcher(path: 'file.txt');
12-
await pumpEventQueue();
13-
deleteFile('file.txt');
14-
await expectRemoveEvent('file.txt');
16+
await expectNoEvents();
1517
});
1618

1719
test('notifies when a file is modified', () async {
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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 'package:test/test.dart';
6+
7+
import '../utils.dart';
8+
9+
void linkTests({required bool isNative}) {
10+
setUp(() {
11+
writeFile('target.txt');
12+
writeLink(link: 'link.txt', target: 'target.txt');
13+
});
14+
15+
test("doesn't notify if nothing is modified", () async {
16+
await startWatcher(path: 'link.txt');
17+
await expectNoEvents();
18+
});
19+
20+
test('notifies when a link is overwritten with an identical file', () async {
21+
await startWatcher(path: 'link.txt');
22+
writeFile('link.txt');
23+
await expectModifyEvent('link.txt');
24+
});
25+
26+
test('notifies when a link is overwritten with a different file', () async {
27+
await startWatcher(path: 'link.txt');
28+
writeFile('link.txt', contents: 'modified');
29+
await expectModifyEvent('link.txt');
30+
});
31+
32+
test(
33+
'notifies when a link target is overwritten with an identical file',
34+
() async {
35+
await startWatcher(path: 'link.txt');
36+
writeFile('target.txt');
37+
38+
// TODO(davidmorgan): reconcile differences.
39+
if (isNative) {
40+
await expectModifyEvent('link.txt');
41+
} else {
42+
await expectNoEvents();
43+
}
44+
},
45+
);
46+
47+
test('notifies when a link target is modified', () async {
48+
await startWatcher(path: 'link.txt');
49+
writeFile('target.txt', contents: 'modified');
50+
51+
// TODO(davidmorgan): reconcile differences.
52+
if (isNative) {
53+
await expectModifyEvent('link.txt');
54+
} else {
55+
await expectNoEvents();
56+
}
57+
});
58+
59+
test('notifies when a link is removed', () async {
60+
await startWatcher(path: 'link.txt');
61+
deleteFile('link.txt');
62+
63+
// TODO(davidmorgan): reconcile differences.
64+
if (isNative) {
65+
await expectNoEvents();
66+
} else {
67+
await expectRemoveEvent('link.txt');
68+
}
69+
});
70+
71+
test('notifies when a link target is removed', () async {
72+
await startWatcher(path: 'link.txt');
73+
deleteFile('target.txt');
74+
await expectRemoveEvent('link.txt');
75+
});
76+
77+
test('notifies when a link target is modified multiple times', () async {
78+
await startWatcher(path: 'link.txt');
79+
80+
writeFile('target.txt', contents: 'modified');
81+
82+
// TODO(davidmorgan): reconcile differences.
83+
if (isNative) {
84+
await expectModifyEvent('link.txt');
85+
} else {
86+
await expectNoEvents();
87+
}
88+
89+
writeFile('target.txt', contents: 'modified again');
90+
91+
// TODO(davidmorgan): reconcile differences.
92+
if (isNative) {
93+
await expectModifyEvent('link.txt');
94+
} else {
95+
await expectNoEvents();
96+
}
97+
});
98+
99+
test('notifies when a link is moved away', () async {
100+
await startWatcher(path: 'link.txt');
101+
renameFile('link.txt', 'new.txt');
102+
103+
// TODO(davidmorgan): reconcile differences.
104+
if (isNative) {
105+
await expectNoEvents();
106+
} else {
107+
await expectRemoveEvent('link.txt');
108+
}
109+
});
110+
111+
test('notifies when a link target is moved away', () async {
112+
await startWatcher(path: 'link.txt');
113+
renameFile('target.txt', 'new.txt');
114+
await expectRemoveEvent('link.txt');
115+
});
116+
117+
test('notifies when an identical file is moved over the link', () async {
118+
await startWatcher(path: 'link.txt');
119+
writeFile('old.txt');
120+
renameFile('old.txt', 'link.txt');
121+
122+
// TODO(davidmorgan): reconcile differences.
123+
if (isNative) {
124+
await expectNoEvents();
125+
} else {
126+
await expectModifyEvent('link.txt');
127+
}
128+
});
129+
130+
test('notifies when an different file is moved over the link', () async {
131+
await startWatcher(path: 'link.txt');
132+
writeFile('old.txt', contents: 'modified');
133+
renameFile('old.txt', 'link.txt');
134+
135+
// TODO(davidmorgan): reconcile differences.
136+
if (isNative) {
137+
await expectNoEvents();
138+
} else {
139+
await expectModifyEvent('link.txt');
140+
}
141+
});
142+
143+
test('notifies when an identical file is moved over the target', () async {
144+
await startWatcher(path: 'link.txt');
145+
writeFile('old.txt');
146+
renameFile('old.txt', 'target.txt');
147+
148+
// TODO(davidmorgan): reconcile differences.
149+
if (isNative) {
150+
await expectModifyEvent('link.txt');
151+
} else {
152+
await expectNoEvents();
153+
}
154+
});
155+
156+
test('notifies when a different file is moved over the target', () async {
157+
await startWatcher(path: 'link.txt');
158+
writeFile('old.txt', contents: 'modified');
159+
renameFile('old.txt', 'target.txt');
160+
161+
// TODO(davidmorgan): reconcile differences.
162+
if (isNative) {
163+
await expectModifyEvent('link.txt');
164+
} else {
165+
await expectNoEvents();
166+
}
167+
});
168+
}

pkgs/watcher/test/file_watcher/native_test.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ 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';
13+
import 'link_tests.dart';
1314

1415
void main() {
1516
watcherFactory = NativeFileWatcher.new;
1617

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

pkgs/watcher/test/file_watcher/polling_test.dart

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
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';
9+
import 'link_tests.dart';
1010

1111
void main() {
12-
watcherFactory = (file) =>
13-
PollingFileWatcher(file, pollingDelay: const Duration(milliseconds: 100));
12+
watcherFactory = (file) => PollingFileWatcher(
13+
file,
14+
pollingDelay: const Duration(milliseconds: 100),
15+
);
1416

15-
setUp(() {
16-
writeFile('file.txt');
17-
});
18-
19-
sharedTests();
17+
fileTests();
18+
linkTests(isNative: false);
2019
}

pkgs/watcher/test/utils.dart

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ Future<void> startWatcher({String? path}) async {
6363
final normalized = p.normalize(p.relative(path, from: d.sandbox));
6464

6565
// Make sure we got a path in the sandbox.
66-
assert(p.isRelative(normalized) && !normalized.startsWith('..'),
67-
'Path is not in the sandbox: $path not in ${d.sandbox}');
66+
assert(
67+
p.isRelative(normalized) && !normalized.startsWith('..'),
68+
'Path is not in the sandbox: $path not in ${d.sandbox}',
69+
);
6870

6971
var mtime = _mockFileModificationTimes[normalized];
7072
return mtime != null ? DateTime.fromMillisecondsSinceEpoch(mtime) : null;
@@ -142,8 +144,12 @@ Future inAnyOrder(Iterable matchers) {
142144
///
143145
/// If both blocks match, the one that consumed more events will be used.
144146
Future allowEither(void Function() block1, void Function() block2) =>
145-
_expectOrCollect(emitsAnyOf(
146-
[_collectStreamMatcher(block1), _collectStreamMatcher(block2)]));
147+
_expectOrCollect(
148+
emitsAnyOf([
149+
_collectStreamMatcher(block1),
150+
_collectStreamMatcher(block2),
151+
]),
152+
);
147153

148154
/// Allows the expectations established in [block] to match the emitted events.
149155
///
@@ -174,6 +180,19 @@ Matcher isModifyEvent(String path) => isWatchEvent(ChangeType.MODIFY, path);
174180
/// [path].
175181
Matcher isRemoveEvent(String path) => isWatchEvent(ChangeType.REMOVE, path);
176182

183+
/// Expects that no events are omitted for [duration].
184+
Future expectNoEvents({Duration duration = const Duration(seconds: 1)}) async {
185+
await Future<void>.delayed(duration);
186+
try {
187+
final event = await _watcherEvents.peek
188+
.then<WatchEvent?>((e) => e)
189+
.timeout(duration, onTimeout: null);
190+
expect(event, null);
191+
} on TimeoutException catch (_) {
192+
// Expected.
193+
}
194+
}
195+
177196
/// Expects that the next event emitted will be for an add event for [path].
178197
Future expectAddEvent(String path) =>
179198
_expectOrCollect(isWatchEvent(ChangeType.ADD, path));
@@ -225,6 +244,34 @@ void writeFile(String path, {String? contents, bool? updateModified}) {
225244
}
226245
}
227246

247+
/// Schedules writing a file in the sandbox at [link] pointing to [target].
248+
///
249+
/// If [updateModified] is `false`, the mock file modification time is not
250+
/// changed.
251+
void writeLink({
252+
required String link,
253+
required String target,
254+
bool? updateModified,
255+
}) {
256+
updateModified ??= true;
257+
258+
var fullPath = p.join(d.sandbox, link);
259+
260+
// Create any needed subdirectories.
261+
var dir = Directory(p.dirname(fullPath));
262+
if (!dir.existsSync()) {
263+
dir.createSync(recursive: true);
264+
}
265+
266+
Link(fullPath).createSync(target);
267+
268+
if (updateModified) {
269+
link = p.normalize(link);
270+
271+
_mockFileModificationTimes[link] = _nextTimestamp++;
272+
}
273+
}
274+
228275
/// Schedules deleting a file in the sandbox at [path].
229276
void deleteFile(String path) {
230277
File(p.join(d.sandbox, path)).deleteSync();
@@ -239,8 +286,11 @@ void renameFile(String from, String to) {
239286
// Make sure we always use the same separator on Windows.
240287
to = p.normalize(to);
241288

242-
_mockFileModificationTimes.update(to, (value) => value + 1,
243-
ifAbsent: () => 1);
289+
_mockFileModificationTimes.update(
290+
to,
291+
(value) => value + 1,
292+
ifAbsent: () => 1,
293+
);
244294
}
245295

246296
/// Schedules creating a directory in the sandbox at [path].

0 commit comments

Comments
 (0)