| 1 |
# frozen_string_literal: false |
| 2 |
begin |
| 3 |
require 'io/console' |
| 4 |
require 'test/unit' |
| 5 |
require 'pty' |
| 6 |
rescue LoadError |
| 7 |
end |
| 8 |
|
| 9 |
class TestIO_Console < Test::Unit::TestCase |
| 10 |
begin |
| 11 |
PATHS = $LOADED_FEATURES.grep(%r"/io/console(?:\.#{RbConfig::CONFIG['DLEXT']}|\.rb|/\w+\.rb)\z") {$`} |
| 12 |
rescue Encoding::CompatibilityError |
| 13 |
$stderr.puts "test_io_console.rb debug" |
| 14 |
$LOADED_FEATURES.each{|path| $stderr.puts [path, path.encoding].inspect} |
| 15 |
raise |
| 16 |
end |
| 17 |
PATHS.uniq! |
| 18 |
|
| 19 |
# FreeBSD seems to hang on TTOU when running parallel tests |
| 20 |
# tested on FreeBSD 11.x. |
| 21 |
# |
| 22 |
# Solaris gets stuck too, even in non-parallel mode. |
| 23 |
# It occurs only in chkbuild. It does not occur when running |
| 24 |
# `make test-all` in SSH terminal. |
| 25 |
# |
| 26 |
# I suspect that it occurs only when having no TTY. |
| 27 |
# (Parallel mode runs tests in child processes, so I guess |
| 28 |
# they has no TTY.) |
| 29 |
# But it does not occur in `make test-all > /dev/null`, so |
| 30 |
# there should be an additional factor, I guess. |
| 31 |
def set_winsize_setup |
| 32 |
@old_ttou = trap(:TTOU, 'IGNORE') if RUBY_PLATFORM =~ /freebsd|solaris/i |
| 33 |
end |
| 34 |
|
| 35 |
def set_winsize_teardown |
| 36 |
trap(:TTOU, @old_ttou) if defined?(@old_ttou) and @old_ttou |
| 37 |
end |
| 38 |
|
| 39 |
def test_failed_path |
| 40 |
exceptions = %w[ENODEV ENOTTY EBADF ENXIO].map {|e| |
| 41 |
Errno.const_get(e) if Errno.const_defined?(e) |
| 42 |
} |
| 43 |
exceptions.compact! |
| 44 |
omit if exceptions.empty? |
| 45 |
File.open(IO::NULL) do |f| |
| 46 |
e = assert_raise(*exceptions) do |
| 47 |
f.echo? |
| 48 |
end |
| 49 |
assert_include(e.message, IO::NULL) |
| 50 |
end |
| 51 |
end |
| 52 |
|
| 53 |
def test_bad_keyword |
| 54 |
omit if RUBY_ENGINE == 'jruby' |
| 55 |
assert_raise_with_message(ArgumentError, /unknown keyword:.*bad/) do |
| 56 |
File.open(IO::NULL) do |f| |
| 57 |
f.raw(bad: 0) |
| 58 |
end |
| 59 |
end |
| 60 |
end |
| 61 |
end |
| 62 |
|
| 63 |
defined?(PTY) and defined?(IO.console) and TestIO_Console.class_eval do |
| 64 |
Bug6116 = '[ruby-dev:45309]' |
| 65 |
|
| 66 |
def test_raw |
| 67 |
helper {|m, s| |
| 68 |
s.print "abc\n" |
| 69 |
assert_equal("abc\r\n", m.gets) |
| 70 |
assert_send([s, :echo?]) |
| 71 |
s.raw { |
| 72 |
assert_not_send([s, :echo?], Bug6116) |
| 73 |
s.print "def\n" |
| 74 |
assert_equal("def\n", m.gets) |
| 75 |
} |
| 76 |
assert_send([s, :echo?]) |
| 77 |
s.print "ghi\n" |
| 78 |
assert_equal("ghi\r\n", m.gets) |
| 79 |
} |
| 80 |
end |
| 81 |
|
| 82 |
def test_raw_minchar |
| 83 |
q = Thread::Queue.new |
| 84 |
helper {|m, s| |
| 85 |
len = 0 |
| 86 |
assert_equal([nil, 0], [s.getch(min: 0), len]) |
| 87 |
main = Thread.current |
| 88 |
go = false |
| 89 |
th = Thread.start { |
| 90 |
q.pop |
| 91 |
sleep 0.01 until main.stop? |
| 92 |
len += 1 |
| 93 |
m.print("a") |
| 94 |
m.flush |
| 95 |
sleep 0.01 until go and main.stop? |
| 96 |
len += 10 |
| 97 |
m.print("1234567890") |
| 98 |
m.flush |
| 99 |
} |
| 100 |
begin |
| 101 |
sleep 0.1 |
| 102 |
q.push(1) |
| 103 |
assert_equal(["a", 1], [s.getch(min: 1), len]) |
| 104 |
go = true |
| 105 |
assert_equal(["1", 11], [s.getch, len]) |
| 106 |
ensure |
| 107 |
th.join |
| 108 |
end |
| 109 |
} |
| 110 |
end |
| 111 |
|
| 112 |
def test_raw_timeout |
| 113 |
helper {|m, s| |
| 114 |
len = 0 |
| 115 |
assert_equal([nil, 0], [s.getch(min: 0, time: 0.1), len]) |
| 116 |
main = Thread.current |
| 117 |
th = Thread.start { |
| 118 |
sleep 0.01 until main.stop? |
| 119 |
len += 2 |
| 120 |
m.print("ab") |
| 121 |
} |
| 122 |
begin |
| 123 |
assert_equal(["a", 2], [s.getch(min: 1, time: 1), len]) |
| 124 |
assert_equal(["b", 2], [s.getch(time: 1), len]) |
| 125 |
ensure |
| 126 |
th.join |
| 127 |
end |
| 128 |
} |
| 129 |
end |
| 130 |
|
| 131 |
def test_raw! |
| 132 |
helper {|m, s| |
| 133 |
s.raw! |
| 134 |
s.print "foo\n" |
| 135 |
assert_equal("foo\n", m.gets) |
| 136 |
} |
| 137 |
end |
| 138 |
|
| 139 |
def test_cooked |
| 140 |
helper {|m, s| |
| 141 |
assert_send([s, :echo?]) |
| 142 |
s.raw { |
| 143 |
s.print "abc\n" |
| 144 |
assert_equal("abc\n", m.gets) |
| 145 |
assert_not_send([s, :echo?], Bug6116) |
| 146 |
s.cooked { |
| 147 |
assert_send([s, :echo?]) |
| 148 |
s.print "def\n" |
| 149 |
assert_equal("def\r\n", m.gets) |
| 150 |
} |
| 151 |
assert_not_send([s, :echo?], Bug6116) |
| 152 |
} |
| 153 |
assert_send([s, :echo?]) |
| 154 |
s.print "ghi\n" |
| 155 |
assert_equal("ghi\r\n", m.gets) |
| 156 |
} |
| 157 |
end |
| 158 |
|
| 159 |
def test_echo |
| 160 |
helper {|m, s| |
| 161 |
assert_send([s, :echo?]) |
| 162 |
m.print "a" |
| 163 |
assert_equal("a", m.readpartial(10)) |
| 164 |
} |
| 165 |
end |
| 166 |
|
| 167 |
def test_noecho |
| 168 |
helper {|m, s| |
| 169 |
s.noecho { |
| 170 |
assert_not_send([s, :echo?]) |
| 171 |
m.print "a" |
| 172 |
sleep 0.1 |
| 173 |
} |
| 174 |
m.print "b" |
| 175 |
assert_equal("b", m.readpartial(10)) |
| 176 |
} |
| 177 |
end |
| 178 |
|
| 179 |
def test_noecho2 |
| 180 |
helper {|m, s| |
| 181 |
assert_send([s, :echo?]) |
| 182 |
m.print "a\n" |
| 183 |
sleep 0.1 |
| 184 |
s.print "b\n" |
| 185 |
sleep 0.1 |
| 186 |
assert_equal("a\r\nb\r\n", m.gets + m.gets) |
| 187 |
assert_equal("a\n", s.gets) |
| 188 |
s.noecho { |
| 189 |
assert_not_send([s, :echo?]) |
| 190 |
m.print "a\n" |
| 191 |
s.print "b\n" |
| 192 |
assert_equal("b\r\n", m.gets) |
| 193 |
assert_equal("a\n", s.gets) |
| 194 |
} |
| 195 |
assert_send([s, :echo?]) |
| 196 |
m.print "a\n" |
| 197 |
sleep 0.1 |
| 198 |
s.print "b\n" |
| 199 |
sleep 0.1 |
| 200 |
assert_equal("a\r\nb\r\n", m.gets + m.gets) |
| 201 |
assert_equal("a\n", s.gets) |
| 202 |
} |
| 203 |
end |
| 204 |
|
| 205 |
def test_setecho |
| 206 |
helper {|m, s| |
| 207 |
assert_send([s, :echo?]) |
| 208 |
s.echo = false |
| 209 |
m.print "a" |
| 210 |
sleep 0.1 |
| 211 |
s.echo = true |
| 212 |
m.print "b" |
| 213 |
assert_equal("b", m.readpartial(10)) |
| 214 |
} |
| 215 |
end |
| 216 |
|
| 217 |
def test_setecho2 |
| 218 |
helper {|m, s| |
| 219 |
assert_send([s, :echo?]) |
| 220 |
m.print "a\n" |
| 221 |
sleep 0.1 |
| 222 |
s.print "b\n" |
| 223 |
sleep 0.1 |
| 224 |
assert_equal("a\r\nb\r\n", m.gets + m.gets) |
| 225 |
assert_equal("a\n", s.gets) |
| 226 |
s.echo = false |
| 227 |
assert_not_send([s, :echo?]) |
| 228 |
m.print "a\n" |
| 229 |
s.print "b\n" |
| 230 |
assert_equal("b\r\n", m.gets) |
| 231 |
assert_equal("a\n", s.gets) |
| 232 |
s.echo = true |
| 233 |
assert_send([s, :echo?]) |
| 234 |
m.print "a\n" |
| 235 |
sleep 0.1 |
| 236 |
s.print "b\n" |
| 237 |
sleep 0.1 |
| 238 |
assert_equal("a\r\nb\r\n", m.gets + m.gets) |
| 239 |
assert_equal("a\n", s.gets) |
| 240 |
} |
| 241 |
end |
| 242 |
|
| 243 |
def test_getpass |
| 244 |
run_pty("p IO.console.getpass('> ')") do |r, w| |
| 245 |
assert_equal("> ", r.readpartial(10)) |
| 246 |
sleep 0.1 |
| 247 |
w.print "asdf\n" |
| 248 |
sleep 0.1 |
| 249 |
assert_equal("\r\n", r.gets) |
| 250 |
assert_equal("\"asdf\"", r.gets.chomp) |
| 251 |
end |
| 252 |
|
| 253 |
run_pty("p IO.console.getpass('> ')") do |r, w| |
| 254 |
assert_equal("> ", r.readpartial(10)) |
| 255 |
sleep 0.1 |
| 256 |
w.print "asdf\C-D\C-D" |
| 257 |
sleep 0.1 |
| 258 |
assert_equal("\r\n", r.gets) |
| 259 |
assert_equal("\"asdf\"", r.gets.chomp) |
| 260 |
end |
| 261 |
|
| 262 |
run_pty("$VERBOSE, $/ = nil, '.'; p IO.console.getpass('> ')") do |r, w| |
| 263 |
assert_equal("> ", r.readpartial(10)) |
| 264 |
sleep 0.1 |
| 265 |
w.print "asdf\n" |
| 266 |
sleep 0.1 |
| 267 |
assert_equal("\r\n", r.gets) |
| 268 |
assert_equal("\"asdf\"", r.gets.chomp) |
| 269 |
end |
| 270 |
end |
| 271 |
|
| 272 |
def test_iflush |
| 273 |
helper {|m, s| |
| 274 |
m.print "a" |
| 275 |
s.iflush |
| 276 |
m.print "b\n" |
| 277 |
m.flush |
| 278 |
assert_equal("b\n", s.gets) |
| 279 |
} |
| 280 |
end |
| 281 |
|
| 282 |
def test_oflush |
| 283 |
helper {|m, s| |
| 284 |
s.print "a" |
| 285 |
s.oflush # oflush may be issued after "a" is already sent. |
| 286 |
s.print "b" |
| 287 |
s.flush |
| 288 |
sleep 0.1 |
| 289 |
assert_include(["b", "ab"], m.readpartial(10)) |
| 290 |
} |
| 291 |
end |
| 292 |
|
| 293 |
def test_ioflush |
| 294 |
helper {|m, s| |
| 295 |
m.print "a" |
| 296 |
s.ioflush |
| 297 |
m.print "b\n" |
| 298 |
m.flush |
| 299 |
assert_equal("b\n", s.gets) |
| 300 |
} |
| 301 |
end |
| 302 |
|
| 303 |
def test_ioflush2 |
| 304 |
helper {|m, s| |
| 305 |
s.print "a" |
| 306 |
s.ioflush # ioflush may be issued after "a" is already sent. |
| 307 |
s.print "b" |
| 308 |
s.flush |
| 309 |
sleep 0.1 |
| 310 |
assert_include(["b", "ab"], m.readpartial(10)) |
| 311 |
} |
| 312 |
end |
| 313 |
|
| 314 |
def test_winsize |
| 315 |
helper {|m, s| |
| 316 |
begin |
| 317 |
assert_equal([0, 0], s.winsize) |
| 318 |
rescue Errno::EINVAL # OpenSolaris 2009.06 TIOCGWINSZ causes Errno::EINVAL before TIOCSWINSZ. |
| 319 |
else |
| 320 |
assert_equal([80, 25], s.winsize = [80, 25]) |
| 321 |
assert_equal([80, 25], s.winsize) |
| 322 |
#assert_equal([80, 25], m.winsize) |
| 323 |
assert_equal([100, 40], m.winsize = [100, 40]) |
| 324 |
#assert_equal([100, 40], s.winsize) |
| 325 |
assert_equal([100, 40], m.winsize) |
| 326 |
end |
| 327 |
} |
| 328 |
end |
| 329 |
|
| 330 |
def test_set_winsize_invalid_dev |
| 331 |
set_winsize_setup |
| 332 |
[IO::NULL, __FILE__].each do |path| |
| 333 |
open(path) do |io| |
| 334 |
begin |
| 335 |
s = io.winsize |
| 336 |
rescue SystemCallError => e |
| 337 |
assert_raise(e.class) {io.winsize = [0, 0]} |
| 338 |
else |
| 339 |
assert(false, "winsize on #{path} succeed: #{s.inspect}") |
| 340 |
end |
| 341 |
assert_raise(ArgumentError) {io.winsize = [0, 0, 0]} |
| 342 |
end |
| 343 |
end |
| 344 |
ensure |
| 345 |
set_winsize_teardown |
| 346 |
end |
| 347 |
|
| 348 |
def test_cursor_position |
| 349 |
run_pty("#{<<~"begin;"}\n#{<<~'end;'}") do |r, w, _| |
| 350 |
begin; |
| 351 |
con = IO.console |
| 352 |
p con.cursor |
| 353 |
con.cursor_down(3); con.puts |
| 354 |
con.cursor_right(4); con.puts |
| 355 |
con.cursor_left(2); con.puts |
| 356 |
con.cursor_up(1); con.puts |
| 357 |
end; |
| 358 |
assert_equal("\e[6n", r.readpartial(5)) |
| 359 |
w.print("\e[12;34R"); w.flush |
| 360 |
assert_equal([11, 33], eval(r.gets)) |
| 361 |
assert_equal("\e[3B", r.gets.chomp) |
| 362 |
assert_equal("\e[4C", r.gets.chomp) |
| 363 |
assert_equal("\e[2D", r.gets.chomp) |
| 364 |
assert_equal("\e[1A", r.gets.chomp) |
| 365 |
end |
| 366 |
end |
| 367 |
|
| 368 |
def assert_ctrl(expect, cc, r, w) |
| 369 |
sleep 0.1 |
| 370 |
w.print cc |
| 371 |
w.flush |
| 372 |
result = EnvUtil.timeout(3) {r.gets} |
| 373 |
assert_equal(expect, result.chomp) |
| 374 |
end |
| 375 |
|
| 376 |
def test_intr |
| 377 |
# This test fails randomly on FreeBSD 13 |
| 378 |
# http://rubyci.s3.amazonaws.com/freebsd13/ruby-master/log/20220304T163001Z.fail.html.gz |
| 379 |
# |
| 380 |
# 1) Failure: |
| 381 |
# TestIO_Console#test_intr [/usr/home/chkbuild/chkbuild/tmp/build/20220304T163001Z/ruby/test/io/console/test_io_console.rb:387]: |
| 382 |
# <"25"> expected but was |
| 383 |
# <"-e:12:in `p': \e[1mexecution expired (\e[1;4mTimeout::Error\e[m\e[1m)\e[m">. |
| 384 |
omit if /freebsd/ =~ RUBY_PLATFORM |
| 385 |
|
| 386 |
run_pty("#{<<~"begin;"}\n#{<<~'end;'}") do |r, w, _| |
| 387 |
begin; |
| 388 |
require 'timeout' |
| 389 |
STDOUT.puts `stty -a`.scan(/\b\w+ *= *\^.;/), "" |
| 390 |
STDOUT.flush |
| 391 |
con = IO.console |
| 392 |
while c = con.getch |
| 393 |
p c.ord |
| 394 |
p con.getch(intr: false).ord |
| 395 |
begin |
| 396 |
p Timeout.timeout(1) {con.getch(intr: true)}.ord |
| 397 |
rescue Timeout::Error, Interrupt => e |
| 398 |
p e |
| 399 |
end |
| 400 |
end |
| 401 |
end; |
| 402 |
ctrl = {} |
| 403 |
r.each do |l| |
| 404 |
break unless /^(\w+) *= *\^(\\?.)/ =~ l |
| 405 |
ctrl[$1] = eval("?\\C-#$2") |
| 406 |
end |
| 407 |
if cc = ctrl["intr"] |
| 408 |
assert_ctrl("#{cc.ord}", cc, r, w) |
| 409 |
assert_ctrl("#{cc.ord}", cc, r, w) |
| 410 |
assert_ctrl("Interrupt", cc, r, w) unless /linux|solaris/ =~ RUBY_PLATFORM |
| 411 |
end |
| 412 |
if cc = ctrl["dsusp"] |
| 413 |
assert_ctrl("#{cc.ord}", cc, r, w) |
| 414 |
assert_ctrl("#{cc.ord}", cc, r, w) |
| 415 |
assert_ctrl("#{cc.ord}", cc, r, w) |
| 416 |
end |
| 417 |
if cc = ctrl["lnext"] |
| 418 |
assert_ctrl("#{cc.ord}", cc, r, w) |
| 419 |
assert_ctrl("#{cc.ord}", cc, r, w) |
| 420 |
assert_ctrl("#{cc.ord}", cc, r, w) |
| 421 |
end |
| 422 |
if cc = ctrl["stop"] |
| 423 |
assert_ctrl("#{cc.ord}", cc, r, w) |
| 424 |
assert_ctrl("#{cc.ord}", cc, r, w) |
| 425 |
assert_ctrl("#{cc.ord}", cc, r, w) |
| 426 |
end |
| 427 |
end |
| 428 |
end |
| 429 |
|
| 430 |
unless IO.console |
| 431 |
def test_close |
| 432 |
assert_equal(["true"], run_pty("IO.console.close; p IO.console.fileno >= 0")) |
| 433 |
assert_equal(["true"], run_pty("IO.console(:close); p IO.console(:tty?)")) |
| 434 |
end |
| 435 |
|
| 436 |
def test_console_kw |
| 437 |
assert_equal(["File"], run_pty("IO.console.close; p IO.console(:clone, freeze: true).class")) |
| 438 |
end |
| 439 |
|
| 440 |
def test_sync |
| 441 |
assert_equal(["true"], run_pty("p IO.console.sync")) |
| 442 |
end |
| 443 |
end |
| 444 |
|
| 445 |
private |
| 446 |
def helper |
| 447 |
m, s = PTY.open |
| 448 |
rescue RuntimeError |
| 449 |
omit $! |
| 450 |
else |
| 451 |
yield m, s |
| 452 |
ensure |
| 453 |
m.close if m |
| 454 |
s.close if s |
| 455 |
end |
| 456 |
|
| 457 |
def run_pty(src, n = 1) |
| 458 |
pend("PTY.spawn cannot control terminal on JRuby") if RUBY_ENGINE == 'jruby' |
| 459 |
|
| 460 |
args = ["-I#{TestIO_Console::PATHS.join(File::PATH_SEPARATOR)}", "-rio/console", "-e", src] |
| 461 |
args.shift if args.first == "-I" # statically linked |
| 462 |
r, w, pid = PTY.spawn(EnvUtil.rubybin, *args) |
| 463 |
rescue RuntimeError |
| 464 |
omit $! |
| 465 |
else |
| 466 |
if block_given? |
| 467 |
yield r, w, pid |
| 468 |
else |
| 469 |
result = [] |
| 470 |
n.times {result << r.gets.chomp} |
| 471 |
result |
| 472 |
end |
| 473 |
ensure |
| 474 |
r.close if r |
| 475 |
w.close if w |
| 476 |
Process.wait(pid) if pid |
| 477 |
end |
| 478 |
end |
| 479 |
|
| 480 |
defined?(IO.console) and TestIO_Console.class_eval do |
| 481 |
if IO.console |
| 482 |
def test_get_winsize_console |
| 483 |
s = IO.console.winsize |
| 484 |
assert_kind_of(Array, s) |
| 485 |
assert_equal(2, s.size) |
| 486 |
assert_kind_of(Integer, s[0]) |
| 487 |
assert_kind_of(Integer, s[1]) |
| 488 |
end |
| 489 |
|
| 490 |
def test_set_winsize_console |
| 491 |
set_winsize_setup |
| 492 |
s = IO.console.winsize |
| 493 |
assert_nothing_raised(TypeError) {IO.console.winsize = s} |
| 494 |
bug = '[ruby-core:82741] [Bug #13888]' |
| 495 |
begin |
| 496 |
IO.console.winsize = [s[0], s[1]+1] |
| 497 |
assert_equal([s[0], s[1]+1], IO.console.winsize, bug) |
| 498 |
rescue Errno::EINVAL # Error if run on an actual console. |
| 499 |
else |
| 500 |
IO.console.winsize = s |
| 501 |
assert_equal(s, IO.console.winsize, bug) |
| 502 |
end |
| 503 |
ensure |
| 504 |
set_winsize_teardown |
| 505 |
end |
| 506 |
|
| 507 |
def test_close |
| 508 |
IO.console.close |
| 509 |
assert_kind_of(IO, IO.console) |
| 510 |
assert_nothing_raised(IOError) {IO.console.fileno} |
| 511 |
|
| 512 |
IO.console(:close) |
| 513 |
assert(IO.console(:tty?)) |
| 514 |
ensure |
| 515 |
IO.console(:close) |
| 516 |
end |
| 517 |
|
| 518 |
def test_console_kw |
| 519 |
io = IO.console(:clone, freeze: true) |
| 520 |
io.close |
| 521 |
assert_kind_of(IO, io) |
| 522 |
end |
| 523 |
|
| 524 |
def test_sync |
| 525 |
assert(IO.console.sync, "console should be unbuffered") |
| 526 |
ensure |
| 527 |
IO.console(:close) |
| 528 |
end |
| 529 |
|
| 530 |
def test_getch_timeout |
| 531 |
assert_nil(IO.console.getch(intr: true, time: 0.1, min: 0)) |
| 532 |
end |
| 533 |
end |
| 534 |
end |
| 535 |
|
| 536 |
defined?(IO.console) and TestIO_Console.class_eval do |
| 537 |
case |
| 538 |
when Process.respond_to?(:daemon) |
| 539 |
noctty = [EnvUtil.rubybin, "-e", "Process.daemon(true)"] |
| 540 |
when !(rubyw = RbConfig::CONFIG["RUBYW_INSTALL_NAME"]).empty? |
| 541 |
dir, base = File.split(EnvUtil.rubybin) |
| 542 |
noctty = [File.join(dir, base.sub(RUBY_ENGINE, rubyw))] |
| 543 |
end |
| 544 |
|
| 545 |
if noctty |
| 546 |
require 'tempfile' |
| 547 |
NOCTTY = noctty |
| 548 |
def test_noctty |
| 549 |
t = Tempfile.new("noctty_out") |
| 550 |
t.close |
| 551 |
t2 = Tempfile.new("noctty_run") |
| 552 |
t2.close |
| 553 |
cmd = [*NOCTTY[1..-1], |
| 554 |
'-e', 'open(ARGV[0], "w") {|f|', |
| 555 |
'-e', 'STDOUT.reopen(f)', |
| 556 |
'-e', 'STDERR.reopen(f)', |
| 557 |
'-e', 'require "io/console"', |
| 558 |
'-e', 'f.puts IO.console.inspect', |
| 559 |
'-e', 'f.flush', |
| 560 |
'-e', 'File.unlink(ARGV[1])', |
| 561 |
'-e', '}', |
| 562 |
'--', t.path, t2.path] |
| 563 |
assert_ruby_status(cmd, rubybin: NOCTTY[0]) |
| 564 |
30.times do |
| 565 |
break unless File.exist?(t2.path) |
| 566 |
sleep 0.1 |
| 567 |
end |
| 568 |
t.open |
| 569 |
assert_equal("nil", t.gets(nil).chomp) |
| 570 |
ensure |
| 571 |
t.close! if t and !t.closed? |
| 572 |
t2.close! |
| 573 |
end |
| 574 |
end |
| 575 |
end |
| 576 |
|
| 577 |
defined?(IO.console) and IO.console and IO.console.respond_to?(:pressed?) and |
| 578 |
TestIO_Console.class_eval do |
| 579 |
def test_pressed_valid |
| 580 |
assert_include([true, false], IO.console.pressed?("HOME")) |
| 581 |
assert_include([true, false], IO.console.pressed?(:"HOME")) |
| 582 |
end |
| 583 |
|
| 584 |
def test_pressed_invalid |
| 585 |
e = assert_raise(ArgumentError) do |
| 586 |
IO.console.pressed?("HOME\0") |
| 587 |
end |
| 588 |
assert_match(/unknown virtual key code/, e.message) |
| 589 |
end |
| 590 |
end |
| 591 |
|
| 592 |
TestIO_Console.class_eval do |
| 593 |
def test_stringio_getch |
| 594 |
assert_ruby_status %w"--disable=gems -rstringio -rio/console", %q{ |
| 595 |
abort unless StringIO.method_defined?(:getch) |
| 596 |
} |
| 597 |
assert_ruby_status %w"--disable=gems -rio/console -rstringio", %q{ |
| 598 |
abort unless StringIO.method_defined?(:getch) |
| 599 |
} |
| 600 |
assert_ruby_status %w"--disable=gems -rstringio", %q{ |
| 601 |
abort if StringIO.method_defined?(:getch) |
| 602 |
} |
| 603 |
end |
| 604 |
end |