Skip to content

Commit 0ce7a52

Browse files
committed
update per JuliaLang#14660, add tests for filename-as-1st-arg read methods
1 parent f290e2e commit 0ce7a52

File tree

2 files changed

+58
-30
lines changed

2 files changed

+58
-30
lines changed

base/io.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ type EachLine
348348
EachLine(stream, ondone) = new(stream, ondone)
349349
end
350350
eachline(stream::IO) = EachLine(stream)
351-
eachline(filename::AbstractString) = EachLine(open(filename), close)
351+
function eachline(filename::AbstractString)
352+
s = open(filename)
353+
EachLine(s, ()->close(s))
354+
end
352355

353356
start(itr::EachLine) = nothing
354357
function done(itr::EachLine, nada)

test/read.jl

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ l = Vector{Tuple{AbstractString,Function}}()
1212

1313
# File
1414
io = (text) -> begin
15-
open(io-> write(io, text), filename, "w")
15+
write(filename, text)
1616
Base.Filesystem.open(filename, Base.Filesystem.JL_O_RDONLY)
1717
end
1818
s = io(text)
@@ -24,7 +24,7 @@ push!(l, ("File", io))
2424

2525
# IOStream
2626
io = (text) -> begin
27-
open(io-> write(io, text), filename, "w")
27+
write(filename, text)
2828
open(filename)
2929
end
3030
s = io(text)
@@ -100,7 +100,7 @@ push!(l, ("PipeEndpoint", io))
100100

101101
# Pipe
102102
io = (text) -> begin
103-
open(io->write(io, text), filename, "w")
103+
write(filename, text)
104104
open(`$(@windows ? "type" : "cat") $filename`)[1]
105105
# Was open(`echo -n $text`)[1]
106106
# See https://github.com/JuliaLang/julia/issues/14747
@@ -128,18 +128,20 @@ end
128128

129129
verbose = false
130130

131-
cleanup()
131+
cleanup()
132132

133133
for (name, f) in l
134134

135135
io = ()->(s=f(text); push!(open_streams, s); s)
136136

137+
write(filename, text)
138+
137139
verbose && println("$name read...")
138140
@test read(io(), UInt8) == read(IOBuffer(text), UInt8)
139-
@test read(io(), UInt8) == open(io->read(io, UInt8), filename)
141+
@test read(io(), UInt8) == read(filename, UInt8)
140142
@test read(io(), Int) == read(IOBuffer(text), Int)
141-
@test read(io(), Int) == open(io->read(io,Int),filename)
142-
cleanup()
143+
@test read(io(), Int) == read(filename,Int)
144+
cleanup()
143145
s1 = io()
144146
s2 = IOBuffer(text)
145147
@test read(s1, UInt32, 2) == read(s2, UInt32, 2)
@@ -176,13 +178,19 @@ for (name, f) in l
176178
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO +1)])
177179
]
178180

179-
verbose && println("$name readall...")
180-
@test readall(io()) == text
181-
cleanup()
181+
write(filename, text)
182+
183+
verbose && println("$name readstring...")
184+
@test readstring(io()) == text
185+
cleanup()
186+
@test readstring(io()) == readstring(filename)
187+
cleanup()
182188

183-
verbose && println("$name readbytes...")
184-
@test readbytes(io()) == Vector{UInt8}(text)
185-
cleanup()
189+
verbose && println("$name read...")
190+
@test read(io()) == Vector{UInt8}(text)
191+
cleanup()
192+
@test read(io()) == read(filename)
193+
cleanup()
186194

187195
verbose && println("$name readbytes!...")
188196
l = length(text)
@@ -198,49 +206,66 @@ for (name, f) in l
198206
@test a1[1:n1] == a2[1:n2]
199207
@test n <= length(text) || eof(s1)
200208
@test n <= length(text) || eof(s2)
201-
cleanup()
209+
cleanup()
202210
end
203211

204212
verbose && println("$name read!...")
205213
l = length(text)
206214
for n = [1, 2, l-2, l-1, l]
207215
@test read!(io(), Vector{UInt8}(n)) ==
208216
read!(IOBuffer(text), Vector{UInt8}(n))
209-
cleanup()
217+
cleanup()
218+
@test read!(io(), Vector{UInt8}(n)) ==
219+
read!(filename, Vector{UInt8}(n))
220+
cleanup()
210221
end
211222
@test_throws EOFError read!(io(), Vector{UInt8}(length(text)+1))
212223

213-
cleanup()
224+
cleanup()
214225

215226
verbose && println("$name readuntil...")
216227
@test readuntil(io(), '\n') == readuntil(IOBuffer(text),'\n')
217-
cleanup()
228+
cleanup()
229+
@test readuntil(io(), '\n') == readuntil(filename,'\n')
230+
cleanup()
218231
@test readuntil(io(), "\n") == readuntil(IOBuffer(text),"\n")
219-
cleanup()
232+
cleanup()
233+
@test readuntil(io(), "\n") == readuntil(filename,"\n")
234+
cleanup()
220235
@test readuntil(io(), ',') == readuntil(IOBuffer(text),',')
221-
cleanup()
236+
cleanup()
237+
@test readuntil(io(), ',') == readuntil(filename,',')
238+
cleanup()
222239

223240
verbose && println("$name readline...")
224241
@test readline(io()) == readline(IOBuffer(text))
225-
cleanup()
242+
cleanup()
243+
@test readline(io()) == readline(filename)
244+
cleanup()
226245

227246
verbose && println("$name readlines...")
228247
@test readlines(io()) == readlines(IOBuffer(text))
229-
cleanup()
248+
cleanup()
249+
@test readlines(io()) == readlines(filename)
250+
cleanup()
230251
@test collect(eachline(io())) == collect(eachline(IOBuffer(text)))
231-
cleanup()
252+
cleanup()
253+
@test collect(eachline(io())) == collect(eachline(filename))
254+
cleanup()
232255

233256
verbose && println("$name countlines...")
234257
@test countlines(io()) == countlines(IOBuffer(text))
235-
cleanup()
258+
cleanup()
236259

237260
verbose && println("$name readcsv...")
238261
@test readcsv(io()) == readcsv(IOBuffer(text))
239-
cleanup()
262+
cleanup()
263+
@test readcsv(io()) == readcsv(filename)
264+
cleanup()
240265
end
241266

242267
text = old_text
243-
268+
write(filename, text)
244269

245270
if !(typeof(io()) in [Base.PipeEndpoint, Pipe, TCPSocket])
246271

@@ -250,19 +275,19 @@ for (name, f) in l
250275
verbose && println("$name seek...")
251276
for n = 0:length(text)-1
252277
@test readlines(seek(io(), n)) == readlines(seek(IOBuffer(text), n))
253-
cleanup()
278+
cleanup()
254279
end
255280
verbose && println("$name skip...")
256281
for n = 0:length(text)-1
257282
@test readlines(seek(io(), n)) == readlines(seek(IOBuffer(text), n))
258283
@test readlines(skip(io(), n)) == readlines(skip(IOBuffer(text), n))
259-
cleanup()
284+
cleanup()
260285
end
261286
verbose && println("$name seekend...")
262-
@test readall(seekend(io())) == ""
287+
@test readstring(seekend(io())) == ""
263288
end
264289

265-
cleanup()
290+
cleanup()
266291
end
267292

268293
end

0 commit comments

Comments
 (0)