Skip to content

Commit ee0f5d7

Browse files
committed
Fix Windows hang when stdin is redirected (#907)
1 parent edf8d6b commit ee0f5d7

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/reline/io.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def self.decide_io_gate
1313
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
1414
require 'reline/io/windows'
1515
io = Reline::Windows.new
16-
if io.msys_tty?
16+
if io.msys_tty? || !STDIN.tty?
1717
Reline::ANSI.new
1818
else
1919
io

test/reline/test_reline.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ def test_dumb_terminal
418418
assert_match(/#<Reline::Dumb/, out.chomp)
419419
end
420420

421+
def test_readline_reads_piped_stdin
422+
out = readline_from_piped_stdin("input\n")
423+
assert_include(out, { result: 'input' }.inspect)
424+
end
425+
426+
def test_readline_returns_nil_on_piped_stdin_eof
427+
out = readline_from_piped_stdin("")
428+
assert_include(out, { result: nil }.inspect)
429+
end
430+
421431
def test_read_eof_returns_input
422432
pend if win?
423433
lib = File.expand_path("../../lib", __dir__)
@@ -460,6 +470,24 @@ def win?
460470
/mswin|mingw/.match?(RUBY_PLATFORM)
461471
end
462472

473+
def readline_from_piped_stdin(stdin)
474+
lib = File.expand_path("../../lib", __dir__)
475+
code = <<~'RUBY'
476+
require 'timeout'
477+
begin
478+
p result: Timeout.timeout(3) { Reline.readline('>') }
479+
rescue Timeout::Error
480+
puts 'timeout'
481+
end
482+
RUBY
483+
484+
IO.popen([Reline.test_rubybin, "-I#{lib}", "-rreline", "-e", code], "r+") do |io|
485+
io.write stdin
486+
io.close_write
487+
io.read
488+
end
489+
end
490+
463491
def test_tty_ambiguous_width
464492
omit unless defined?(PTY)
465493
ruby_file = Tempfile.create('rubyfile')

0 commit comments

Comments
 (0)