File: in_sub_process.rb

package info (click to toggle)
ruby-rspec 3.4.0c3e0m1s1-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 6,124 kB
  • sloc: ruby: 59,418; sh: 1,405; makefile: 98
file content (52 lines) | stat: -rw-r--r-- 1,447 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module RSpec
  module Support
    module InSubProcess
      if Process.respond_to?(:fork) && !(Ruby.jruby? && RUBY_VERSION == '1.8.7')

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

        # rubocop:disable MethodLength
        def in_sub_process(prevent_warnings=true)
          readme, writeme = IO.pipe

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

            begin
              yield
              warning_preventer.verify_no_warnings! if prevent_warnings
            rescue Support::AllExceptionsExceptOnesWeMustNotRescue => 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
        # rubocop:enable MethodLength
        alias :in_sub_process_if_possible :in_sub_process
      else
        def in_sub_process(*)
          skip "This spec requires forking to work properly, " \
               "and your platform does not support forking"
        end

        def in_sub_process_if_possible(*)
          yield
        end
      end
    end
  end
end