5
5
@TestOn ('windows' )
6
6
library ;
7
7
8
+ import 'dart:async' ;
9
+ import 'dart:io' ;
10
+
11
+ import 'package:path/path.dart' as p;
8
12
import 'package:test/test.dart' ;
9
13
import 'package:watcher/src/directory_watcher/windows.dart' ;
10
14
import 'package:watcher/watcher.dart' ;
@@ -20,4 +24,41 @@ void main() {
20
24
test ('DirectoryWatcher creates a WindowsDirectoryWatcher on Windows' , () {
21
25
expect (DirectoryWatcher ('.' ), const TypeMatcher <WindowsDirectoryWatcher >());
22
26
});
27
+
28
+ test ('Regression test for https://github.com/dart-lang/tools/issues/2110' ,
29
+ () async {
30
+ late StreamSubscription <WatchEvent > sub;
31
+ try {
32
+ final temp = Directory .systemTemp.createTempSync ();
33
+ final watcher = DirectoryWatcher (temp.path);
34
+ final events = < WatchEvent > [];
35
+ sub = watcher.events.listen (events.add);
36
+ await watcher.ready;
37
+
38
+ // Create a file in a directory that doesn't exist. This forces the
39
+ // directory to be created first before the child file.
40
+ //
41
+ // When directory creation is detected by the watcher, it calls
42
+ // `Directory.list` on the directory to determine if there's files that
43
+ // have been created or modified. It's possible that the watcher will have
44
+ // already detected the file creation event before `Directory.list`
45
+ // returns. Before https://github.com/dart-lang/tools/issues/2110 was
46
+ // resolved, the check to ensure an event hadn't already been emitted for
47
+ // the file creation was incorrect, leading to the event being emitted
48
+ // again in some circumstances.
49
+ final file = File (p.join (temp.path, 'foo' , 'file.txt' ))
50
+ ..createSync (recursive: true );
51
+
52
+ // Introduce a short delay to allow for the directory watcher to detect
53
+ // the creation of foo/ and foo/file.txt.
54
+ await Future <void >.delayed (const Duration (seconds: 1 ));
55
+
56
+ // There should only be a single file added event.
57
+ expect (events, hasLength (1 ));
58
+ expect (events.first.toString (),
59
+ WatchEvent (ChangeType .ADD , file.path).toString ());
60
+ } finally {
61
+ await sub.cancel ();
62
+ }
63
+ });
23
64
}
0 commit comments