# frozen_string_literal: true

# RuboCop can be run in contexts where unexpected other libraries are included,
# which may interfere with its normal behavior. In order to test those
# situations, it may be necessary to require another library for the duration
# of one spec
module HostEnvironmentSimulatorHelper
  def in_its_own_process_with(*files)
    if ::Process.respond_to?(:fork)
      pid = ::Process.fork do
        # Need to write coverage result under different name
        if defined?(SimpleCov)
          SimpleCov.coverage_dir "coverage/ignored_results_#{Process.pid}"
          SimpleCov.pid = Process.pid
        end

        files.each { |file| require file }
        yield
      end
      ::Process.wait(pid)

      # assert that the block did not fail
      expect($CHILD_STATUS).to be_success
    else
      warn 'Process.fork is not available.'
    end
  end
end

# frozen_string_literal: true

# RuboCop can be run in contexts where unexpected other libraries are included,
# which may interfere with its normal behavior. In order to test those
# situations, it may be necessary to require another library for the duration
# of one spec
module HostEnvironmentSimulatorHelper
  def in_its_own_process_with(*files)
    if ::Process.respond_to?(:fork)
      pid = ::Process.fork do
        # Need to write coverage result under different name
        if defined?(SimpleCov)
          SimpleCov.command_name "rspec-fork-#{Process.pid}"
          SimpleCov.pid = Process.pid
        end

        files.each { |file| require file }
        yield
      end
      ::Process.wait(pid)

      # assert that the block did not fail
      expect($CHILD_STATUS).to be_success
    else
      warn 'Process.fork is not available.'
    end
  end
end

Add coverage for subprocess specs
Add coverage for subprocess specs

Since PR https://github.com/rubocop-hq/rubocop/pull/6255 the coverage results
from the different suites (one per tested Ruby version) are merged together, and
the result is sent to Code Climate.

That means we no longer need to ignore coverage results from the specs that are
run in a separate process. We just need to give these coverage results their
own name (e.g. a namespace) and set a new PID (I think to stop SimpleCov's
`at_exit` hook when the fork ends) and these subprocess results will be merged
with the coverage results of the rest of the suite.
