Skip to content

Fix readlines(::Cmd), was accidentally broken in #20203 #20281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function readlines(filename::AbstractString; chomp::Bool=true)
readlines(f, chomp=chomp)
end
end
readlines(s::IO=STDIN; chomp::Bool=true) = collect(eachline(s, chomp=chomp))
readlines(s=STDIN; chomp::Bool=true) = collect(eachline(s, chomp=chomp))

## byte-order mark, ntoh & hton ##

Expand Down
63 changes: 34 additions & 29 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,52 @@

valgrind_off = ccall(:jl_running_on_valgrind, Cint, ()) == 0

yes = `yes`
echo = `echo`
yescmd = `yes`
echocmd = `echo`
sortcmd = `sort`
printf = `printf`
printfcmd = `printf`
truecmd = `true`
falsecmd = `false`
catcmd = `cat`
shcmd = `sh`
sleepcmd = `sleep`
lscmd = `ls`
if is_windows()
try # use busybox-w32 on windows
success(`busybox`)
yes = `busybox yes`
echo = `busybox echo`
yescmd = `busybox yes`
echocmd = `busybox echo`
sortcmd = `busybox sort`
printf = `busybox printf`
printfcmd = `busybox printf`
truecmd = `busybox true`
falsecmd = `busybox false`
catcmd = `busybox cat`
shcmd = `busybox sh`
sleepcmd = `busybox sleep`
lscmd = `busybox ls`
end
end

#### Examples used in the manual ####

@test readstring(`$echo hello \| sort`) == "hello | sort\n"
@test readstring(pipeline(`$echo hello`, sortcmd)) == "hello\n"
@test length(spawn(pipeline(`$echo hello`, sortcmd)).processes) == 2
@test readstring(`$echocmd hello \| sort`) == "hello | sort\n"
@test readstring(pipeline(`$echocmd hello`, sortcmd)) == "hello\n"
@test length(spawn(pipeline(`$echocmd hello`, sortcmd)).processes) == 2

out = readstring(`$echo hello` & `$echo world`)
out = readstring(`$echocmd hello` & `$echocmd world`)
@test search(out,"world") != 0:-1
@test search(out,"hello") != 0:-1
@test readstring(pipeline(`$echo hello` & `$echo world`, sortcmd)) == "hello\nworld\n"
@test readstring(pipeline(`$echocmd hello` & `$echocmd world`, sortcmd)) == "hello\nworld\n"

@test (run(`$printf " \033[34m[stdio passthrough ok]\033[0m\n"`); true)
@test (run(`$printfcmd " \033[34m[stdio passthrough ok]\033[0m\n"`); true)

# Test for SIGPIPE being treated as normal termination (throws an error if broken)
is_unix() && run(pipeline(yes, `head`, DevNull))
is_unix() && run(pipeline(yescmd, `head`, DevNull))

begin
a = Base.Condition()
@schedule begin
p = spawn(pipeline(yes,DevNull))
p = spawn(pipeline(yescmd,DevNull))
Base.notify(a,p)
@test !success(p)
end
Expand Down Expand Up @@ -84,7 +86,7 @@ end

# STDIN Redirection
let file = tempname()
run(pipeline(`$echo hello world`, file))
run(pipeline(`$echocmd hello world`, file))
@test readstring(pipeline(file, catcmd)) == "hello world\n"
@test open(readstring, pipeline(file, catcmd), "r") == "hello world\n"
rm(file)
Expand All @@ -104,7 +106,7 @@ if !is_windows() # WINNT reports operation not supported on socket (ENOTSUP) for
end
t2 = @async begin
sock = connect(fetch(r))
run(pipeline(`$echo hello world`, sock))
run(pipeline(`$echocmd hello world`, sock))
close(sock)
return true
end
Expand Down Expand Up @@ -212,7 +214,7 @@ if valgrind_off
end

# issue #6310
@test readstring(pipeline(`$echo "2+2"`, `$exename --startup-file=no`)) == "4\n"
@test readstring(pipeline(`$echocmd "2+2"`, `$exename --startup-file=no`)) == "4\n"

# issue #5904
@test run(pipeline(ignorestatus(falsecmd), truecmd)) === nothing
Expand Down Expand Up @@ -270,9 +272,9 @@ end
# issue #10994: libuv can't handle strings containing NUL
let bad = "bad\0name"
@test_throws ArgumentError run(`$bad`)
@test_throws ArgumentError run(`$echo $bad`)
@test_throws ArgumentError run(setenv(`$echo hello`, bad=>"good"))
@test_throws ArgumentError run(setenv(`$echo hello`, "good"=>bad))
@test_throws ArgumentError run(`$echocmd $bad`)
@test_throws ArgumentError run(setenv(`$echocmd hello`, bad=>"good"))
@test_throws ArgumentError run(setenv(`$echocmd hello`, "good"=>bad))
end

# issue #12829
Expand Down Expand Up @@ -391,17 +393,17 @@ end
# equality tests for Cmd
@test Base.Cmd(``) == Base.Cmd(``)
@test Base.Cmd(`lsof -i :9090`) == Base.Cmd(`lsof -i :9090`)
@test Base.Cmd(`$echo test`) == Base.Cmd(`$echo test`)
@test Base.Cmd(``) != Base.Cmd(`$echo test`)
@test Base.Cmd(`$echocmd test`) == Base.Cmd(`$echocmd test`)
@test Base.Cmd(``) != Base.Cmd(`$echocmd test`)
@test Base.Cmd(``, ignorestatus=true) != Base.Cmd(``, ignorestatus=false)
@test Base.Cmd(``, dir="TESTS") != Base.Cmd(``, dir="TEST")
@test Base.Set([``, ``]) == Base.Set([``])
@test Set([``, echo]) != Set([``, ``])
@test Set([echo, ``, ``, echo]) == Set([echo, ``])
@test Set([``, echocmd]) != Set([``, ``])
@test Set([echocmd, ``, ``, echocmd]) == Set([echocmd, ``])

# equality tests for AndCmds
@test Base.AndCmds(`$echo abc`, `$echo def`) == Base.AndCmds(`$echo abc`, `$echo def`)
@test Base.AndCmds(`$echo abc`, `$echo def`) != Base.AndCmds(`$echo abc`, `$echo xyz`)
@test Base.AndCmds(`$echocmd abc`, `$echocmd def`) == Base.AndCmds(`$echocmd abc`, `$echocmd def`)
@test Base.AndCmds(`$echocmd abc`, `$echocmd def`) != Base.AndCmds(`$echocmd abc`, `$echocmd xyz`)

# test for correct error when an empty command is spawned (Issue 19094)
@test_throws ArgumentError run(Base.Cmd(``))
Expand All @@ -411,13 +413,13 @@ end

@test_throws ArgumentError spawn(Base.Cmd(``))
@test_throws ArgumentError spawn(Base.AndCmds(``, ``))
@test_throws ArgumentError spawn(Base.AndCmds(``, `$echo test`))
@test_throws ArgumentError spawn(Base.AndCmds(`$echo test`, ``))
@test_throws ArgumentError spawn(Base.AndCmds(``, `$echocmd test`))
@test_throws ArgumentError spawn(Base.AndCmds(`$echocmd test`, ``))

# tests for reducing over collection of Cmd
@test_throws ArgumentError reduce(&, Base.AbstractCmd[])
@test_throws ArgumentError reduce(&, Base.Cmd[])
@test reduce(&, [`$echo abc`, `$echo def`, `$echo hij`]) == `$echo abc` & `$echo def` & `$echo hij`
@test reduce(&, [`$echocmd abc`, `$echocmd def`, `$echocmd hij`]) == `$echocmd abc` & `$echocmd def` & `$echocmd hij`

# test for proper handling of FD exhaustion
if is_unix()
Expand Down Expand Up @@ -453,3 +455,6 @@ let p=Pipe()
@async close(p.in)
@test readstring(p.out) == "Int128(0xffffffffffffffffffffffffffffffff)"
end

# readlines(::Cmd), accidentally broken in #20203
@test sort(readlines(`$lscmd -A`)) == sort(readdir())
4 changes: 2 additions & 2 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ let io = IOBuffer()
@test !contains(str, "backtrace()")
end

msg = readstring(ignorestatus(`$(Base.julia_cmd()) --startup-file=no --color=no -e '
msg = readstring(pipeline(ignorestatus(`$(Base.julia_cmd()) --startup-file=no --color=no -e '
using Base.Test

foo(x) = length(x)^2
Expand All @@ -444,7 +444,7 @@ foo(x) = length(x)^2
@test foo(zeros(2)) == 4
@test foo(ones(4)) == 15
end
end'`))
end'`), stderr=DevNull))

@test contains(msg,
"""
Expand Down