File: in_sub_process.rb

package info (click to toggle)
ruby-rspec-expectations 2.14.2-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 920 kB
  • sloc: ruby: 8,202; makefile: 4
file content (38 lines) | stat: -rw-r--r-- 787 bytes parent folder | download | duplicates (2)
1
2
3
4
5
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
32
33
34
35
36
37
38
module InSubProcess
  if RUBY_PLATFORM == 'java'
    def in_sub_process
      pending "This spec requires forking to work properly, " +
              "and JRuby does not support forking"
    end
  else
    # Useful as a way to isolate a global change to a subprocess.
    def in_sub_process
      readme, writeme = IO.pipe

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

        writeme.write Marshal.dump(value)

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

      writeme.close
      Process.waitpid(pid)

      if exception = Marshal.load(readme.read)
        raise exception
      end

      readme.close
    end
  end
end