File: reopen_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (261 lines) | stat: -rw-r--r-- 6,835 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
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

require 'fcntl'

describe "IO#reopen" do
  before :each do
    @name = tmp("io_reopen.txt")
    @other_name = tmp("io_reopen_other.txt")

    @io = new_io @name
    @other_io = File.open @other_name, "w"
  end

  after :each do
    @io.close unless @io.closed?
    @other_io.close unless @other_io.closed?
    rm_r @name, @other_name
  end

  it "calls #to_io to convert an object" do
    obj = mock("io")
    obj.should_receive(:to_io).and_return(@other_io)
    @io.reopen obj
  end

  it "changes the class of the instance to the class of the object returned by #to_io" do
    obj = mock("io")
    obj.should_receive(:to_io).and_return(@other_io)
    @io.reopen(obj).should be_an_instance_of(File)
  end

  it "raises an IOError if the object returned by #to_io is closed" do
    obj = mock("io")
    obj.should_receive(:to_io).and_return(IOSpecs.closed_io)
    lambda { @io.reopen obj }.should raise_error(IOError)
  end

  it "raises an TypeError if #to_io does not return an IO instance" do
    obj = mock("io")
    obj.should_receive(:to_io).and_return("something else")
    lambda { @io.reopen obj }.should raise_error(TypeError)
  end

  it "raises an IOError when called on a closed stream with an object" do
    @io.close
    obj = mock("io")
    obj.should_not_receive(:to_io)
    lambda { @io.reopen(STDOUT) }.should raise_error(IOError)
  end

  it "raises an IOError if the IO argument is closed" do
    lambda { @io.reopen(IOSpecs.closed_io) }.should raise_error(IOError)
  end

  it "raises an IOError when called on a closed stream with an IO" do
    @io.close
    lambda { @io.reopen(STDOUT) }.should raise_error(IOError)
  end
end

describe "IO#reopen with a String" do
  before :each do
    @name = fixture __FILE__, "numbered_lines.txt"
    @other_name = tmp("io_reopen.txt")
    touch @other_name
    @io = IOSpecs.io_fixture "lines.txt"

    @tmp_file = tmp("reopen")
  end

  after :each do
    @io.close unless @io.closed?
    rm_r @other_name, @tmp_file
  end

  it "does not raise an exception when called on a closed stream with a path" do
    @io.close
    @io.reopen @name, "r"
    @io.closed?.should be_false
    @io.gets.should == "Line 1: One\n"
  end

  it "returns self" do
    @io.reopen(@name).should equal(@io)
  end

  it "positions a newly created instance at the beginning of the new stream" do
    @io.reopen(@name)
    @io.gets.should == "Line 1: One\n"
  end

  it "positions an instance that has been read from at the beginning of the new stream" do
    @io.gets
    @io.reopen(@name)
    @io.gets.should == "Line 1: One\n"
  end

  platform_is_not :windows do
    it "passes all mode flags through" do
      @io.reopen(@name, "ab")
      (@io.fcntl(Fcntl::F_GETFL) & File::APPEND).should == File::APPEND
    end
  end

  it "effects exec/system/fork performed after it" do
    ruby_exe fixture(__FILE__, "reopen_stdout.rb"), :args => @tmp_file
    @tmp_file.should have_data("from system\nfrom exec", "r")
  end

  ruby_version_is "1.9" do
    it "calls #to_path on non-String arguments" do
      obj = mock('path')
      obj.should_receive(:to_path).and_return(@other_name)
      @io.reopen(obj)
    end
  end
end

describe "IO#reopen with a String" do
  before :each do
    @name = tmp("io_reopen.txt")
    @other_name = tmp("io_reopen_other.txt")

    rm_r @other_name
  end

  after :each do
    @io.close unless @io.closed?
    rm_r @name, @other_name
  end

  it "opens a path after writing to the original file descriptor" do
    @io = new_io @name, "w"

    @io.print "original data"
    @io.reopen @other_name
    @io.print "new data"
    @io.flush

    @name.should have_data("original data")
    @other_name.should have_data("new data")
  end

  it "creates the file if it doesn't exist if the IO is opened in write mode" do
    @io = new_io @name, "w"

    @io.reopen(@other_name)
    File.exists?(@other_name).should be_true
  end

  it "creates the file if it doesn't exist if the IO is opened in write mode" do
    @io = new_io @name, "a"

    @io.reopen(@other_name)
    File.exists?(@other_name).should be_true
  end
end

describe "IO#reopen with a String" do
  before :each do
    @name = tmp("io_reopen.txt")
    @other_name = tmp("io_reopen_other.txt")

    touch @name
    rm_r @other_name
  end

  after :each do
    # Do not close @io, the exception leaves MRI with an invalid
    # IO and an Errno::EBADF will be raised on #close.
    rm_r @name, @other_name
  end

  it "raises an Errno::ENOENT if the file does not exist and the IO is not opened in write mode" do
    @io = new_io @name, "r"
    lambda { @io.reopen(@other_name) }.should raise_error(Errno::ENOENT)
  end
end

describe "IO#reopen with an IO" do
  before :each do
    @name = tmp("io_reopen.txt")
    @other_name = tmp("io_reopen_other.txt")
    touch(@other_name) do |f|
      f.puts "Line 1"
      f.puts "Line 2"
    end

    @io = new_io @name
    @other_io = new_io @other_name, "r"
  end

  after :each do
    @io.close unless @io.closed?
    @other_io.close unless @other_io.closed?
    rm_r @name, @other_name
  end

  it "does not call #to_io" do
    # Why do we not use #should_not_receive(:to_io) here? Because
    # MRI actually changes the class of @io in the call to #reopen
    # but does not preserve the existing singleton class of @io.
    def @io.to_io; flunk; end
    @io.reopen(@other_io).should be_an_instance_of(IO)
  end

  it "does not change the object_id" do
    obj_id = @io.object_id
    @io.reopen @other_io
    @io.object_id.should == obj_id
  end

  it "reads from the beginning if the other IO has not been read from" do
    @io.reopen @other_io
    @io.gets.should == "Line 1\n"
  end

  ruby_bug '#', '1.8' do
    it "reads from the current position of the other IO's stream" do
      @other_io.gets.should == "Line 1\n"
      @io.reopen @other_io
      @io.gets.should == "Line 2\n"
    end
  end
end

describe "IO#reopen with an IO" do
  before :each do
    @name = tmp("io_reopen.txt")
    @other_name = tmp("io_reopen_other.txt")

    @io = new_io @name
    @other_io = File.open @other_name, "w"
  end

  after :each do
    @io.close unless @io.closed?
    @other_io.close unless @other_io.closed?
    rm_r @name, @other_name
  end

  it "associates the IO instance with the other IO's stream" do
    @other_name.should have_data("")
    @io.reopen @other_io
    @io.print "io data"
    @io.flush
    @name.should have_data("")
    @other_name.should have_data("io data")
  end

  it "may change the class of the instance" do
    @io.reopen @other_io
    @io.should be_an_instance_of(File)
  end

  it "sets path equals to the other IO's path if other IO is File" do
    @io.reopen @other_io
    @io.path.should == @other_io.path
  end
end