Fix flaky test on Windows. (#2278)
diff --git a/pkgs/watcher/test/directory_watcher/file_tests.dart b/pkgs/watcher/test/directory_watcher/file_tests.dart index 912c119..da9a1e9 100644 --- a/pkgs/watcher/test/directory_watcher/file_tests.dart +++ b/pkgs/watcher/test/directory_watcher/file_tests.dart
@@ -493,7 +493,9 @@ // there is no file left behind. renameDir('watched/x', 'b'); - await expectNoEvents(); + expect( + foldDeletes(await takeEvents(duration: const Duration(seconds: 1))), + isEmpty); }); test('subdirectory watching is robust against races', () async {
diff --git a/pkgs/watcher/test/utils.dart b/pkgs/watcher/test/utils.dart index 26de5db..073675d 100644 --- a/pkgs/watcher/test/utils.dart +++ b/pkgs/watcher/test/utils.dart
@@ -205,6 +205,21 @@ return result; } +/// Returns a copy of [events] without events about paths that have a REMOVE +/// event as the last event. +/// +/// For example, drops all events for a path with an ADD then a MODIFY then a +/// REMOVE; keeps all events for a path with ADD, REMOVE, ADD. +/// +/// This allows tests to avoid flakes due to equivalent but different events +/// about transient files. +List<WatchEvent> foldDeletes(List<WatchEvent> events) { + final lastEventByPath = {for (final event in events) event.path: event}; + return events + .where((event) => lastEventByPath[event.path]!.type != ChangeType.REMOVE) + .toList(); +} + /// Expects that the next event emitted will be for an add event for [path]. Future expectAddEvent(String path) => _expect(isWatchEvent(ChangeType.ADD, path));