Class: RSpec::CircleCI::Coverage

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/circleci/coverage.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCoverage

Returns a new instance of Coverage.



20
21
22
23
24
# File 'lib/rspec/circleci/coverage.rb', line 20

def initialize
  @coverage_data = Hash.new { |h, k| h[k] = {} }
  @before_coverage = {}
  @installed = false
end

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



9
10
11
# File 'lib/rspec/circleci/coverage.rb', line 9

def instance
  @instance
end

Class Method Details

.installObject



12
13
14
15
16
17
18
# File 'lib/rspec/circleci/coverage.rb', line 12

def self.install
  # Only install if CIRCLECI_COVERAGE environment variable is set
  return unless ENV['CIRCLECI_COVERAGE']

  self.instance ||= new
  instance.install
end

Instance Method Details

#capture_coverageObject



60
61
62
# File 'lib/rspec/circleci/coverage.rb', line 60

def capture_coverage
  @before_coverage = ::Coverage.peek_result.dup
end

#coverage_fileObject



103
104
105
# File 'lib/rspec/circleci/coverage.rb', line 103

def coverage_file
  ENV['CIRCLECI_COVERAGE']
end

#extract_lines(coverage_value) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/rspec/circleci/coverage.rb', line 133

def extract_lines(coverage_value)
  case coverage_value
  when Array then coverage_value
  when Hash then coverage_value[:lines]
  else nil
  end
end

#format_test_key(example) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/rspec/circleci/coverage.rb', line 107

def format_test_key(example)
  file_path = example.[:file_path]
  # Clean path to remove ./ and other redundant components
  file_path = Pathname.new(file_path).cleanpath.to_s if file_path
  description = example.full_description
  "#{file_path}!!#{description}|run"
end

#in_project_scope?(file) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rspec/circleci/coverage.rb', line 121

def in_project_scope?(file)
  # Check if file is within the project directory
  # Files outside the project (system files, gems, etc.) should be filtered out
  return false unless file.start_with?(Dir.pwd)

  # Exclude gem files (even if they're in .bundle or vendor directories)
  return false if file.include?('/gems/')
  return false if file.include?('/.bundle/')
  return false if file.include?('/vendor/')
  true
end

#installObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rspec/circleci/coverage.rb', line 26

def install
  return if @installed
  @installed = true

  # Start coverage tracking if not already running
  unless ::Coverage.running?
    ::Coverage.start(lines: true, eval: true)
  end

  coverage_instance = self

  # Hook into RSpec lifecycle
  RSpec.configure do |config|
    config.before(:suite) do
      $stdout.write("rspec-circleci-coverage: generating CircleCI coverage JSON...\n")
    end

    config.before(:each) do |_|
      # Take a snapshot before each test
      coverage_instance.capture_coverage
    end

    config.after(:each) do |example|
      # Take a snapshot after each test and record coverage
      coverage_instance.record_test_coverage(example)
    end

    config.after(:suite) do
      # Write coverage data to file
      coverage_instance.write_coverage_data
    end
  end
end

#record_test_coverage(example) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rspec/circleci/coverage.rb', line 64

def record_test_coverage(example)
  # Take a snapshot after each test
  after_coverage = ::Coverage.peek_result

  # Calculate which lines were executed during this test
  test_key = format_test_key(example)

  after_coverage.each do |file, coverage|
    # Skip files outside project scope, spec files, and rspec gem internals
    next unless in_project_scope?(file)
    next if file.end_with?('_spec.rb')

    lines = extract_lines(coverage)
    next if lines.nil?

    before_lines = extract_lines(@before_coverage[file])

    # Any line whose count increased means this test executed the file.
    executed = lines.each_with_index.any? do |count, index|
      next false if count.nil?
      # Treat a missing before-count as 0 so the first render of an
      # eval'd file (e.g. a compiled template) is attributed to this test.
      before_count = before_lines ? (before_lines[index] || 0) : 0
      count > before_count
    end

    @coverage_data[relative_path(file)][test_key] = true if executed
  end
end

#relative_path(file) ⇒ Object



115
116
117
118
119
# File 'lib/rspec/circleci/coverage.rb', line 115

def relative_path(file)
  # Convert absolute path to relative path from current directory
  pathname = Pathname.new(file)
  pathname.absolute? ? pathname.relative_path_from(Dir.pwd).to_s : file
end

#write_coverage_dataObject



94
95
96
97
98
99
100
101
# File 'lib/rspec/circleci/coverage.rb', line 94

def write_coverage_data
  if @coverage_data.empty?
    $stdout.write("rspec-circleci-coverage: warning: no coverage data collected\n")
  end

  File.write(coverage_file, JSON.pretty_generate(@coverage_data))
  $stdout.write("rspec-circleci-coverage: wrote #{coverage_file}\n")
end