Updated RubySpec source to 55122684.
[rbx.git] / spec / frozen / 1.8 / core / io / reopen_spec.rb
blob46af03a6db3f9c88748ed50f9329bd92d8387579
1 require File.dirname(__FILE__) + '/../../spec_helper'
2 require File.dirname(__FILE__) + '/fixtures/classes'
4 describe "IO#reopen" do
5   before :each do
6     # for reading
7     @name1 = IOSpecs.gets_fixtures
8     @name2 = File.dirname(__FILE__) + '/fixtures/numbered_lines.txt'
9     @file1 = File.new(@name1)
10     @file2 = File.new(@name2)
12     # for writing
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+")
17   end
19   after :each do
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?
24     File.delete(@name1_w)
25     File.delete(@name2_w)
26   end
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)
31     }
32   end
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
39   end
40   
41   it "reassociates self with a new stream opened on path, when self in initial state" do
42     @file1.reopen(@name2)
43     @file1.gets.should == "Line 1: One\n"
44   end
46   it "reassociates self with a new stream opened on path, after some reads" do
47     # reade some first
48     4.times {@file1.gets; @file2.gets}
50     @file1.reopen(@name2)
51     @file1.gets.should == "Line 1: One\n"
52   end
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")
60     @file1_w.close
61     @file2_w.close
62     File.readlines(@name1_w).should == ["line2-F2\n", "line2-F1\n"]
63     File.readlines(@name2_w).should == ["line1-F2\n"]
64   end
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
70     # read some first
71     @file1.gets
72     @file2.gets
74     pos = @file2.pos
75     @file1.reopen(@file2)
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.
80     @file1.pos = pos
82     @file1.gets.should == "Line 2: Two\n"
83   end
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
89       # reade some first
90       @file1.sysread(length)
91       @file2.sysread(length)
93       @file1.reopen(@file2)
94       @file1.sysread(length).should == "Line 2: Two\n"
95     end
96   end
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")
104     @file1_w.close
105     @file2_w.close
106     File.readlines(@name1_w).should == ["line1-F1\n", "line2-F1\n", "line2-F2\n"]
107     File.readlines(@name2_w).should == ["line1-F2\n"]
108   end