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
|
require_relative '../fixtures/classes'
# This group of specs may ONLY contain specs that do successfully create
# an IO instance from the file descriptor returned by #new_fd helper.
describe :io_new, shared: true do
before :each do
@name = tmp("io_new.txt")
@fd = new_fd @name
@io = nil
end
after :each do
if @io
@io.close
elsif @fd
IO.new(@fd, "w").close
end
rm_r @name
end
it "creates an IO instance from a Fixnum argument" do
@io = IO.send(@method, @fd, "w")
@io.should be_an_instance_of(IO)
end
it "creates an IO instance when STDOUT is closed" do
suppress_warning do
stdout = STDOUT
stdout_file = tmp("stdout.txt")
begin
@io = IO.send(@method, @fd, "w")
@io.should be_an_instance_of(IO)
ensure
STDOUT = stdout
rm_r stdout_file
end
end
end
it "creates an IO instance when STDERR is closed" do
suppress_warning do
stderr = STDERR
stderr_file = tmp("stderr.txt")
STDERR = new_io stderr_file
STDERR.close
begin
@io = IO.send(@method, @fd, "w")
@io.should be_an_instance_of(IO)
ensure
STDERR = stderr
rm_r stderr_file
end
end
end
it "calls #to_int on an object to convert to a Fixnum" do
obj = mock("file descriptor")
obj.should_receive(:to_int).and_return(@fd)
@io = IO.send(@method, obj, "w")
@io.should be_an_instance_of(IO)
end
it "accepts a :mode option" do
@io = IO.send(@method, @fd, mode: "w")
@io.write("foo").should == 3
end
it "accepts a mode argument set to nil with a valid :mode option" do
@io = IO.send(@method, @fd, nil, mode: "w")
@io.write("foo").should == 3
end
it "accepts a mode argument with a :mode option set to nil" do
@io = IO.send(@method, @fd, "w", mode: nil)
@io.write("foo").should == 3
end
it "uses the external encoding specified in the mode argument" do
@io = IO.send(@method, @fd, 'w:utf-8')
@io.external_encoding.to_s.should == 'UTF-8'
end
it "uses the external and the internal encoding specified in the mode argument" do
@io = IO.send(@method, @fd, 'w:utf-8:ISO-8859-1')
@io.external_encoding.to_s.should == 'UTF-8'
@io.internal_encoding.to_s.should == 'ISO-8859-1'
end
it "uses the external encoding specified via the :external_encoding option" do
@io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8')
@io.external_encoding.to_s.should == 'UTF-8'
end
it "uses the internal encoding specified via the :internal_encoding option" do
@io = IO.send(@method, @fd, 'w', internal_encoding: 'ibm866')
@io.internal_encoding.to_s.should == 'IBM866'
end
it "uses the colon-separated encodings specified via the :encoding option" do
@io = IO.send(@method, @fd, 'w', encoding: 'utf-8:ISO-8859-1')
@io.external_encoding.to_s.should == 'UTF-8'
@io.internal_encoding.to_s.should == 'ISO-8859-1'
end
it "uses the :encoding option as the external encoding when only one is given" do
@io = IO.send(@method, @fd, 'w', encoding: 'ISO-8859-1')
@io.external_encoding.to_s.should == 'ISO-8859-1'
end
it "uses the :encoding options as the external encoding when it's an Encoding object" do
@io = IO.send(@method, @fd, 'w', encoding: Encoding::ISO_8859_1)
@io.external_encoding.should == Encoding::ISO_8859_1
end
it "ignores the :encoding option when the :external_encoding option is present" do
-> {
@io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8', encoding: 'iso-8859-1:iso-8859-1')
}.should complain(/Ignoring encoding parameter/)
@io.external_encoding.to_s.should == 'UTF-8'
end
it "ignores the :encoding option when the :internal_encoding option is present" do
-> {
@io = IO.send(@method, @fd, 'w', internal_encoding: 'ibm866', encoding: 'iso-8859-1:iso-8859-1')
}.should complain(/Ignoring encoding parameter/)
@io.internal_encoding.to_s.should == 'IBM866'
end
it "uses the encoding specified via the :mode option hash" do
@io = IO.send(@method, @fd, mode: 'w:utf-8:ISO-8859-1')
@io.external_encoding.to_s.should == 'UTF-8'
@io.internal_encoding.to_s.should == 'ISO-8859-1'
end
it "ignores the :internal_encoding option when the same as the external encoding" do
@io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8', internal_encoding: 'utf-8')
@io.external_encoding.to_s.should == 'UTF-8'
@io.internal_encoding.to_s.should == ''
end
it "sets internal encoding to nil when passed '-'" do
@io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8', internal_encoding: '-')
@io.external_encoding.to_s.should == 'UTF-8'
@io.internal_encoding.to_s.should == ''
end
it "sets binmode from mode string" do
@io = IO.send(@method, @fd, 'wb')
@io.binmode?.should == true
end
it "does not set binmode without being asked" do
@io = IO.send(@method, @fd, 'w')
@io.binmode?.should == false
end
it "sets binmode from :binmode option" do
@io = IO.send(@method, @fd, 'w', binmode: true)
@io.binmode?.should == true
end
it "does not set binmode from false :binmode" do
@io = IO.send(@method, @fd, 'w', binmode: false)
@io.binmode?.should == false
end
it "sets external encoding to binary with binmode in mode string" do
@io = IO.send(@method, @fd, 'wb')
@io.external_encoding.should == Encoding::BINARY
end
# #5917
it "sets external encoding to binary with :binmode option" do
@io = IO.send(@method, @fd, 'w', binmode: true)
@io.external_encoding.should == Encoding::BINARY
end
it "does not use binary encoding when mode encoding is specified" do
@io = IO.send(@method, @fd, 'wb:iso-8859-1')
@io.external_encoding.to_s.should == 'ISO-8859-1'
end
it "does not use binary encoding when :encoding option is specified" do
@io = IO.send(@method, @fd, 'wb', encoding: "iso-8859-1")
@io.external_encoding.to_s.should == 'ISO-8859-1'
end
it "does not use binary encoding when :external_encoding option is specified" do
@io = IO.send(@method, @fd, 'wb', external_encoding: "iso-8859-1")
@io.external_encoding.to_s.should == 'ISO-8859-1'
end
it "does not use binary encoding when :internal_encoding option is specified" do
@io = IO.send(@method, @fd, 'wb', internal_encoding: "ibm866")
@io.internal_encoding.to_s.should == 'IBM866'
end
it "accepts nil options" do
@io = suppress_keyword_warning do
IO.send(@method, @fd, 'w', nil)
end
@io.write("foo").should == 3
end
it "coerces mode with #to_str" do
mode = mock("mode")
mode.should_receive(:to_str).and_return('w')
@io = IO.send(@method, @fd, mode)
end
it "coerces mode with #to_int" do
mode = mock("mode")
mode.should_receive(:to_int).and_return(File::WRONLY)
@io = IO.send(@method, @fd, mode)
end
it "coerces mode with #to_str when passed in options" do
mode = mock("mode")
mode.should_receive(:to_str).and_return('w')
@io = IO.send(@method, @fd, mode: mode)
end
it "coerces mode with #to_int when passed in options" do
mode = mock("mode")
mode.should_receive(:to_int).and_return(File::WRONLY)
@io = IO.send(@method, @fd, mode: mode)
end
it "coerces :encoding option with #to_str" do
encoding = mock("encoding")
encoding.should_receive(:to_str).and_return('utf-8')
@io = IO.send(@method, @fd, 'w', encoding: encoding)
end
it "coerces :external_encoding option with #to_str" do
encoding = mock("encoding")
encoding.should_receive(:to_str).and_return('utf-8')
@io = IO.send(@method, @fd, 'w', external_encoding: encoding)
end
it "coerces :internal_encoding option with #to_str" do
encoding = mock("encoding")
encoding.should_receive(:to_str).at_least(:once).and_return('utf-8')
@io = IO.send(@method, @fd, 'w', internal_encoding: encoding)
end
it "coerces options as third argument with #to_hash" do
options = mock("options")
options.should_receive(:to_hash).and_return({})
@io = IO.send(@method, @fd, 'w', **options)
end
it "coerces options as second argument with #to_hash" do
options = mock("options")
options.should_receive(:to_hash).and_return({})
@io = IO.send(@method, @fd, **options)
end
it "accepts an :autoclose option" do
@io = IO.send(@method, @fd, 'w', autoclose: false)
@io.autoclose?.should == false
@io.autoclose = true
end
it "accepts any truthy option :autoclose" do
@io = IO.send(@method, @fd, 'w', autoclose: 42)
@io.autoclose?.should == true
end
end
# This group of specs may ONLY contain specs that do not actually create
# an IO instance from the file descriptor returned by #new_fd helper.
describe :io_new_errors, shared: true do
before :each do
@name = tmp("io_new.txt")
@fd = new_fd @name
end
after :each do
IO.new(@fd, "w").close if @fd
rm_r @name
end
it "raises an Errno::EBADF if the file descriptor is not valid" do
-> { IO.send(@method, -1, "w") }.should raise_error(Errno::EBADF)
end
it "raises an IOError if passed a closed stream" do
-> { IO.send(@method, IOSpecs.closed_io.fileno, 'w') }.should raise_error(IOError)
end
platform_is_not :windows do
it "raises an Errno::EINVAL if the new mode is not compatible with the descriptor's current mode" do
-> { IO.send(@method, @fd, "r") }.should raise_error(Errno::EINVAL)
end
end
it "raises ArgumentError if passed an empty mode string" do
-> { IO.send(@method, @fd, "") }.should raise_error(ArgumentError)
end
it "raises an error if passed modes two ways" do
-> {
IO.send(@method, @fd, "w", mode: "w")
}.should raise_error(ArgumentError)
end
it "raises an error if passed encodings two ways" do
-> {
@io = IO.send(@method, @fd, 'w:ISO-8859-1', encoding: 'ISO-8859-1')
}.should raise_error(ArgumentError)
-> {
@io = IO.send(@method, @fd, 'w:ISO-8859-1', external_encoding: 'ISO-8859-1')
}.should raise_error(ArgumentError)
-> {
@io = IO.send(@method, @fd, 'w:ISO-8859-1:UTF-8', internal_encoding: 'ISO-8859-1')
}.should raise_error(ArgumentError)
end
it "raises an error if passed matching binary/text mode two ways" do
-> {
@io = IO.send(@method, @fd, "wb", binmode: true)
}.should raise_error(ArgumentError)
-> {
@io = IO.send(@method, @fd, "wt", textmode: true)
}.should raise_error(ArgumentError)
-> {
@io = IO.send(@method, @fd, "wb", textmode: false)
}.should raise_error(ArgumentError)
-> {
@io = IO.send(@method, @fd, "wt", binmode: false)
}.should raise_error(ArgumentError)
end
it "raises an error if passed conflicting binary/text mode two ways" do
-> {
@io = IO.send(@method, @fd, "wb", binmode: false)
}.should raise_error(ArgumentError)
-> {
@io = IO.send(@method, @fd, "wt", textmode: false)
}.should raise_error(ArgumentError)
-> {
@io = IO.send(@method, @fd, "wb", textmode: true)
}.should raise_error(ArgumentError)
-> {
@io = IO.send(@method, @fd, "wt", binmode: true)
}.should raise_error(ArgumentError)
end
it "raises an error when trying to set both binmode and textmode" do
-> {
@io = IO.send(@method, @fd, "w", textmode: true, binmode: true)
}.should raise_error(ArgumentError)
-> {
@io = IO.send(@method, @fd, File::Constants::WRONLY, textmode: true, binmode: true)
}.should raise_error(ArgumentError)
end
it "raises ArgumentError if not passed a hash or nil for options" do
-> {
@io = IO.send(@method, @fd, 'w', false)
}.should raise_error(ArgumentError)
-> {
@io = IO.send(@method, @fd, false, false)
}.should raise_error(ArgumentError)
-> {
@io = IO.send(@method, @fd, nil, false)
}.should raise_error(ArgumentError)
end
it "raises TypeError if passed a hash for mode and nil for options" do
-> {
suppress_keyword_warning do
@io = IO.send(@method, @fd, {mode: 'w'}, nil)
end
}.should raise_error(TypeError)
end
end
|