filewatcher 1.1.1 → 2.0.0.beta5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,354 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'fileutils'
4
- require_relative '../lib/filewatcher'
5
-
6
- describe Filewatcher do
7
- before do
8
- FileUtils.mkdir_p WatchRun::TMP_DIR
9
- end
10
-
11
- after do
12
- FileUtils.rm_r WatchRun::TMP_DIR
13
-
14
- interval = 0.1
15
- wait = 5
16
- count = 0
17
- while File.exist?(WatchRun::TMP_DIR) && count < (wait / interval)
18
- sleep interval
19
- end
20
- end
21
-
22
- describe '#initialize' do
23
- it 'should exclude selected file patterns' do
24
- wr = RubyWatchRun.new(
25
- filewatcher: Filewatcher.new(
26
- File.expand_path('test/tmp/**/*'),
27
- exclude: File.expand_path('test/tmp/**/*.txt')
28
- )
29
- )
30
-
31
- wr.run
32
-
33
- wr.processed.should.be.empty
34
- end
35
-
36
- it 'should handle absolute paths with globs' do
37
- wr = RubyWatchRun.new(
38
- filewatcher: Filewatcher.new(
39
- File.expand_path('test/tmp/**/*')
40
- )
41
- )
42
-
43
- wr.run
44
-
45
- wr.processed.should.equal(
46
- [[wr.filename, :updated]]
47
- )
48
- end
49
-
50
- it 'should handle globs' do
51
- wr = RubyWatchRun.new(
52
- filewatcher: Filewatcher.new('test/tmp/**/*')
53
- )
54
-
55
- wr.run
56
-
57
- wr.processed.should.equal(
58
- [[wr.filename, :updated]]
59
- )
60
- end
61
-
62
- it 'should handle explicit relative paths with globs' do
63
- wr = RubyWatchRun.new(
64
- filewatcher: Filewatcher.new('./test/tmp/**/*')
65
- )
66
-
67
- wr.run
68
-
69
- wr.processed.should.equal(
70
- [[wr.filename, :updated]]
71
- )
72
- end
73
-
74
- it 'should handle explicit relative paths' do
75
- wr = RubyWatchRun.new(
76
- filewatcher: Filewatcher.new('./test/tmp')
77
- )
78
-
79
- wr.run
80
-
81
- wr.processed.should.equal(
82
- [[wr.filename, :updated]]
83
- )
84
- end
85
-
86
- it 'should handle tilde expansion' do
87
- filename = File.expand_path('~/file_watcher_1.txt')
88
-
89
- wr = RubyWatchRun.new(
90
- filename: filename,
91
- filewatcher: Filewatcher.new('~/file_watcher_1.txt')
92
- )
93
-
94
- wr.run
95
-
96
- wr.processed.should.equal(
97
- [[filename, :updated]]
98
- )
99
- end
100
-
101
- it 'should immediately run with corresponding option' do
102
- wr = RubyWatchRun.new(
103
- filewatcher: Filewatcher.new('**/*', immediate: true)
104
- )
105
-
106
- wr.start
107
- wr.stop
108
-
109
- wr.processed.should.equal [['', '']]
110
- wr.watched.should.be > 0
111
- end
112
-
113
- it 'should not be executed without immediate option and changes' do
114
- wr = RubyWatchRun.new(
115
- filewatcher: Filewatcher.new('**/*', immediate: false)
116
- )
117
-
118
- wr.start
119
- wr.stop
120
-
121
- wr.processed.should.be.empty
122
- wr.watched.should.equal 0
123
- end
124
- end
125
-
126
- describe '#watch' do
127
- it 'should detect file deletions' do
128
- wr = RubyWatchRun.new(action: :delete)
129
-
130
- wr.run
131
-
132
- wr.processed.should.equal(
133
- [[wr.filename, :deleted]]
134
- )
135
- end
136
-
137
- it 'should detect file additions' do
138
- wr = RubyWatchRun.new(action: :create)
139
-
140
- wr.run
141
-
142
- wr.processed.should.equal(
143
- [[wr.filename, :created]]
144
- )
145
- end
146
-
147
- it 'should detect file updates' do
148
- wr = RubyWatchRun.new(action: :update)
149
-
150
- wr.run
151
-
152
- wr.processed.should.equal(
153
- [[wr.filename, :updated]]
154
- )
155
- end
156
-
157
- it 'should detect new files in subfolders' do
158
- FileUtils.mkdir_p subfolder = File.expand_path('test/tmp/new_sub_folder')
159
-
160
- wr = RubyWatchRun.new(
161
- filename: File.join(subfolder, 'file.txt'),
162
- action: :create,
163
- every: true
164
- )
165
- wr.run
166
- wr.processed.should.equal(
167
- [
168
- [subfolder, :updated],
169
- [wr.filename, :created]
170
- ]
171
- )
172
- end
173
-
174
- it 'should detect new subfolders' do
175
- subfolder = 'new_sub_folder'
176
-
177
- wr = RubyWatchRun.new(
178
- filename: subfolder,
179
- directory: true,
180
- action: :create
181
- )
182
-
183
- wr.run
184
-
185
- wr.processed.should.equal(
186
- [[wr.filename, :created]]
187
- )
188
- end
189
- end
190
-
191
- describe '#stop' do
192
- it 'should work' do
193
- wr = RubyWatchRun.new
194
-
195
- wr.start
196
-
197
- wr.filewatcher.stop
198
-
199
- # Proves thread successfully joined
200
- wr.thread.join.should.equal wr.thread
201
- end
202
- end
203
-
204
- describe '#pause, #resume' do
205
- it 'should work' do
206
- wr = RubyWatchRun.new(action: :create, every: true)
207
-
208
- wr.start
209
-
210
- wr.filewatcher.pause
211
-
212
- (1..4).each do |n|
213
- File.write("test/tmp/file#{n}.txt", "content#{n}")
214
- end
215
- # Give filewatcher time to respond
216
- sleep 1 * RubyWatchRun::SLEEP_MULTIPLIER
217
-
218
- # update block should not have been called
219
- wr.processed.should.be.empty
220
-
221
- wr.filewatcher.resume
222
- # Give filewatcher time to respond
223
- sleep 1 * RubyWatchRun::SLEEP_MULTIPLIER
224
-
225
- # update block still should not have been called
226
- wr.processed.should.be.empty
227
-
228
- added_files = (5..7).to_a.map do |n|
229
- File.write(file = "test/tmp/file#{n}.txt", "content#{n}")
230
- file
231
- end
232
- # Give filewatcher time to respond
233
- sleep 1 * RubyWatchRun::SLEEP_MULTIPLIER
234
-
235
- wr.filewatcher.stop
236
- wr.stop
237
- wr.processed.map(&:first).should include_all_files(added_files)
238
- end
239
- end
240
-
241
- describe '#finalize' do
242
- it 'should process all remaining changes' do
243
- wr = RubyWatchRun.new(action: :create, every: true)
244
-
245
- wr.start
246
-
247
- wr.filewatcher.stop
248
- wr.thread.join
249
-
250
- added_files = (1..4).to_a.map do |n|
251
- File.write(file = "test/tmp/file#{n}.txt", "content#{n}")
252
- file
253
- end
254
-
255
- wr.filewatcher.finalize
256
-
257
- wr.processed.map(&:first).should include_all_files(added_files)
258
- end
259
- end
260
-
261
- describe 'executable' do
262
- tmp_dir = ShellWatchRun::TMP_DIR
263
-
264
- it 'should run' do
265
- null_output = Gem.win_platform? ? 'NUL' : '/dev/null'
266
- system("#{ShellWatchRun::EXECUTABLE} > #{null_output}")
267
- .should.be.true
268
- end
269
-
270
- it 'should set correct ENV variables for file creation' do
271
- filename = 'foo.txt'
272
-
273
- swr = ShellWatchRun.new(
274
- filename: filename,
275
- action: :create,
276
- dumper: :env
277
- )
278
-
279
- swr.run
280
-
281
- File.read(ShellWatchRun::ENV_FILE)
282
- .should.equal(
283
- %W[
284
- #{tmp_dir}/#{filename}
285
- #{filename}
286
- created
287
- #{tmp_dir}
288
- #{tmp_dir}/#{filename}
289
- test/tmp/#{filename}
290
- ].join(', ')
291
- )
292
- end
293
-
294
- it 'should set correct ENV variables for file deletion' do
295
- filename = 'foo.txt'
296
-
297
- swr = ShellWatchRun.new(
298
- filename: filename,
299
- action: :delete,
300
- dumper: :env
301
- )
302
-
303
- swr.run
304
-
305
- File.read(ShellWatchRun::ENV_FILE)
306
- .should.equal(
307
- %W[
308
- #{tmp_dir}/#{filename}
309
- #{filename}
310
- deleted
311
- #{tmp_dir}
312
- #{tmp_dir}/#{filename}
313
- test/tmp/#{filename}
314
- ].join(', ')
315
- )
316
- end
317
-
318
- it 'should be executed immediately with corresponding option' do
319
- swr = ShellWatchRun.new(
320
- options: { immediate: true },
321
- dumper: :watched
322
- )
323
-
324
- swr.start
325
- swr.stop
326
-
327
- File.exist?(ShellWatchRun::ENV_FILE).should.be.true
328
- File.read(ShellWatchRun::ENV_FILE).should.equal 'watched'
329
- end
330
-
331
- it 'should not be executed without immediate option and changes' do
332
- swr = ShellWatchRun.new(
333
- options: {},
334
- dumper: :watched
335
- )
336
-
337
- swr.start
338
- swr.stop
339
-
340
- File.exist?(ShellWatchRun::ENV_FILE).should.be.false
341
- end
342
-
343
- it 'should work with restart option' do
344
- swr = ShellWatchRun.new(
345
- options: { restart: true }
346
- )
347
-
348
- swr.run
349
-
350
- File.exist?(ShellWatchRun::ENV_FILE).should.be.true
351
- File.read(ShellWatchRun::ENV_FILE).should.equal 'watched'
352
- end
353
- end
354
- end