/tools/Ruby/lib/ruby/site_ruby/1.8/rubygems/mock_gem_ui.rb

http://github.com/agross/netopenspace · Ruby · 64 lines · 43 code · 18 blank · 3 comment · 1 complexity · 858ce5be55b8d66ce98f122e85bef1d2 MD5 · raw file

  1. require 'stringio'
  2. require 'rubygems/user_interaction'
  3. ##
  4. # This Gem::StreamUI subclass records input and output to StringIO for
  5. # retrieval during tests.
  6. class Gem::MockGemUi < Gem::StreamUI
  7. class TermError < RuntimeError; end
  8. class SystemExitException < RuntimeError; end
  9. module TTY
  10. attr_accessor :tty
  11. def tty?()
  12. @tty = true unless defined?(@tty)
  13. @tty
  14. end
  15. def noecho
  16. yield self
  17. end
  18. end
  19. def initialize(input = "")
  20. ins = StringIO.new input
  21. outs = StringIO.new
  22. errs = StringIO.new
  23. ins.extend TTY
  24. outs.extend TTY
  25. errs.extend TTY
  26. super ins, outs, errs, true
  27. @terminated = false
  28. end
  29. def input
  30. @ins.string
  31. end
  32. def output
  33. @outs.string
  34. end
  35. def error
  36. @errs.string
  37. end
  38. def terminated?
  39. @terminated
  40. end
  41. def terminate_interaction(status=0)
  42. @terminated = true
  43. raise TermError unless status == 0
  44. raise SystemExitException, status
  45. end
  46. end