summaryrefslogtreecommitdiff
path: root/spec/ruby/core/file/open_spec.rb
blob: b5add5dff40337fd8bb028bf6f497608be3ede62 (plain)
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# encoding: utf-8

require_relative '../../spec_helper'
require_relative 'fixtures/common'
require_relative 'shared/open'

describe "File.open" do
  before :all do
    @file = tmp("file_open.txt")
    @unicode_path = tmp("こんにちは.txt")
    @nonexistent = tmp("fake.txt")
    rm_r @file, @nonexistent
  end

  before :each do
    ScratchPad.record []

    @fh = @fd = nil
    @flags = File::CREAT | File::TRUNC | File::WRONLY
    touch @file
  end

  after :each do
    @fh.close if @fh and not @fh.closed?
    rm_r @file, @unicode_path, @nonexistent
  end

  describe "with a block" do
    it "does not raise error when file is closed inside the block" do
      @fh = File.open(@file) { |fh| fh.close; fh }
      @fh.closed?.should == true
    end

    it "invokes close on an opened file when exiting the block" do
      File.open(@file, 'r') { |f| FileSpecs.make_closer f }

      ScratchPad.recorded.should == [:file_opened, :file_closed]
    end

    it "propagates non-StandardErrors produced by close" do
      lambda {
        File.open(@file, 'r') { |f| FileSpecs.make_closer f, Exception }
      }.should raise_error(Exception)

      ScratchPad.recorded.should == [:file_opened, :file_closed]
    end

    it "propagates StandardErrors produced by close" do
      lambda {
        File.open(@file, 'r') { |f| FileSpecs.make_closer f, StandardError }
      }.should raise_error(StandardError)

      ScratchPad.recorded.should == [:file_opened, :file_closed]
    end

    it "does not propagate IOError with 'closed stream' message produced by close" do
      File.open(@file, 'r') { |f| FileSpecs.make_closer f, IOError.new('closed stream') }

      ScratchPad.recorded.should == [:file_opened, :file_closed]
    end
  end

  it "opens the file (basic case)" do
    @fh = File.open(@file)
    @fh.should be_kind_of(File)
    File.exist?(@file).should == true
  end

  it "opens the file with unicode characters" do
    @fh = File.open(@unicode_path, "w")
    @fh.should be_kind_of(File)
    File.exist?(@unicode_path).should == true
  end

  it "opens a file when called with a block" do
    File.open(@file) { |fh| }
    File.exist?(@file).should == true
  end

  it "opens with mode string" do
    @fh = File.open(@file, 'w')
    @fh.should be_kind_of(File)
    File.exist?(@file).should == true
  end

  it "opens a file with mode string and block" do
    File.open(@file, 'w') { |fh| }
    File.exist?(@file).should == true
  end

  it "opens a file with mode num" do
    @fh = File.open(@file, @flags)
    @fh.should be_kind_of(File)
    File.exist?(@file).should == true
  end

  it "opens a file with mode num and block" do
    File.open(@file, 'w') { |fh| }
    File.exist?(@file).should == true
  end

  it "opens a file with mode and permission as nil" do
    @fh = File.open(@file, nil, nil)
    @fh.should be_kind_of(File)
  end

  # For this test we delete the file first to reset the perms
  it "opens the file when passed mode, num and permissions" do
    rm_r @file
    File.umask(0011)
    @fh = File.open(@file, @flags, 0755)
    @fh.should be_kind_of(File)
    platform_is_not :windows do
      @fh.lstat.mode.to_s(8).should == "100744"
    end
    File.exist?(@file).should == true
  end

  # For this test we delete the file first to reset the perms
  it "opens the file when passed mode, num, permissions and block" do
    rm_r @file
    File.umask(0022)
    File.open(@file, "w", 0755){ |fh| }
    platform_is_not :windows do
      File.stat(@file).mode.to_s(8).should == "100755"
    end
    File.exist?(@file).should == true
  end

  it "creates the file and returns writable descriptor when called with 'w' mode and r-o permissions" do
    # it should be possible to write to such a file via returned descriptior,
    # even though the file permissions are r-r-r.

    File.open(@file, "w", 0444) { |f| f.write("test") }
    File.read(@file).should == "test"
  end

  platform_is_not :windows do
    it "opens the existing file, does not change permissions even when they are specified" do
      File.chmod(0664, @file)
      orig_perms = File.stat(@file).mode.to_s(8)
      File.open(@file, "w", 0444) { |f| f.write("test") }

      File.stat(@file).mode.to_s(8).should == orig_perms
      File.read(@file).should == "test"
    end
  end

  platform_is_not :windows do
    as_user do
      it "creates a new write-only file when invoked with 'w' and '0222'" do
        rm_r @file
        File.open(@file, 'w', 0222) {}
        File.readable?(@file).should == false
        File.writable?(@file).should == true
      end
    end
  end

  it "opens the file when call with fd" do
    @fh = File.open(@file)
    fh_copy = File.open(@fh.fileno)
    fh_copy.autoclose = false
    fh_copy.should be_kind_of(File)
    File.exist?(@file).should == true
  end

  it "opens a file with a file descriptor d and a block" do
    @fh = File.open(@file)
    @fh.should be_kind_of(File)

    lambda {
      File.open(@fh.fileno) do |fh|
        @fd = fh.fileno
        @fh.close
      end
    }.should raise_error(Errno::EBADF)
    lambda { File.open(@fd) }.should raise_error(Errno::EBADF)

    File.exist?(@file).should == true
  end

  it "opens a file that no exists when use File::WRONLY mode" do
    lambda { File.open(@nonexistent, File::WRONLY) }.should raise_error(Errno::ENOENT)
  end

  it "opens a file that no exists when use File::RDONLY mode" do
    lambda { File.open(@nonexistent, File::RDONLY) }.should raise_error(Errno::ENOENT)
  end

  it "opens a file that no exists when use 'r' mode" do
    lambda { File.open(@nonexistent, 'r') }.should raise_error(Errno::ENOENT)
  end

  it "opens a file that no exists when use File::EXCL mode" do
    lambda { File.open(@nonexistent, File::EXCL) }.should raise_error(Errno::ENOENT)
  end

  it "opens a file that no exists when use File::NONBLOCK mode" do
    lambda { File.open(@nonexistent, File::NONBLOCK) }.should raise_error(Errno::ENOENT)
  end

  platform_is_not :openbsd, :windows do
    it "opens a file that no exists when use File::TRUNC mode" do
      lambda { File.open(@nonexistent, File::TRUNC) }.should raise_error(Errno::ENOENT)
    end
  end

  platform_is :openbsd, :windows do
    it "does not open a file that does no exists when using File::TRUNC mode" do
      lambda { File.open(@nonexistent, File::TRUNC) }.should raise_error(Errno::EINVAL)
    end
  end

  platform_is_not :windows do
    it "opens a file that no exists when use File::NOCTTY mode" do
      lambda { File.open(@nonexistent, File::NOCTTY) }.should raise_error(Errno::ENOENT)
    end
  end

  it "opens a file that no exists when use File::CREAT mode" do
    @fh = File.open(@nonexistent, File::CREAT) { |f| f }
    @fh.should be_kind_of(File)
    File.exist?(@file).should == true
  end

  it "opens a file that no exists when use 'a' mode" do
    @fh = File.open(@nonexistent, 'a') { |f| f }
    @fh.should be_kind_of(File)
    File.exist?(@file).should == true
  end

  it "opens a file that no exists when use 'w' mode" do
    @fh = File.open(@nonexistent, 'w') { |f| f }
    @fh.should be_kind_of(File)
    File.exist?(@file).should == true
  end

  # Check the grants associated to the differents open modes combinations.
  it "raises an ArgumentError exception when call with an unknown mode" do
    lambda { File.open(@file, "q") }.should raise_error(ArgumentError)
  end

  it "can read in a block when call open with RDONLY mode" do
    File.open(@file, File::RDONLY) do |f|
      f.gets.should == nil
    end
  end

  it "can read in a block when call open with 'r' mode" do
    File.open(@file, "r") do |f|
      f.gets.should == nil
    end
  end

  it "raises an IO exception when write in a block opened with RDONLY mode" do
    File.open(@file, File::RDONLY) do |f|
      lambda { f.puts "writing ..." }.should raise_error(IOError)
    end
  end

  it "raises an IO exception when write in a block opened with 'r' mode" do
    File.open(@file, "r") do |f|
      lambda { f.puts "writing ..." }.should raise_error(IOError)
    end
  end

  it "can't write in a block when call open with File::WRONLY||File::RDONLY mode" do
    File.open(@file, File::WRONLY|File::RDONLY ) do |f|
      f.puts("writing").should == nil
    end
  end

  it "can't read in a block when call open with File::WRONLY||File::RDONLY mode" do
    lambda {
      File.open(@file, File::WRONLY|File::RDONLY ) do |f|
        f.gets.should == nil
      end
    }.should raise_error(IOError)
  end

  it "can write in a block when call open with WRONLY mode" do
    File.open(@file, File::WRONLY) do |f|
      f.puts("writing").should == nil
    end
  end

  it "can write in a block when call open with 'w' mode" do
    File.open(@file, "w") do |f|
      f.puts("writing").should == nil
    end
  end

  it "raises an IOError when read in a block opened with WRONLY mode" do
    File.open(@file, File::WRONLY) do |f|
      lambda { f.gets  }.should raise_error(IOError)
    end
  end

  it "raises an IOError when read in a block opened with 'w' mode" do
    File.open(@file, "w") do |f|
      lambda { f.gets   }.should raise_error(IOError)
    end
  end

  it "raises an IOError when read in a block opened with 'a' mode" do
    File.open(@file, "a") do |f|
      lambda { f.gets  }.should raise_error(IOError)
    end
  end

  it "raises an IOError when read in a block opened with 'a' mode" do
    File.open(@file, "a") do |f|
      f.puts("writing").should == nil
      lambda { f.gets }.should raise_error(IOError)
    end
  end

  it "raises an IOError when read in a block opened with 'a' mode" do
    File.open(@file, File::WRONLY|File::APPEND ) do |f|
      lambda { f.gets }.should raise_error(IOError)
    end
  end

  it "raises an IOError when read in a block opened with File::WRONLY|File::APPEND mode" do
    File.open(@file, File::WRONLY|File::APPEND ) do |f|
      f.puts("writing").should == nil
      lambda { f.gets }.should raise_error(IOError)
    end
  end

  it "raises an IOError when read in a block opened with File::RDONLY|File::APPEND mode" do
    lambda {
      File.open(@file, File::RDONLY|File::APPEND ) do |f|
        f.puts("writing")
      end
    }.should raise_error(IOError)
  end

  it "can read and write in a block when call open with RDWR mode" do
    File.open(@file, File::RDWR) do |f|
      f.gets.should == nil
      f.puts("writing").should == nil
      f.rewind
      f.gets.should == "writing\n"
    end
  end

  it "can't read in a block when call open with File::EXCL mode" do
    lambda {
      File.open(@file, File::EXCL) do |f|
        f.puts("writing").should == nil
      end
    }.should raise_error(IOError)
  end

  it "can read in a block when call open with File::EXCL mode" do
    File.open(@file, File::EXCL) do |f|
      f.gets.should == nil
    end
  end

  it "can read and write in a block when call open with File::RDWR|File::EXCL mode" do
    File.open(@file, File::RDWR|File::EXCL) do |f|
      f.gets.should == nil
      f.puts("writing").should == nil
      f.rewind
      f.gets.should == "writing\n"
    end
  end

  it "raises an Errorno::EEXIST if the file exists when open with File::CREAT|File::EXCL" do
    lambda {
      File.open(@file, File::CREAT|File::EXCL) do |f|
        f.puts("writing")
      end
    }.should raise_error(Errno::EEXIST)
  end

  it "creates a new file when use File::WRONLY|File::APPEND mode" do
    @fh = File.open(@file, File::WRONLY|File::APPEND)
    @fh.should be_kind_of(File)
    File.exist?(@file).should == true
  end

  it "opens a file when use File::WRONLY|File::APPEND mode" do
    File.open(@file, File::WRONLY) do |f|
      f.puts("hello file")
    end
    File.open(@file, File::RDWR|File::APPEND) do |f|
      f.puts("bye file")
      f.rewind
      f.gets.should == "hello file\n"
      f.gets.should == "bye file\n"
      f.gets.should == nil
    end
  end

  it "raises an IOError if the file exists when open with File::RDONLY|File::APPEND" do
    lambda {
      File.open(@file, File::RDONLY|File::APPEND) do |f|
        f.puts("writing").should == nil
      end
    }.should raise_error(IOError)
  end

  platform_is_not :openbsd, :windows do
    it "truncates the file when passed File::TRUNC mode" do
      File.open(@file, File::RDWR) { |f| f.puts "hello file" }
      @fh = File.open(@file, File::TRUNC)
      @fh.gets.should == nil
    end

    it "can't read in a block when call open with File::TRUNC mode" do
      File.open(@file, File::TRUNC) do |f|
        f.gets.should == nil
      end
    end
  end

  it "opens a file when use File::WRONLY|File::TRUNC mode" do
    fh1 = File.open(@file, "w")
    begin
      @fh = File.open(@file, File::WRONLY|File::TRUNC)
      @fh.should be_kind_of(File)
      File.exist?(@file).should == true
    ensure
      fh1.close
    end
  end

  platform_is_not :openbsd, :windows do
    it "can't write in a block when call open with File::TRUNC mode" do
      lambda {
        File.open(@file, File::TRUNC) do |f|
          f.puts("writing")
        end
      }.should raise_error(IOError)
    end

    it "raises an Errorno::EEXIST if the file exists when open with File::RDONLY|File::TRUNC" do
      lambda {
        File.open(@file, File::RDONLY|File::TRUNC) do |f|
          f.puts("writing").should == nil
        end
      }.should raise_error(IOError)
    end
  end

  platform_is :openbsd, :windows do
    it "can't write in a block when call open with File::TRUNC mode" do
      lambda {
        File.open(@file, File::TRUNC) do |f|
          f.puts("writing")
        end
      }.should raise_error(Errno::EINVAL)
    end

    it "raises an Errorno::EEXIST if the file exists when open with File::RDONLY|File::TRUNC" do
      lambda {
        File.open(@file, File::RDONLY|File::TRUNC) do |f|
          f.puts("writing").should == nil
        end
      }.should raise_error(Errno::EINVAL)
    end
  end

  platform_is_not :windows do
    as_user do
      it "raises an Errno::EACCES when opening non-permitted file" do
        @fh = File.open(@file, "w")
        @fh.chmod(000)
        lambda { fh1 = File.open(@file); fh1.close }.should raise_error(Errno::EACCES)
      end
    end
  end

  as_user do
    it "raises an Errno::EACCES when opening read-only file" do
      @fh = File.open(@file, "w")
      @fh.chmod(0444)
      lambda { File.open(@file, "w") }.should raise_error(Errno::EACCES)
    end
  end

  it "opens a file for binary read" do
    @fh = File.open(@file, "rb")
    @fh.should be_kind_of(File)
    File.exist?(@file).should == true
  end

  it "opens a file for binary write" do
    @fh = File.open(@file, "wb")
    @fh.should be_kind_of(File)
    File.exist?(@file).should == true
  end

  it "opens a file for read-write and truncate the file" do
    File.open(@file, "w") { |f| f.puts "testing" }
    File.size(@file).should > 0
    File.open(@file, "w+") do |f|
      f.pos.should == 0
      f.eof?.should == true
    end
    File.size(@file).should == 0
  end

  it "opens a file for binary read-write starting at the beginning of the file" do
    File.open(@file, "w") { |f| f.puts "testing" }
    File.size(@file).should > 0
    File.open(@file, "rb+") do |f|
      f.pos.should == 0
      f.eof?.should == false
    end
  end

  it "opens a file for binary read-write and truncate the file" do
    File.open(@file, "w") { |f| f.puts "testing" }
    File.size(@file).should > 0
    File.open(@file, "wb+") do |f|
      f.pos.should == 0
      f.eof?.should == true
    end
    File.size(@file).should == 0
  end

  ruby_version_is "2.3" do
    platform_is :linux do
      guard -> { defined?(File::TMPFILE) } do
        it "creates an unnamed temporary file with File::TMPFILE" do
          dir = tmp("tmpfilespec")
          mkdir_p dir
          begin
            Dir["#{dir}/*"].should == []
            File.open(dir, "r+", flags: File::TMPFILE) do |io|
              io.write("ruby")
              io.flush
              io.rewind
              io.read.should == "ruby"
              Dir["#{dir}/*"].should == []
            end
          rescue Errno::EOPNOTSUPP, Errno::EINVAL
            # EOPNOTSUPP: no support from the filesystem
            # EINVAL: presumably bug in glibc
            1.should == 1
          ensure
            rm_r dir
          end
        end
      end
    end
  end

  it "raises a TypeError if passed a filename that is not a String or Integer type" do
    lambda { File.open(true)  }.should raise_error(TypeError)
    lambda { File.open(false) }.should raise_error(TypeError)
    lambda { File.open(nil)   }.should raise_error(TypeError)
  end

  it "raises a SystemCallError if passed an invalid Integer type" do
    lambda { File.open(-1)    }.should raise_error(SystemCallError)
  end

  it "raises an ArgumentError if passed the wrong number of arguments" do
    lambda { File.open(@file, File::CREAT, 0755, 'test') }.should raise_error(ArgumentError)
  end

  it "raises an ArgumentError if passed an invalid string for mode" do
    lambda { File.open(@file, 'fake') }.should raise_error(ArgumentError)
  end

  it "defaults external_encoding to ASCII-8BIT for binary modes" do
    File.open(@file, 'rb') {|f| f.external_encoding.should == Encoding::ASCII_8BIT}
    File.open(@file, 'wb+') {|f| f.external_encoding.should == Encoding::ASCII_8BIT}
  end

  it "uses the second argument as an options Hash" do
    @fh = File.open(@file, mode: "r")
    @fh.should be_an_instance_of(File)
  end

  it "calls #to_hash to convert the second argument to a Hash" do
    options = mock("file open options")
    options.should_receive(:to_hash).and_return({ mode: "r" })

    @fh = File.open(@file, options)
  end

  ruby_version_is "2.3" do
    it "accepts extra flags as a keyword argument and combine with a string mode" do
      lambda {
        File.open(@file, "w", flags: File::EXCL) { }
      }.should raise_error(Errno::EEXIST)

      lambda {
        File.open(@file, mode: "w", flags: File::EXCL) { }
      }.should raise_error(Errno::EEXIST)
    end

    it "accepts extra flags as a keyword argument and combine with an integer mode" do
      lambda {
        File.open(@file, File::WRONLY | File::CREAT, flags: File::EXCL) { }
      }.should raise_error(Errno::EEXIST)
    end
  end

  platform_is_not :windows do
    describe "on a FIFO" do
      before :each do
        @fifo = tmp("File_open_fifo")
        system "mkfifo #{@fifo}"
      end

      after :each do
        rm_r @fifo
      end

      it "opens it as a normal file" do
        file_w, file_r, read_bytes, written_length = nil

        # open in threads, due to blocking open and writes
        writer = Thread.new do
          file_w = File.open(@fifo, 'w')
          written_length = file_w.syswrite('hello')
        end
        reader = Thread.new do
          file_r = File.open(@fifo, 'r')
          read_bytes = file_r.sysread(5)
        end

        begin
          writer.join
          reader.join

          written_length.should == 5
          read_bytes.should == 'hello'
        ensure
          file_w.close if file_w
          file_r.close if file_r
        end
      end
    end
  end

end

describe "File.open when passed a file descriptor" do
  before do
    @content = "File#open when passed a file descriptor"
    @name = tmp("file_open_with_fd.txt")
    @fd = new_fd @name, fmode("w:utf-8")
    @file = nil
  end

  after do
    @file.close if @file and not @file.closed?
    rm_r @name
  end

  it "opens a file" do
    @file = File.open(@fd, "w")
    @file.should be_an_instance_of(File)
    @file.fileno.should equal(@fd)
    @file.write @content
    @file.flush
    File.read(@name).should == @content
  end

  it "opens a file when passed a block" do
    @file = File.open(@fd, "w") do |f|
      f.should be_an_instance_of(File)
      f.fileno.should equal(@fd)
      f.write @content
      f
    end
    File.read(@name).should == @content
  end
end

platform_is_not :windows do
  describe "File.open" do
    it_behaves_like :open_directory, :open
  end
end