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.



211
212
213
214
215
216
217
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 211

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



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

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

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

Instance Attribute Details

#backingObject (readonly)

Returns the value of attribute backing.



240
241
242
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 240

def backing
  @backing
end

#managerObject (readonly)

Returns the value of attribute manager.



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

def manager
  @manager
end

#sourceObject (readonly)

Returns the value of attribute source.



209
210
211
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 209

def source
  @source
end

Instance Method Details

#<<(value) ⇒ Object



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

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

#activate!Object



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

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

#binmodeObject



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

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

#closeObject



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

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)


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

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

#deactivate!Object



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

def deactivate!
  @raw_mode = true
end

#descriptor_backing?Boolean

Returns:

  • (Boolean)


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

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

#external_encodingObject



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

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

#filenoObject



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

def fileno
  @backing.fileno
end

#flushObject



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

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

#initialize_copy(other) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 219

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_encodingObject



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

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


295
296
297
298
299
300
301
302
303
304
305
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 295

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



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

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

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



281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 281

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



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

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

#puts(*values) ⇒ Object



275
276
277
278
279
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 275

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

#raw_mode?Boolean

Returns:

  • (Boolean)


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

def raw_mode?
  @raw_mode
end

#reopen(target, *arguments) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 366

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)


414
415
416
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 414

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

#set_encoding(*arguments) ⇒ Object



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

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

#syncObject



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

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

#sync=(value) ⇒ Object



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

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

#syswrite(value) ⇒ Object



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

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

#to_ioObject



362
363
364
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 362

def to_io
  self
end

#tty?Boolean Also known as: isatty

Returns:

  • (Boolean)


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

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

#write(value) ⇒ Object



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

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

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



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

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

#write_nonblock(value, exception: true) ⇒ Object



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

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