Skip to content

Commit fc484f1

Browse files
committed
Reuse SimpleCov.grouped from Result#groups
`Result` carried a private `apply_groups` that was line-for-line identical to `SimpleCov.grouped` except for the source of the file set and group config. Add an optional `groups:` keyword to `SimpleCov.grouped` (defaulted to `SimpleCov.groups`) so `Result#groups` can delegate, and drop the duplicate.
1 parent d022ee0 commit fc484f1

2 files changed

Lines changed: 8 additions & 21 deletions

File tree

lib/simplecov/result.rb

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ def coverage_for(path)
7474
source_file_for(path)&.coverage_statistics
7575
end
7676

77-
# Returns a Hash of groups for this result. Define groups using SimpleCov.add_group 'Models', 'app/models'
77+
# Returns a Hash of groups for this result. Define groups using SimpleCov.group 'Models', 'app/models'
7878
def groups
79-
@groups ||= apply_groups
79+
@groups ||= SimpleCov.grouped(files, groups: @groups_config)
8080
end
8181

8282
# Applies the configured SimpleCov.formatter on this result. Returns
@@ -154,22 +154,5 @@ def apply_cover_filters!(cover_filters)
154154
@files.select { |source_file| cover_filters.any? { |filter| filter.matches?(source_file) } }
155155
)
156156
end
157-
158-
# Build the per-group FileLists from `@groups_config`, plus the
159-
# implicit "Ungrouped" bucket for files that no group filter
160-
# matched.
161-
def apply_groups
162-
return {} if @groups_config.empty?
163-
164-
grouped = @groups_config.transform_values do |filter|
165-
SimpleCov::FileList.new(files.select { |source_file| filter.matches?(source_file) })
166-
end
167-
168-
in_group = grouped.values.flat_map(&:to_a)
169-
ungrouped = files.reject { |source_file| in_group.include?(source_file) }
170-
grouped["Ungrouped"] = SimpleCov::FileList.new(ungrouped) if ungrouped.any?
171-
172-
grouped
173-
end
174157
end
175158
end

lib/simplecov/result_processing.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ def filtered(files)
5959
SimpleCov::FileList.new result
6060
end
6161

62-
# Applies the configured groups to the given array of SimpleCov::SourceFile items
63-
def grouped(files)
62+
# Bin the given source files by group filter. `groups:` defaults to
63+
# `SimpleCov.groups`; pass a Hash explicitly to bin against a
64+
# different group config (e.g., the snapshot a Result captured at
65+
# construction). Files matched by no group fall into the implicit
66+
# "Ungrouped" bucket.
67+
def grouped(files, groups: self.groups)
6468
return {} if groups.empty?
6569

6670
grouped = groups.transform_values do |filter|

0 commit comments

Comments
 (0)