123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Core::ExampleStatusMerger Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: rspec-core/lib/rspec/core/example_status_persister.rb

Overview

Merges together a list of example statuses from this run and a list from previous runs (presumably loaded from disk). Each example status object is expected to be a hash with at least an :example_id and a :status key. Examples that were loaded but not executed (due to filtering, –fail-fast or whatever) should have a :status of UNKNOWN_STATUS.

This will produce a new list that:

- Will be missing examples from previous runs that we know for sure
  no longer exist.
- Will have the latest known status for any examples that either
  definitively do exist or may still exist.
- Is sorted by file name and example definition order, so that
  the saved file is easily scannable if users want to inspect it.

Class Method Summary

Instance Method Summary

Class Method Details

.merge(this_run, from_previous_runs)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_status_persister.rb', line 75

def self.merge(this_run, from_previous_runs)
  new(this_run, from_previous_runs).merge
end

Instance Method Details

#delete_previous_examples_that_no_longer_exist (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_status_persister.rb', line 102

def delete_previous_examples_that_no_longer_exist
  @from_previous_runs.delete_if do |ex_id, _|
    example_must_no_longer_exist?(ex_id)
  end
end

#example_must_no_longer_exist?(ex_id) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_status_persister.rb', line 108

def example_must_no_longer_exist?(ex_id)
  # Obviously, it exists if it was loaded for this spec run...
  return false if @this_run.key?(ex_id)

  spec_file = spec_file_from(ex_id)

  # `this_run` includes examples that were loaded but not executed.
  # Given that, if the spec file for this example was loaded,
  # but the id does not still exist, it's safe to assume that
  # the example must no longer exist.
  return true if loaded_spec_files.include?(spec_file)

  # The example may still exist as long as the file exists...
  !@file_exists_cache[spec_file]
end

#hash_from(example_list) (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_status_persister.rb', line 95

def hash_from(example_list)
  example_list.inject({}) do |hash, example|
    hash[example.fetch(:example_id)] = example
    hash
  end
end

#loaded_spec_files (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_status_persister.rb', line 124

def loaded_spec_files
  @loaded_spec_files ||= Set.new(@this_run.keys.map(&method(:spec_file_from)))
end

#merge

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_status_persister.rb', line 85

def merge
  delete_previous_examples_that_no_longer_exist

  @this_run.merge(@from_previous_runs) do |_ex_id, new, old|
    new.fetch(:status) == Configuration::UNKNOWN_STATUS ? old : new
  end.values.sort_by(&method(:sort_value_from))
end

#sort_value_from(example) (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_status_persister.rb', line 132

def sort_value_from(example)
  file, scoped_id = Example.parse_id(example.fetch(:example_id))
  [file, *scoped_id.split(":").map(&method(:Integer))]
end

#spec_file_from(ex_id) (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/example_status_persister.rb', line 128

def spec_file_from(ex_id)
  ex_id.split("[").first
end