feather_watch 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,11 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
 
5
4
  describe FeatherWatch::Watcher do
6
5
  describe ".initialize" do
7
6
  it "does not crash" do
8
- expect{FeatherWatch::Watcher.new("/",lambda { |e| })}.to_not raise_error
9
- expect{FeatherWatch::Watcher.new("/",lambda { |e| }, false)}.to_not raise_error
7
+ expect{FeatherWatch::Watcher.new(Dir.pwd, lambda { |e| })}.to_not raise_error
8
+ expect{FeatherWatch::Watcher.new(Dir.pwd, lambda { |e| }, false)}.to_not raise_error
10
9
  end
11
10
  it "calls DarwinWatcher.new on darwin" do
12
11
  allow(Kernel).to receive(:require).with('rb-fsevent')
@@ -15,13 +14,14 @@ describe FeatherWatch::Watcher do
15
14
  allow(FeatherWatch::OS).to receive(:windows?) {false}
16
15
  allow(FeatherWatch::OS).to receive(:unix?) {false}
17
16
 
18
- path = "/"
17
+ path = Dir.pwd
19
18
  callback = lambda { |e| }
20
19
  verbose = false
20
+ silence_exceptions = true
21
21
 
22
- expect(FeatherWatch::Core::DarwinWatcher).to receive(:new).with([path],callback,verbose)
22
+ expect(FeatherWatch::Core::DarwinWatcher).to receive(:new).with([path],callback,verbose,silence_exceptions)
23
23
 
24
- FeatherWatch::Watcher.new(path,callback,verbose)
24
+ FeatherWatch::Watcher.new(path,callback,verbose,silence_exceptions)
25
25
  end
26
26
  it "calls LinuxWatcher.new on linux" do
27
27
  allow(Kernel).to receive(:require).with('rb-inotify')
@@ -30,13 +30,14 @@ describe FeatherWatch::Watcher do
30
30
  allow(FeatherWatch::OS).to receive(:windows?) {false}
31
31
  allow(FeatherWatch::OS).to receive(:unix?) {false}
32
32
 
33
- path = "/"
33
+ path = Dir.pwd
34
34
  callback = lambda { |e| }
35
35
  verbose = false
36
+ silence_exceptions = true
36
37
 
37
- expect(FeatherWatch::Core::LinuxWatcher).to receive(:new).with([path],callback,verbose)
38
+ expect(FeatherWatch::Core::LinuxWatcher).to receive(:new).with([path],callback,verbose,silence_exceptions)
38
39
 
39
- FeatherWatch::Watcher.new(path,callback,verbose)
40
+ FeatherWatch::Watcher.new(path,callback,verbose,silence_exceptions)
40
41
  end
41
42
  it "calls WindowsWatcher.new on windows" do
42
43
  allow(Kernel).to receive(:require).with('wdm')
@@ -45,13 +46,14 @@ describe FeatherWatch::Watcher do
45
46
  allow(FeatherWatch::OS).to receive(:windows?) {true}
46
47
  allow(FeatherWatch::OS).to receive(:unix?) {false}
47
48
 
48
- path = "/"
49
+ path = Dir.pwd
49
50
  callback = lambda { |e| }
50
51
  verbose = false
52
+ silence_exceptions = true
51
53
 
52
- expect(FeatherWatch::Core::WindowsWatcher).to receive(:new).with([path],callback,verbose)
54
+ expect(FeatherWatch::Core::WindowsWatcher).to receive(:new).with([path],callback,verbose,silence_exceptions)
53
55
 
54
- FeatherWatch::Watcher.new(path,callback,verbose)
56
+ FeatherWatch::Watcher.new(path,callback,verbose,silence_exceptions)
55
57
  end
56
58
  end
57
59
 
@@ -63,14 +65,15 @@ describe FeatherWatch::Watcher do
63
65
  allow(FeatherWatch::OS).to receive(:windows?) {false}
64
66
  allow(FeatherWatch::OS).to receive(:unix?) {false}
65
67
 
66
- path = "/"
68
+ path = Dir.pwd
67
69
  callback = lambda { |e| }
68
70
  verbose = false
71
+ silence_exceptions = true
69
72
 
70
73
  darwin_spy = spy "DarwinWatcher"
71
- expect(FeatherWatch::Core::DarwinWatcher).to receive(:new).with([path],callback,verbose) {darwin_spy}
74
+ expect(FeatherWatch::Core::DarwinWatcher).to receive(:new).with([path],callback,verbose,silence_exceptions) {darwin_spy}
72
75
  expect(darwin_spy).to receive(:start)
73
- FeatherWatch::Watcher.new(path,callback,verbose).start
76
+ FeatherWatch::Watcher.new(path,callback,verbose,silence_exceptions).start
74
77
  end
75
78
  it "calls start on a LinuxWatcher on linux" do
76
79
  allow(Kernel).to receive(:require).with('rb-inotify')
@@ -79,14 +82,15 @@ describe FeatherWatch::Watcher do
79
82
  allow(FeatherWatch::OS).to receive(:windows?) {false}
80
83
  allow(FeatherWatch::OS).to receive(:unix?) {false}
81
84
 
82
- path = "/"
85
+ path = Dir.pwd
83
86
  callback = lambda { |e| }
84
87
  verbose = false
88
+ silence_exceptions = true
85
89
 
86
90
  linux_spy = spy "LinuxWatcher"
87
- expect(FeatherWatch::Core::LinuxWatcher).to receive(:new).with([path],callback,verbose) {linux_spy}
91
+ expect(FeatherWatch::Core::LinuxWatcher).to receive(:new).with([path],callback,verbose,silence_exceptions) {linux_spy}
88
92
  expect(linux_spy).to receive(:start)
89
- FeatherWatch::Watcher.new(path,callback,verbose).start
93
+ FeatherWatch::Watcher.new(path,callback,verbose,silence_exceptions).start
90
94
  end
91
95
  it "calls start on a WindowsWatcher on windows" do
92
96
  allow(Kernel).to receive(:require).with('wdm')
@@ -95,14 +99,15 @@ describe FeatherWatch::Watcher do
95
99
  allow(FeatherWatch::OS).to receive(:windows?) {true}
96
100
  allow(FeatherWatch::OS).to receive(:unix?) {false}
97
101
 
98
- path = "/"
102
+ path = Dir.pwd
99
103
  callback = lambda { |e| }
100
104
  verbose = false
105
+ silence_exceptions = true
101
106
 
102
107
  windows_spy = spy "WindowsWatcher"
103
- expect(FeatherWatch::Core::WindowsWatcher).to receive(:new).with([path],callback,verbose) {windows_spy}
108
+ expect(FeatherWatch::Core::WindowsWatcher).to receive(:new).with([path],callback,verbose,silence_exceptions) {windows_spy}
104
109
  expect(windows_spy).to receive(:start)
105
- FeatherWatch::Watcher.new(path,callback,verbose).start
110
+ FeatherWatch::Watcher.new(path,callback,verbose,silence_exceptions).start
106
111
  end
107
112
  end
108
113
 
@@ -114,14 +119,15 @@ describe FeatherWatch::Watcher do
114
119
  allow(FeatherWatch::OS).to receive(:windows?) {false}
115
120
  allow(FeatherWatch::OS).to receive(:unix?) {false}
116
121
 
117
- path = "/"
122
+ path = Dir.pwd
118
123
  callback = lambda { |e| }
119
124
  verbose = false
125
+ silence_exceptions = true
120
126
 
121
127
  darwin_spy = spy "DarwinWatcher"
122
- expect(FeatherWatch::Core::DarwinWatcher).to receive(:new).with([path],callback,verbose) {darwin_spy}
128
+ expect(FeatherWatch::Core::DarwinWatcher).to receive(:new).with([path],callback,verbose,silence_exceptions) {darwin_spy}
123
129
  expect(darwin_spy).to receive(:stop)
124
- FeatherWatch::Watcher.new(path,callback,verbose).stop
130
+ FeatherWatch::Watcher.new(path,callback,verbose,silence_exceptions).stop
125
131
  end
126
132
  it "calls stop on a LinuxWatcher on linux" do
127
133
  allow(Kernel).to receive(:require).with('rb-inotify')
@@ -130,14 +136,15 @@ describe FeatherWatch::Watcher do
130
136
  allow(FeatherWatch::OS).to receive(:windows?) {false}
131
137
  allow(FeatherWatch::OS).to receive(:unix?) {false}
132
138
 
133
- path = "/"
139
+ path = Dir.pwd
134
140
  callback = lambda { |e| }
135
141
  verbose = false
142
+ silence_exceptions = true
136
143
 
137
144
  linux_spy = spy "LinuxWatcher"
138
- expect(FeatherWatch::Core::LinuxWatcher).to receive(:new).with([path],callback,verbose) {linux_spy}
145
+ expect(FeatherWatch::Core::LinuxWatcher).to receive(:new).with([path],callback,verbose,silence_exceptions) {linux_spy}
139
146
  expect(linux_spy).to receive(:stop)
140
- FeatherWatch::Watcher.new(path,callback,verbose).stop
147
+ FeatherWatch::Watcher.new(path,callback,verbose,silence_exceptions).stop
141
148
  end
142
149
  it "calls stop on a WindowsWatcher on windows" do
143
150
  allow(Kernel).to receive(:require).with('wdm')
@@ -146,14 +153,20 @@ describe FeatherWatch::Watcher do
146
153
  allow(FeatherWatch::OS).to receive(:windows?) {true}
147
154
  allow(FeatherWatch::OS).to receive(:unix?) {false}
148
155
 
149
- path = "/"
156
+ path = Dir.pwd
150
157
  callback = lambda { |e| }
151
158
  verbose = false
159
+ silence_exceptions = true
152
160
 
153
161
  windows_spy = spy "WindowsWatcher"
154
- expect(FeatherWatch::Core::WindowsWatcher).to receive(:new).with([path],callback,verbose) {windows_spy}
162
+ expect(FeatherWatch::Core::WindowsWatcher).to receive(:new).with([path],callback,verbose,silence_exceptions) {windows_spy}
155
163
  expect(windows_spy).to receive(:stop)
156
- FeatherWatch::Watcher.new(path,callback,verbose).stop
164
+ FeatherWatch::Watcher.new(path,callback,verbose,silence_exceptions).stop
157
165
  end
166
+
167
+ it "raises error when directory does not exist" do
168
+ expect{FeatherWatch::Watcher.new("this is not a valid path",callback,verbose)}.to raise_error
169
+ end
170
+
158
171
  end
159
172
  end
@@ -0,0 +1,340 @@
1
+ require 'spec_helper'
2
+
3
+ module WDM
4
+ end
5
+ class WDM::Monitor
6
+ end
7
+
8
+ describe FeatherWatch::Core::WindowsWatcher do
9
+
10
+ let(:path) {File.join(Dir.pwd, "test_folder")}
11
+ let(:file_path) {File.join(Dir.pwd, "test_folder", "new_file")}
12
+
13
+ after(:each) do
14
+ FileUtils.rm_rf(File.join(Dir.pwd, "test_folder"))
15
+ end
16
+ describe ".initialize" do
17
+ it "catches exceptions in user callback and print to STDERR" do
18
+ wdm_spy = spy("wdm spy")
19
+ expect(WDM::Monitor).to receive(:new).and_return(wdm_spy)
20
+
21
+ callback_spy = spy("Callback Spy")
22
+ verbose = false
23
+ silence_exceptions = false
24
+
25
+ the_callback = nil
26
+ expect(wdm_spy).to receive(:watch_recursively) do |watch_path, *flags, &callback|
27
+ the_callback = callback
28
+ expect(watch_path).to eq(path)
29
+ end
30
+
31
+
32
+ expect(callback_spy).to receive(:call).with({status: :removed ,file: file_path, event: anything}).and_raise("some error")
33
+ expect(STDERR).to receive(:puts).exactly(4).times.with(kind_of(String))
34
+
35
+
36
+ watcher = FeatherWatch::Core::WindowsWatcher.new(path,callback_spy,verbose, silence_exceptions)
37
+ watcher.start
38
+
39
+ event_spy = spy("Event spy")
40
+ allow(event_spy).to receive(:type).and_return(:removed)
41
+ allow(event_spy).to receive(:path).and_return(file_path)
42
+ expect{the_callback.call(event_spy)}.to_not raise_error
43
+
44
+ watcher.stop
45
+ end
46
+ it "executes callback with :removed if file does not exist" do
47
+ wdm_spy = spy("wdm spy")
48
+ expect(WDM::Monitor).to receive(:new).and_return(wdm_spy)
49
+
50
+ callback_spy = spy("Callback Spy")
51
+ verbose = false
52
+ silence_exceptions = true
53
+
54
+ the_callback = nil
55
+ expect(wdm_spy).to receive(:watch_recursively) do |watch_path, *flags, &callback|
56
+ the_callback = callback
57
+ expect(watch_path).to eq(path)
58
+ end
59
+
60
+
61
+ expect(callback_spy).to receive(:call).with({status: :removed ,file: file_path, event: anything})
62
+ expect(STDERR).to_not receive(:puts)
63
+
64
+
65
+ watcher = FeatherWatch::Core::WindowsWatcher.new(path,callback_spy,verbose, silence_exceptions)
66
+ watcher.start
67
+
68
+ event_spy = spy("Event spy")
69
+ allow(event_spy).to receive(:type).and_return(:removed)
70
+ allow(event_spy).to receive(:path).and_return(file_path)
71
+ expect{the_callback.call(event_spy)}.to_not raise_error
72
+
73
+ watcher.stop
74
+ end
75
+
76
+ it "executes callback with :modified if file exist" do
77
+ wdm_spy = spy("wdm spy")
78
+ expect(WDM::Monitor).to receive(:new).and_return(wdm_spy)
79
+
80
+ callback_spy = spy("Callback Spy")
81
+ verbose = false
82
+ silence_exceptions = true
83
+
84
+ the_callback = nil
85
+ expect(wdm_spy).to receive(:watch_recursively) do |watch_path, *flags, &callback|
86
+ the_callback = callback
87
+ expect(watch_path).to eq(path)
88
+ end
89
+
90
+
91
+ expect(callback_spy).to receive(:call).with({status: :added ,file: file_path, event: anything})
92
+ expect(STDERR).to_not receive(:puts)
93
+
94
+ watcher = FeatherWatch::Core::WindowsWatcher.new(path,callback_spy,verbose, silence_exceptions)
95
+ watcher.start
96
+
97
+ event_spy = spy("Event spy")
98
+ allow(event_spy).to receive(:type).and_return(:added)
99
+ allow(event_spy).to receive(:path).and_return(file_path)
100
+ expect{the_callback.call(event_spy)}.to_not raise_error
101
+
102
+ watcher.stop
103
+ end
104
+
105
+
106
+ it "wdm-event with :added produces :added FeatherWatch-event" do
107
+ wdm_spy = spy("wdm spy")
108
+ expect(WDM::Monitor).to receive(:new).and_return(wdm_spy)
109
+
110
+ callback_spy = spy("Callback Spy")
111
+ verbose = false
112
+ silence_exceptions = true
113
+
114
+ the_callback = nil
115
+ expect(wdm_spy).to receive(:watch_recursively) do |watch_path, *flags, &callback|
116
+ the_callback = callback
117
+ expect(watch_path).to eq(path)
118
+ end
119
+
120
+
121
+ expect(callback_spy).to receive(:call).with({status: :added ,file: file_path, event: anything})
122
+ expect(STDERR).to_not receive(:puts)
123
+
124
+ watcher = FeatherWatch::Core::WindowsWatcher.new(path,callback_spy,verbose, silence_exceptions)
125
+ watcher.start
126
+
127
+ event_spy = spy("Event spy")
128
+ allow(event_spy).to receive(:type).and_return(:added)
129
+ allow(event_spy).to receive(:path).and_return(file_path)
130
+ the_callback.call(event_spy)
131
+
132
+ watcher.stop
133
+ end
134
+ it "wdm-event with :renamed_new_file produces :added FeatherWatch-event" do
135
+ wdm_spy = spy("wdm spy")
136
+ expect(WDM::Monitor).to receive(:new).and_return(wdm_spy)
137
+
138
+ callback_spy = spy("Callback Spy")
139
+ verbose = false
140
+ silence_exceptions = true
141
+
142
+ the_callback = nil
143
+ expect(wdm_spy).to receive(:watch_recursively) do |watch_path, *flags, &callback|
144
+ the_callback = callback
145
+ expect(watch_path).to eq(path)
146
+ end
147
+
148
+
149
+ expect(callback_spy).to receive(:call).with({status: :added ,file: file_path, event: anything})
150
+ expect(STDERR).to_not receive(:puts)
151
+
152
+ watcher = FeatherWatch::Core::WindowsWatcher.new(path,callback_spy,verbose, silence_exceptions)
153
+ watcher.start
154
+
155
+ event_spy = spy("Event spy")
156
+ allow(event_spy).to receive(:type).and_return(:renamed_new_file)
157
+ allow(event_spy).to receive(:path).and_return(file_path)
158
+ the_callback.call(event_spy)
159
+
160
+ watcher.stop
161
+ end
162
+
163
+ it "wdm-event with :removed produces :removed FeatherWatch-event" do
164
+ wdm_spy = spy("wdm spy")
165
+ expect(WDM::Monitor).to receive(:new).and_return(wdm_spy)
166
+
167
+ callback_spy = spy("Callback Spy")
168
+ verbose = false
169
+ silence_exceptions = true
170
+
171
+ the_callback = nil
172
+ expect(wdm_spy).to receive(:watch_recursively) do |watch_path, *flags, &callback|
173
+ the_callback = callback
174
+ expect(watch_path).to eq(path)
175
+ end
176
+
177
+
178
+ expect(callback_spy).to receive(:call).with({status: :removed ,file: file_path, event: anything})
179
+ expect(STDERR).to_not receive(:puts)
180
+
181
+ watcher = FeatherWatch::Core::WindowsWatcher.new(path,callback_spy,verbose, silence_exceptions)
182
+ watcher.start
183
+
184
+ event_spy = spy("Event spy")
185
+ allow(event_spy).to receive(:type).and_return(:removed)
186
+ allow(event_spy).to receive(:path).and_return(file_path)
187
+ the_callback.call(event_spy)
188
+
189
+ watcher.stop
190
+ end
191
+
192
+
193
+ it "wdm-event with :renamed_old_file produces :removed FeatherWatch-event" do
194
+ wdm_spy = spy("wdm spy")
195
+ expect(WDM::Monitor).to receive(:new).and_return(wdm_spy)
196
+
197
+ callback_spy = spy("Callback Spy")
198
+ verbose = false
199
+ silence_exceptions = true
200
+
201
+ the_callback = nil
202
+ expect(wdm_spy).to receive(:watch_recursively) do |watch_path, *flags, &callback|
203
+ the_callback = callback
204
+ expect(watch_path).to eq(path)
205
+ end
206
+
207
+
208
+ expect(callback_spy).to receive(:call).with({status: :removed ,file: file_path, event: anything})
209
+ expect(STDERR).to_not receive(:puts)
210
+
211
+ watcher = FeatherWatch::Core::WindowsWatcher.new(path,callback_spy,verbose, silence_exceptions)
212
+ watcher.start
213
+
214
+ event_spy = spy("Event spy")
215
+ allow(event_spy).to receive(:type).and_return(:renamed_old_file)
216
+ allow(event_spy).to receive(:path).and_return(file_path)
217
+ the_callback.call(event_spy)
218
+
219
+ watcher.stop
220
+ end
221
+
222
+ it "wdm-event with :modified produces :modified FeatherWatch-event" do
223
+ wdm_spy = spy("wdm spy")
224
+ expect(WDM::Monitor).to receive(:new).and_return(wdm_spy)
225
+
226
+ callback_spy = spy("Callback Spy")
227
+ verbose = false
228
+ silence_exceptions = true
229
+
230
+ the_callback = nil
231
+ expect(wdm_spy).to receive(:watch_recursively) do |watch_path, *flags, &callback|
232
+ the_callback = callback
233
+ expect(watch_path).to eq(path)
234
+ end
235
+
236
+
237
+ expect(callback_spy).to receive(:call).with({status: :modified ,file: file_path, event: anything})
238
+ expect(STDERR).to_not receive(:puts)
239
+
240
+ watcher = FeatherWatch::Core::WindowsWatcher.new(path,callback_spy,verbose, silence_exceptions)
241
+ watcher.start
242
+
243
+ event_spy = spy("Event spy")
244
+ allow(event_spy).to receive(:type).and_return(:modified)
245
+ allow(event_spy).to receive(:path).and_return(file_path)
246
+ the_callback.call(event_spy)
247
+
248
+ watcher.stop
249
+ end
250
+
251
+ it "wdm-event with :attrib produces :modified FeatherWatch-event" do
252
+ wdm_spy = spy("wdm spy")
253
+ expect(WDM::Monitor).to receive(:new).and_return(wdm_spy)
254
+
255
+ callback_spy = spy("Callback Spy")
256
+ verbose = false
257
+ silence_exceptions = true
258
+
259
+ the_callback = nil
260
+ expect(wdm_spy).to receive(:watch_recursively) do |watch_path, *flags, &callback|
261
+ the_callback = callback
262
+ expect(watch_path).to eq(path)
263
+ end
264
+
265
+
266
+ expect(callback_spy).to receive(:call).with({status: :modified ,file: file_path, event: anything})
267
+ expect(STDERR).to_not receive(:puts)
268
+
269
+ watcher = FeatherWatch::Core::WindowsWatcher.new(path,callback_spy,verbose, silence_exceptions)
270
+ watcher.start
271
+
272
+ event_spy = spy("Event spy")
273
+ allow(event_spy).to receive(:type).and_return(:attrib)
274
+ allow(event_spy).to receive(:path).and_return(file_path)
275
+ the_callback.call(event_spy)
276
+
277
+ watcher.stop
278
+ end
279
+
280
+
281
+ it "Prints error to STDERR when type is nil and verbose" do
282
+ wdm_spy = spy("wdm spy")
283
+ expect(WDM::Monitor).to receive(:new).and_return(wdm_spy)
284
+ allow(STDOUT).to receive(:puts)
285
+
286
+ callback_spy = spy("Callback Spy")
287
+ verbose = true
288
+ silence_exceptions = true
289
+
290
+ the_callback = nil
291
+ expect(wdm_spy).to receive(:watch_recursively) do |watch_path, *flags, &callback|
292
+ the_callback = callback
293
+ expect(watch_path).to eq(path)
294
+ end
295
+
296
+
297
+ expect(callback_spy).to_not receive(:call)
298
+ expect(STDERR).to receive(:puts).exactly(1).times.with(kind_of(String))
299
+
300
+ watcher = FeatherWatch::Core::WindowsWatcher.new(path,callback_spy,verbose, silence_exceptions)
301
+ watcher.start
302
+
303
+ event_spy = spy("Event spy")
304
+ allow(event_spy).to receive(:type).and_return(nil)
305
+ allow(event_spy).to receive(:path).and_return(file_path)
306
+ the_callback.call(event_spy)
307
+
308
+ watcher.stop
309
+ end
310
+ it "Does not print to STDERR when type is nil and not verbose" do
311
+ wdm_spy = spy("wdm spy")
312
+ expect(WDM::Monitor).to receive(:new).and_return(wdm_spy)
313
+
314
+ callback_spy = spy("Callback Spy")
315
+ verbose = false
316
+ silence_exceptions = true
317
+
318
+ the_callback = nil
319
+ expect(wdm_spy).to receive(:watch_recursively) do |watch_path, *flags, &callback|
320
+ the_callback = callback
321
+ expect(watch_path).to eq(path)
322
+ end
323
+
324
+
325
+ expect(callback_spy).to_not receive(:call)
326
+ expect(STDERR).to_not receive(:puts)
327
+
328
+ watcher = FeatherWatch::Core::WindowsWatcher.new(path,callback_spy,verbose, silence_exceptions)
329
+ watcher.start
330
+
331
+ event_spy = spy("Event spy")
332
+ allow(event_spy).to receive(:type).and_return(nil)
333
+ allow(event_spy).to receive(:path).and_return(file_path)
334
+ the_callback.call(event_spy)
335
+
336
+ watcher.stop
337
+ end
338
+ end
339
+
340
+ end