class SimpleCov::Parallel::Adapter::CircleCI

@api private

Constants

MethodStasher

Public Class Methods

available?() click to toggle source
# File lib/simplecov/parallel/adapter/circleci.rb, line 8
def self.available?
  ENV.key?('CIRCLECI')
end

Public Instance Methods

activate() click to toggle source
# File lib/simplecov/parallel/adapter/circleci.rb, line 12
def activate
  require 'circleci/parallel'
  require 'fileutils'
  configure_circleci_parallel
  configure_simplecov
end

Private Instance Methods

configure_circleci_parallel() click to toggle source
# File lib/simplecov/parallel/adapter/circleci.rb, line 21
def configure_circleci_parallel
  ::CircleCI::Parallel.configure do |config|
    config.on_each_slave_node.before_sync do
      export_slave_node_result
    end

    config.on_master_node.after_download do
      merge_slave_node_results
      merge_master_node_result_and_format
    end
  end
end
configure_simplecov() click to toggle source
# File lib/simplecov/parallel/adapter/circleci.rb, line 34
def configure_simplecov
  SimpleCov.command_name("#{SimpleCov.command_name} #{current_node.name}")

  SimpleCov.at_exit do
    puts 'Merging SimpleCov result into the master node...'
    ::CircleCI::Parallel.sync
  end
end
current_node() click to toggle source
# File lib/simplecov/parallel/adapter/circleci.rb, line 100
def current_node
  ::CircleCI::Parallel.current_node
end
export_slave_node_result() click to toggle source
# File lib/simplecov/parallel/adapter/circleci.rb, line 43
def export_slave_node_result
  # Export result to coverage/.resultset.json
  # https://github.com/colszowka/simplecov/blob/v0.12.0/lib/simplecov.rb#L89
  raise 'SimpleCov.use_merging must be set to true' unless SimpleCov.use_merging
  SimpleCov.result

  FileUtils.copy(
    SimpleCov::ResultMerger.resultset_path,
    ::CircleCI::Parallel.local_data_dir
  )
end
load_results_from_dir(dir) click to toggle source
# File lib/simplecov/parallel/adapter/circleci.rb, line 70
def load_results_from_dir(dir)
  with_changing_resultset_path(dir) do
    SimpleCov::ResultMerger.clear_resultset
    SimpleCov::ResultMerger.results
  end
end
merge_and_save_results(results) click to toggle source
# File lib/simplecov/parallel/adapter/circleci.rb, line 77
def merge_and_save_results(results)
  results.each { |result| SimpleCov::ResultMerger.store_result(result) }
end
merge_master_node_result_and_format() click to toggle source
# File lib/simplecov/parallel/adapter/circleci.rb, line 62
def merge_master_node_result_and_format
  # Invoking `SimpleCov.result` here merges the master node data into the slave node data.
  # `SimpleCov.result.format!` is the default behavior of at_exit:
  # https://github.com/colszowka/simplecov/blob/v0.12.0/lib/simplecov/configuration.rb#L172
  SimpleCov::ResultMerger.clear_resultset
  SimpleCov.result.format!
end
merge_slave_node_results() click to toggle source
# File lib/simplecov/parallel/adapter/circleci.rb, line 55
def merge_slave_node_results
  Dir.glob('node*') do |node_dir|
    results = load_results_from_dir(node_dir)
    merge_and_save_results(results)
  end
end
with_changing_resultset_path(dir) { || ... } click to toggle source
# File lib/simplecov/parallel/adapter/circleci.rb, line 81
def with_changing_resultset_path(dir)
  # Actually we don't want to do this hack but changing resultset_path by modifying
  # SimpleCov.root and SimpleCov.coverage_dir does not work well because .root is used in
  # the root filter, which is invoked from SimpleCov::Result#initialize.
  # https://github.com/colszowka/simplecov/blob/v0.12.0/lib/simplecov/defaults.rb#L9
  method_stasher = MethodStasher.new(SimpleCov::ResultMerger, :resultset_path)
  method_stasher.stash

  SimpleCov::ResultMerger.define_singleton_method(:resultset_path) do
    File.join(dir, '.resultset.json')
  end

  begin
    yield
  ensure
    method_stasher.pop
  end
end