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
|
# -*- encoding: utf-8 -*-
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/readlines'
describe "IO#readlines" do
before :each do
@io = IOSpecs.io_fixture "lines.txt"
@orig_extenc = Encoding.default_external
Encoding.default_external = Encoding::UTF_8
end
after :each do
@io.close unless @io.closed?
Encoding.default_external = @orig_extenc
end
it "raises an IOError if the stream is closed" do
@io.close
-> { @io.readlines }.should raise_error(IOError)
end
describe "when passed no arguments" do
before :each do
suppress_warning {@sep, $/ = $/, " "}
end
after :each do
suppress_warning {$/ = @sep}
end
it "returns an Array containing lines based on $/" do
@io.readlines.should == IOSpecs.lines_space_separator
end
end
describe "when passed no arguments" do
it "updates self's position" do
@io.readlines
@io.pos.should eql(137)
end
it "updates self's lineno based on the number of lines read" do
@io.readlines
@io.lineno.should eql(9)
end
it "does not change $_" do
$_ = "test"
@io.readlines
$_.should == "test"
end
it "returns an empty Array when self is at the end" do
@io.readlines.should == IOSpecs.lines
@io.readlines.should == []
end
end
describe "when passed nil" do
it "returns the remaining content as one line starting at the current position" do
@io.readlines(nil).should == [IOSpecs.lines.join]
end
end
describe "when passed an empty String" do
it "returns an Array containing all paragraphs" do
@io.readlines("").should == IOSpecs.paragraphs
end
end
describe "when passed a separator" do
it "returns an Array containing lines based on the separator" do
@io.readlines("r").should == IOSpecs.lines_r_separator
end
it "returns an empty Array when self is at the end" do
@io.readlines
@io.readlines("r").should == []
end
it "updates self's lineno based on the number of lines read" do
@io.readlines("r")
@io.lineno.should eql(5)
end
it "updates self's position based on the number of characters read" do
@io.readlines("r")
@io.pos.should eql(137)
end
it "does not change $_" do
$_ = "test"
@io.readlines("r")
$_.should == "test"
end
it "tries to convert the passed separator to a String using #to_str" do
obj = mock('to_str')
obj.stub!(:to_str).and_return("r")
@io.readlines(obj).should == IOSpecs.lines_r_separator
end
end
describe "when passed limit" do
it "raises ArgumentError when passed 0 as a limit" do
-> { @io.readlines(0) }.should raise_error(ArgumentError)
end
it "does not accept Integers that don't fit in a C off_t" do
-> { @io.readlines(2**128) }.should raise_error(RangeError)
end
end
describe "when passed chomp" do
it "returns the first line without a trailing newline character" do
@io.readlines(chomp: true).should == IOSpecs.lines_without_newline_characters
end
it "raises exception when options passed as Hash" do
-> { @io.readlines({ chomp: true }) }.should raise_error(TypeError)
-> {
@io.readlines("\n", 1, { chomp: true })
}.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 0..2)")
end
end
describe "when passed arbitrary keyword argument" do
it "tolerates it" do
@io.readlines(chomp: true, foo: :bar).should == IOSpecs.lines_without_newline_characters
end
end
end
describe "IO#readlines" do
before :each do
@name = tmp("io_readlines")
end
after :each do
rm_r @name
end
it "raises an IOError if the stream is opened for append only" do
-> do
File.open(@name, "a:utf-8") { |f| f.readlines }
end.should raise_error(IOError)
end
it "raises an IOError if the stream is opened for write only" do
-> do
File.open(@name, "w:utf-8") { |f| f.readlines }
end.should raise_error(IOError)
end
end
describe "IO.readlines" do
before :each do
@external = Encoding.default_external
Encoding.default_external = Encoding::UTF_8
@name = fixture __FILE__, "lines.txt"
ScratchPad.record []
end
after :each do
Encoding.default_external = @external
end
it "does not change $_" do
$_ = "test"
IO.readlines(@name)
$_.should == "test"
end
describe "when passed a string that starts with a |" do
it "gets data from the standard out of the subprocess" do
cmd = "|sh -c 'echo hello;echo line2'"
platform_is :windows do
cmd = "|cmd.exe /C echo hello&echo line2"
end
lines = nil
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
lines = IO.readlines(cmd)
end
lines.should == ["hello\n", "line2\n"]
end
platform_is_not :windows do
it "gets data from a fork when passed -" do
lines = nil
suppress_warning do # https://bugs.ruby-lang.org/issues/19630
lines = IO.readlines("|-")
end
if lines # parent
lines.should == ["hello\n", "from a fork\n"]
else
puts "hello"
puts "from a fork"
exit!
end
end
end
end
ruby_version_is "3.3" do
# https://bugs.ruby-lang.org/issues/19630
it "warns about deprecation given a path with a pipe" do
cmd = "|echo ok"
-> {
IO.readlines(cmd)
}.should complain(/IO process creation with a leading '\|'/)
end
end
it_behaves_like :io_readlines, :readlines
it_behaves_like :io_readlines_options_19, :readlines
end
describe "IO.readlines" do
before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal
@name = fixture __FILE__, "lines.txt"
@dollar_slash = $/
end
after :each do
Encoding.default_external = @external
Encoding.default_internal = @internal
suppress_warning {$/ = @dollar_slash}
end
it "encodes lines using the default external encoding" do
Encoding.default_external = Encoding::UTF_8
lines = IO.readlines(@name)
lines.all? { |s| s.encoding == Encoding::UTF_8 }.should be_true
end
it "encodes lines using the default internal encoding, when set" do
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_16
suppress_warning {$/ = $/.encode Encoding::UTF_16}
lines = IO.readlines(@name)
lines.all? { |s| s.encoding == Encoding::UTF_16 }.should be_true
end
it "ignores the default internal encoding if the external encoding is BINARY" do
Encoding.default_external = Encoding::BINARY
Encoding.default_internal = Encoding::UTF_8
lines = IO.readlines(@name)
lines.all? { |s| s.encoding == Encoding::BINARY }.should be_true
end
end
|