Sha256: c9cadc3d52e530d37d50cf59084541894378e31120f0986e620209206420002f

Contents?: true

Size: 1.73 KB

Versions: 33

Compression:

Stored size: 1.73 KB

Contents

require 'thread'

module Cucumber
  module Formatter
    module Interceptor
      class Pipe
        attr_reader :pipe
        def initialize(pipe)
          @pipe = pipe
          @buffer = []
          @wrapped = true
        end

        def write(str)
          lock.synchronize do 
            @buffer << str if @wrapped
            return @pipe.write(str)
          end
        end

        def buffer
          lock.synchronize do
            return @buffer.dup
          end
        end

        def unwrap!
          @wrapped = false
          @pipe
        end

        def method_missing(method, *args, &blk)
          @pipe.send(method, *args, &blk)
        end

        def respond_to?(method, include_private=false)
          super || @pipe.respond_to?(method, include_private)
        end

        def self.validate_pipe(pipe)
          unless [:stdout, :stderr].include? pipe
            raise ArgumentError, '#wrap only accepts :stderr or :stdout'
          end
        end

        def self.unwrap!(pipe)
          validate_pipe pipe
          wrapped = nil
          case pipe
          when :stdout
            wrapped = $stdout
            $stdout = wrapped.unwrap! if $stdout.respond_to?(:unwrap!)
          when :stderr
            wrapped = $stderr
            $stderr = wrapped.unwrap! if $stderr.respond_to?(:unwrap!)
          end
          wrapped
        end

        def self.wrap(pipe)
          validate_pipe pipe

          case pipe
          when :stderr
            $stderr = self.new($stderr)
            return $stderr
          when :stdout
            $stdout = self.new($stdout)
            return $stdout
          end
        end

      private
        def lock
          @lock||=Mutex.new
        end
      end
    end
  end
end

Version data entries

33 entries across 31 versions & 3 rubygems

Version Path
cucumber-2.99.0 lib/cucumber/formatter/interceptor.rb
mobiusloop-0.1.5 lib/cucumber/formatter/interceptor.rb
cucumber-2.4.0 lib/cucumber/formatter/interceptor.rb
mobiusloop-0.1.3 lib/cucumber/formatter/interceptor.rb
mobiusloop-0.1.2 lib/cucumber/formatter/interceptor.rb
cucumber-2.3.3 lib/cucumber/formatter/interceptor.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/cucumber-1.3.18/lib/cucumber/formatter/interceptor.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/cucumber-1.3.18/lib/cucumber/formatter/interceptor.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/cucumber-1.3.16/lib/cucumber/formatter/interceptor.rb
cucumber-2.3.2 lib/cucumber/formatter/interceptor.rb
cucumber-2.3.1 lib/cucumber/formatter/interceptor.rb
cucumber-2.3.0 lib/cucumber/formatter/interceptor.rb
cucumber-2.2.0 lib/cucumber/formatter/interceptor.rb
cucumber-2.1.0 lib/cucumber/formatter/interceptor.rb
cucumber-2.0.2 lib/cucumber/formatter/interceptor.rb
cucumber-2.0.1 lib/cucumber/formatter/interceptor.rb
cucumber-1.3.20 lib/cucumber/formatter/interceptor.rb
cucumber-2.0.0 lib/cucumber/formatter/interceptor.rb
cucumber-2.0.0.rc.5 lib/cucumber/formatter/interceptor.rb
cucumber-1.3.19 lib/cucumber/formatter/interceptor.rb