Skip to content

Commit 883a791

Browse files
committed
Unify watcher tests.
1 parent c8532c8 commit 883a791

File tree

7 files changed

+39
-38
lines changed

7 files changed

+39
-38
lines changed

pkgs/watcher/lib/src/directory_watcher/polling.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,19 @@ class _PollingDirectoryWatcher
160160

161161
if (_events.isClosed) return;
162162

163-
_lastModifieds[file] = modified;
164163
_polledFiles.add(file);
164+
if (modified == null) {
165+
// The file was in the directory listing but has been removed since then.
166+
// Don't add to _lastModifieds, it will be reported as a REMOVE.
167+
return;
168+
}
169+
_lastModifieds[file] = modified;
165170

166171
// Only notify if we're ready to emit events.
167172
if (!isReady) return;
168173

169174
var type = lastModified == null ? ChangeType.ADD : ChangeType.MODIFY;
175+
170176
_events.add(WatchEvent(type, file));
171177
}
172178

pkgs/watcher/test/directory_watcher/file_tests.dart

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:watcher/src/utils.dart';
1111

1212
import '../utils.dart';
1313

14-
void fileTests() {
14+
void fileTests({required bool isNative}) {
1515
test('does not notify for files that already exist when started', () async {
1616
// Make some pre-existing files.
1717
writeFile('a.txt');
@@ -291,6 +291,29 @@ void fileTests() {
291291
})));
292292
});
293293

294+
test(
295+
'emits events for many nested files moved out then immediately back in',
296+
() async {
297+
withPermutations(
298+
(i, j, k) => writeFile('dir/sub/sub-$i/sub-$j/file-$k.txt'));
299+
300+
await startWatcher(path: 'dir');
301+
302+
renameDir('dir/sub', 'sub');
303+
renameDir('sub', 'dir/sub');
304+
305+
if (isNative) {
306+
await inAnyOrder(withPermutations(
307+
(i, j, k) => isRemoveEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
308+
await inAnyOrder(withPermutations(
309+
(i, j, k) => isAddEvent('dir/sub/sub-$i/sub-$j/file-$k.txt')));
310+
} else {
311+
// Polling watchers can't detect this as directory contents mtimes
312+
// aren't updated.
313+
await expectNoEvents();
314+
}
315+
});
316+
294317
test(
295318
'emits events for many files added at once in a subdirectory with the '
296319
'same name as a removed file', () async {

pkgs/watcher/test/directory_watcher/linux_test.dart

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,10 @@ import 'link_tests.dart';
1616
void main() {
1717
watcherFactory = LinuxDirectoryWatcher.new;
1818

19-
fileTests();
19+
fileTests(isNative: true);
2020
linkTests(isNative: true);
2121

2222
test('DirectoryWatcher creates a LinuxDirectoryWatcher on Linux', () {
2323
expect(DirectoryWatcher('.'), const TypeMatcher<LinuxDirectoryWatcher>());
2424
});
25-
26-
test('emits events for many nested files moved out then immediately back in',
27-
() async {
28-
withPermutations(
29-
(i, j, k) => writeFile('dir/sub/sub-$i/sub-$j/file-$k.txt'));
30-
await startWatcher(path: 'dir');
31-
32-
renameDir('dir/sub', 'sub');
33-
renameDir('sub', 'dir/sub');
34-
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')));
39-
});
4025
}

pkgs/watcher/test/directory_watcher/mac_os_test.dart

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'link_tests.dart';
1616
void main() {
1717
watcherFactory = MacOSDirectoryWatcher.new;
1818

19-
fileTests();
19+
fileTests(isNative: true);
2020
linkTests(isNative: true);
2121

2222
test('DirectoryWatcher creates a MacOSDirectoryWatcher on Mac OS', () {
@@ -36,21 +36,6 @@ void main() {
3636
await expectAddEvent('dir/newer.txt');
3737
});
3838

39-
test('emits events for many nested files moved out then immediately back in',
40-
() async {
41-
withPermutations(
42-
(i, j, k) => writeFile('dir/sub/sub-$i/sub-$j/file-$k.txt'));
43-
44-
await startWatcher(path: 'dir');
45-
46-
renameDir('dir/sub', 'sub');
47-
renameDir('sub', 'dir/sub');
48-
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')));
53-
});
5439
test('does not suppress files with the same prefix as a directory', () async {
5540
// Regression test for https://github.com/dart-lang/watcher/issues/83
5641
writeFile('some_name.txt');

pkgs/watcher/test/directory_watcher/polling_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void main() {
2121
group('with mock mtime', () {
2222
setUp(enableMockModificationTimes);
2323

24-
fileTests();
24+
fileTests(isNative: false);
2525
linkTests(isNative: false);
2626

2727
test('does not notify if the modification time did not change', () async {
@@ -38,7 +38,7 @@ void main() {
3838
group('with real mtime', () {
3939
setUp(enableWaitingForDifferentModificationTimes);
4040

41-
fileTests();
41+
fileTests(isNative: false);
4242
linkTests(isNative: false);
4343
});
4444
}

pkgs/watcher/test/directory_watcher/windows_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import 'link_tests.dart';
2121
void main() {
2222
watcherFactory = WindowsDirectoryWatcher.new;
2323

24-
fileTests();
24+
fileTests(isNative: true);
2525
linkTests(isNative: true);
2626

2727
test('DirectoryWatcher creates a WindowsDirectoryWatcher on Windows', () {

pkgs/watcher/test/utils.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ void renameDir(String from, String to) {
393393
final knownFilePaths = mockFileModificationTimes.keys.toList();
394394
for (final filePath in knownFilePaths) {
395395
if (p.isWithin(from, filePath)) {
396-
mockFileModificationTimes[filePath.replaceAll(from, to)] =
396+
final movedPath = '$to/${filePath.substring(from.length + 1)}';
397+
print('$filePath -> $movedPath');
398+
mockFileModificationTimes[movedPath] =
397399
mockFileModificationTimes[filePath]!;
398400
mockFileModificationTimes.remove(filePath);
399401
}

0 commit comments

Comments
 (0)