0

I'm using Ruby PTY to integration-test a script that takes input and returns corresponding output.

The problem is that sometimes this script crashes, raising Errno::EIO.

In my case the script crashes returning a certain traceback.

My question is, how can I capture the spawned process's traceback and print it?

PTY.spawn('ruby script.rb') do |reader, writer, pid|
  reader.expect(/Input 1:/)
  writer.puts('1')

  reader.expect(/Input 2:/)
  writer.puts('2')

  # Assume exception occurs now, script.sh
  # prints some 'undefined variable' traceback
  reader.expect(/Input 3:/)
  writer.puts('3')
end
TheNavigat
  • 864
  • 10
  • 30
  • you can redirect the error stream to the output stream , so you'll have to run 'ruby script.rb 2>&1' – oneLeggedChicken Aug 01 '17 at 16:06
  • How can I capture the output when the error is raised, though? Just print `reader`? Will it even contain any data when the error is raised? – TheNavigat Aug 02 '17 at 06:55
  • yes , apparently the error raised is not an issue you should check this answer on that part https://stackoverflow.com/a/1162850 , so you should just add an exception for the IO error and then you're good to go you will still have the stack trace for the command available from the stdout buffer `reader` – oneLeggedChicken Aug 02 '17 at 07:54
  • I used `each` to print `reader` in the rescued exception, it didn't work. `'each': Input/output error @ io_fillbuf - fd:7 /dev/pts/3 (Errno::EIO)` – TheNavigat Aug 07 '17 at 13:59
  • well you should output that outside the exception block i actually made a script to test when i mentioned that answer above and it seemed to work require 'pty' PTY.spawn('ruby script.rb 2>&1') do |reader, writer, pid| begin reader.each{|line| puts line} rescue Errno::EIO end end – oneLeggedChicken Aug 08 '17 at 14:32
  • Hmmm, this is different from what I'm trying to do. In my case the script is responsive, so I "expect" output, pass in input, and so on and so forth. The problem is that when an `expect` fails, I can't get that last X lines of output. Is that clearer? Can you please take a look at the code I've written above again? – TheNavigat Aug 17 '17 at 10:25
  • There are some answers to the issue of `Errno::EIO` being raised [here](https://stackoverflow.com/questions/10238298/ruby-on-linux-pty-goes-away-without-eof-raises-errnoeio/72976571), including [mine](https://stackoverflow.com/a/72976571/4869068) (which avoids raising the exception). – tangle Jul 14 '22 at 07:13

0 Answers0