1
1
require " ./spec_helper"
2
2
3
- TMP_DIR = " spec/ tmp"
3
+ TMP_DIR = File .join( " spec" , " tmp" )
4
4
5
5
describe FileWatcher do
6
6
around_each do |example |
7
7
FileUtils .rm_rf(TMP_DIR )
8
8
9
- with_timeout(2 .second) do
9
+ with_timeout(1 .second) do
10
10
example.run
11
11
end
12
12
rescue TimeoutError
@@ -21,7 +21,9 @@ describe FileWatcher do
21
21
create_file(File .join(TMP_DIR , " example.txt" ))
22
22
end
23
23
24
- FileWatcher .watch(File .join(TMP_DIR , " **" , " *" ), interval: 0.01 .seconds) do |event |
24
+ pattern : String = Path [TMP_DIR , " **" , " *" ].to_posix.to_s
25
+
26
+ FileWatcher .watch(pattern, interval: 0.01 .seconds) do |event |
25
27
event.type.added?.should be_true
26
28
event.path.should eq File .join(TMP_DIR , " example.txt" )
27
29
break
@@ -35,7 +37,9 @@ describe FileWatcher do
35
37
File .delete(File .join(TMP_DIR , " example.txt" ))
36
38
end
37
39
38
- FileWatcher .watch(File .join(TMP_DIR , " **" , " *" ), interval: 0.01 .seconds) do |event |
40
+ pattern : String = Path [TMP_DIR , " **" , " *" ].to_posix.to_s
41
+
42
+ FileWatcher .watch(pattern, interval: 0.01 .seconds) do |event |
39
43
event.type.deleted?.should be_true
40
44
event.path.should eq File .join(TMP_DIR , " example.txt" )
41
45
break
@@ -49,7 +53,9 @@ describe FileWatcher do
49
53
FileUtils .touch(File .join(TMP_DIR , " example.txt" ))
50
54
end
51
55
52
- FileWatcher .watch(File .join(TMP_DIR , " **" , " *" ), interval: 0.01 .seconds) do |event |
56
+ pattern : String = Path [TMP_DIR , " **" , " *" ].to_posix.to_s
57
+
58
+ FileWatcher .watch(pattern, interval: 0.01 .seconds) do |event |
53
59
event.type.changed?.should be_true
54
60
event.path.should eq File .join(TMP_DIR , " example.txt" )
55
61
break
@@ -63,7 +69,9 @@ describe FileWatcher do
63
69
create_file(File .join(TMP_DIR , " text.txt" ))
64
70
end
65
71
66
- FileWatcher .watch(File .join(TMP_DIR , " **" , " *.json" ), interval: 0.01 .seconds) do |event |
72
+ pattern : String = Path [TMP_DIR , " **" , " *.json" ].to_posix.to_s
73
+
74
+ FileWatcher .watch(pattern, interval: 0.01 .seconds) do |event |
67
75
event.type.added?.should be_true
68
76
event.path.should eq File .join(TMP_DIR , " data.json" )
69
77
break
@@ -75,7 +83,9 @@ describe FileWatcher do
75
83
create_file(File .join(TMP_DIR , " example.txt" ))
76
84
end
77
85
78
- FileWatcher .watch(Path [TMP_DIR , " **" , " *" ], interval: 0.01 .seconds) do |event |
86
+ pattern : Path = Path [TMP_DIR , " **" , " *" ].to_posix
87
+
88
+ FileWatcher .watch(pattern, interval: 0.01 .seconds) do |event |
79
89
event.type.added?.should be_true
80
90
event.path.should eq File .join(TMP_DIR , " example.txt" )
81
91
break
@@ -90,30 +100,27 @@ describe FileWatcher do
90
100
91
101
events = [] of FileWatcher ::Event
92
102
93
- FileWatcher .watch(
94
- Path [TMP_DIR , " folder_a" , " *.txt" ],
95
- File .join(TMP_DIR , " folder_b/*.txt" ),
96
- interval: 0.01 .seconds
97
- ) do |event |
103
+ pattern_a : Path = Path [TMP_DIR , " folder_a" , " *.txt" ].to_posix
104
+ pattern_b : String = Path [TMP_DIR , " folder_b" , " *.txt" ].to_posix.to_s
105
+
106
+ FileWatcher .watch(pattern_a, pattern_b, interval: 0.01 .seconds) do |event |
98
107
events << event
99
108
100
109
break if events.size == 2
101
110
end
102
111
103
- events.should contain FileWatcher ::Event .new(File .join(TMP_DIR , " folder_a/ foo.txt" ), :added )
104
- events.should contain FileWatcher ::Event .new(File .join(TMP_DIR , " folder_b/ bar.txt" ), :added )
112
+ events.should contain FileWatcher ::Event .new(File .join(TMP_DIR , " folder_a" , " foo.txt" ), :added )
113
+ events.should contain FileWatcher ::Event .new(File .join(TMP_DIR , " folder_b" , " bar.txt" ), :added )
105
114
end
106
115
107
116
it " accepts match_option" do
108
117
spawn do
109
118
create_file(File .join(TMP_DIR , " .dotfile" ))
110
119
end
111
120
112
- FileWatcher .watch(
113
- Path [TMP_DIR , " **" , " *" ],
114
- interval: 0.01 .seconds,
115
- match_option: File ::MatchOptions ::DotFiles
116
- ) do |event |
121
+ pattern : String = Path [TMP_DIR , " **" , " *" ].to_posix.to_s
122
+
123
+ FileWatcher .watch(pattern, interval: 0.01 .seconds, match_option: File ::MatchOptions ::DotFiles ) do |event |
117
124
event.type.added?.should be_true
118
125
event.path.should eq File .join(TMP_DIR , " .dotfile" )
119
126
break
@@ -128,12 +135,17 @@ describe FileWatcher do
128
135
end
129
136
130
137
spawn do
131
- create_file(File .join(TMP_DIR , " original/ example.txt" ))
138
+ create_file(File .join(TMP_DIR , " original" , " example.txt" ))
132
139
end
133
140
134
- FileWatcher .watch(" spec/tmp/**/*.txt" , interval: 0.01 .seconds, follow_symlinks: true ) do |event |
141
+ pattern : String = Path [TMP_DIR , " **" , " *.txt" ].to_posix.to_s
142
+
143
+ FileWatcher .watch(pattern, interval: 0.01 .seconds, follow_symlinks: true ) do |event |
144
+ # ignores if original file event is emitted first
145
+ next if event.path == File .join(TMP_DIR , " original" , " example.txt" )
146
+
135
147
event.type.added?.should be_true
136
- event.path.should eq File .join(TMP_DIR , " symlink/ example.txt" )
148
+ event.path.should eq File .join(TMP_DIR , " symlink" , " example.txt" )
137
149
break
138
150
end
139
151
end
@@ -143,14 +155,16 @@ describe FileWatcher do
143
155
create_file(File .join(TMP_DIR , " example.txt" ))
144
156
end
145
157
158
+ pattern : String = Path [TMP_DIR , " **" , " *" ].to_posix.to_s
159
+
146
160
started_at = Time .utc
147
161
148
- FileWatcher .watch(File .join( TMP_DIR , " ** " , " * " ), interval: 1 .second ) do |event |
162
+ FileWatcher .watch(pattern, interval: 0.5 .seconds ) do |event |
149
163
event.type.added?.should be_true
150
164
event.path.should eq File .join(TMP_DIR , " example.txt" )
151
165
152
166
now = Time .utc
153
- now.should be_close(started_at + 1 .second , 0.1 .seconds)
167
+ now.should be_close(started_at + 0.5 .seconds , 0.1 .seconds)
154
168
155
169
break
156
170
end
0 commit comments