-
Notifications
You must be signed in to change notification settings - Fork 242
/
record_spec.rb
404 lines (336 loc) · 11.1 KB
/
record_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# frozen_string_literal: true
RSpec.describe Listen::Record do
let(:dir) { instance_double(Pathname, to_s: '/dir') }
let(:record) { Listen::Record.new(dir) }
def dir_entries_for(hash)
hash.each do |dir, entries|
allow(::Dir).to receive(:entries).with(dir) { entries }
end
end
def real_directory(hash)
dir_entries_for(hash)
hash.each do |dir, _|
realpath(dir)
end
end
def file(path)
allow(::Dir).to receive(:entries).with(path).and_raise(Errno::ENOTDIR)
path
end
def lstat(path, stat = nil)
stat ||= instance_double(::File::Stat, mtime: 2.3, mode: 0755, size: 42)
allow(::File).to receive(:lstat).with(path).and_return(stat)
stat
end
def realpath(path)
allow(::File).to receive(:realpath).with(path).and_return(path)
path
end
def symlink(hash_or_dir)
if hash_or_dir.is_a?(String)
allow(::File).to receive(:realpath).with(hash_or_dir).
and_return(hash_or_dir)
else
hash_or_dir.each do |dir, real_path|
allow(::File).to receive(:realpath).with(dir).and_return(real_path)
end
end
end
def record_tree(record)
record.instance_variable_get(:@tree)
end
describe '#update_file' do
context 'with path in watched dir' do
it 'sets path by spliting dirname and basename' do
record.update_file('file.rb', mtime: 1.1)
expect(record_tree(record)).to eq('file.rb' => { mtime: 1.1 })
end
it 'sets path and keeps old data not overwritten' do
record.update_file('file.rb', foo: 1, bar: 2)
record.update_file('file.rb', foo: 3)
watched_dir = record_tree(record)
expect(watched_dir).to eq('file.rb' => { foo: 3, bar: 2 })
end
end
context 'with subdir path' do
it 'sets path by spliting dirname and basename' do
record.update_file('path/file.rb', mtime: 1.1)
expect(record_tree(record)['path']).to eq('file.rb' => { mtime: 1.1 })
end
it 'sets path and keeps old data not overwritten' do
record.update_file('path/file.rb', foo: 1, bar: 2)
record.update_file('path/file.rb', foo: 3)
file_data = record_tree(record)['path']['file.rb']
expect(file_data).to eq(foo: 3, bar: 2)
end
end
end
describe '#add_dir' do
it 'sets itself when .' do
record.add_dir('.')
expect(record_tree(record)).to eq({})
end
it 'sets itself when nil' do
record.add_dir(nil)
expect(record_tree(record)).to eq({})
end
it 'sets itself when empty' do
record.add_dir('')
expect(record_tree(record)).to eq({})
end
it 'correctly sets new directory data' do
record.add_dir('path/subdir')
expect(record_tree(record)).to eq('path/subdir' => {})
end
it 'sets path and keeps old data not overwritten' do
record.add_dir('path/subdir')
record.update_file('path/subdir/file.rb', mtime: 1.1)
record.add_dir('path/subdir')
record.update_file('path/subdir/file2.rb', mtime: 1.2)
record.add_dir('path/subdir')
watched = record_tree(record)
expect(watched.keys).to eq ['path/subdir']
expect(watched['path/subdir'].keys).to eq %w[file.rb file2.rb]
subdir = watched['path/subdir']
expect(subdir['file.rb']).to eq(mtime: 1.1)
expect(subdir['file2.rb']).to eq(mtime: 1.2)
end
end
describe '#unset_path' do
context 'within watched dir' do
context 'when path is present' do
before { record.update_file('file.rb', mtime: 1.1) }
it 'unsets path' do
record.unset_path('file.rb')
expect(record_tree(record)).to eq({})
end
end
context 'when path not present' do
it 'unsets path' do
record.unset_path('file.rb')
expect(record_tree(record)).to eq({})
end
end
end
context 'within subdir' do
context 'when path is present' do
before { record.update_file('path/file.rb', mtime: 1.1) }
it 'unsets path' do
record.unset_path('path/file.rb')
expect(record_tree(record)).to eq('path' => {})
end
end
context 'when path not present' do
it 'unsets path' do
record.unset_path('path/file.rb')
expect(record_tree(record)).to eq({})
end
end
end
end
describe '#file_data' do
context 'with path in watched dir' do
context 'when path is present' do
before { record.update_file('file.rb', mtime: 1.1) }
it 'returns file data' do
expect(record.file_data('file.rb')).to eq(mtime: 1.1)
end
end
context 'path not present' do
it 'return empty hash' do
expect(record.file_data('file.rb')).to be_empty
end
end
end
context 'with path in subdir' do
context 'when path is present' do
before { record.update_file('path/file.rb', mtime: 1.1) }
it 'returns file data' do
expected = { mtime: 1.1 }
expect(record.file_data('path/file.rb')).to eq expected
end
end
context 'path not present' do
it 'return empty hash' do
expect(record.file_data('path/file.rb')).to be_empty
end
end
end
end
describe '#dir_entries' do
context 'in watched dir' do
subject { record.dir_entries('.') }
context 'with no entries' do
it { should be_empty }
end
context 'with file.rb in record' do
before { record.update_file('file.rb', mtime: 1.1) }
it { should eq('file.rb' => { mtime: 1.1 }) }
end
context 'with subdir/file.rb in record' do
before { record.update_file('subdir/file.rb', mtime: 1.1) }
it { should eq('subdir' => {}) }
end
end
context 'when there is a file with the same name as a dir' do
subject { record.dir_entries('cypress') }
before do
record.update_file('cypress.json', mtime: 1.1)
record.update_file('cypress/README.md', mtime: 1.2)
record.update_file('a/b/cypress/d', mtime: 1.3)
record.update_file('a/b/c/cypress', mtime: 1.3)
end
it { should eq('README.md' => { mtime: 1.2 }) }
end
context 'when there is a file with a similar name to a dir' do
subject { record.dir_entries('app') }
before do
record.update_file('appspec.yml', mtime: 1.1)
record.update_file('app/README.md', mtime: 1.2)
record.update_file('spec/app/foo', mtime: 1.3)
end
it { should eq('README.md' => { mtime: 1.2 }) }
end
context 'in subdir /path' do
subject { record.dir_entries('path') }
context 'with no entries' do
it { should be_empty }
end
context 'with path/file.rb already in record' do
before { record.update_file('path/file.rb', mtime: 1.1) }
it { should eq('file.rb' => { mtime: 1.1 }) }
end
context 'with path/subdir' do
before { record.add_dir('path/subdir') }
it { should eq('subdir' => {}) }
end
end
end
describe '#build' do
let(:dir1) { Pathname('/dir1') }
before do
stubs = {
::File => %w[lstat realpath],
::Dir => %w[entries exist?]
}
stubs.each do |klass, meths|
meths.each do |meth|
allow(klass).to receive(meth.to_sym) do |*args|
fail "stub called: #{klass}.#{meth}(#{args.map(&:inspect) * ', '})"
end
end
end
end
it 're-inits paths' do
real_directory('/dir1' => [])
real_directory('/dir' => [])
record.update_file('path/file.rb', mtime: 1.1)
record.build
expect(record_tree(record)).to eq({})
expect(record.file_data('path/file.rb')).to be_empty
end
let(:foo_stat) { instance_double(::File::Stat, mtime: 1.0, mode: 0644, size: 42) }
let(:bar_stat) { instance_double(::File::Stat, mtime: 2.3, mode: 0755, size: 42) }
context 'with no subdirs' do
before do
real_directory('/dir' => %w[foo bar])
lstat(file('/dir/foo'), foo_stat)
lstat(file('/dir/bar'), bar_stat)
real_directory('/dir2' => [])
end
it 'builds record' do
record.build
expect(record_tree(record)).
to eq(
'foo' => { mtime: 1.0, mode: 0644, size: 42 },
'bar' => { mtime: 2.3, mode: 0755, size: 42 })
end
end
context 'with subdir containing files' do
before do
real_directory('/dir' => %w[dir1 dir2])
real_directory('/dir/dir1' => %w[foo])
real_directory('/dir/dir1/foo' => %w[bar])
lstat(file('/dir/dir1/foo/bar'))
real_directory('/dir/dir2' => [])
end
it 'builds record' do
record.build
expect(record_tree(record)).
to eq(
'dir1' => {},
'dir1/foo' => { 'bar' => { mtime: 2.3, mode: 0755, size: 42 } },
'dir2' => {}
)
end
end
context 'with subdir containing dirs' do
before do
real_directory('/dir' => %w[dir1 dir2])
real_directory('/dir/dir1' => %w[foo])
real_directory('/dir/dir1/foo' => %w[bar baz])
real_directory('/dir/dir1/foo/bar' => [])
real_directory('/dir/dir1/foo/baz' => [])
real_directory('/dir/dir2' => [])
allow(::File).to receive(:realpath) { |path| path }
end
it 'builds record' do
record.build
expect(record_tree(record)).
to eq(
'dir1' => {},
'dir1/foo' => {},
'dir1/foo/bar' => {},
'dir1/foo/baz' => {},
'dir2' => {}
)
end
end
context 'with subdir containing symlink to parent' do
subject { record.paths }
before do
real_directory('/dir' => %w[dir1 dir2])
real_directory('/dir/dir1' => %w[foo])
dir_entries_for('/dir/dir1/foo' => %w[dir1])
symlink('/dir/dir1/foo' => '/dir/dir1')
real_directory('/dir/dir2' => [])
end
it 'shows a warning' do
expect_any_instance_of(Listen::Record::SymlinkDetector).to receive(:warn).
with(/directory is already being watched/)
record.build
# expect { record.build }.
# to raise_error(RuntimeError, /Failed due to looped symlinks/)
end
end
context 'with a normal symlinked directory to another' do
subject { record.paths }
before do
real_directory('/dir' => %w[dir1])
real_directory('/dir/dir1' => %w[foo])
symlink('/dir/dir1/foo' => '/dir/dir2')
dir_entries_for('/dir/dir1/foo' => %w[bar])
lstat(realpath(file('/dir/dir1/foo/bar')))
real_directory('/dir/dir2' => %w[bar])
lstat(file('/dir/dir2/bar'))
end
it 'shows message' do
expect(STDERR).to_not receive(:puts)
record.build
end
end
context 'with subdir containing symlinked file' do
subject { record.paths }
before do
real_directory('/dir' => %w[dir1 dir2])
real_directory('/dir/dir1' => %w[foo])
lstat(file('/dir/dir1/foo'))
real_directory('/dir/dir2' => [])
end
it 'shows a warning' do
expect(STDERR).to_not receive(:puts)
record.build
end
end
end
end