PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/test/mri/io/console/test_io_console.rb

http://github.com/jruby/jruby
Ruby | 353 lines | 326 code | 26 blank | 1 comment | 13 complexity | 9e1328fc8f8d6900f7a342b9c4f8560d MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, GPL-2.0, JSON, LGPL-2.1
  1. # frozen_string_literal: false
  2. begin
  3. require 'io/console'
  4. require 'test/unit'
  5. require 'pty'
  6. rescue LoadError
  7. end
  8. class TestIO_Console < Test::Unit::TestCase
  9. Bug6116 = '[ruby-dev:45309]'
  10. def test_raw
  11. helper {|m, s|
  12. s.print "abc\n"
  13. assert_equal("abc\r\n", m.gets)
  14. assert_send([s, :echo?])
  15. s.raw {
  16. assert_not_send([s, :echo?], Bug6116)
  17. s.print "def\n"
  18. assert_equal("def\n", m.gets)
  19. }
  20. assert_send([s, :echo?])
  21. s.print "ghi\n"
  22. assert_equal("ghi\r\n", m.gets)
  23. }
  24. end
  25. def test_raw_minchar
  26. helper {|m, s|
  27. len = 0
  28. assert_equal([nil, 0], [s.getch(min: 0), len])
  29. main = Thread.current
  30. go = false
  31. th = Thread.start {
  32. len += 1
  33. m.print("a")
  34. m.flush
  35. sleep 0.01 until go and main.stop?
  36. len += 10
  37. m.print("1234567890")
  38. m.flush
  39. }
  40. begin
  41. assert_equal(["a", 1], [s.getch(min: 1), len])
  42. go = true
  43. assert_equal(["1", 11], [s.getch, len])
  44. ensure
  45. th.join
  46. end
  47. }
  48. end
  49. def test_raw_timeout
  50. helper {|m, s|
  51. len = 0
  52. assert_equal([nil, 0], [s.getch(min: 0, time: 0.1), len])
  53. main = Thread.current
  54. th = Thread.start {
  55. sleep 0.01 until main.stop?
  56. len += 2
  57. m.print("ab")
  58. }
  59. begin
  60. assert_equal(["a", 2], [s.getch(min: 1, time: 1), len])
  61. assert_equal(["b", 2], [s.getch(time: 1), len])
  62. ensure
  63. th.join
  64. end
  65. }
  66. end
  67. def test_raw!
  68. helper {|m, s|
  69. s.raw!
  70. s.print "foo\n"
  71. assert_equal("foo\n", m.gets)
  72. }
  73. end
  74. def test_cooked
  75. helper {|m, s|
  76. assert_send([s, :echo?])
  77. s.raw {
  78. s.print "abc\n"
  79. assert_equal("abc\n", m.gets)
  80. assert_not_send([s, :echo?], Bug6116)
  81. s.cooked {
  82. assert_send([s, :echo?])
  83. s.print "def\n"
  84. assert_equal("def\r\n", m.gets)
  85. }
  86. assert_not_send([s, :echo?], Bug6116)
  87. }
  88. assert_send([s, :echo?])
  89. s.print "ghi\n"
  90. assert_equal("ghi\r\n", m.gets)
  91. }
  92. end
  93. def test_echo
  94. helper {|m, s|
  95. assert_send([s, :echo?])
  96. m.print "a"
  97. assert_equal("a", m.readpartial(10))
  98. }
  99. end
  100. def test_noecho
  101. helper {|m, s|
  102. s.noecho {
  103. assert_not_send([s, :echo?])
  104. m.print "a"
  105. sleep 0.1
  106. }
  107. m.print "b"
  108. assert_equal("b", m.readpartial(10))
  109. }
  110. end
  111. def test_noecho2
  112. helper {|m, s|
  113. assert_send([s, :echo?])
  114. m.print "a\n"
  115. sleep 0.1
  116. s.print "b\n"
  117. sleep 0.1
  118. assert_equal("a\r\nb\r\n", m.readpartial(10))
  119. assert_equal("a\n", s.readpartial(10))
  120. s.noecho {
  121. assert_not_send([s, :echo?])
  122. m.print "a\n"
  123. s.print "b\n"
  124. assert_equal("b\r\n", m.readpartial(10))
  125. assert_equal("a\n", s.readpartial(10))
  126. }
  127. assert_send([s, :echo?])
  128. m.print "a\n"
  129. sleep 0.1
  130. s.print "b\n"
  131. sleep 0.1
  132. assert_equal("a\r\nb\r\n", m.readpartial(10))
  133. assert_equal("a\n", s.readpartial(10))
  134. }
  135. end
  136. def test_setecho
  137. helper {|m, s|
  138. assert_send([s, :echo?])
  139. s.echo = false
  140. m.print "a"
  141. sleep 0.1
  142. s.echo = true
  143. m.print "b"
  144. assert_equal("b", m.readpartial(10))
  145. }
  146. end
  147. def test_setecho2
  148. helper {|m, s|
  149. assert_send([s, :echo?])
  150. m.print "a\n"
  151. sleep 0.1
  152. s.print "b\n"
  153. sleep 0.1
  154. assert_equal("a\r\nb\r\n", m.readpartial(10))
  155. assert_equal("a\n", s.readpartial(10))
  156. s.echo = false
  157. assert_not_send([s, :echo?])
  158. m.print "a\n"
  159. s.print "b\n"
  160. assert_equal("b\r\n", m.readpartial(10))
  161. assert_equal("a\n", s.readpartial(10))
  162. s.echo = true
  163. assert_send([s, :echo?])
  164. m.print "a\n"
  165. sleep 0.1
  166. s.print "b\n"
  167. sleep 0.1
  168. assert_equal("a\r\nb\r\n", m.readpartial(10))
  169. assert_equal("a\n", s.readpartial(10))
  170. }
  171. end
  172. def test_getpass
  173. skip unless IO.method_defined?("getpass")
  174. run_pty("p IO.console.getpass('> ')") do |r, w|
  175. assert_equal("> ", r.readpartial(10))
  176. w.print "asdf\n"
  177. sleep 1
  178. assert_equal("\r\n", r.gets)
  179. assert_equal("\"asdf\"", r.gets.chomp)
  180. end
  181. end
  182. def test_iflush
  183. helper {|m, s|
  184. m.print "a"
  185. s.iflush
  186. m.print "b\n"
  187. assert_equal("b\n", s.readpartial(10))
  188. }
  189. end
  190. def test_oflush
  191. helper {|m, s|
  192. s.print "a"
  193. s.oflush # oflush may be issued after "a" is already sent.
  194. s.print "b"
  195. assert_include(["b", "ab"], m.readpartial(10))
  196. }
  197. end
  198. def test_ioflush
  199. helper {|m, s|
  200. m.print "a"
  201. s.ioflush
  202. m.print "b\n"
  203. assert_equal("b\n", s.readpartial(10))
  204. }
  205. end
  206. def test_ioflush2
  207. helper {|m, s|
  208. s.print "a"
  209. s.ioflush # ioflush may be issued after "a" is already sent.
  210. s.print "b"
  211. assert_include(["b", "ab"], m.readpartial(10))
  212. }
  213. end
  214. def test_winsize
  215. helper {|m, s|
  216. begin
  217. assert_equal([0, 0], s.winsize)
  218. rescue Errno::EINVAL # OpenSolaris 2009.06 TIOCGWINSZ causes Errno::EINVAL before TIOCSWINSZ.
  219. end
  220. }
  221. end
  222. if IO.console
  223. def test_close
  224. IO.console.close
  225. assert_kind_of(IO, IO.console)
  226. assert_nothing_raised(IOError) {IO.console.fileno}
  227. IO.console(:close)
  228. assert(IO.console(:tty?))
  229. ensure
  230. IO.console(:close)
  231. end
  232. def test_sync
  233. assert(IO.console.sync, "console should be unbuffered")
  234. ensure
  235. IO.console(:close)
  236. end
  237. else
  238. def test_close
  239. assert_equal(["true"], run_pty("IO.console.close; p IO.console.fileno >= 0"))
  240. assert_equal(["true"], run_pty("IO.console(:close); p IO.console(:tty?)"))
  241. end
  242. def test_sync
  243. assert_equal(["true"], run_pty("p IO.console.sync"))
  244. end
  245. end
  246. private
  247. def helper
  248. m, s = PTY.open
  249. rescue RuntimeError
  250. skip $!
  251. else
  252. yield m, s
  253. ensure
  254. m.close if m
  255. s.close if s
  256. end
  257. def run_pty(src, n = 1)
  258. r, w, pid = PTY.spawn(EnvUtil.rubybin, "-rio/console", "-e", src)
  259. rescue RuntimeError
  260. skip $!
  261. else
  262. if block_given?
  263. yield r, w, pid
  264. else
  265. result = []
  266. n.times {result << r.gets.chomp}
  267. result
  268. end
  269. ensure
  270. r.close if r
  271. w.close if w
  272. Process.wait(pid) if pid
  273. end
  274. end if defined?(PTY) and defined?(IO::console)
  275. class TestIO_Console < Test::Unit::TestCase
  276. case
  277. when Process.respond_to?(:daemon)
  278. noctty = [EnvUtil.rubybin, "-e", "Process.daemon(true)"]
  279. when !(rubyw = RbConfig::CONFIG["RUBYW_INSTALL_NAME"]).empty?
  280. dir, base = File.split(EnvUtil.rubybin)
  281. base = base.sub(/ruby/, rubyw) if base != rubyw
  282. noctty = [File.join(dir, base)]
  283. end
  284. if noctty
  285. require 'tempfile'
  286. NOCTTY = noctty
  287. def test_noctty
  288. t = Tempfile.new("noctty_out")
  289. t.close
  290. t2 = Tempfile.new("noctty_run")
  291. t2.close
  292. cmd = [*NOCTTY[1..-1],
  293. '-e', 'open(ARGV[0], "w") {|f|',
  294. '-e', 'STDOUT.reopen(f)',
  295. '-e', 'STDERR.reopen(f)',
  296. '-e', 'require "io/console"',
  297. '-e', 'f.puts IO.console.inspect',
  298. '-e', 'f.flush',
  299. '-e', 'File.unlink(ARGV[1])',
  300. '-e', '}',
  301. '--', t.path, t2.path]
  302. assert_ruby_status(cmd, rubybin: NOCTTY[0])
  303. 30.times do
  304. break unless File.exist?(t2.path)
  305. sleep 0.1
  306. end
  307. t.open
  308. assert_equal("nil", t.gets(nil).chomp)
  309. ensure
  310. t.close! if t and !t.closed?
  311. t2.close!
  312. end
  313. end
  314. end if defined?(IO.console)
  315. class TestIO_Console < Test::Unit::TestCase
  316. def test_stringio_getch
  317. assert_separately %w"--disable=gems -rstringio -rio/console", %q{
  318. assert_operator(StringIO, :method_defined?, :getch)
  319. }
  320. assert_separately %w"--disable=gems -rio/console -rstringio", %q{
  321. assert_operator(StringIO, :method_defined?, :getch)
  322. }
  323. assert_separately %w"--disable=gems -rstringio", %q{
  324. assert_not_operator(StringIO, :method_defined?, :getch)
  325. }
  326. end
  327. end