Skip to content

Commit 9fafd3c

Browse files
committed
fix results collation
1 parent efb3e20 commit 9fafd3c

File tree

8 files changed

+30
-119
lines changed

8 files changed

+30
-119
lines changed

Gemfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
source "https://rubygems.org"
44

5-
# rubocop:disable Bundler/DuplicatedGem
65
case ENV["SIMPLECOV_HTML_MODE"]
76
when "local"
87
# Use local copy of simplecov-html in development when checked out
@@ -11,8 +10,6 @@ when "github"
1110
# Use development version of html formatter from github
1211
gem "simplecov-html", github: "simplecov-ruby/simplecov-html"
1312
end
14-
# rubocop:enable Bundler/DuplicatedGem
15-
1613
group :development do
1714
gem "apparition", "~> 0.6.0"
1815
gem "aruba", "~> 1.0"

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,4 @@ DEPENDENCIES
179179
webrick
180180

181181
BUNDLED WITH
182-
2.2.8
182+
2.2.16

features/method_coverage.feature

Lines changed: 0 additions & 37 deletions
This file was deleted.

lib/simplecov/combine/files_combiner.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module FilesCombiner
1616
#
1717
def combine(cov_a, cov_b)
1818
combination = {}
19+
1920
combination[:lines] = Combine.combine(LinesCombiner, cov_a[:lines], cov_b[:lines])
2021

2122
if SimpleCov.branch_coverage? # rubocop:disable Style/IfUnlessModifier

lib/simplecov/result.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,14 @@ def to_hash
7373
# Loads a SimpleCov::Result#to_hash dump
7474
def self.from_hash(hash)
7575
SimpleCov::ResultSerialization.deserialize(hash)
76-
# hash.map do |command_name, data|
77-
# new(data.fetch("coverage"), command_name: command_name, created_at: Time.at(data["timestamp"]))
78-
# end
7976
end
8077

8178
private
8279

83-
# TODO[@tycooon]: remove?
84-
# def coverage
85-
# keys = original_result.keys & filenames
86-
# Hash[keys.zip(original_result.values_at(*keys))]
87-
# end
80+
def coverage
81+
keys = original_result.keys & filenames
82+
Hash[keys.zip(original_result.values_at(*keys))]
83+
end
8884

8985
# Applies all configured SimpleCov filters on this result's source files
9086
def filter!

lib/simplecov/result_merger.rb

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,14 @@ def merge_results(*file_paths, ignore_timeout: false)
3131
# In big CI setups you might deal with 100s of CI jobs and each one producing Megabytes
3232
# of data. Reading them all in easily produces Gigabytes of memory consumption which
3333
# we want to avoid.
34-
#
35-
# For similar reasons a SimpleCov::Result is only created in the end as that'd create
36-
# even more data especially when it also reads in all source files.
37-
initial_memo = valid_results(file_paths.shift, ignore_timeout: ignore_timeout)
38-
39-
command_names, coverage = file_paths.reduce(initial_memo) do |memo, file_path|
40-
merge_coverage(memo, valid_results(file_path, ignore_timeout: ignore_timeout))
41-
end
4234

43-
create_result(command_names, coverage)
35+
results = file_paths.map { |path| valid_results(path, ignore_timeout: ignore_timeout) }
36+
merge_coverage(results)
4437
end
4538

4639
def valid_results(file_path, ignore_timeout: false)
47-
results = parse_file(file_path)
40+
raw_results = parse_file(file_path)
41+
results = Result.from_hash(raw_results)
4842
merge_valid_results(results, ignore_timeout: ignore_timeout)
4943
end
5044

@@ -72,42 +66,25 @@ def parse_json(content)
7266
end
7367

7468
def merge_valid_results(results, ignore_timeout: false)
75-
results = results.select { |_command_name, data| within_merge_timeout?(data) } unless ignore_timeout
76-
77-
command_plus_coverage = results.map do |command_name, data|
78-
[[command_name], adapt_result(data.fetch("coverage"))]
79-
end
80-
81-
# one file itself _might_ include multiple test runs
82-
merge_coverage(*command_plus_coverage)
69+
results = results.select { |x| within_merge_timeout?(x) } unless ignore_timeout
70+
merge_coverage(results)
8371
end
8472

85-
def within_merge_timeout?(data)
86-
time_since_result_creation(data) < SimpleCov.merge_timeout
73+
def within_merge_timeout?(result)
74+
Time.now - result.created_at < SimpleCov.merge_timeout
8775
end
8876

89-
def time_since_result_creation(data)
90-
Time.now - Time.at(data.fetch("timestamp"))
91-
end
92-
93-
def create_result(command_names, coverage)
94-
return nil unless coverage
95-
96-
command_name = command_names.reject(&:empty?).sort.join(", ")
97-
SimpleCov::Result.new(coverage, command_name: command_name)
98-
end
77+
def merge_coverage(results)
78+
results = results.compact
9979

100-
def merge_coverage(*results)
101-
return [[""], nil] if results.empty?
80+
return nil if results.size.zero?
10281
return results.first if results.size == 1
10382

104-
results.reduce do |(memo_command, memo_coverage), (command, coverage)|
105-
# timestamp is dropped here, which is intentional (we merge it, it gets a new time stamp as of now)
106-
merged_coverage = Combine.combine(Combine::ResultsCombiner, memo_coverage, coverage)
107-
merged_command = memo_command + command
108-
109-
[merged_command, merged_coverage]
110-
end
83+
parsed_results = results.map(&:original_result)
84+
combined_result = SimpleCov::Combine::ResultsCombiner.combine(*parsed_results)
85+
result = SimpleCov::Result.new(combined_result)
86+
result.command_name = results.map(&:command_name).reject(&:empty?).sort.join(", ")
87+
result
11188
end
11289

11390
#
@@ -118,9 +95,8 @@ def merged_result
11895
# conceptually this is just doing `merge_results(resultset_path)`
11996
# it's more involved to make syre `synchronize_resultset` is only used around reading
12097
resultset_hash = read_resultset
121-
command_names, coverage = merge_valid_results(resultset_hash)
122-
123-
create_result(command_names, coverage)
98+
results = Result.from_hash(resultset_hash)
99+
merge_valid_results(results)
124100
end
125101

126102
def read_resultset
@@ -164,31 +140,6 @@ def synchronize_resultset
164140
@resultset_locked = false
165141
end
166142
end
167-
168-
# We changed the format of the raw result data in simplecov, as people are likely
169-
# to have "old" resultsets lying around (but not too old so that they're still
170-
# considered we can adapt them).
171-
# See https://github.com/simplecov-ruby/simplecov/pull/824#issuecomment-576049747
172-
def adapt_result(result)
173-
if pre_simplecov_0_18_result?(result)
174-
adapt_pre_simplecov_0_18_result(result)
175-
else
176-
result
177-
end
178-
end
179-
180-
# pre 0.18 coverage data pointed from file directly to an array of line coverage
181-
def pre_simplecov_0_18_result?(result)
182-
_key, data = result.first
183-
184-
data.is_a?(Array)
185-
end
186-
187-
def adapt_pre_simplecov_0_18_result(result)
188-
result.transform_values do |line_coverage_data|
189-
{"lines" => line_coverage_data}
190-
end
191-
end
192143
end
193144
end
194145
end

spec/result_merger_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
it "has the result stored" do
124124
SimpleCov::ResultMerger.merge_and_store(resultset1_path, resultset2_path)
125125

126-
expect_resultset_1_and_2_merged(SimpleCov::ResultMerger.read_resultset)
126+
expect_resultset_1_and_2_merged(SimpleCov::ResultMerger.merged_result.to_hash)
127127
end
128128
end
129129

spec/simplecov_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,18 @@
207207
"result1, result2" => {
208208
"coverage" => {
209209
source_fixture("sample.rb") => {
210-
"lines" => [1, 1, 2, 2, nil, nil, 2, 2, nil, nil]
210+
lines: [1, 1, 2, 2, nil, nil, 2, 2, nil, nil]
211211
}
212212
}
213213
}
214214
}
215215
end
216216

217217
let(:collated) do
218-
JSON.parse(File.read(resultset_path)).transform_values { |v| v.reject { |k| k == "timestamp" } }
218+
JSON.parse(File.read(resultset_path)).transform_values do |data|
219+
data["coverage"].values.first.transform_keys!(&:to_sym)
220+
data.reject { |k| k == "timestamp" }
221+
end
219222
end
220223

221224
context "when no files to be merged" do

0 commit comments

Comments
 (0)