memfs 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +29 -0
- data/CHANGELOG.md +7 -0
- data/Guardfile +5 -6
- data/README.md +6 -6
- data/Rakefile +9 -1
- data/lib/memfs.rb +6 -4
- data/lib/memfs/dir.rb +17 -21
- data/lib/memfs/fake/entry.rb +2 -2
- data/lib/memfs/fake/file/content.rb +3 -2
- data/lib/memfs/file.rb +54 -116
- data/lib/memfs/file/stat.rb +2 -2
- data/lib/memfs/file_system.rb +11 -13
- data/lib/memfs/io.rb +201 -0
- data/lib/memfs/version.rb +1 -1
- data/memfs.gemspec +17 -17
- data/memfs.png +0 -0
- data/spec/fileutils_spec.rb +233 -228
- data/spec/memfs/dir_spec.rb +76 -76
- data/spec/memfs/fake/directory_spec.rb +20 -20
- data/spec/memfs/fake/entry_spec.rb +24 -24
- data/spec/memfs/fake/file/content_spec.rb +43 -45
- data/spec/memfs/fake/file_spec.rb +14 -14
- data/spec/memfs/fake/symlink_spec.rb +22 -22
- data/spec/memfs/file/stat_spec.rb +314 -314
- data/spec/memfs/file_spec.rb +1636 -970
- data/spec/memfs/file_system_spec.rb +83 -83
- data/spec/memfs_spec.rb +15 -15
- data/spec/spec_helper.rb +17 -1
- metadata +36 -57
@@ -2,29 +2,29 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module MemFs
|
4
4
|
describe FileSystem do
|
5
|
-
subject {
|
5
|
+
subject { _fs }
|
6
6
|
|
7
7
|
before :each do
|
8
8
|
subject.mkdir '/test-dir'
|
9
9
|
end
|
10
10
|
|
11
11
|
describe '#chdir' do
|
12
|
-
it
|
12
|
+
it 'changes the current working directory' do
|
13
13
|
subject.chdir '/test-dir'
|
14
14
|
expect(subject.getwd).to eq('/test-dir')
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it 'raises an error if directory does not exist' do
|
18
18
|
expect { subject.chdir('/nowhere') }.to raise_error(Errno::ENOENT)
|
19
19
|
end
|
20
20
|
|
21
|
-
it
|
21
|
+
it 'raises an error if the destination is not a directory' do
|
22
22
|
subject.touch('/test-file')
|
23
23
|
expect { subject.chdir('/test-file') }.to raise_error(Errno::ENOTDIR)
|
24
24
|
end
|
25
25
|
|
26
|
-
context
|
27
|
-
it
|
26
|
+
context 'when a block is given' do
|
27
|
+
it 'changes current working directory for the block' do
|
28
28
|
location = nil
|
29
29
|
subject.chdir '/test-dir' do
|
30
30
|
location = subject.getwd
|
@@ -32,16 +32,16 @@ module MemFs
|
|
32
32
|
expect(location).to eq('/test-dir')
|
33
33
|
end
|
34
34
|
|
35
|
-
it
|
35
|
+
it 'gets back to previous directory once the block is finished' do
|
36
36
|
subject.chdir '/'
|
37
37
|
expect {
|
38
38
|
subject.chdir('/test-dir') {}
|
39
|
-
}.to_not change{subject.getwd}
|
39
|
+
}.to_not change { subject.getwd }
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
context
|
44
|
-
it
|
43
|
+
context 'when the destination is a symlink' do
|
44
|
+
it 'sets current directory as the last link chain target' do
|
45
45
|
subject.symlink('/test-dir', '/test-link')
|
46
46
|
subject.chdir('/test-link')
|
47
47
|
expect(subject.getwd).to eq('/test-dir')
|
@@ -50,14 +50,14 @@ module MemFs
|
|
50
50
|
end
|
51
51
|
|
52
52
|
describe '#chmod' do
|
53
|
-
it
|
53
|
+
it 'changes permission bits on the named file' do
|
54
54
|
subject.touch('/some-file')
|
55
55
|
subject.chmod(0777, '/some-file')
|
56
56
|
expect(subject.find!('/some-file').mode).to be(0100777)
|
57
57
|
end
|
58
58
|
|
59
|
-
context
|
60
|
-
it
|
59
|
+
context 'when the named file is a symlink' do
|
60
|
+
it 'changes the permission bits on the symlink itself' do
|
61
61
|
subject.touch('/some-file')
|
62
62
|
subject.symlink('/some-file', '/some-link')
|
63
63
|
subject.chmod(0777, '/some-link')
|
@@ -66,56 +66,56 @@ module MemFs
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
describe
|
69
|
+
describe '#chown' do
|
70
70
|
before :each do
|
71
71
|
subject.touch '/test-file'
|
72
72
|
end
|
73
73
|
|
74
|
-
it
|
74
|
+
it 'changes the owner of the named file to the given numeric owner id' do
|
75
75
|
subject.chown(42, nil, '/test-file')
|
76
76
|
expect(subject.find!('/test-file').uid).to be(42)
|
77
77
|
end
|
78
78
|
|
79
|
-
it
|
79
|
+
it 'changes the group of the named file to the given numeric group id' do
|
80
80
|
subject.chown(nil, 42, '/test-file')
|
81
81
|
expect(subject.find!('/test-file').gid).to be(42)
|
82
82
|
end
|
83
83
|
|
84
|
-
it
|
84
|
+
it 'ignores nil user id' do
|
85
85
|
expect {
|
86
86
|
subject.chown(nil, 42, '/test-file')
|
87
|
-
}.to_not change{subject.find!('/test-file').uid}
|
87
|
+
}.to_not change { subject.find!('/test-file').uid }
|
88
88
|
end
|
89
89
|
|
90
|
-
it
|
90
|
+
it 'ignores nil group id' do
|
91
91
|
expect {
|
92
92
|
subject.chown(42, nil, '/test-file')
|
93
|
-
}.to_not change{subject.find!('/test-file').gid}
|
93
|
+
}.to_not change { subject.find!('/test-file').gid }
|
94
94
|
end
|
95
95
|
|
96
|
-
it
|
96
|
+
it 'ignores -1 user id' do
|
97
97
|
expect {
|
98
98
|
subject.chown(-1, 42, '/test-file')
|
99
|
-
}.to_not change{subject.find!('/test-file').uid}
|
99
|
+
}.to_not change { subject.find!('/test-file').uid }
|
100
100
|
end
|
101
101
|
|
102
|
-
it
|
102
|
+
it 'ignores -1 group id' do
|
103
103
|
expect {
|
104
104
|
subject.chown(42, -1, '/test-file')
|
105
|
-
}.to_not change{subject.find!('/test-file').gid}
|
105
|
+
}.to_not change { subject.find!('/test-file').gid }
|
106
106
|
end
|
107
107
|
|
108
|
-
context
|
108
|
+
context 'when the named entry is a symlink' do
|
109
109
|
before :each do
|
110
110
|
subject.symlink '/test-file', '/test-link'
|
111
111
|
end
|
112
112
|
|
113
|
-
it
|
113
|
+
it 'changes the owner on the last target of the link chain' do
|
114
114
|
subject.chown(42, nil, '/test-link')
|
115
115
|
expect(subject.find!('/test-file').uid).to be(42)
|
116
116
|
end
|
117
117
|
|
118
|
-
it
|
118
|
+
it 'changes the group on the last target of the link chain' do
|
119
119
|
subject.chown(nil, 42, '/test-link')
|
120
120
|
expect(subject.find!('/test-file').gid).to be(42)
|
121
121
|
end
|
@@ -133,19 +133,19 @@ module MemFs
|
|
133
133
|
end
|
134
134
|
|
135
135
|
describe '#clear!' do
|
136
|
-
it
|
136
|
+
it 'clear the registred entries' do
|
137
137
|
subject.clear!
|
138
138
|
expect(subject.root.entry_names).to eq(%w[. .. tmp])
|
139
139
|
end
|
140
140
|
|
141
|
-
it
|
141
|
+
it 'sets the current directory to /' do
|
142
142
|
subject.clear!
|
143
143
|
expect(subject.getwd).to eq('/')
|
144
144
|
end
|
145
145
|
end
|
146
146
|
|
147
147
|
describe '#entries' do
|
148
|
-
it
|
148
|
+
it 'returns an array containing all of the filenames in the given directory' do
|
149
149
|
%w[/test-dir/new-dir /test-dir/new-dir2].each { |dir| subject.mkdir dir }
|
150
150
|
subject.touch '/test-dir/test-file', '/test-dir/test-file2'
|
151
151
|
expect(subject.entries('/test-dir')).to eq(%w[. .. new-dir new-dir2 test-file test-file2])
|
@@ -153,21 +153,21 @@ module MemFs
|
|
153
153
|
end
|
154
154
|
|
155
155
|
describe '#find' do
|
156
|
-
context
|
157
|
-
it
|
156
|
+
context 'when the entry for the given path exists' do
|
157
|
+
it 'returns the entry' do
|
158
158
|
entry = subject.find('/test-dir')
|
159
159
|
expect(entry).not_to be_nil
|
160
160
|
end
|
161
161
|
end
|
162
162
|
|
163
|
-
context
|
164
|
-
it
|
163
|
+
context 'when there is no entry for the given path' do
|
164
|
+
it 'returns nil' do
|
165
165
|
entry = subject.find('/no-file')
|
166
166
|
expect(entry).to be_nil
|
167
167
|
end
|
168
168
|
end
|
169
169
|
|
170
|
-
context
|
170
|
+
context 'when a part of the given path is a symlink' do
|
171
171
|
before :each do
|
172
172
|
subject.symlink('/test-dir', '/test-dir-link')
|
173
173
|
subject.symlink('/no-dir', '/test-no-link')
|
@@ -175,14 +175,14 @@ module MemFs
|
|
175
175
|
end
|
176
176
|
|
177
177
|
context "and the symlink's target exists" do
|
178
|
-
it
|
178
|
+
it 'returns the entry' do
|
179
179
|
entry = subject.find('/test-dir-link/test-file')
|
180
180
|
expect(entry).not_to be_nil
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
184
184
|
context "and the symlink's target does not exist" do
|
185
|
-
it
|
185
|
+
it 'returns nil' do
|
186
186
|
entry = subject.find('/test-no-link/test-file')
|
187
187
|
expect(entry).to be_nil
|
188
188
|
end
|
@@ -191,34 +191,34 @@ module MemFs
|
|
191
191
|
end
|
192
192
|
|
193
193
|
describe '#find!' do
|
194
|
-
context
|
195
|
-
it
|
194
|
+
context 'when the entry for the given path exists' do
|
195
|
+
it 'returns the entry' do
|
196
196
|
entry = subject.find!('/test-dir')
|
197
197
|
expect(entry).not_to be_nil
|
198
198
|
end
|
199
199
|
end
|
200
200
|
|
201
|
-
context
|
202
|
-
it
|
201
|
+
context 'when there is no entry for the given path' do
|
202
|
+
it 'raises an exception' do
|
203
203
|
expect { subject.find!('/no-file') }.to raise_exception
|
204
204
|
end
|
205
205
|
end
|
206
206
|
|
207
|
-
context
|
207
|
+
context 'when a part of the given path is a symlink' do
|
208
208
|
before :each do
|
209
|
-
|
210
|
-
|
209
|
+
_fs.symlink('/test-dir', '/test-dir-link')
|
210
|
+
_fs.touch('/test-dir/test-file')
|
211
211
|
end
|
212
212
|
|
213
213
|
context "and the symlink's target exists" do
|
214
|
-
it
|
214
|
+
it 'returns the entry' do
|
215
215
|
entry = subject.find!('/test-dir-link/test-file')
|
216
216
|
expect(entry).not_to be_nil
|
217
217
|
end
|
218
218
|
end
|
219
219
|
|
220
220
|
context "and the symlink's target does not exist" do
|
221
|
-
it
|
221
|
+
it 'raises an exception' do
|
222
222
|
expect {
|
223
223
|
subject.find!('/test-no-link/test-file')
|
224
224
|
}.to raise_error
|
@@ -228,33 +228,33 @@ module MemFs
|
|
228
228
|
end
|
229
229
|
|
230
230
|
describe '#find_directory!' do
|
231
|
-
it
|
231
|
+
it 'returns the named directory' do
|
232
232
|
expect(subject.find_directory!('/test-dir')).to be_a(Fake::Directory)
|
233
233
|
end
|
234
234
|
|
235
|
-
it
|
235
|
+
it 'raises an error if the named entry is not a directory' do
|
236
236
|
subject.touch '/test-file'
|
237
237
|
expect { subject.find_directory!('/test-file') }.to raise_error(Errno::ENOTDIR)
|
238
238
|
end
|
239
239
|
end
|
240
240
|
|
241
241
|
describe '#find_parent!' do
|
242
|
-
it
|
242
|
+
it 'returns the parent directory of the named entry' do
|
243
243
|
expect(subject.find_parent!('/test-dir/test-file')).to be_a(Fake::Directory)
|
244
244
|
end
|
245
245
|
|
246
|
-
it
|
246
|
+
it 'raises an error if the parent directory does not exist' do
|
247
247
|
expect { subject.find_parent!('/no-dir/test-file') }.to raise_error(Errno::ENOENT)
|
248
248
|
end
|
249
249
|
|
250
|
-
it
|
250
|
+
it 'raises an error if the parent is not a directory' do
|
251
251
|
subject.touch('/test-file')
|
252
252
|
expect { subject.find_parent!('/test-file/test') }.to raise_error(Errno::ENOTDIR)
|
253
253
|
end
|
254
254
|
end
|
255
255
|
|
256
256
|
describe '#getwd' do
|
257
|
-
it
|
257
|
+
it 'returns the current working directory' do
|
258
258
|
subject.chdir '/test-dir'
|
259
259
|
expect(subject.getwd).to eq('/test-dir')
|
260
260
|
end
|
@@ -265,18 +265,18 @@ module MemFs
|
|
265
265
|
subject.touch('/some-file')
|
266
266
|
end
|
267
267
|
|
268
|
-
it
|
268
|
+
it 'creates a hard link +dest+ that points to +src+' do
|
269
269
|
subject.link('/some-file', '/some-link')
|
270
270
|
expect(subject.find!('/some-link').content).to be(subject.find!('/some-file').content)
|
271
271
|
end
|
272
272
|
|
273
|
-
it
|
273
|
+
it 'does not create a symbolic link' do
|
274
274
|
subject.link('/some-file', '/some-link')
|
275
275
|
expect(subject.find!('/some-link')).not_to be_a(Fake::Symlink)
|
276
276
|
end
|
277
277
|
|
278
|
-
context
|
279
|
-
it
|
278
|
+
context 'when +new_name+ already exists' do
|
279
|
+
it 'raises an exception' do
|
280
280
|
subject.touch('/some-link')
|
281
281
|
expect { subject.link('/some-file', '/some-link') }.to raise_error(SystemCallError)
|
282
282
|
end
|
@@ -284,28 +284,28 @@ module MemFs
|
|
284
284
|
end
|
285
285
|
|
286
286
|
describe '#mkdir' do
|
287
|
-
it
|
287
|
+
it 'creates a directory' do
|
288
288
|
subject.mkdir '/new-dir'
|
289
289
|
expect(subject.find!('/new-dir')).to be_a(Fake::Directory)
|
290
290
|
end
|
291
291
|
|
292
|
-
context
|
293
|
-
it
|
292
|
+
context 'when a relative path is given' do
|
293
|
+
it 'creates a directory in current directory' do
|
294
294
|
subject.chdir '/test-dir'
|
295
295
|
subject.mkdir 'new-dir'
|
296
296
|
expect(subject.find!('/test-dir/new-dir')).to be_a(Fake::Directory)
|
297
297
|
end
|
298
298
|
end
|
299
299
|
|
300
|
-
context
|
301
|
-
it
|
300
|
+
context 'when the directory already exists' do
|
301
|
+
it 'raises an exception' do
|
302
302
|
expect { subject.mkdir('/') }.to raise_error(Errno::EEXIST)
|
303
303
|
end
|
304
304
|
end
|
305
305
|
end
|
306
306
|
|
307
307
|
describe '#new' do
|
308
|
-
it
|
308
|
+
it 'creates the root directory' do
|
309
309
|
expect(subject.find!('/')).to be(subject.root)
|
310
310
|
end
|
311
311
|
end
|
@@ -322,38 +322,38 @@ module MemFs
|
|
322
322
|
end
|
323
323
|
end
|
324
324
|
|
325
|
-
describe
|
325
|
+
describe '#pwd' do
|
326
326
|
it_behaves_like 'aliased method', :pwd, :getwd
|
327
327
|
end
|
328
328
|
|
329
|
-
describe
|
330
|
-
it
|
329
|
+
describe '#rename' do
|
330
|
+
it 'renames the given file to the new name' do
|
331
331
|
subject.touch('/test-file')
|
332
332
|
subject.rename('/test-file', '/test-file2')
|
333
333
|
expect(subject.find('/test-file2')).not_to be_nil
|
334
334
|
end
|
335
335
|
|
336
|
-
it
|
336
|
+
it 'removes the old file' do
|
337
337
|
subject.touch('/test-file')
|
338
338
|
subject.rename('/test-file', '/test-file2')
|
339
339
|
expect(subject.find('/test-file')).to be_nil
|
340
340
|
end
|
341
341
|
|
342
|
-
it
|
342
|
+
it 'can move a file in another directory' do
|
343
343
|
subject.touch('/test-file')
|
344
344
|
subject.rename('/test-file', '/test-dir/test-file')
|
345
345
|
expect(subject.find('/test-dir/test-file')).not_to be_nil
|
346
346
|
end
|
347
347
|
end
|
348
348
|
|
349
|
-
describe
|
350
|
-
it
|
349
|
+
describe '#rmdir' do
|
350
|
+
it 'removes the given directory' do
|
351
351
|
subject.rmdir('/test-dir')
|
352
352
|
expect(subject.find('/test-dir')).to be_nil
|
353
353
|
end
|
354
354
|
|
355
|
-
context
|
356
|
-
it
|
355
|
+
context 'when the directory is not empty' do
|
356
|
+
it 'raises an exception' do
|
357
357
|
subject.mkdir('/test-dir/test-sub-dir')
|
358
358
|
expect { subject.rmdir('/test-dir') }.to raise_error(Errno::ENOTEMPTY)
|
359
359
|
end
|
@@ -361,13 +361,13 @@ module MemFs
|
|
361
361
|
end
|
362
362
|
|
363
363
|
describe '#symlink' do
|
364
|
-
it
|
364
|
+
it 'creates a symbolic link' do
|
365
365
|
subject.symlink('/some-file', '/some-link')
|
366
366
|
expect(subject.find!('/some-link')).to be_a(Fake::Symlink)
|
367
367
|
end
|
368
368
|
|
369
|
-
context
|
370
|
-
it
|
369
|
+
context 'when +new_name+ already exists' do
|
370
|
+
it 'raises an exception' do
|
371
371
|
subject.touch('/some-file')
|
372
372
|
subject.touch('/some-file2')
|
373
373
|
expect { subject.symlink('/some-file', '/some-file2') }.to raise_error(Errno::EEXIST)
|
@@ -376,12 +376,12 @@ module MemFs
|
|
376
376
|
end
|
377
377
|
|
378
378
|
describe '#touch' do
|
379
|
-
it
|
379
|
+
it 'creates a regular file' do
|
380
380
|
subject.touch '/some-file'
|
381
381
|
expect(subject.find!('/some-file')).to be_a(Fake::File)
|
382
382
|
end
|
383
383
|
|
384
|
-
it
|
384
|
+
it 'creates a regular file for each named filed' do
|
385
385
|
subject.touch '/some-file', '/some-file2'
|
386
386
|
expect(subject.find!('/some-file2')).to be_a(Fake::File)
|
387
387
|
end
|
@@ -392,7 +392,7 @@ module MemFs
|
|
392
392
|
subject.touch '/some-file'
|
393
393
|
end
|
394
394
|
|
395
|
-
context
|
395
|
+
context 'when the named file already exists' do
|
396
396
|
let(:time) { Time.now - 5000 }
|
397
397
|
before :each do
|
398
398
|
subject.touch '/some-file'
|
@@ -400,27 +400,27 @@ module MemFs
|
|
400
400
|
file.atime = file.mtime = time
|
401
401
|
end
|
402
402
|
|
403
|
-
it
|
403
|
+
it 'sets the access time of the touched file' do
|
404
404
|
subject.touch '/some-file'
|
405
405
|
expect(subject.find!('/some-file').atime).not_to eq(time)
|
406
406
|
end
|
407
407
|
|
408
|
-
it
|
408
|
+
it 'sets the modification time of the touched file' do
|
409
409
|
subject.touch '/some-file'
|
410
410
|
expect(subject.find!('/some-file').atime).not_to eq(time)
|
411
411
|
end
|
412
412
|
end
|
413
413
|
end
|
414
414
|
|
415
|
-
describe
|
416
|
-
it
|
415
|
+
describe '#unlink' do
|
416
|
+
it 'deletes the named file' do
|
417
417
|
subject.touch('/some-file')
|
418
418
|
subject.unlink('/some-file')
|
419
419
|
expect(subject.find('/some-file')).to be_nil
|
420
420
|
end
|
421
421
|
|
422
|
-
context
|
423
|
-
it
|
422
|
+
context 'when the entry is a directory' do
|
423
|
+
it 'raises an exception' do
|
424
424
|
expect { subject.unlink('/test-dir') }.to raise_error
|
425
425
|
end
|
426
426
|
end
|