Module: RSpec::FlakeClassifier::Integrations::RSpecCovers

Defined in:
lib/rspec/flake/classifier/integrations.rb

Class Method Summary collapse

Class Method Details

.coverage_for(example) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rspec/flake/classifier/integrations.rb', line 54

def coverage_for(example)
  Integrations.try_require("rspec/covers")
  result = reporter_result(example)
  return line_hash(result.executed_locations) if result&.respond_to?(:executed_locations)

  receivers = [
    Integrations.constant("RSpec::Covers"),
    Integrations.constant("RSpec::Covers::Probe"),
    Integrations.constant("RSpec::Covers::Store"),
    Integrations.constant("RSpec::Covers::Coverage"),
    Integrations.constant("Rspec::Covers")
  ]
  Integrations.call_first(
    receivers,
    %i[coverage_for covered_lines_for example_coverage result_for for_example],
    example
  )
end

.line_hash(locations) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/rspec/flake/classifier/integrations.rb', line 84

def line_hash(locations)
  Array(locations).each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |location, result|
    file = location.respond_to?(:file) ? location.file : location[:file]
    line = location.respond_to?(:line) ? location.line : location[:line]
    result[relative_path(file)] << line.to_i if file && line
  end.transform_values { |lines| lines.uniq.sort }
end

.relative_path(file) ⇒ Object



92
93
94
95
96
97
# File 'lib/rspec/flake/classifier/integrations.rb', line 92

def relative_path(file)
  path = file.to_s
  return path unless path.start_with?(Dir.pwd)

  path.delete_prefix("#{Dir.pwd}/")
end

.reporter_result(example) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/rspec/flake/classifier/integrations.rb', line 73

def reporter_result(example)
  covers = Integrations.constant("RSpec::Covers")
  return nil unless covers&.respond_to?(:reporter)
  return nil unless covers.reporter.respond_to?(:results)

  covers.reporter.results.reverse.find do |result|
    result.respond_to?(:id) && result.id == example.id ||
      result.respond_to?(:example) && result.example.equal?(example)
  end
end