-
Notifications
You must be signed in to change notification settings - Fork 553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite merge helpers to work with frozen objects #558
Changes from 1 commit
9bb1aa3
a7747c1
d08e569
c29bbcd
7142edf
6dafc25
794cecb
fbebb99
e864390
c7a9d82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module SimpleCov | ||
module MergeHelper | ||
class << self | ||
# Merges two Coverage.result hashes | ||
def merge_resultsets(a, b) | ||
(a.keys | b.keys).each_with_object({}) do |filename, merged| | ||
result1 = a[filename] | ||
result2 = b[filename] | ||
merged[filename] = if result1 && result2 | ||
merge_arrays(result1, result2) | ||
else | ||
(result1 || result2).dup | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def merge_arrays(a, b) | ||
a.zip(b).map do |count1, count2| | ||
sum = count1.to_i + count2.to_i | ||
if sum.zero? && (count1.nil? || count2.nil?) | ||
nil | ||
else | ||
sum | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ class Result | |
# Initialize a new SimpleCov::Result from given Coverage.result (a Hash of filenames each containing an array of | ||
# coverage data) | ||
def initialize(original_result) | ||
original_result = original_result.dup.extend(SimpleCov::HashMergeHelper) unless original_result.is_a? SimpleCov::HashMergeHelper | ||
@original_result = original_result.freeze | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
@files = SimpleCov::FileList.new(original_result.map do |filename, coverage| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good. In my own testing I found that the def src
# We intentionally read source code lazily to
# suppress reading unused source code.
@src ||= File.open(filename, "rb", &:readlines)
end require 'simplecov'
require 'json'
SimpleCov.coverage_dir 'coveragetest'
SimpleCov.formatters = [SimpleCov::Formatter::SimpleFormatter]
sourcefiles = Dir.glob('./resultset*')
# work around `if File.file?(filename)` when running on other filesystem
add_files = ->(sr) { files = sr.original_result.map {|filename, coverage| SimpleCov::SourceFile.new(filename, coverage) }.compact.sort_by(&:filename); sr.instance_variable_set(:@files, SimpleCov::FileList.new(files)); sr }
results = sourcefiles.map { |file| SimpleCov::Result.from_hash(JSON.parse(File.read(file))) }.each do |result|
add_files.(result)
end; nil
result = SimpleCov::ResultMerger.merge_results(*results)
add_files.(result)
result.format! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so erm, I don't fully understand. Is this good to merge now or is this a bigger problem? (first you said it's probably good for another PR but then you said it doesn't work with the html formatter, so I'm a bit confused) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PragTob good to merge. The 'good for another pr', would be a way to merge files not represented on the current file system, since that's out of scope. This works fine when running on the same filesystem (or if you were to munge the paths). |
||
SimpleCov::SourceFile.new(filename, coverage) if File.file?(filename) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,7 @@ def results | |
def merged_result | ||
merged = {} | ||
results.each do |result| | ||
merged = result.original_result.merge_resultset(merged) | ||
merged = SimpleCov::MergeHelper.merge_resultsets(result.original_result, merged) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't it be nice if this were module SimpleCov::RawCoverage
module_function
def merge_results(*results)
Array(results).reduce({}) do |result, merged|
merge_resultset(result, merged)
end
end
def merge_resultset(resultset1, resultset2)
(resultset1.keys | resultset2.keys).each_with_object({}) do |filename, merged_resultset|
coverage1 = resultset1[filename]
coverage2 = resultset2[filename]
merged_resultset[filename] = merge_file_coverage(coverage1, coverage2)
end
end
def merge_file_coverage(coverage1, coverage2)
if coverage1 && coverage2
coverage1.zip(coverage2).map { |count1, count2|
sum = count1.to_i + count2.to_i
if sum.zero? && (count1.nil? || count2.nil?)
nil
else
sum
end
}
else
(coverage1 || coverage2).dup
end
end
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had actually been thinking about adding a method that takes an array of Earlier you mentioned putting this code in ResultMerger, but now it sounds like you'd like it in a new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have a strong feeling about it except that this something I've been meaning to do or hoping someone would do for months so 👍 to you! Only real concern is that we want to make a small public interface so that we can change internal details. So, maybe a method on ResultMerger that calls the helper or whatever, and we can change the whatever however whenever :) |
||
end | ||
result = SimpleCov::Result.new(merged) | ||
# Specify the command name | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a long needed feature. Any reason it shouldn't just be a function on the ResultMerger?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Putting in on ResultMerger seems sensible. I considered it, then thought maybe it'd be nicer to keep this code self-contained. I.e., ResultMerger manages
.resultset.json
, and this module manages merging results in-memory. But it was a close call, and if you prefer moving it to ResultMerger I'll do that.