Module: RSpec::Support::InSubProcess

Defined in:
lib/rspec/support/spec/in_sub_process.rb

Instance Method Summary collapse

Instance Method Details

#in_sub_processObject

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rspec/support/spec/in_sub_process.rb', line 6

def in_sub_process
  readme, writeme = IO.pipe

  pid = Process.fork do
    exception = nil
    begin
      yield
    rescue Exception => e
      exception = e
    end

    writeme.write Marshal.dump(exception)

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

  writeme.close
  Process.waitpid(pid)

  exception = Marshal.load(readme.read)
  readme.close

  raise exception if exception
end