2015-12-16 05:07:31 +00:00
|
|
|
# frozen_string_literal: false
|
2011-01-23 14:03:24 +00:00
|
|
|
begin
|
|
|
|
require 'io/console'
|
|
|
|
require 'test/unit'
|
2011-06-13 17:23:18 +00:00
|
|
|
require 'pty'
|
2011-01-23 14:03:24 +00:00
|
|
|
rescue LoadError
|
|
|
|
end
|
2010-05-12 14:15:30 +00:00
|
|
|
|
|
|
|
class TestIO_Console < Test::Unit::TestCase
|
2016-04-15 01:06:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defined?(PTY) and defined?(IO.console) and TestIO_Console.class_eval do
|
2012-03-06 01:11:47 +00:00
|
|
|
Bug6116 = '[ruby-dev:45309]'
|
|
|
|
|
2010-05-12 14:15:30 +00:00
|
|
|
def test_raw
|
2010-09-17 07:52:21 +00:00
|
|
|
helper {|m, s|
|
2010-05-12 14:15:30 +00:00
|
|
|
s.print "abc\n"
|
|
|
|
assert_equal("abc\r\n", m.gets)
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 14:15:30 +00:00
|
|
|
s.raw {
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_not_send([s, :echo?], Bug6116)
|
2010-05-12 14:15:30 +00:00
|
|
|
s.print "def\n"
|
|
|
|
assert_equal("def\n", m.gets)
|
|
|
|
}
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 14:15:30 +00:00
|
|
|
s.print "ghi\n"
|
|
|
|
assert_equal("ghi\r\n", m.gets)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-02-03 07:54:18 +00:00
|
|
|
def test_raw_minchar
|
|
|
|
helper {|m, s|
|
2014-05-30 13:28:39 +00:00
|
|
|
len = 0
|
2013-02-05 04:27:57 +00:00
|
|
|
assert_equal([nil, 0], [s.getch(min: 0), len])
|
|
|
|
main = Thread.current
|
|
|
|
go = false
|
2013-02-03 07:54:18 +00:00
|
|
|
th = Thread.start {
|
|
|
|
len += 1
|
2013-02-05 04:27:57 +00:00
|
|
|
m.print("a")
|
|
|
|
m.flush
|
|
|
|
sleep 0.01 until go and main.stop?
|
2013-02-03 07:54:18 +00:00
|
|
|
len += 10
|
2013-02-05 04:27:57 +00:00
|
|
|
m.print("1234567890")
|
|
|
|
m.flush
|
2013-02-03 07:54:18 +00:00
|
|
|
}
|
2014-05-30 13:28:39 +00:00
|
|
|
begin
|
|
|
|
assert_equal(["a", 1], [s.getch(min: 1), len])
|
|
|
|
go = true
|
|
|
|
assert_equal(["1", 11], [s.getch, len])
|
|
|
|
ensure
|
|
|
|
th.join
|
|
|
|
end
|
2013-02-03 07:54:18 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_raw_timeout
|
|
|
|
helper {|m, s|
|
2014-05-30 13:28:39 +00:00
|
|
|
len = 0
|
2013-02-05 04:27:57 +00:00
|
|
|
assert_equal([nil, 0], [s.getch(min: 0, time: 0.1), len])
|
|
|
|
main = Thread.current
|
2013-02-03 07:54:18 +00:00
|
|
|
th = Thread.start {
|
2013-02-05 04:27:57 +00:00
|
|
|
sleep 0.01 until main.stop?
|
|
|
|
len += 2
|
|
|
|
m.print("ab")
|
2013-02-03 07:54:18 +00:00
|
|
|
}
|
2014-05-30 13:28:39 +00:00
|
|
|
begin
|
|
|
|
assert_equal(["a", 2], [s.getch(min: 1, time: 1), len])
|
|
|
|
assert_equal(["b", 2], [s.getch(time: 1), len])
|
|
|
|
ensure
|
|
|
|
th.join
|
|
|
|
end
|
2013-02-03 07:54:18 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-01-11 15:58:40 +00:00
|
|
|
def test_raw!
|
|
|
|
helper {|m, s|
|
|
|
|
s.raw!
|
|
|
|
s.print "foo\n"
|
|
|
|
assert_equal("foo\n", m.gets)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-11-18 07:12:14 +00:00
|
|
|
def test_cooked
|
|
|
|
helper {|m, s|
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_send([s, :echo?])
|
2011-11-18 07:12:14 +00:00
|
|
|
s.raw {
|
|
|
|
s.print "abc\n"
|
|
|
|
assert_equal("abc\n", m.gets)
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_not_send([s, :echo?], Bug6116)
|
2011-11-18 07:12:14 +00:00
|
|
|
s.cooked {
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_send([s, :echo?])
|
2011-11-18 07:12:14 +00:00
|
|
|
s.print "def\n"
|
|
|
|
assert_equal("def\r\n", m.gets)
|
|
|
|
}
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_not_send([s, :echo?], Bug6116)
|
2011-11-18 07:12:14 +00:00
|
|
|
}
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_send([s, :echo?])
|
2011-11-18 07:12:14 +00:00
|
|
|
s.print "ghi\n"
|
|
|
|
assert_equal("ghi\r\n", m.gets)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-07-03 14:29:26 +00:00
|
|
|
def test_echo
|
2010-09-17 07:52:21 +00:00
|
|
|
helper {|m, s|
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_send([s, :echo?])
|
2010-07-03 14:29:26 +00:00
|
|
|
m.print "a"
|
|
|
|
assert_equal("a", m.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-05-12 14:15:30 +00:00
|
|
|
def test_noecho
|
2010-09-17 07:52:21 +00:00
|
|
|
helper {|m, s|
|
2010-07-03 14:29:26 +00:00
|
|
|
s.noecho {
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_not_send([s, :echo?])
|
2010-07-03 14:29:26 +00:00
|
|
|
m.print "a"
|
|
|
|
sleep 0.1
|
|
|
|
}
|
|
|
|
m.print "b"
|
|
|
|
assert_equal("b", m.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_noecho2
|
2010-09-17 07:52:21 +00:00
|
|
|
helper {|m, s|
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 14:15:30 +00:00
|
|
|
m.print "a\n"
|
2010-06-11 16:14:15 +00:00
|
|
|
sleep 0.1
|
2010-05-12 14:15:30 +00:00
|
|
|
s.print "b\n"
|
2010-06-11 16:14:15 +00:00
|
|
|
sleep 0.1
|
2010-05-12 14:15:30 +00:00
|
|
|
assert_equal("a\r\nb\r\n", m.readpartial(10))
|
|
|
|
assert_equal("a\n", s.readpartial(10))
|
|
|
|
s.noecho {
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_not_send([s, :echo?])
|
2010-05-12 14:15:30 +00:00
|
|
|
m.print "a\n"
|
|
|
|
s.print "b\n"
|
|
|
|
assert_equal("b\r\n", m.readpartial(10))
|
|
|
|
assert_equal("a\n", s.readpartial(10))
|
|
|
|
}
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 14:15:30 +00:00
|
|
|
m.print "a\n"
|
2010-06-11 16:14:15 +00:00
|
|
|
sleep 0.1
|
2010-05-12 14:15:30 +00:00
|
|
|
s.print "b\n"
|
2010-06-11 16:14:15 +00:00
|
|
|
sleep 0.1
|
2010-05-12 14:15:30 +00:00
|
|
|
assert_equal("a\r\nb\r\n", m.readpartial(10))
|
|
|
|
assert_equal("a\n", s.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_setecho
|
2010-09-17 07:52:21 +00:00
|
|
|
helper {|m, s|
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_send([s, :echo?])
|
2010-07-03 14:29:26 +00:00
|
|
|
s.echo = false
|
|
|
|
m.print "a"
|
|
|
|
sleep 0.1
|
|
|
|
s.echo = true
|
|
|
|
m.print "b"
|
|
|
|
assert_equal("b", m.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_setecho2
|
2010-09-17 07:52:21 +00:00
|
|
|
helper {|m, s|
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 14:15:30 +00:00
|
|
|
m.print "a\n"
|
2010-06-11 16:14:15 +00:00
|
|
|
sleep 0.1
|
2010-05-12 14:15:30 +00:00
|
|
|
s.print "b\n"
|
2010-06-11 16:14:15 +00:00
|
|
|
sleep 0.1
|
2010-05-12 14:15:30 +00:00
|
|
|
assert_equal("a\r\nb\r\n", m.readpartial(10))
|
|
|
|
assert_equal("a\n", s.readpartial(10))
|
|
|
|
s.echo = false
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_not_send([s, :echo?])
|
2010-05-12 14:15:30 +00:00
|
|
|
m.print "a\n"
|
|
|
|
s.print "b\n"
|
|
|
|
assert_equal("b\r\n", m.readpartial(10))
|
|
|
|
assert_equal("a\n", s.readpartial(10))
|
|
|
|
s.echo = true
|
2012-03-06 01:11:47 +00:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 14:15:30 +00:00
|
|
|
m.print "a\n"
|
2010-06-11 16:14:15 +00:00
|
|
|
sleep 0.1
|
2010-05-12 14:15:30 +00:00
|
|
|
s.print "b\n"
|
2010-06-11 16:14:15 +00:00
|
|
|
sleep 0.1
|
2010-05-12 14:15:30 +00:00
|
|
|
assert_equal("a\r\nb\r\n", m.readpartial(10))
|
|
|
|
assert_equal("a\n", s.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-12-08 13:26:56 +00:00
|
|
|
def test_getpass
|
|
|
|
skip unless IO.method_defined?("getpass")
|
|
|
|
run_pty("p IO.console.getpass('> ')") do |r, w|
|
|
|
|
assert_equal("> ", r.readpartial(10))
|
|
|
|
w.print "asdf\n"
|
2015-12-09 10:45:18 +00:00
|
|
|
sleep 1
|
|
|
|
assert_equal("\r\n", r.gets)
|
2015-12-08 13:26:56 +00:00
|
|
|
assert_equal("\"asdf\"", r.gets.chomp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-12 14:15:30 +00:00
|
|
|
def test_iflush
|
2010-09-17 07:52:21 +00:00
|
|
|
helper {|m, s|
|
2010-07-03 14:29:26 +00:00
|
|
|
m.print "a"
|
2010-05-12 14:15:30 +00:00
|
|
|
s.iflush
|
|
|
|
m.print "b\n"
|
|
|
|
assert_equal("b\n", s.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-05-13 09:45:29 +00:00
|
|
|
def test_oflush
|
2010-09-17 07:52:21 +00:00
|
|
|
helper {|m, s|
|
2010-07-03 14:29:26 +00:00
|
|
|
s.print "a"
|
|
|
|
s.oflush # oflush may be issued after "a" is already sent.
|
|
|
|
s.print "b"
|
2011-02-12 14:17:54 +00:00
|
|
|
assert_include(["b", "ab"], m.readpartial(10))
|
2010-05-13 09:45:29 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-07-03 14:29:26 +00:00
|
|
|
def test_ioflush
|
2010-09-17 07:52:21 +00:00
|
|
|
helper {|m, s|
|
2010-07-03 14:29:26 +00:00
|
|
|
m.print "a"
|
|
|
|
s.ioflush
|
|
|
|
m.print "b\n"
|
|
|
|
assert_equal("b\n", s.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ioflush2
|
2010-09-17 07:52:21 +00:00
|
|
|
helper {|m, s|
|
2010-07-03 14:29:26 +00:00
|
|
|
s.print "a"
|
|
|
|
s.ioflush # ioflush may be issued after "a" is already sent.
|
|
|
|
s.print "b"
|
2011-02-12 14:17:54 +00:00
|
|
|
assert_include(["b", "ab"], m.readpartial(10))
|
2010-07-03 14:29:26 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_winsize
|
2010-09-17 07:52:21 +00:00
|
|
|
helper {|m, s|
|
2010-07-03 15:44:12 +00:00
|
|
|
begin
|
|
|
|
assert_equal([0, 0], s.winsize)
|
|
|
|
rescue Errno::EINVAL # OpenSolaris 2009.06 TIOCGWINSZ causes Errno::EINVAL before TIOCSWINSZ.
|
|
|
|
end
|
2010-07-03 14:29:26 +00:00
|
|
|
}
|
|
|
|
end
|
2010-09-17 07:52:21 +00:00
|
|
|
|
2011-06-16 00:08:56 +00:00
|
|
|
if IO.console
|
2014-12-25 01:09:17 +00:00
|
|
|
def test_close
|
|
|
|
IO.console.close
|
|
|
|
assert_kind_of(IO, IO.console)
|
|
|
|
assert_nothing_raised(IOError) {IO.console.fileno}
|
2014-12-25 07:14:12 +00:00
|
|
|
|
|
|
|
IO.console(:close)
|
|
|
|
assert(IO.console(:tty?))
|
|
|
|
ensure
|
|
|
|
IO.console(:close)
|
2014-12-25 01:09:17 +00:00
|
|
|
end
|
|
|
|
|
2011-06-16 00:08:56 +00:00
|
|
|
def test_sync
|
|
|
|
assert(IO.console.sync, "console should be unbuffered")
|
2014-12-25 07:14:12 +00:00
|
|
|
ensure
|
|
|
|
IO.console(:close)
|
2011-06-16 00:08:56 +00:00
|
|
|
end
|
|
|
|
else
|
2014-12-25 01:09:17 +00:00
|
|
|
def test_close
|
|
|
|
assert_equal(["true"], run_pty("IO.console.close; p IO.console.fileno >= 0"))
|
2014-12-25 07:14:12 +00:00
|
|
|
assert_equal(["true"], run_pty("IO.console(:close); p IO.console(:tty?)"))
|
2014-12-25 01:09:17 +00:00
|
|
|
end
|
|
|
|
|
2011-06-16 00:08:56 +00:00
|
|
|
def test_sync
|
2014-12-25 01:09:17 +00:00
|
|
|
assert_equal(["true"], run_pty("p IO.console.sync"))
|
2011-06-16 00:08:56 +00:00
|
|
|
end
|
2011-06-13 14:33:36 +00:00
|
|
|
end
|
|
|
|
|
2010-09-17 07:52:21 +00:00
|
|
|
private
|
|
|
|
def helper
|
|
|
|
m, s = PTY.open
|
|
|
|
rescue RuntimeError
|
|
|
|
skip $!
|
|
|
|
else
|
|
|
|
yield m, s
|
|
|
|
ensure
|
|
|
|
m.close if m
|
|
|
|
s.close if s
|
|
|
|
end
|
2014-12-25 01:09:17 +00:00
|
|
|
|
2014-12-25 06:17:49 +00:00
|
|
|
def run_pty(src, n = 1)
|
2014-12-25 01:09:17 +00:00
|
|
|
r, w, pid = PTY.spawn(EnvUtil.rubybin, "-rio/console", "-e", src)
|
|
|
|
rescue RuntimeError
|
|
|
|
skip $!
|
|
|
|
else
|
2014-12-25 02:37:17 +00:00
|
|
|
if block_given?
|
2015-12-08 13:26:56 +00:00
|
|
|
yield r, w, pid
|
2014-12-25 02:37:17 +00:00
|
|
|
else
|
2015-12-08 13:26:56 +00:00
|
|
|
result = []
|
|
|
|
n.times {result << r.gets.chomp}
|
2014-12-25 02:37:17 +00:00
|
|
|
result
|
|
|
|
end
|
2014-12-25 01:09:17 +00:00
|
|
|
ensure
|
|
|
|
r.close if r
|
|
|
|
w.close if w
|
2015-12-08 13:26:56 +00:00
|
|
|
Process.wait(pid) if pid
|
2014-12-25 01:09:17 +00:00
|
|
|
end
|
2016-04-15 01:06:49 +00:00
|
|
|
end
|
2011-06-13 14:28:53 +00:00
|
|
|
|
2016-04-15 01:06:49 +00:00
|
|
|
defined?(IO.console) and TestIO_Console.class_eval do
|
2011-06-14 04:07:03 +00:00
|
|
|
case
|
|
|
|
when Process.respond_to?(:daemon)
|
2011-08-04 22:00:36 +00:00
|
|
|
noctty = [EnvUtil.rubybin, "-e", "Process.daemon(true)"]
|
2011-06-14 04:07:03 +00:00
|
|
|
when !(rubyw = RbConfig::CONFIG["RUBYW_INSTALL_NAME"]).empty?
|
|
|
|
dir, base = File.split(EnvUtil.rubybin)
|
2011-08-04 22:00:36 +00:00
|
|
|
noctty = [File.join(dir, base.sub(/ruby/, rubyw))]
|
|
|
|
end
|
2011-06-14 04:07:03 +00:00
|
|
|
|
2011-08-04 22:00:36 +00:00
|
|
|
if noctty
|
|
|
|
require 'tempfile'
|
|
|
|
NOCTTY = noctty
|
2011-06-14 04:07:03 +00:00
|
|
|
def test_noctty
|
2014-12-28 00:24:47 +00:00
|
|
|
t = Tempfile.new("noctty_out")
|
2011-06-13 17:23:18 +00:00
|
|
|
t.close
|
2014-12-28 00:24:47 +00:00
|
|
|
t2 = Tempfile.new("noctty_run")
|
2011-08-04 22:00:36 +00:00
|
|
|
t2.close
|
2014-05-10 12:15:19 +00:00
|
|
|
cmd = [*NOCTTY[1..-1],
|
2013-04-28 03:54:26 +00:00
|
|
|
'-e', 'open(ARGV[0], "w") {|f|',
|
|
|
|
'-e', 'STDOUT.reopen(f)',
|
|
|
|
'-e', 'STDERR.reopen(f)',
|
|
|
|
'-e', 'require "io/console"',
|
|
|
|
'-e', 'f.puts IO.console.inspect',
|
|
|
|
'-e', 'f.flush',
|
|
|
|
'-e', 'File.unlink(ARGV[1])',
|
|
|
|
'-e', '}',
|
2011-08-04 22:00:36 +00:00
|
|
|
'--', t.path, t2.path]
|
2014-05-10 12:15:19 +00:00
|
|
|
assert_ruby_status(cmd, rubybin: NOCTTY[0])
|
2013-04-28 03:54:26 +00:00
|
|
|
30.times do
|
|
|
|
break unless File.exist?(t2.path)
|
|
|
|
sleep 0.1
|
|
|
|
end
|
2011-06-13 17:23:18 +00:00
|
|
|
t.open
|
2013-04-28 03:54:26 +00:00
|
|
|
assert_equal("nil", t.gets(nil).chomp)
|
2011-06-17 22:34:07 +00:00
|
|
|
ensure
|
|
|
|
t.close! if t and !t.closed?
|
2011-08-04 22:00:36 +00:00
|
|
|
t2.close!
|
2011-06-13 17:23:18 +00:00
|
|
|
end
|
2011-06-13 14:28:53 +00:00
|
|
|
end
|
2016-04-15 01:06:49 +00:00
|
|
|
end
|
2012-01-25 18:24:09 +00:00
|
|
|
|
2016-04-15 01:07:14 +00:00
|
|
|
defined?(IO.console) and IO.console and IO.console.respond_to?(:pressed?) and
|
|
|
|
TestIO_Console.class_eval do
|
|
|
|
def test_pressed_valid
|
|
|
|
assert_include([true, false], IO.console.pressed?("HOME"))
|
|
|
|
assert_include([true, false], IO.console.pressed?(:"HOME"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pressed_invalid
|
|
|
|
e = assert_raise(ArgumentError) do
|
|
|
|
IO.console.pressed?("HOME\0")
|
|
|
|
end
|
|
|
|
assert_match(/unknown virtual key code/, e.message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-15 01:06:49 +00:00
|
|
|
TestIO_Console.class_eval do
|
2012-01-25 18:24:09 +00:00
|
|
|
def test_stringio_getch
|
2013-02-03 07:54:24 +00:00
|
|
|
assert_separately %w"--disable=gems -rstringio -rio/console", %q{
|
|
|
|
assert_operator(StringIO, :method_defined?, :getch)
|
|
|
|
}
|
|
|
|
assert_separately %w"--disable=gems -rio/console -rstringio", %q{
|
|
|
|
assert_operator(StringIO, :method_defined?, :getch)
|
|
|
|
}
|
|
|
|
assert_separately %w"--disable=gems -rstringio", %q{
|
|
|
|
assert_not_operator(StringIO, :method_defined?, :getch)
|
|
|
|
}
|
2012-01-25 18:24:09 +00:00
|
|
|
end
|
|
|
|
end
|