1 require File.dirname(__FILE__) + '/../../spec_helper'
2 require File.dirname(__FILE__) + '/fixtures/classes'
4 describe "IO#reopen" do
7 @name1 = IOSpecs.gets_fixtures
8 @name2 = File.dirname(__FILE__) + '/fixtures/numbered_lines.txt'
9 @file1 = File.new(@name1)
10 @file2 = File.new(@name2)
13 @name1_w = tmp("IO_reopen_file1") + $$.to_s
14 @name2_w = tmp("IO_reopen_file2") + $$.to_s
15 @file1_w = File.new(@name1_w, "w+")
16 @file2_w = File.new(@name2_w, "w+")
20 @file1.close unless @file1.closed?
21 @file2.close unless @file2.closed?
22 @file1_w.close unless @file1_w.closed?
23 @file2_w.close unless @file2_w.closed?
28 it "raises IOError on closed stream" do
29 File.open(File.dirname(__FILE__) + '/fixtures/gets.txt', 'r') { |f|
30 lambda { f.reopen(IOSpecs.closed_file) }.should raise_error(IOError)
34 it "reassociates self to another file/descriptor but returns self" do
35 @file1.reopen(@file2).should == @file1
36 @file2.reopen(@file1).should == @file2
37 @file1.reopen(@name2).should == @file1
38 @file2.reopen(@name2).should == @file2
41 it "reassociates self with a new stream opened on path, when self in initial state" do
43 @file1.gets.should == "Line 1: One\n"
46 it "reassociates self with a new stream opened on path, after some reads" do
48 4.times {@file1.gets; @file2.gets}
51 @file1.gets.should == "Line 1: One\n"
54 it "reassociates self with a new stream opened on path, after some writes" do
55 @file1_w.puts("line1-F1")
56 @file2_w.puts("line1-F2")
57 @file2_w.reopen(@name1_w)
58 @file1_w.puts("line2-F1")
59 @file2_w.puts("line2-F2")
62 File.readlines(@name1_w).should == ["line2-F2\n", "line2-F1\n"]
63 File.readlines(@name2_w).should == ["line1-F2\n"]
66 # JRUBY-2071: File#reopen blows with IllegalArgumentException in some cases
67 it "reassociates self with the I/O stream specified as an argument, after some reads" do
68 length = 12 # length of first lines in numbered_lines.txt
76 @file1.pos.should == pos
78 # MRI behavior: after reopen the buffers are not corrected,
79 # so we need the following line, or next gets wourd return nil.
82 @file1.gets.should == "Line 2: Two\n"
85 platform_is_not :darwin, :freebsd do
86 it "reassociates self with the I/O stream specified as an argument, after some sysreads" do
87 length = 12 # length of first lines in numbered_lines.txt
90 @file1.sysread(length)
91 @file2.sysread(length)
94 @file1.sysread(length).should == "Line 2: Two\n"
98 it "reassociates self with the I/O stream specified as an argument, after some writes" do
99 @file1_w.puts("line1-F1")
100 @file2_w.puts("line1-F2")
101 @file2_w.reopen(@file1_w)
102 @file1_w.puts("line2-F1")
103 @file2_w.puts("line2-F2")
106 File.readlines(@name1_w).should == ["line1-F1\n", "line2-F1\n", "line2-F2\n"]
107 File.readlines(@name2_w).should == ["line1-F2\n"]