Class: RSpecTracer::CoverageReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_tracer/coverage_reporter.rb

Constant Summary collapse

COVERAGE_MODE =
{
  array: 'array',
  hash: 'hash'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCoverageReporter

Returns a new instance of CoverageReporter.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rspec_tracer/coverage_reporter.rb', line 12

def initialize
  @mode = if !RSpecTracer.simplecov? || ::Coverage.peek_result.first.last.is_a?(Array)
            COVERAGE_MODE[:array]
          else
            COVERAGE_MODE[:hash]
          end

  @examples_coverage = Hash.new do |examples, example_id|
    examples[example_id] = Hash.new do |files, file_path|
      files[file_path] = {}
    end
  end
end

Instance Attribute Details

#coverageObject (readonly)

Returns the value of attribute coverage.



10
11
12
# File 'lib/rspec_tracer/coverage_reporter.rb', line 10

def coverage
  @coverage
end

#coverage_statObject (readonly)

Returns the value of attribute coverage_stat.



10
11
12
# File 'lib/rspec_tracer/coverage_reporter.rb', line 10

def coverage_stat
  @coverage_stat
end

#examples_coverageObject (readonly)

Returns the value of attribute examples_coverage.



10
11
12
# File 'lib/rspec_tracer/coverage_reporter.rb', line 10

def examples_coverage
  @examples_coverage
end

#modeObject (readonly)

Returns the value of attribute mode.



10
11
12
# File 'lib/rspec_tracer/coverage_reporter.rb', line 10

def mode
  @mode
end

Instance Method Details

#compute_diff(example_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rspec_tracer/coverage_reporter.rb', line 30

def compute_diff(example_id)
  peek_coverage.each_pair do |file_path, current_stats|
    unless @coverage.key?(file_path)
      missing_file_diff_coverage(example_id, file_path, current_stats)

      next
    end

    next if current_stats == @coverage[file_path]

    existing_file_diff_coverage(example_id, file_path, current_stats)
  end
end

#generate_final_coverageObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/rspec_tracer/coverage_reporter.rb', line 82

def generate_final_coverage
  return if @coverage_stat

  all_files = final_coverage_files
  @coverage = @coverage.slice(*all_files)

  all_files.each do |file_path|
    @coverage[file_path] ||= line_stub(file_path).freeze
  end
end

#generate_final_examples_coverageObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rspec_tracer/coverage_reporter.rb', line 44

def generate_final_examples_coverage
  filtered_files = Set.new

  @examples_coverage.each_key do |example_id|
    @examples_coverage[example_id].select! do |file_path|
      next false if filtered_files.include?(file_path)

      file_name = RSpecTracer::SourceFile.file_name(file_path)

      if RSpecTracer.coverage_filters.any? { |filter| filter.match?(file_name: file_name) }
        filtered_files << file_path

        false
      else
        true
      end
    end
  end
end

#merge_coverage(missed_coverage) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rspec_tracer/coverage_reporter.rb', line 64

def merge_coverage(missed_coverage)
  record_coverage

  missed_coverage.each_pair do |file_path, line_coverage|
    line_coverage_dup = if @coverage.key?(file_path)
                          @coverage[file_path].dup
                        else
                          line_stub(file_path)
                        end

    line_coverage.each_pair do |line_number, strength|
      line_coverage_dup[line_number.to_i] += strength
    end

    @coverage[file_path] = line_coverage_dup.freeze
  end
end

#record_coverageObject



26
27
28
# File 'lib/rspec_tracer/coverage_reporter.rb', line 26

def record_coverage
  @coverage = peek_coverage
end