2011-01-23 09:03:24 -05:00
|
|
|
begin
|
|
|
|
require 'io/console'
|
|
|
|
require 'test/unit'
|
2011-06-13 13:23:18 -04:00
|
|
|
require 'pty'
|
2011-01-23 09:03:24 -05:00
|
|
|
rescue LoadError
|
|
|
|
end
|
2012-01-25 13:24:09 -05:00
|
|
|
require_relative '../../ruby/envutil'
|
2010-05-12 10:15:30 -04:00
|
|
|
|
|
|
|
class TestIO_Console < Test::Unit::TestCase
|
2012-03-05 20:11:47 -05:00
|
|
|
Bug6116 = '[ruby-dev:45309]'
|
|
|
|
|
2010-05-12 10:15:30 -04:00
|
|
|
def test_raw
|
2010-09-17 03:52:21 -04:00
|
|
|
helper {|m, s|
|
2010-05-12 10:15:30 -04:00
|
|
|
s.print "abc\n"
|
|
|
|
assert_equal("abc\r\n", m.gets)
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 10:15:30 -04:00
|
|
|
s.raw {
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_not_send([s, :echo?], Bug6116)
|
2010-05-12 10:15:30 -04:00
|
|
|
s.print "def\n"
|
|
|
|
assert_equal("def\n", m.gets)
|
|
|
|
}
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 10:15:30 -04:00
|
|
|
s.print "ghi\n"
|
|
|
|
assert_equal("ghi\r\n", m.gets)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-11-18 02:12:14 -05:00
|
|
|
def test_cooked
|
|
|
|
helper {|m, s|
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_send([s, :echo?])
|
2011-11-18 02:12:14 -05:00
|
|
|
s.raw {
|
|
|
|
s.print "abc\n"
|
|
|
|
assert_equal("abc\n", m.gets)
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_not_send([s, :echo?], Bug6116)
|
2011-11-18 02:12:14 -05:00
|
|
|
s.cooked {
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_send([s, :echo?])
|
2011-11-18 02:12:14 -05:00
|
|
|
s.print "def\n"
|
|
|
|
assert_equal("def\r\n", m.gets)
|
|
|
|
}
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_not_send([s, :echo?], Bug6116)
|
2011-11-18 02:12:14 -05:00
|
|
|
}
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_send([s, :echo?])
|
2011-11-18 02:12:14 -05:00
|
|
|
s.print "ghi\n"
|
|
|
|
assert_equal("ghi\r\n", m.gets)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-07-03 10:29:26 -04:00
|
|
|
def test_echo
|
2010-09-17 03:52:21 -04:00
|
|
|
helper {|m, s|
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_send([s, :echo?])
|
2010-07-03 10:29:26 -04:00
|
|
|
m.print "a"
|
|
|
|
assert_equal("a", m.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-05-12 10:15:30 -04:00
|
|
|
def test_noecho
|
2010-09-17 03:52:21 -04:00
|
|
|
helper {|m, s|
|
2010-07-03 10:29:26 -04:00
|
|
|
s.noecho {
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_not_send([s, :echo?])
|
2010-07-03 10:29:26 -04:00
|
|
|
m.print "a"
|
|
|
|
sleep 0.1
|
|
|
|
}
|
|
|
|
m.print "b"
|
|
|
|
assert_equal("b", m.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_noecho2
|
2010-09-17 03:52:21 -04:00
|
|
|
helper {|m, s|
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 10:15:30 -04:00
|
|
|
m.print "a\n"
|
2010-06-11 12:14:15 -04:00
|
|
|
sleep 0.1
|
2010-05-12 10:15:30 -04:00
|
|
|
s.print "b\n"
|
2010-06-11 12:14:15 -04:00
|
|
|
sleep 0.1
|
2010-05-12 10:15:30 -04:00
|
|
|
assert_equal("a\r\nb\r\n", m.readpartial(10))
|
|
|
|
assert_equal("a\n", s.readpartial(10))
|
|
|
|
s.noecho {
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_not_send([s, :echo?])
|
2010-05-12 10:15:30 -04: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-05 20:11:47 -05:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 10:15:30 -04:00
|
|
|
m.print "a\n"
|
2010-06-11 12:14:15 -04:00
|
|
|
sleep 0.1
|
2010-05-12 10:15:30 -04:00
|
|
|
s.print "b\n"
|
2010-06-11 12:14:15 -04:00
|
|
|
sleep 0.1
|
2010-05-12 10:15:30 -04: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 03:52:21 -04:00
|
|
|
helper {|m, s|
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_send([s, :echo?])
|
2010-07-03 10:29:26 -04: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 03:52:21 -04:00
|
|
|
helper {|m, s|
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 10:15:30 -04:00
|
|
|
m.print "a\n"
|
2010-06-11 12:14:15 -04:00
|
|
|
sleep 0.1
|
2010-05-12 10:15:30 -04:00
|
|
|
s.print "b\n"
|
2010-06-11 12:14:15 -04:00
|
|
|
sleep 0.1
|
2010-05-12 10:15:30 -04:00
|
|
|
assert_equal("a\r\nb\r\n", m.readpartial(10))
|
|
|
|
assert_equal("a\n", s.readpartial(10))
|
|
|
|
s.echo = false
|
2012-03-05 20:11:47 -05:00
|
|
|
assert_not_send([s, :echo?])
|
2010-05-12 10:15:30 -04: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-05 20:11:47 -05:00
|
|
|
assert_send([s, :echo?])
|
2010-05-12 10:15:30 -04:00
|
|
|
m.print "a\n"
|
2010-06-11 12:14:15 -04:00
|
|
|
sleep 0.1
|
2010-05-12 10:15:30 -04:00
|
|
|
s.print "b\n"
|
2010-06-11 12:14:15 -04:00
|
|
|
sleep 0.1
|
2010-05-12 10:15:30 -04:00
|
|
|
assert_equal("a\r\nb\r\n", m.readpartial(10))
|
|
|
|
assert_equal("a\n", s.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_iflush
|
2010-09-17 03:52:21 -04:00
|
|
|
helper {|m, s|
|
2010-07-03 10:29:26 -04:00
|
|
|
m.print "a"
|
2010-05-12 10:15:30 -04:00
|
|
|
s.iflush
|
|
|
|
m.print "b\n"
|
|
|
|
assert_equal("b\n", s.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-05-13 05:45:29 -04:00
|
|
|
def test_oflush
|
2010-09-17 03:52:21 -04:00
|
|
|
helper {|m, s|
|
2010-07-03 10:29:26 -04:00
|
|
|
s.print "a"
|
|
|
|
s.oflush # oflush may be issued after "a" is already sent.
|
|
|
|
s.print "b"
|
2011-02-12 09:17:54 -05:00
|
|
|
assert_include(["b", "ab"], m.readpartial(10))
|
2010-05-13 05:45:29 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-07-03 10:29:26 -04:00
|
|
|
def test_ioflush
|
2010-09-17 03:52:21 -04:00
|
|
|
helper {|m, s|
|
2010-07-03 10:29:26 -04:00
|
|
|
m.print "a"
|
|
|
|
s.ioflush
|
|
|
|
m.print "b\n"
|
|
|
|
assert_equal("b\n", s.readpartial(10))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ioflush2
|
2010-09-17 03:52:21 -04:00
|
|
|
helper {|m, s|
|
2010-07-03 10:29:26 -04:00
|
|
|
s.print "a"
|
|
|
|
s.ioflush # ioflush may be issued after "a" is already sent.
|
|
|
|
s.print "b"
|
2011-02-12 09:17:54 -05:00
|
|
|
assert_include(["b", "ab"], m.readpartial(10))
|
2010-07-03 10:29:26 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_winsize
|
2010-09-17 03:52:21 -04:00
|
|
|
helper {|m, s|
|
2010-07-03 11:44:12 -04:00
|
|
|
begin
|
|
|
|
assert_equal([0, 0], s.winsize)
|
|
|
|
rescue Errno::EINVAL # OpenSolaris 2009.06 TIOCGWINSZ causes Errno::EINVAL before TIOCSWINSZ.
|
|
|
|
end
|
2010-07-03 10:29:26 -04:00
|
|
|
}
|
|
|
|
end
|
2010-09-17 03:52:21 -04:00
|
|
|
|
2011-06-15 20:08:56 -04:00
|
|
|
if IO.console
|
|
|
|
def test_sync
|
|
|
|
assert(IO.console.sync, "console should be unbuffered")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
def test_sync
|
|
|
|
r, w, pid = PTY.spawn(EnvUtil.rubybin, "-rio/console", "-e", "p IO.console.class")
|
2011-08-01 02:36:09 -04:00
|
|
|
rescue RuntimeError
|
|
|
|
skip $!
|
|
|
|
else
|
2011-06-15 20:08:56 -04:00
|
|
|
con = r.gets.chomp
|
|
|
|
Process.wait(pid)
|
|
|
|
assert_match("File", con)
|
|
|
|
end
|
2011-06-13 10:33:36 -04:00
|
|
|
end
|
|
|
|
|
2010-09-17 03:52:21 -04: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
|
2011-01-23 09:03:24 -05:00
|
|
|
end if defined?(PTY) and defined?(IO::console)
|
2011-06-13 10:28:53 -04:00
|
|
|
|
|
|
|
class TestIO_Console < Test::Unit::TestCase
|
2011-06-14 00:07:03 -04:00
|
|
|
case
|
|
|
|
when Process.respond_to?(:daemon)
|
2011-08-04 18:00:36 -04:00
|
|
|
noctty = [EnvUtil.rubybin, "-e", "Process.daemon(true)"]
|
2011-06-14 00:07:03 -04:00
|
|
|
when !(rubyw = RbConfig::CONFIG["RUBYW_INSTALL_NAME"]).empty?
|
|
|
|
dir, base = File.split(EnvUtil.rubybin)
|
2011-08-04 18:00:36 -04:00
|
|
|
noctty = [File.join(dir, base.sub(/ruby/, rubyw))]
|
|
|
|
end
|
2011-06-14 00:07:03 -04:00
|
|
|
|
2011-08-04 18:00:36 -04:00
|
|
|
if noctty
|
|
|
|
require 'tempfile'
|
|
|
|
NOCTTY = noctty
|
2011-06-14 00:07:03 -04:00
|
|
|
def test_noctty
|
2011-06-13 13:23:18 -04:00
|
|
|
t = Tempfile.new("console")
|
|
|
|
t.close
|
2011-08-04 18:00:36 -04:00
|
|
|
t2 = Tempfile.new("console")
|
|
|
|
t2.close
|
|
|
|
cmd = NOCTTY + [
|
2012-01-25 13:24:09 -05:00
|
|
|
'--disable=gems',
|
2011-08-04 18:00:36 -04:00
|
|
|
'-rio/console',
|
|
|
|
'-e', 'open(ARGV[0], "w") {|f| f.puts IO.console.inspect}',
|
|
|
|
'-e', 'File.unlink(ARGV[1])',
|
|
|
|
'--', t.path, t2.path]
|
2011-06-13 13:23:18 -04:00
|
|
|
system(*cmd)
|
2011-08-04 18:00:36 -04:00
|
|
|
sleep 0.1 while File.exist?(t2.path)
|
2011-06-13 13:23:18 -04:00
|
|
|
t.open
|
|
|
|
assert_equal("nil", t.gets.chomp)
|
2011-06-17 18:34:07 -04:00
|
|
|
ensure
|
|
|
|
t.close! if t and !t.closed?
|
2011-08-04 18:00:36 -04:00
|
|
|
t2.close!
|
2011-06-13 13:23:18 -04:00
|
|
|
end
|
2011-06-13 10:28:53 -04:00
|
|
|
end
|
2011-06-13 13:23:18 -04:00
|
|
|
end if defined?(IO.console)
|
2012-01-25 13:24:09 -05:00
|
|
|
|
|
|
|
class TestIO_Console < Test::Unit::TestCase
|
|
|
|
def test_stringio_getch
|
|
|
|
assert_ruby_status(%w"--disable=gems -rstringio -rio/console", "exit(StringIO.method_defined?(:getch))")
|
|
|
|
assert_ruby_status(%w"--disable=gems -rio/console -rstringio", "exit(StringIO.method_defined?(:getch))")
|
|
|
|
assert_ruby_status(%w"--disable=gems -rstringio", "exit(!StringIO.method_defined?(:getch))")
|
|
|
|
end
|
|
|
|
end
|