Skip to content

Commit 1e457b1

Browse files
author
Michael Kessler
committed
Initial listener spec (without options)
1 parent 191bc74 commit 1e457b1

File tree

3 files changed

+352
-0
lines changed

3 files changed

+352
-0
lines changed

spec/listener/listener_spec.rb

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
require 'spec_helper'
2+
require 'support/fixtures'
3+
4+
describe Listener do
5+
describe '.listen' do
6+
context 'single file operations' do
7+
context 'when a file is created' do
8+
it 'detects the added file' do
9+
fixtures do |path|
10+
modified, added, removed = listen(path) do
11+
touch 'new_file.rb'
12+
end
13+
14+
added.should =~ %w(newfile.rb)
15+
modified.should be_empty
16+
removed.should be_empty
17+
end
18+
end
19+
20+
context 'given a new created directory' do
21+
it 'detects the added file' do
22+
fixtures do |path|
23+
modified, added, removed = listen(path) do
24+
mkdir 'a_directory'
25+
touch 'a_directory/new_file.rb'
26+
end
27+
28+
added.should =~ %w(a_directory/newfile.rb)
29+
modified.should be_empty
30+
removed.should be_empty
31+
end
32+
end
33+
end
34+
35+
context 'given an existing directory' do
36+
it 'detects the added file' do
37+
fixtures do |path|
38+
mkdir 'a_directory'
39+
40+
modified, added, removed = listen(path) do
41+
touch 'a_directory/new_file.rb'
42+
end
43+
44+
added.should =~ %w(a_directory/newfile.rb)
45+
modified.should be_empty
46+
removed.should be_empty
47+
end
48+
end
49+
end
50+
end
51+
52+
context 'when a file is modified' do
53+
it 'detects the modified file' do
54+
fixtures do |path|
55+
touch 'exisiting_file.txt'
56+
57+
modified, added, removed = listen(path) do
58+
touch 'exisiting_file.txt'
59+
end
60+
61+
added.should be_empty
62+
modified.should =~ %w(existing_file.txt)
63+
removed.should be_empty
64+
end
65+
end
66+
67+
context 'given a hidden file' do
68+
it 'detects the modified file' do
69+
fixtures do |path|
70+
touch '.hidden'
71+
72+
modified, added, removed = listen(path) do
73+
touch '.hidden'
74+
end
75+
76+
added.should be_empty
77+
modified.should =~ %w(.hidden)
78+
removed.should be_empty
79+
end
80+
end
81+
end
82+
83+
context 'given a file mode change' do
84+
it 'does not detect the mode change' do
85+
fixtures do |path|
86+
touch 'run.rb'
87+
88+
modified, added, removed = listen(path) do
89+
chmod 0777, 'run.rb'
90+
end
91+
92+
added.should be_empty
93+
modified.should be_empty
94+
removed.should be_empty
95+
end
96+
end
97+
end
98+
99+
context 'given an existing directory' do
100+
fixtures do |path|
101+
mkdir 'a_directory'
102+
touch 'a_directory/exisiting_file.txt'
103+
104+
modified, added, removed = listen(path) do
105+
touch 'a_directory/exisiting_file.txt'
106+
end
107+
108+
added.should be_empty
109+
modified.should =~ %w(a_directory/existing_file.txt)
110+
removed.should be_empty
111+
end
112+
113+
end
114+
end
115+
116+
context 'when a file is moved' do
117+
it 'detects the file move' do
118+
fixtures do |path|
119+
touch 'move_me.txt'
120+
121+
modified, added, removed = listen(path) do
122+
mv 'move_me.txt', 'new_name.txt'
123+
end
124+
125+
added.should =~ %w(new_name.txt)
126+
modified.should be_empty
127+
removed.should =~ %w(move_me.txt)
128+
end
129+
end
130+
131+
context 'given an existing directory' do
132+
it 'detects the file move into the directory' do
133+
fixtures do |path|
134+
mkdir 'the_directory'
135+
touch 'move_me.txt'
136+
137+
modified, added, removed = listen(path) do
138+
mv 'move_me.txt', 'the_directory/move_me.txt'
139+
end
140+
141+
added.should =~ %w(the_directory/move_me.txt)
142+
modified.should be_empty
143+
removed.should =~ %w(move_me.txt)
144+
end
145+
end
146+
147+
it 'detects a file move out of the directory' do
148+
fixtures do |path|
149+
mkdir 'the_directory'
150+
touch 'the_directory/move_me.txt'
151+
152+
modified, added, removed = listen(path) do
153+
mv 'the_directory/move_me.txt', 'i_am_here.txt'
154+
end
155+
156+
added.should =~ %w(i_am_here.txt)
157+
modified.should be_empty
158+
removed.should =~ %w(the_directory/move_me.txt)
159+
end
160+
end
161+
162+
it 'detects a file move between two directories' do
163+
fixtures do |path|
164+
mkdir 'from_directory'
165+
touch 'from_directory/move_me.txt'
166+
mkdir 'to_directory'
167+
168+
modified, added, removed = listen(path) do
169+
mv 'from_directory/move_me.txt', 'to_directory/move_me.txt'
170+
end
171+
172+
added.should =~ %w(to_directory/move_me.txt)
173+
modified.should be_empty
174+
removed.should =~ %w(from_directory/move_me.txt)
175+
end
176+
end
177+
end
178+
end
179+
180+
context 'when a file is deleted' do
181+
it 'detects the file removal' do
182+
fixtures do |path|
183+
touch 'unnecessary.txt'
184+
185+
modified, added, removed = listen(path) do
186+
rm 'unnecessary.txt'
187+
end
188+
189+
added.should be_empty
190+
modified.should be_empty
191+
removed.should =~ %w(unnecessary.txt)
192+
end
193+
end
194+
195+
context 'given an existing directory' do
196+
it 'detects the file removal' do
197+
fixtures do |path|
198+
mkdir 'a_directory'
199+
touch 'a_directory/do_not_use.rb'
200+
201+
modified, added, removed = listen(path) do
202+
rm 'a_directory/do_not_use.rb'
203+
end
204+
205+
added.should be_empty
206+
modified.should be_empty
207+
removed.should =~ %w(a_directory/do_not_use.rb)
208+
end
209+
end
210+
end
211+
end
212+
end
213+
214+
context 'multiple file operations' do
215+
it 'detects the added files' do
216+
fixtures do |path|
217+
modified, added, removed = listen(path) do
218+
touch 'a_file.rb'
219+
touch 'b_file.rb'
220+
mkdir 'the_directory'
221+
touch 'the_directory/a_file.rb'
222+
touch 'the_directory/b_file.rb'
223+
end
224+
225+
added.should =~ %w(a_file.rb b_file.rb the_directory/a_file.rb the_directory/b_file.rb)
226+
modified.should be_empty
227+
removed.should be_empty
228+
end
229+
end
230+
231+
it 'detects the modified files' do
232+
fixtures do |path|
233+
touch 'a_file.rb'
234+
touch 'b_file.rb'
235+
mkdir 'the_directory'
236+
touch 'the_directory/a_file.rb'
237+
touch 'the_directory/b_file.rb'
238+
239+
modified, added, removed = listen(path) do
240+
touch 'b_file.rb'
241+
touch 'the_directory/a_file.rb'
242+
end
243+
244+
added.should be_empty
245+
modified.should =~ %w(b_file.rb the_directory/a_file.rb)
246+
removed.should be_empty
247+
end
248+
end
249+
250+
it 'detects the removed files' do
251+
fixtures do |path|
252+
touch 'a_file.rb'
253+
touch 'b_file.rb'
254+
mkdir 'the_directory'
255+
touch 'the_directory/a_file.rb'
256+
touch 'the_directory/b_file.rb'
257+
258+
modified, added, removed = listen(path) do
259+
rm 'b_file.rb'
260+
rm 'the_directory/a_file.rb'
261+
end
262+
263+
added.should be_empty
264+
modified.should be_empty
265+
removed.should =~ %w(b_file.rb the_directory/a_file.rb)
266+
end
267+
end
268+
end
269+
270+
context 'single directory operations' do
271+
it 'detects a moved directory' do
272+
fixtures do |path|
273+
mkdir 'the_directory'
274+
touch 'the_directory/a_file.rb'
275+
touch 'the_directory/b_file.rb'
276+
277+
modified, added, removed = listen(path) do
278+
mv 'the_directory', 'renamed'
279+
end
280+
281+
added.should =~ %w(renamed/a_file.rb renamed/b_file.rb)
282+
modified.should be_empty
283+
removed.should =~ %w(the_directory/a_file.rb the_directory/b_file.rb)
284+
end
285+
end
286+
287+
it 'detects a removed directory' do
288+
fixtures do |path|
289+
mkdir 'the_directory'
290+
touch 'the_directory/a_file.rb'
291+
touch 'the_directory/b_file.rb'
292+
293+
modified, added, removed = listen(path) do
294+
rm_rf 'the_directory'
295+
end
296+
297+
added.should be_empty
298+
modified.should be_empty
299+
removed.should =~ %w(the_directory/a_file.rb the_directory/b_file.rb)
300+
end
301+
end
302+
end
303+
end
304+
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'listener'
2+
13
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
24
RSpec.configure do |config|
35
config.treat_symbols_as_metadata_keys_with_true_values = true

spec/support/fixtures.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'tmpdir'
2+
3+
include FileUtils
4+
5+
# Prepares the temporary fixture directory and
6+
# cleans it afterwards.
7+
#
8+
# @yield [path] an empty fixture directory
9+
# @yieldparam [String] path the path to the fixture directory
10+
#
11+
def fixtures
12+
path = File.expand_path(File.join(Dir.tmpdir, 'listener'))
13+
FileUtils.mkdir_p(path)
14+
15+
pwd = FileUtils.pwd
16+
FileUtils.cd(path)
17+
18+
yield(path)
19+
20+
ensure
21+
FileUtils.cd pwd
22+
FileUtils.rm_rf(path) if File.exists?(path)
23+
end
24+
25+
# Start the listener
26+
#
27+
# @param [String] path the path to watch
28+
# @param [Hash] options the listener options
29+
# @yield The block to listen for file changes
30+
# @return [Array, Array, Array] the file changes
31+
#
32+
def listen(path, options={})
33+
modified = []
34+
added = []
35+
removed = []
36+
37+
watcher = Listener.listen(path, options) do |m, a, r|
38+
modified += m
39+
added += a
40+
removed += r
41+
end
42+
43+
yield
44+
45+
[modified, added, removed]
46+
end

0 commit comments

Comments
 (0)