You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

interceptor.rb 1.8 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # frozen_string_literal: true
  2. module Cucumber
  3. module Formatter
  4. module Interceptor
  5. class Pipe
  6. attr_reader :pipe
  7. def initialize(pipe)
  8. @pipe = pipe
  9. @buffer = StringIO.new
  10. @wrapped = true
  11. @lock = Mutex.new
  12. end
  13. def write(str)
  14. @lock.synchronize do
  15. @buffer << str if @wrapped
  16. return @pipe.write(str)
  17. end
  18. end
  19. def buffer_string
  20. @lock.synchronize do
  21. return @buffer.string.dup
  22. end
  23. end
  24. def unwrap!
  25. @wrapped = false
  26. @pipe
  27. end
  28. def method_missing(method, *args, &blk)
  29. @pipe.respond_to?(method) ? @pipe.send(method, *args, &blk) : super
  30. end
  31. def respond_to_missing?(method, include_private = false)
  32. super || @pipe.respond_to?(method, include_private)
  33. end
  34. def self.validate_pipe(pipe)
  35. raise ArgumentError, '#wrap only accepts :stderr or :stdout' unless %i[stdout stderr].include? pipe
  36. end
  37. def self.unwrap!(pipe)
  38. validate_pipe pipe
  39. wrapped = nil
  40. case pipe
  41. when :stdout
  42. wrapped = $stdout
  43. $stdout = wrapped.unwrap! if $stdout.respond_to?(:unwrap!)
  44. when :stderr
  45. wrapped = $stderr
  46. $stderr = wrapped.unwrap! if $stderr.respond_to?(:unwrap!)
  47. end
  48. wrapped
  49. end
  50. def self.wrap(pipe)
  51. validate_pipe pipe
  52. case pipe
  53. when :stderr
  54. $stderr = new($stderr)
  55. $stderr
  56. when :stdout
  57. $stdout = new($stdout)
  58. $stdout
  59. end
  60. end
  61. end
  62. end
  63. end
  64. end

No Description

Contributors (1)