Skip to content

Commit 6571289

Browse files
committed
Fix test handling of link timestamps.
1 parent 4c7dae1 commit 6571289

File tree

5 files changed

+209
-98
lines changed

5 files changed

+209
-98
lines changed

pkgs/watcher/lib/src/stat.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ MockTimeCallback? _mockTimeCallback;
1616
/// The OS file modification time has pretty rough granularity (like a few
1717
/// seconds) which can make for slow tests that rely on modtime. This lets you
1818
/// replace it with something you control.
19-
void mockGetModificationTime(MockTimeCallback callback) {
19+
void mockGetModificationTime(MockTimeCallback? callback) {
2020
_mockTimeCallback = callback;
2121
}
2222

pkgs/watcher/test/directory_watcher/polling_test.dart

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,26 @@ void main() {
1616
watcherFactory = (dir) => PollingDirectoryWatcher(dir,
1717
pollingDelay: const Duration(milliseconds: 100));
1818

19-
sharedTests();
20-
21-
test('does not notify if the modification time did not change', () async {
22-
writeFile('a.txt', contents: 'before');
23-
writeFile('b.txt', contents: 'before');
24-
await startWatcher();
25-
writeFile('a.txt', contents: 'after', updateModified: false);
26-
writeFile('b.txt', contents: 'after');
27-
await expectModifyEvent('b.txt');
19+
// Filesystem modification times can be low resolution, mock them.
20+
group('with mock mtime', () {
21+
setUp(enableMockModificationTimes);
22+
23+
sharedTests();
24+
25+
test('does not notify if the modification time did not change', () async {
26+
writeFile('a.txt', contents: 'before');
27+
writeFile('b.txt', contents: 'before');
28+
await startWatcher();
29+
writeFile('a.txt', contents: 'after', updateModified: false);
30+
writeFile('b.txt', contents: 'after');
31+
await expectModifyEvent('b.txt');
32+
});
33+
});
34+
35+
// Also test with delayed writes and real mtimes.
36+
group('with real mtime', () {
37+
setUp(enableWaitingForDifferentModificationTimes);
38+
39+
sharedTests();
2840
});
2941
}

pkgs/watcher/test/file_watcher/link_tests.dart

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,25 @@ void linkTests({required bool isNative}) {
2020
test('notifies when a link is overwritten with an identical file', () async {
2121
await startWatcher(path: 'link.txt');
2222
writeFile('link.txt');
23-
await expectModifyEvent('link.txt');
23+
24+
// TODO(davidmorgan): reconcile differences.
25+
if (isNative) {
26+
await expectNoEvents();
27+
} else {
28+
await expectModifyEvent('link.txt');
29+
}
2430
});
2531

2632
test('notifies when a link is overwritten with a different file', () async {
2733
await startWatcher(path: 'link.txt');
2834
writeFile('link.txt', contents: 'modified');
29-
await expectModifyEvent('link.txt');
35+
36+
// TODO(davidmorgan): reconcile differences.
37+
if (isNative) {
38+
await expectNoEvents();
39+
} else {
40+
await expectModifyEvent('link.txt');
41+
}
3042
});
3143

3244
test(
@@ -35,25 +47,15 @@ void linkTests({required bool isNative}) {
3547
await startWatcher(path: 'link.txt');
3648
writeFile('target.txt');
3749

38-
// TODO(davidmorgan): reconcile differences.
39-
if (isNative) {
40-
await expectModifyEvent('link.txt');
41-
} else {
42-
await expectNoEvents();
43-
}
50+
await expectModifyEvent('link.txt');
4451
},
4552
);
4653

4754
test('notifies when a link target is modified', () async {
4855
await startWatcher(path: 'link.txt');
4956
writeFile('target.txt', contents: 'modified');
5057

51-
// TODO(davidmorgan): reconcile differences.
52-
if (isNative) {
53-
await expectModifyEvent('link.txt');
54-
} else {
55-
await expectNoEvents();
56-
}
58+
await expectModifyEvent('link.txt');
5759
});
5860

5961
test('notifies when a link is removed', () async {
@@ -79,21 +81,11 @@ void linkTests({required bool isNative}) {
7981

8082
writeFile('target.txt', contents: 'modified');
8183

82-
// TODO(davidmorgan): reconcile differences.
83-
if (isNative) {
84-
await expectModifyEvent('link.txt');
85-
} else {
86-
await expectNoEvents();
87-
}
84+
await expectModifyEvent('link.txt');
8885

8986
writeFile('target.txt', contents: 'modified again');
9087

91-
// TODO(davidmorgan): reconcile differences.
92-
if (isNative) {
93-
await expectModifyEvent('link.txt');
94-
} else {
95-
await expectNoEvents();
96-
}
88+
await expectModifyEvent('link.txt');
9789
});
9890

9991
test('notifies when a link is moved away', () async {
@@ -145,24 +137,14 @@ void linkTests({required bool isNative}) {
145137
writeFile('old.txt');
146138
renameFile('old.txt', 'target.txt');
147139

148-
// TODO(davidmorgan): reconcile differences.
149-
if (isNative) {
150-
await expectModifyEvent('link.txt');
151-
} else {
152-
await expectNoEvents();
153-
}
140+
await expectModifyEvent('link.txt');
154141
});
155142

156143
test('notifies when a different file is moved over the target', () async {
157144
await startWatcher(path: 'link.txt');
158145
writeFile('old.txt', contents: 'modified');
159146
renameFile('old.txt', 'target.txt');
160147

161-
// TODO(davidmorgan): reconcile differences.
162-
if (isNative) {
163-
await expectModifyEvent('link.txt');
164-
} else {
165-
await expectNoEvents();
166-
}
148+
await expectModifyEvent('link.txt');
167149
});
168150
}

pkgs/watcher/test/file_watcher/polling_test.dart

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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';
56
import 'package:watcher/watcher.dart';
67

78
import '../utils.dart';
@@ -13,7 +14,21 @@ void main() {
1314
watcherFactory = (file) =>
1415
PollingFileWatcher(file, pollingDelay: const Duration(milliseconds: 100));
1516

16-
fileTests(isNative: false);
17-
linkTests(isNative: false);
18-
startupRaceTests(isNative: false);
17+
// Filesystem modification times can be low resolution, mock them.
18+
group('with mock mtime', () {
19+
setUp(enableMockModificationTimes);
20+
21+
fileTests(isNative: false);
22+
linkTests(isNative: false);
23+
startupRaceTests(isNative: false);
24+
});
25+
26+
// Also test with delayed writes and real mtimes.
27+
group('with real mtime', () {
28+
setUp(enableWaitingForDifferentModificationTimes);
29+
fileTests(isNative: false);
30+
linkTests(isNative: false);
31+
// Don't run `startupRaceTests`, polling can't have a race and the test is
32+
// too slow on Windows when waiting for modification times.
33+
});
1934
}

0 commit comments

Comments
 (0)