diff --git a/test/misc.jl b/test/misc.jl index ad03f45118402..f079e4214cec8 100644 --- a/test/misc.jl +++ b/test/misc.jl @@ -510,16 +510,31 @@ end # PR #28038 (prompt/getpass stream args) @test_throws MethodError Base.getpass(IOBuffer(), stdout, "pass") -let buf = IOBuffer() - @test Base.prompt(IOBuffer("foo\nbar\n"), buf, "baz") == "foo" - @test String(take!(buf)) == "baz: " - @test Base.prompt(IOBuffer("\n"), buf, "baz", default="foobar") == "foobar" - @test String(take!(buf)) == "baz [foobar]: " - @test Base.prompt(IOBuffer("blah\n"), buf, "baz", default="foobar") == "blah" - take!(buf) - # prompt timeout - @test Base.prompt(stdin, buf, "baz", default="foobar", timeout = 1) == "foobar" - @test String(take!(buf)) == "baz [foobar] timeout 1 second: timed out\n" +original_stdin = stdin +try + let buf = IOBuffer() + @test Base.prompt(IOBuffer("foo\nbar\n"), buf, "baz") == "foo" + @test String(take!(buf)) == "baz: " + @test Base.prompt(IOBuffer("\n"), buf, "baz", default="foobar") == "foobar" + @test String(take!(buf)) == "baz [foobar]: " + @test Base.prompt(IOBuffer("blah\n"), buf, "baz", default="foobar") == "blah" + take!(buf) + # prompt timeout + + (rd, wr) = redirect_stdin() + @test Base.prompt(rd, buf, "baz", default="foobar", timeout = 1) == "foobar" + @test String(take!(buf)) == "baz [foobar] timeout 1 second: timed out\n" + @test Base.prompt(rd, buf, "baz", default="foobar", timeout = 2) == "foobar" + @test String(take!(buf)) == "baz [foobar] timeout 2 seconds: timed out\n" + @test Base.prompt(IOBuffer("foo\n"), buf, "baz", default="foobar", timeout = 1) == "foo" + @test String(take!(buf)) == "baz [foobar] timeout 1 second: " + @test Base.prompt(IOBuffer("\n"), buf, "baz", default="foobar", timeout = 1) == "foobar" + @test String(take!(buf)) == "baz [foobar] timeout 1 second: " + end +catch + rethrow() +finally + redirect_stdin(original_stdin) end # these tests are not in a test block so that they will compile separately