-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmemory_file_system_spec.rb
More file actions
670 lines (525 loc) · 19 KB
/
memory_file_system_spec.rb
File metadata and controls
670 lines (525 loc) · 19 KB
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# frozen_string_literal: true
require "dry/files/memory_file_system"
require "English"
RSpec.describe Dry::Files::MemoryFileSystem do
let(:newline) { $INPUT_RECORD_SEPARATOR }
describe "#initialize" do
it "returns a new instance" do
expect(subject).to be_kind_of(described_class)
end
end
describe "#open" do
context "when file doesn't exist" do
it "creates file with empty content and yields node" do
path = subject.join("open-new")
subject.open(path, Dry::Files::OPEN_MODE) do |file|
expect(file).to be_kind_of(Dry::Files::MemoryFileSystem::Node)
end
expect(path).to have_file_contents("")
end
end
context "when already exist" do
it "creates file with empty content and yields node" do
path = subject.join("open-non-existing")
subject.write("open-non-existing", content = "foo")
subject.open(path, Dry::Files::OPEN_MODE) do |file|
expect(file).to be_kind_of(Dry::Files::MemoryFileSystem::Node)
end
expect(path).to have_file_contents(content)
end
end
context "when no block is given" do
it "returns the open file object" do
path = subject.join("open-new")
file = subject.open(path, Dry::Files::OPEN_MODE)
expect(file).to be_kind_of(Dry::Files::MemoryFileSystem::Node)
expect(path).to have_file_contents("")
end
end
end
describe "#read" do
it "reads file" do
path = subject.join("read")
subject.write(path, expected = "Hello#{newline}World")
expect(subject.read(path)).to eq(expected)
end
it "raises error when path is a directory" do
path = subject.join("read-directory")
subject.mkdir(path)
expect { subject.read(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::EISDIR)
expect(exception.message).to include(path.to_s)
end
end
it "raises error when path doesn't exist" do
path = subject.join("read-does-not-exist")
expect { subject.read(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::ENOENT)
expect(exception.message).to include(path.to_s)
end
end
end
describe "#readlines" do
it "reads file and returns array of lines" do
path = subject.join("readlines-file")
subject.write(path, "hello#{newline}world")
expect(subject.readlines(path)).to eq(%W[hello#{newline} world])
end
it "reads empty file and returns empty array" do
path = subject.join("readlines-empty-file")
subject.touch(path)
expect(subject.readlines(path)).to eq([])
end
it "raises error if file doesn't exist" do
path = subject.join("readlines-non-existing-file")
expect { subject.readlines(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::ENOENT)
expect(exception.message).to include(path.to_s)
end
end
it "raises error if path is a directory" do
path = subject.join("readlines", "directory")
subject.mkdir(path)
expect { subject.readlines(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::EISDIR)
expect(exception.message).to include(path.to_s)
end
end
xit "it raises error if path isn't readable"
end
describe "#touch" do
it "creates an empty file" do
path = subject.join("touch")
subject.touch(path)
expect(path).to be_found
expect(path).to have_file_contents("")
end
it "creates intermediate directories" do
path = subject.join("path", "to", "file", "touch")
subject.touch(path)
expect(path).to be_found
expect(path).to have_file_contents("")
end
it "leaves untouched existing file" do
path = subject.join("touch")
subject.write(path, "foo")
subject.touch(path)
expect(path).to be_found
expect(path).to have_file_contents("foo")
end
it "raises error if path is a directory" do
path = subject.join("touch-directory")
subject.mkdir(path)
expect { subject.touch(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::EISDIR)
expect(exception.message).to include(path.to_s)
end
end
end
describe "#write" do
it "creates an file with given contents (string)" do
path = subject.join("write")
subject.write(path, "Hello#{newline}World")
expect(path).to be_found
expect(path).to have_file_contents("Hello#{newline}World")
end
it "creates an file with given contents (array)" do
path = subject.join("write")
content = ["# frozen_string_literal: true#{newline}", newline, "module Foo#{newline}", " class App#{newline}", " CONSTANT = 23#{newline}", newline, " def call(*)#{newline}", " end#{newline}", " end#{newline}", "end#{newline}"]
expected = <<~CONTENT
# frozen_string_literal: true
module Foo
class App
CONSTANT = 23
def call(*)
end
end
end
CONTENT
subject.write(path, content)
expect(path).to be_found
expect(path).to have_file_contents(expected)
end
it "creates intermediate directories" do
path = subject.join("path", "to", "file", "write")
subject.write(path, ":)")
expect(path).to be_found
expect(path).to have_file_contents(":)")
end
it "overwrites file when it already exist" do
path = subject.join("write")
subject.write(path, "many many many many words")
subject.write(path, "new words")
expect(path).to be_found
expect(path).to have_file_contents("new words")
end
xit "raises error when path isn't writeable" do
path = subject.join("write-not-writeable")
path.mkpath
mode = path.stat.mode
begin
path.chmod(0o000)
expect { subject.write(path.join("file-not-writeable"), "content") }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::EACCES)
expect(exception.message).to include(path.to_s)
end
ensure
path.chmod(mode)
end
end
end
describe "#join" do
it "joins a single entry" do
path = "path"
expect(subject.join(path)).to eq(path)
path = Pathname.new(path)
expect(subject.join(path)).to eq(path.to_s)
end
it "joins multiple entries" do
path = %w[path to file]
expected = path.join(File::SEPARATOR)
expect(subject.join(path)).to eq(expected)
path = path.map { |p| Pathname.new(p) }
expect(subject.join(path)).to eq(expected)
end
end
describe "#expand_path" do
it "expands path from given directory" do
dir = subject.join("expand-path", "given-dir")
expected = subject.join(dir, path = "file")
expect(subject.expand_path(path, dir)).to eq(expected)
end
it "returns absolute path as it is" do
path = ::File::SEPARATOR + subject.join("expand-path", "absolute")
expect(subject.expand_path(path, subject.pwd)).to eq(path)
end
end
describe "#pwd" do
it "returns root directory by default" do
expect(subject.pwd).to eq("/")
end
end
describe "#chdir" do
it "changes current working directory" do
current_directory = subject.pwd
subject.mkdir(dir = "path/to/dir")
subject.chdir(dir) do
expect(subject.pwd).to eq("dir")
end
expect(subject.pwd).to eq(current_directory)
end
it "raises error if directory cannot be found" do
path = subject.join("chdir-non-existing")
expect { subject.chdir(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::ENOENT)
expect(exception.message).to include(path.to_s)
end
end
it "raises error if argument is a file" do
path = subject.join("chdir-file")
subject.touch(path)
expect { subject.chdir(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::ENOTDIR)
expect(exception.message).to match(path.to_s)
end
end
end
describe "#mkdir" do
it "creates directory" do
path = subject.join("mkdir")
subject.mkdir(path)
expect(subject.directory?(path)).to be(true)
end
it "creates intermediate directories" do
path = subject.join("path", "to", "mkdir")
subject.mkdir(path)
expect(subject.directory?(path)).to be(true)
end
it "raises error when path is a file" do
path = subject.join("mkdir-is-file")
subject.write(path, content = "foo")
expect { subject.mkdir(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::EEXIST)
expect(exception.message).to include(path.to_s)
end
# ensure it doesn't override already existing file
expect(subject.directory?(path)).to be(false)
expect(subject.read(path)).to eq(content)
end
xit "raises error when path isn't writeable" do
path = subject.join("mkdir-not-writeable")
path.mkpath
mode = path.stat.mode
begin
path.chmod(0o000)
expect { subject.mkdir(path.join("dir-not-writeable")) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::EACCES)
expect(exception.message).to include(path.to_s)
end
ensure
path.chmod(mode)
end
end
end
describe "#mkdir_p" do
it "creates directory" do
directory = subject.join("mkdir_p")
path = subject.join(directory, "file.rb")
subject.mkdir_p(path)
expect(subject.directory?(directory)).to be(true)
expect(path).to_not be_found
end
it "creates intermediate directories" do
directory = subject.join("path", "to", "mkdir_p")
path = subject.join(directory, "file.rb")
subject.mkdir_p(path)
expect(subject.directory?(directory)).to be(true)
expect(path).to_not be_found
end
# It fails due to an RSpec formatter that crashes
xit "raises error when path is a file" do
file = subject.join("mkdir_p", "file")
subject.write(file, content = "foo")
path = subject.join(file, "nested")
expect { subject.mkdir_p(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::EEXIST)
expect(exception.message).to include(path.to_s)
end
# ensure it doesn't override already existing file
expect(subject.directory?(path)).to be(false)
expect(subject.read(path)).to eq(content)
end
xit "raises error when path isn't writeable" do
parent = subject.join("path")
parent.mkpath
mode = parent.stat.mode
begin
parent.chmod(0o000)
path = parent.join("to", "mkdir_p", "dir-not-writeable")
expect { subject.mkdir_p(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::EACCES)
expect(exception.message).to include(parent.to_s)
end
ensure
parent.chmod(mode)
end
end
end
describe "#cp" do
let(:source) { subject.join("..", "source") }
before do
subject.rm(source) if subject.exist?(source)
end
it "creates a file with given contents" do
subject.write(source, "the source")
destination = subject.join("cp")
subject.cp(source, destination)
expect(destination).to be_found
expect(destination).to have_file_contents("the source")
end
it "creates intermediate directories" do
source = subject.join("..", "source")
subject.write(source, "the source for intermediate directories")
destination = subject.join("cp", "destination")
subject.cp(source, destination)
expect(destination).to be_found
expect(destination).to have_file_contents("the source for intermediate directories")
end
it "overrides already existing file" do
source = subject.join("..", "source")
subject.write(source, "the source")
destination = subject.join("cp")
subject.write(destination, "the destination")
subject.cp(source, destination)
expect(destination).to be_found
expect(destination).to have_file_contents("the source")
end
it "raises error when source cannot be found" do
source = subject.join("missing-source")
destination = subject.join("cp")
expect { subject.cp(source, destination) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::ENOENT)
expect(exception.message).to include(source.to_s)
end
end
end
describe "#rm" do
it "deletes path" do
path = subject.join("delete", "file")
subject.touch(path)
subject.rm(path)
expect(path).to_not be_found
end
it "raises error if path doesn't exist" do
path = subject.join("delete", "file")
expect { subject.rm(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::ENOENT)
expect(exception.message).to include(path.to_s)
end
end
it "raises error if path is a directory" do
path = subject.join("delete", "directory")
subject.mkdir(path)
expect { subject.rm(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::EPERM)
expect(exception.message).to include(path.to_s)
end
end
end
describe "#rm_rf" do
it "deletes directory" do
path = subject.join("delete", "directory")
subject.mkdir(path)
subject.rm_rf(path)
expect(path).to_not be_found
end
it "deletes a file" do
path = subject.join("delete_directory", "file")
subject.touch(path)
subject.rm_rf(path)
expect(path).to_not be_found
end
it "raises error if directory doesn't exist" do
path = subject.join("delete", "directory")
expect { subject.rm_rf(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::ENOENT)
expect(exception.message).to include(path.to_s)
end
end
end
describe "#chmod" do
it "sets UNIX mode" do
path = subject.join("chmod")
subject.touch(path)
subject.chmod(path, mode = 0o755)
expect(subject.mode(path)).to eq(mode)
end
it "raises error if path doesn't exist" do
path = subject.join("chmod")
expect { subject.chmod(path, 0o755) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::ENOENT)
expect(exception.message).to include(path.to_s)
end
end
end
describe "#mode" do
it "gets UNIX mode" do
path = subject.join("mode")
subject.touch(path)
subject.chmod(path, mode = 0o755)
expect(subject.mode(path)).to eq(mode)
end
it "raises error if path doesn't exist" do
path = subject.join("mode")
expect { subject.mode(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::ENOENT)
expect(exception.message).to include(path.to_s)
end
end
end
describe "#exist?" do
it "returns true for file" do
path = subject.join("exist-file")
subject.touch(path)
expect(subject.exist?(path)).to be(true)
end
it "returns true for directory" do
path = subject.join("exist-dir")
subject.mkdir(path)
expect(subject.exist?(path)).to be(true)
end
it "returns false for non-existing file" do
path = subject.join("exist-non-existing")
expect(subject.exist?(path)).to be(false)
end
end
describe "#directory?" do
it "returns true for directory" do
path = subject.join("directory-dir")
subject.mkdir(path)
expect(subject.directory?(path)).to be(true)
end
it "returns false for file" do
path = subject.join("directory-file")
subject.touch(path)
expect(subject.directory?(path)).to be(false)
end
it "returns false for non-existing path" do
path = subject.join("directory-non-existing")
expect(subject.directory?(path)).to be(false)
end
end
describe "#executable?" do
it "returns true when file is executable" do
path = subject.join("executable-exec")
subject.touch(path)
subject.chmod(path, 0o744)
expect(subject.executable?(path)).to be(true)
end
it "returns false when file isn't executable" do
path = subject.join("executable-non-exec")
subject.touch(path)
expect(subject.executable?(path)).to be(false)
end
it "returns false when file doesn't exist" do
path = subject.join("executable-non-existing")
expect(subject.executable?(path)).to be(false)
end
it "returns true for directory" do
path = subject.join("executable-directory")
subject.mkdir(path)
expect(subject.executable?(path)).to be(true)
end
end
describe "#entries" do
it "raises an error when the path does not exist" do
path = subject.join("file-1.txt")
expect { subject.entries(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::ENOENT)
expect(exception.message).to include(path.to_s)
end
end
it "raises an error when the path is a file" do
path = subject.join("file-1.txt")
subject.touch(path)
expect { subject.entries(path) }.to raise_error do |exception|
expect(exception).to be_kind_of(Dry::Files::IOError)
expect(exception.cause).to be_kind_of(Errno::ENOTDIR)
expect(exception.message).to include(path.to_s)
end
end
it "returns entries when the path is a directory" do
subject.touch(subject.join("file-1.txt"))
subject.touch(subject.join("file-2.txt"))
expect(subject.entries(subject.join)).to eq [
".",
"..",
"file-1.txt",
"file-2.txt"
]
end
it "returns an array with only relative paths on an empty directory" do
subject.mkdir("empty")
expect(subject.entries(subject.join("empty"))).to eq [".", ".."]
end
end
end