Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/cover_rage/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

module CoverRage
Record = Data.define(:path, :revision, :source, :execution_count, :last_executed_at) do
def self.merge(existing, current)
def self.merge(existing_records, current_records)
records_to_save = []
current.each do |record|
found = existing.find { _1.path == record.path }
current_records.each do |current_record|
existing_record = existing_records.find { _1.path == current_record.path }
records_to_save <<
if found.nil? || record.revision != found.revision
record
if existing_record.nil? || current_record.revision != existing_record.revision
current_record
else
record + found
existing_record + current_record
end
end
records_to_save
Expand All @@ -19,13 +19,17 @@ def self.merge(existing, current)
def +(other)
with(
execution_count: execution_count.map.with_index do |item, index|
item.nil? ? nil : item + other.execution_count[index]
other_item = other.execution_count[index]
if item.nil? && other_item.nil? then nil
elsif item.nil? || other_item.nil? then other_item

@requiemformemories requiemformemories Jul 15, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Should we merge the data using the same logic that last_executed_at is using?

For example, if other_item is nil but item is 1 (somehow the unused code is now used), should it be item instead of other_item?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment, I've made the change 👍

else item + other_item
end
end,
last_executed_at: last_executed_at.map.with_index do |item, index|
if item.nil? && other.last_executed_at[index].nil? then nil
elsif item.nil? then other.last_executed_at[index]
elsif other.last_executed_at[index].nil? then item
else [item, other.last_executed_at[index]].max
other_item = other.last_executed_at[index]
if item.nil? && other_item.nil? then nil
elsif item.nil? || other_item.nil? then other_item
else [item, other_item].max
end
end
)
Expand Down
88 changes: 88 additions & 0 deletions test/test_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,92 @@ def test_sum
assert_equal [3, nil], result.execution_count
assert_equal [200, nil], result.last_executed_at
end

def test_sum_uses_other_when_exactly_one_execution_count_is_nil
r1 = CoverRage::Record.new(
path: '',
revision: '',
source: '',
execution_count: [1, nil],
last_executed_at: [100, nil]
)
r2 = CoverRage::Record.new(
path: '',
revision: '',
source: '',
execution_count: [nil, 2],
last_executed_at: [nil, 200]
)
result = r1 + r2
assert_equal [nil, 2], result.execution_count
assert_equal [nil, 200], result.last_executed_at
end

def test_merge_when_current_record_measures_more_lines
assert_equal(
[
CoverRage::Record.new(
path: 'foo.rb',
revision: '1',
source: '',
execution_count: [3, 1],
last_executed_at: [200, 200]
)
],
CoverRage::Record.merge(
[
CoverRage::Record.new(
path: 'foo.rb',
revision: '1',
source: '',
execution_count: [1, nil],
last_executed_at: [100, nil]
)
],
[
CoverRage::Record.new(
path: 'foo.rb',
revision: '1',
source: '',
execution_count: [2, 1],
last_executed_at: [200, 200]
)
]
)
)
end

def test_merge_when_current_record_measures_fewer_lines
assert_equal(
[
CoverRage::Record.new(
path: 'foo.rb',
revision: '1',
source: '',
execution_count: [3, nil],
last_executed_at: [200, nil]
)
],
CoverRage::Record.merge(
[
CoverRage::Record.new(
path: 'foo.rb',
revision: '1',
source: '',
execution_count: [1, 1],
last_executed_at: [100, 100]
)
],
[
CoverRage::Record.new(
path: 'foo.rb',
revision: '1',
source: '',
execution_count: [2, nil],
last_executed_at: [200, nil]
)
]
)
)
end
end
Loading