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