Class: RSpec::CapturingFormatter::StreamProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/capturing_formatter/stream_proxy.rb

Overview

Presents an IO-like stream whose writes are captured or passed unchanged to its backing stream.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backing, source, manager) ⇒ StreamProxy

Returns a new instance of StreamProxy.



206
207
208
209
210
211
212
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 206

def initialize(backing, source, manager)
  @backing = backing
  @source = source
  @manager = manager
  @raw_mode = false
  @closed = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ Object



403
404
405
406
407
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 403

def method_missing(name, *arguments, &block)
  return super unless @backing.respond_to?(name)

  @backing.public_send(name, *arguments, &block)
end

Instance Attribute Details

#backing ⇒ Object (readonly)

Returns the value of attribute backing.



235
236
237
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 235

def backing
  @backing
end

#manager ⇒ Object (readonly)

Returns the value of attribute manager.



233
234
235
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 233

def manager
  @manager
end

#source ⇒ Object (readonly)

Returns the value of attribute source.



204
205
206
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 204

def source
  @source
end

Instance Method Details

#<<(value) ⇒ Object



265
266
267
268
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 265

def <<(value)
  write(value)
  self
end

#activate! ⇒ Object



237
238
239
240
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 237

def activate!
  @raw_mode = false
  @closed = false
end

#binmode ⇒ Object



348
349
350
351
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 348

def binmode
  @backing.binmode if @backing.respond_to?(:binmode)
  self
end

#close ⇒ Object



392
393
394
395
396
397
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 392

def close
  @closed = true
  # A retained proxy can outlive capture; do not close a backing stream restored as a process global.
  @backing.close unless @backing.equal?($stdout) || @backing.equal?($stderr)
  nil
end

#closed? ⇒ Boolean

Returns:

  • (Boolean)


331
332
333
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 331

def closed?
  @closed || (@backing.respond_to?(:closed?) && @backing.closed?)
end

#deactivate! ⇒ Object



242
243
244
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 242

def deactivate!
  @raw_mode = true
end

#descriptor_backing? ⇒ Boolean

Returns:

  • (Boolean)


388
389
390
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 388

def descriptor_backing?
  defined?(IO) && @backing.is_a?(IO)
end

#external_encoding ⇒ Object



335
336
337
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 335

def external_encoding
  @backing.respond_to?(:external_encoding) ? @backing.external_encoding : Encoding::UTF_8
end

#fileno ⇒ Object



353
354
355
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 353

def fileno
  @backing.fileno
end

#flush ⇒ Object



313
314
315
316
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 313

def flush
  @manager.bypass { @backing.flush } if @backing.respond_to?(:flush)
  self
end

#initialize_copy(other) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 214

def initialize_copy(other)
  super
  @manager = other.manager
  # RSpec's any-process output matcher reopens from a clone and needs its current backing stream.
  @manager.synchronize do
    @backing = begin
      other.backing.dup
    rescue
      other.backing
    end
    @raw_mode = other.raw_mode?
    @closed = false
  end
end

#internal_encoding ⇒ Object



339
340
341
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 339

def internal_encoding
  @backing.respond_to?(:internal_encoding) ? @backing.internal_encoding : nil
end


290
291
292
293
294
295
296
297
298
299
300
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 290

def print(*values)
  values = [$_] if values.empty? && defined?($_) && !$_.nil?
  separator = $OUTPUT_FIELD_SEPARATOR
  terminator = $OUTPUT_RECORD_SEPARATOR
  values.each_with_index do |value, index|
    write(separator) if index.positive? && separator
    write(value)
  end
  write(terminator) if values.any? && terminator
  nil
end

#printf(format_string, *values) ⇒ Object



302
303
304
305
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 302

def printf(format_string, *values)
  write(format_string % values)
  nil
end

#put_value(value, ancestors = []) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 276

def put_value(value, ancestors = [])
  if value.is_a?(Array)
    if ancestors.include?(value.object_id)
      write("[...]\n")
    else
      value.each { |entry| put_value(entry, ancestors + [value.object_id]) }
    end
  else
    text = value.nil? ? "\n" : value.to_s
    newline = "\n".encode(text.encoding)
    write(text.end_with?(newline) ? text : text + newline)
  end
end

#putc(value) ⇒ Object



307
308
309
310
311
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 307

def putc(value)
  character = value.is_a?(Integer) ? (value & 0xFF).chr(Encoding::BINARY) : value.to_s[0]
  write(character)
  value
end

#puts(*values) ⇒ Object



270
271
272
273
274
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 270

def puts(*values)
  values = [nil] if values.empty?
  values.each { |value| put_value(value) }
  nil
end

#raw_mode? ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 229

def raw_mode?
  @raw_mode
end

#reopen(target, *arguments) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 361

def reopen(target, *arguments)
  # Proxy targets restore matcher snapshots; other targets must bypass capture.
  @manager.synchronize do
    if target.is_a?(StreamProxy)
      if descriptor_backing?
        @backing.reopen(target.backing)
      else
        @backing = target.backing
      end
      @raw_mode = target.raw_mode?
    elsif descriptor_backing? && (target.respond_to?(:read) || target.respond_to?(:write))
      @backing.reopen(target.respond_to?(:to_io) ? target.to_io : target, *arguments)
      @raw_mode = true
    elsif target.respond_to?(:read) || target.respond_to?(:write)
      @backing = target
      @raw_mode = true
    elsif @backing.respond_to?(:reopen)
      @backing.reopen(target, *arguments)
      @raw_mode = true
    else
      @backing = target
      @raw_mode = true
    end
  end
  self
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


409
410
411
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 409

def respond_to_missing?(name, include_private = false)
  @backing.respond_to?(name, include_private) || super
end

#set_encoding(*arguments) ⇒ Object



343
344
345
346
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 343

def set_encoding(*arguments)
  @backing.set_encoding(*arguments) if @backing.respond_to?(:set_encoding)
  self
end

#sync ⇒ Object



318
319
320
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 318

def sync
  @backing.sync if @backing.respond_to?(:sync)
end

#sync=(value) ⇒ Object



322
323
324
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 322

def sync=(value)
  @backing.sync = value if @backing.respond_to?(:sync=)
end

#syswrite(value) ⇒ Object



260
261
262
263
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 260

def syswrite(value)
  string = String(value)
  @manager.handle_write(self, string, :syswrite)
end

#to_io ⇒ Object



357
358
359
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 357

def to_io
  self
end

#tty? ⇒ Boolean Also known as: isatty

Returns:

  • (Boolean)


326
327
328
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 326

def tty?
  @backing.respond_to?(:tty?) && @backing.tty?
end

#write(value) ⇒ Object



246
247
248
249
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 246

def write(value)
  string = String(value)
  @manager.handle_write(self, string, :write)
end

#write_backing(value, method = :write, **options) ⇒ Object



399
400
401
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 399

def write_backing(value, method = :write, **options)
  @manager.bypass { @backing.public_send(method, value, **options) }
end

#write_nonblock(value, exception: true) ⇒ Object



251
252
253
254
255
256
257
258
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 251

def write_nonblock(value, exception: true)
  string = String(value)
  @manager.handle_write(self, string, :write_nonblock, exception: exception)
rescue IO::WaitWritable, Errno::EAGAIN
  raise if exception

  :wait_writable
end