File: dir_spec.rb

package info (click to toggle)
ruby-memfs 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 516 kB
  • ctags: 259
  • sloc: ruby: 6,267; makefile: 3
file content (517 lines) | stat: -rw-r--r-- 15,306 bytes parent folder | download
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
require 'spec_helper'

module MemFs
  describe Dir do
    subject { described_class.new('/test') }

    before { described_class.mkdir '/test' }

    it 'is Enumerable' do
      expect(subject).to be_an(Enumerable)
    end

    describe '[]' do
      context 'when a string is given' do
        it 'acts like calling glob' do
          expect(described_class['/*']).to eq %w[/tmp /test]
        end
      end

      context 'when a list of strings is given' do
        it 'acts like calling glob' do
          expect(described_class['/tm*', '/te*']).to eq %w[/tmp /test]
        end
      end
    end

    describe '.chdir' do
      it 'changes the current working directory' do
        described_class.chdir '/test'
        expect(described_class.getwd).to eq('/test')
      end

      it 'returns zero' do
        expect(described_class.chdir('/test')).to be_zero
      end

      it 'raises an error when the folder does not exist' do
        expect { described_class.chdir('/nowhere') }.to raise_error(Errno::ENOENT)
      end

      context 'when a block is given' do
        it 'changes current working directory for the block' do
          described_class.chdir '/test' do
            expect(described_class.pwd).to eq('/test')
          end
        end

        it 'gets back to previous directory once the block is finished' do
          described_class.chdir '/'
          expect {
            described_class.chdir('/test') {}
          }.to_not change { described_class.pwd }
        end
      end
    end

    describe '.chroot' do
      before { allow(Process).to receive_messages(uid: 0) }

      it "changes the process's idea of the file system root" do

        described_class.mkdir('/test/subdir')
        described_class.chroot('/test')

        expect(File.exist?('/subdir')).to be true
      end

      it 'returns zero' do
        expect(described_class.chroot('/test')).to eq 0
      end

      context 'when the given path is a file' do
        before { _fs.touch('/test/test-file') }

        it 'raises an exception' do
          expect { described_class.chroot('/test/test-file') }.to raise_error(Errno::ENOTDIR)
        end
      end

      context "when the given path doesn't exist" do
        it 'raises an exception' do
          expect { described_class.chroot('/no-dir') }.to raise_error(Errno::ENOENT)
        end
      end

      context 'when the user is not root' do
        before { allow(Process).to receive_messages(uid: 42) }

        it 'raises an exception' do
          expect { described_class.chroot('/no-dir') }.to raise_error(Errno::EPERM)
        end
      end
    end

    describe '.delete' do
      subject { described_class }
      it_behaves_like 'aliased method', :delete, :rmdir
    end

    describe '.entries' do
      it 'returns an array containing all of the filenames in the given directory' do
        %w[/test/dir1 /test/dir2].each { |dir| described_class.mkdir dir }
        _fs.touch '/test/file1', '/test/file2'
        expect(described_class.entries('/test')).to eq(%w[. .. dir1 dir2 file1 file2])
      end
    end

    describe '.exist?' do
      subject { described_class }
      it_behaves_like 'aliased method', :exist?, :exists?
    end

    describe '.exists?' do
      it 'returns true if the given +path+ exists and is a directory' do
        described_class.mkdir('/test-dir')
        expect(described_class.exists?('/test-dir')).to be true
      end

      it 'returns false if the given +path+ does not exist' do
        expect(described_class.exists?('/test-dir')).to be false
      end

      it 'returns false if the given +path+ is not a directory' do
        _fs.touch('/test-file')
        expect(described_class.exists?('/test-file')).to be false
      end
    end

    describe '.foreach' do
      before :each do
        _fs.touch('/test/test-file', '/test/test-file2')
      end

      context 'when a block is given' do
        it 'calls the block once for each entry in the named directory' do
          expect { |blk|
            described_class.foreach('/test', &blk)
          }.to yield_control.exactly(4).times
        end

        it 'passes each entry as a parameter to the block' do
          expect { |blk|
            described_class.foreach('/test', &blk)
          }.to yield_successive_args('.', '..', 'test-file', 'test-file2')
        end

        context "and the directory doesn't exist" do
          it 'raises an exception' do
            expect { described_class.foreach('/no-dir') {} }.to raise_error
          end
        end

        context 'and the given path is not a directory' do
          it 'raises an exception' do
            expect {
              described_class.foreach('/test/test-file') {}
            }.to raise_error
          end
        end
      end

      context 'when no block is given' do
        it 'returns an enumerator' do
          list = described_class.foreach('/test-dir')
          expect(list).to be_an(Enumerator)
        end

        context "and the directory doesn't exist" do
          it 'returns an enumerator' do
            list = described_class.foreach('/no-dir')
            expect(list).to be_an(Enumerator)
          end
        end

        context 'and the given path is not a directory' do
          it 'returns an enumerator' do
            list = described_class.foreach('/test-dir/test-file')
            expect(list).to be_an(Enumerator)
          end
        end
      end
    end

    describe '.getwd' do
      it 'returns the path to the current working directory' do
        expect(described_class.getwd).to eq(FileSystem.instance.getwd)
      end
    end

    describe '.glob' do
      before do
        _fs.clear!
        3.times do |dirnum|
          _fs.mkdir "/test#{dirnum}"
          _fs.mkdir "/test#{dirnum}/subdir"
          3.times do |filenum|
            _fs.touch "/test#{dirnum}/subdir/file#{filenum}"
          end
        end
      end

      shared_examples 'returning matching filenames' do |pattern, filenames|
        it "with #{pattern}" do
          expect(described_class.glob(pattern)).to eq filenames
        end
      end

      it_behaves_like 'returning matching filenames', '/', %w[/]
      it_behaves_like 'returning matching filenames', '/test0', %w[/test0]
      it_behaves_like 'returning matching filenames', '/*', %w[/tmp /test0 /test1 /test2]
      it_behaves_like 'returning matching filenames', '/test*', %w[/test0 /test1 /test2]
      it_behaves_like 'returning matching filenames', '/*0', %w[/test0]
      it_behaves_like 'returning matching filenames', '/*es*', %w[/test0 /test1 /test2]
      it_behaves_like 'returning matching filenames', '/**/file0', %w[/test0/subdir/file0 /test1/subdir/file0 /test2/subdir/file0]
      it_behaves_like 'returning matching filenames', '/test?', %w[/test0 /test1 /test2]
      it_behaves_like 'returning matching filenames', '/test[01]', %w[/test0 /test1]
      it_behaves_like 'returning matching filenames', '/test[^2]', %w[/test0 /test1]

      if defined?(File::FNM_EXTGLOB)
        it_behaves_like 'returning matching filenames', '/test{1,2}', %w[/test1 /test2]
      end

      context 'when a flag is given' do
        it 'uses it to compare filenames' do
          expect(described_class.glob('/TEST*', File::FNM_CASEFOLD)).to eq \
            %w[/test0 /test1 /test2]
        end
      end

      context 'when a block is given' do
        it 'calls the block with every matching filenames' do
          expect { |blk| described_class.glob('/test*', &blk) }.to \
            yield_successive_args('/test0', '/test1', '/test2')
        end

        it 'returns nil' do
          expect(described_class.glob('/*') {}).to be nil
        end
      end

      context 'when pattern is an array of patterns' do
        it 'returns the list of files matching any pattern' do
          expect(described_class.glob(['/*0', '/*1'])).to eq %w[/test0 /test1]
        end
      end
    end

    describe '.home' do
      xit 'returns the home directory of the current user' do
        expect(described_class.home).to eq ENV['HOME']
      end

      context 'when a username is given' do
        xit 'returns the home directory of the given user' do
          home_dir = described_class.home(ENV['USER'])
          expect(home_dir).to eq ENV['HOME']
        end
      end
    end

    describe '.mkdir' do
      it 'creates a directory' do
        described_class.mkdir '/new-folder'
        expect(File.directory?('/new-folder')).to be true
      end

      it 'sets directory permissions to default 0777' do
        described_class.mkdir '/new-folder'
        expect(File.stat('/new-folder').mode).to eq(0100777)
      end

      context 'when permissions are specified' do
        it 'sets directory permissions to specified value' do
          described_class.mkdir '/new-folder', 0644
          expect(File.stat('/new-folder').mode).to eq(0100644)
        end
      end

      context 'when the directory already exist' do
        it 'raises an exception' do
          expect { described_class.mkdir('/') }.to raise_error(Errno::EEXIST)
        end
      end
    end

    describe '.open' do
      context 'when no block is given' do
        it 'returns the opened directory' do
          expect(described_class.open('/test')).to be_a(Dir)
        end
      end

      context 'when a block is given' do
        it 'calls the block with the opened directory as argument' do
          expect { |blk| described_class.open('/test', &blk) }.to yield_with_args(Dir)
        end

        it 'returns nil' do
          expect(described_class.open('/test') {}).to be_nil
        end

        it 'ensures the directory is closed' do
          dir = nil
          described_class.open('/test') { |d| dir = d }
          expect { dir.close }.to raise_error(IOError)
        end
      end

      context "when the given directory doesn't exist" do
        it 'raises an exception' do
          expect { described_class.open('/no-dir') }.to raise_error
        end
      end

      context 'when the given path is not a directory' do
        before { _fs.touch('/test/test-file') }

        it 'raises an exception' do
          expect { described_class.open('/test/test-file') }.to raise_error
        end
      end
    end

    describe '.new' do
      context "when the given directory doesn't exist" do
        it 'raises an exception' do
          expect { described_class.new('/no-dir') }.to raise_error
        end
      end

      context 'when the given path is not a directory' do
        before { _fs.touch('/test/test-file') }

        it 'raises an exception' do
          expect { described_class.new('/test/test-file') }.to raise_error
        end
      end
    end

    describe '.pwd' do
      subject { described_class }
      it_behaves_like 'aliased method', :pwd, :getwd
    end

    describe '.rmdir' do
      it 'deletes the named directory' do
        described_class.mkdir('/test-dir')
        described_class.rmdir('/test-dir')
        expect(described_class.exists?('/test-dir')).to be false
      end

      context 'when the directory is not empty' do
        it 'raises an exception' do
          described_class.mkdir('/test-dir')
          described_class.mkdir('/test-dir/test-sub-dir')
          expect { described_class.rmdir('/test-dir') }.to raise_error(Errno::ENOTEMPTY)
        end
      end
    end

    describe '.tmpdir' do
      it 'returns /tmp' do
        expect(described_class.tmpdir).to eq '/tmp'
      end
    end

    describe '.unlink' do
      subject { described_class }
      it_behaves_like 'aliased method', :unlink, :rmdir
    end

    describe '#close' do
      it 'closes the directory' do
        dir = described_class.open('/test')
        dir.close
        expect { dir.close }.to raise_error(IOError)
      end
    end

    describe '#each' do
      before { _fs.touch('/test/test-file', '/test/test-file2') }

      it 'calls the block once for each entry in this directory' do
        expect { |blk| subject.each(&blk) }.to yield_control.exactly(4).times
      end

      it 'passes the filename of each entry as a parameter to the block' do
        expect { |blk|
          subject.each(&blk)
        }.to yield_successive_args('.', '..', 'test-file', 'test-file2')
      end

      context 'when no block is given' do
        it 'returns an enumerator' do
          expect(subject.each).to be_an(Enumerator)
        end
      end
    end

    describe '#path' do
      it "returns the path parameter passed to dir's constructor" do
        expect(subject.path).to eq '/test'
      end
    end

    describe '#pos' do
      it 'returns the current position in dir' do
        3.times { subject.read }
        expect(subject.pos).to eq 3
      end
    end

    describe '#pos=' do
      before { 3.times { subject.read } }

      it 'seeks to a particular location in dir' do
        subject.pos = 1
        expect(subject.pos).to eq 1
      end

      it 'returns the given position' do
        expect(subject.pos = 2).to eq 2
      end

      context 'when the location has not been seeked yet' do
        it "doesn't change the location" do
          subject.pos = 42
          expect(subject.pos).to eq 3
        end
      end

      context 'when the location is negative' do
        it "doesn't change the location" do
          subject.pos = -1
          expect(subject.pos).to eq 3
        end
      end
    end

    describe '#read' do
      before do
        _fs.touch('/test/a')
        _fs.touch('/test/b')
      end

      it 'reads the next entry from dir and returns it' do
        expect(subject.read).to eq '.'
      end

      context 'when calling several times' do
        it 'returns the next entry each time' do
          2.times { subject.read }
          expect(subject.read).to eq 'a'
        end
      end

      context 'when there are no entries left' do
        it 'returns nil' do
          4.times { subject.read }
          expect(subject.read).to be_nil
        end
      end
    end

    describe '#rewind' do
      it 'repositions dir to the first entry' do
        3.times { subject.read }
        subject.rewind
        expect(subject.read).to eq '.'
      end

      it 'returns the dir itself' do
        expect(subject.rewind).to be subject
      end
    end

    describe '#seek' do
      before { 3.times { subject.read } }

      it 'seeks to a particular location in dir' do
        subject.seek(1)
        expect(subject.pos).to eq 1
      end

      it 'returns the dir itself' do
        expect(subject.seek(2)).to be subject
      end

      context 'when the location has not been seeked yet' do
        it "doesn't change the location" do
          subject.seek(42)
          expect(subject.pos).to eq 3
        end
      end

      context 'when the location is negative' do
        it "doesn't change the location" do
          subject.seek(-1)
          expect(subject.pos).to eq 3
        end
      end
    end

    describe '#tell' do
      it 'returns the current position in dir' do
        3.times { subject.read }
        expect(subject.tell).to eq 3
      end
    end

    describe '#to_path' do
      it "returns the path parameter passed to dir's constructor" do
        expect(subject.to_path).to eq '/test'
      end
    end
  end
end