Skip to content

Commit 01f3bad

Browse files
committed
Fix load failure under SimpleCov 1.x json_formatter shim
simplecov 1.x ships its own simplecov_json_formatter.rb that can win the require over the standalone gem depending on $LOAD_PATH order, leaving SimpleCovJSONFormatter::ResultHashFormatter undefined (green locally, broke CI). Require the standalone gem's classes by their unambiguous subpaths, and make UndercoverSimplecovFormatter own its #format instead of inheriting the version-divergent JSONFormatter#format.
1 parent d8cb41e commit 01f3bad

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

lib/undercover/simplecov_formatter.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# frozen_string_literal: true
22

33
require 'simplecov'
4-
require 'simplecov_json_formatter'
4+
require 'json'
5+
6+
# simplecov >= 1.0 ships its own simplecov_json_formatter.rb shim that can win
7+
# the require over the standalone gem depending on $LOAD_PATH order. Require the
8+
# standalone gem's classes by their subpaths (which simplecov does not ship) so
9+
# the SimpleCovJSONFormatter::* constants are always present.
10+
require 'simplecov_json_formatter/result_hash_formatter'
11+
require 'simplecov_json_formatter/result_exporter'
512

613
# Patch ResultExporter to allow setting a custom export_path
714
module SimpleCovJSONFormatter
@@ -125,11 +132,20 @@ def format_files
125132
end
126133
end
127134

128-
class UndercoverSimplecovFormatter < SimpleCov::Formatter::JSONFormatter
135+
class UndercoverSimplecovFormatter
129136
class << self
130137
attr_accessor :output_filename
131138
end
132139

140+
# Own #format instead of inheriting SimpleCov::Formatter::JSONFormatter's:
141+
# simplecov >= 1.0 reimplemented it to bypass #format_result and always
142+
# write coverage.json, dropping undercover's meta and custom filename.
143+
def format(result)
144+
result_hash = format_result(result)
145+
SimpleCovJSONFormatter::ResultExporter.new(result_hash).export
146+
result_hash
147+
end
148+
133149
def format_result(result)
134150
result_hash_formater = ResultHashFormatterWithRoot.new(result)
135151
result_hash_formater.format

spec/undercover_simplecov_formatter_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@
7070
output = formatter.format_result(result)
7171
expect(output).to eq({meta: {}, coverage: {}})
7272
end
73+
74+
it 'writes the formatted result through ResultExporter and returns the hash' do
75+
result = double('result')
76+
formatter = Undercover::UndercoverSimplecovFormatter.new
77+
result_hash = {meta: {timestamp: 1}, coverage: {}}
78+
allow(formatter).to receive(:format_result).with(result).and_return(result_hash)
79+
80+
exporter = instance_double(SimpleCovJSONFormatter::ResultExporter)
81+
expect(SimpleCovJSONFormatter::ResultExporter).to receive(:new).with(result_hash).and_return(exporter)
82+
expect(exporter).to receive(:export)
83+
84+
expect(formatter.format(result)).to eq(result_hash)
85+
end
7386
end
7487

7588
describe 'Undercover::SimplecovResultAdapter.parse' do

0 commit comments

Comments
 (0)