123456789_123456789_123456789_123456789_123456789_

Module: RSpec::Support::InSubProcess

Relationships & Source Files
Namespace Children
Classes:
Defined in: rspec-support/lib/rspec/support/spec/in_sub_process.rb

Instance Method Summary

Instance Method Details

#in_sub_process Also known as: #in_sub_process_if_possible

Useful as a way to isolate a global change to a subprocess.

See additional method definition at line 12.

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/spec/in_sub_process.rb', line 62

def in_sub_process(prevent_warnings=true) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  exception_reader, exception_writer = IO.pipe
  result_reader, result_writer = IO.pipe

  # Set binary mode to avoid errors surrounding ascii-8bit to utf-8 conversion
  # this happens with warnings on rspec-rails for example
  [exception_reader, exception_writer, result_reader, result_writer].each { |io| io.binmode }

  pid = Process.fork do
    warning_preventer = $stderr = RSpec::Support::StdErrSplitter.new($stderr)

    begin
      result = yield
      warning_preventer.verify_no_warnings! if prevent_warnings
      # rubocop:disable Lint/HandleExceptions
    rescue Support::AllExceptionsExceptOnesWeMustNotRescue => exception
      # rubocop:enable Lint/HandleExceptions
    end

    exception_writer.write marshal_dump_with_unmarshable_object_handling(exception)
    exception_reader.close
    exception_writer.close

    result_writer.write marshal_dump_with_unmarshable_object_handling(result)
    result_reader.close
    result_writer.close

    exit! # prevent at_exit hooks from running (e.g. minitest)
  end

  exception_writer.close
  result_writer.close
  Process.waitpid(pid)

  exception = Marshal.load(exception_reader.read)
  exception_reader.close
  raise exception if exception

  result = Marshal.load(result_reader.read)
  result_reader.close
  result
end

#in_sub_process_if_possible

Alias for #in_sub_process.

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/spec/in_sub_process.rb', line 67

alias :in_sub_process_if_possible :in_sub_process

#marshal_dump_with_unmarshable_object_handling(object)

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/spec/in_sub_process.rb', line 56

def marshal_dump_with_unmarshable_object_handling(object)
  Marshal.dump(object)
rescue TypeError => error
  Marshal.dump(UnmarshableObject.new(error))
end