Skip to content

Commit d022ee0

Browse files
committed
Tighten remaining Ruby helpers
Use Hash#slice, ENV.values_at, Array#size, and compact glob assembly where they express the same work directly. Build grouped-file membership as a Set so ungrouped detection stays linear. Rename the root regexp helper while keeping the legacy root_regx shim, refresh it when SimpleCov.root changes, and update the focused specs around the new helper shapes.
1 parent 5449ffa commit d022ee0

9 files changed

Lines changed: 41 additions & 36 deletions

File tree

lib/simplecov/command_guesser.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ def guess
2222
# When parallel_tests (or a compatible runner) is driving the suite,
2323
# tag the command name with this worker's position in the pool.
2424
def parallel_data
25-
groups = ENV.fetch("PARALLEL_TEST_GROUPS", nil)
26-
number = ENV.fetch("TEST_ENV_NUMBER", nil)
25+
groups, number = ENV.values_at("PARALLEL_TEST_GROUPS", "TEST_ENV_NUMBER")
2726
return unless groups && number
2827

2928
# parallel_tests sets the first worker's TEST_ENV_NUMBER to "" rather

lib/simplecov/file_list.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ def missed_lines
4545
def never_lines
4646
return 0.0 if empty?
4747

48-
sum { |f| f.never_lines.count }
48+
sum { |f| f.never_lines.size }
4949
end
5050

5151
# Returns the count of skipped lines
5252
def skipped_lines
5353
return 0.0 if empty?
5454

55-
sum { |f| f.skipped_lines.count }
55+
sum { |f| f.skipped_lines.size }
5656
end
5757

5858
# Computes the coverage based upon lines covered and lines missed for each file

lib/simplecov/formatter/json_formatter/source_file_formatter.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def ensure_utf8(str)
3737
end
3838

3939
def line_coverage_section
40-
covered = @source_file.covered_lines.count
41-
missed = @source_file.missed_lines.count
40+
covered = @source_file.covered_lines.size
41+
missed = @source_file.missed_lines.size
4242
{
4343
lines: @source_file.lines.map { |line| format_line(line) },
4444
lines_covered_percent: @source_file.covered_percent,
@@ -52,19 +52,19 @@ def branch_coverage_section
5252
{
5353
branches: @source_file.branches.map { |branch| format_branch(branch) },
5454
branches_covered_percent: @source_file.branches_coverage_percent,
55-
covered_branches: @source_file.covered_branches.count,
56-
missed_branches: @source_file.missed_branches.count,
57-
total_branches: @source_file.total_branches.count
55+
covered_branches: @source_file.covered_branches.size,
56+
missed_branches: @source_file.missed_branches.size,
57+
total_branches: @source_file.total_branches.size
5858
}
5959
end
6060

6161
def method_coverage_section
6262
{
6363
methods: @source_file.methods.map { |method| format_method(method) },
6464
methods_covered_percent: @source_file.methods_coverage_percent,
65-
covered_methods: @source_file.covered_methods.count,
66-
missed_methods: @source_file.missed_methods.count,
67-
total_methods: @source_file.methods.count
65+
covered_methods: @source_file.covered_methods.size,
66+
missed_methods: @source_file.missed_methods.size,
67+
total_methods: @source_file.methods.size
6868
}
6969
end
7070

lib/simplecov/profiles/root_filter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
# place; this profile is the user-facing entry point that tools like
77
# `SimpleCov.filtered` apply.
88
skip do |src|
9-
src.filename !~ SimpleCov::UselessResultsRemover.root_regx
9+
src.filename !~ SimpleCov::UselessResultsRemover.root_regex
1010
end
1111
end

lib/simplecov/result.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ def warn_about_missing_source_files(missing, input_size)
132132
end
133133

134134
def coverage
135-
keys = original_result.keys & filenames
136-
keys.zip(original_result.values_at(*keys)).to_h
135+
original_result.slice(*filenames)
137136
end
138137

139138
# Applies the given filter chain to `@files`, dropping each source

lib/simplecov/result_processing.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def grouped(files)
6767
SimpleCov::FileList.new(files.select { |source_file| filter.matches?(source_file) })
6868
end
6969

70-
in_group = grouped.values.flat_map(&:to_a)
70+
in_group = grouped_file_set(grouped)
7171
ungrouped = files.reject { |source_file| in_group.include?(source_file) }
7272
grouped["Ungrouped"] = SimpleCov::FileList.new(ungrouped) if ungrouped.any?
7373

@@ -104,6 +104,10 @@ def initial_setup(profile, &block)
104104
configure(&block) if block
105105
end
106106

107+
def grouped_file_set(grouped)
108+
grouped.values.each_with_object(Set.new) { |file_list, set| set.merge(file_list) }
109+
end
110+
107111
# Finds files that were to be tracked but were not loaded, and
108112
# initializes their line-by-line coverage to zero (or nil for
109113
# comments / whitespace).
@@ -119,10 +123,7 @@ def add_not_loaded_files(result)
119123
# with every string glob declared via `cover` (also restrictive,
120124
# but the restriction lives in `Result#apply_cover_filters!`).
121125
def unloaded_file_discovery_globs
122-
globs = []
123-
globs << tracked_files if tracked_files
124-
globs.concat(cover_globs)
125-
globs
126+
[tracked_files, *cover_globs].compact
126127
end
127128

128129
# Expand the given globs relative to SimpleCov.root, not Dir.pwd —

lib/simplecov/useless_results_remover.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ module SimpleCov
77
# run never make it into the formatted result.
88
module UselessResultsRemover
99
def self.call(coverage_result)
10-
coverage_result.select { |path, _coverage| path.match?(root_regx) }
10+
coverage_result.select { |path, _coverage| path.match?(root_regex) }
1111
end
1212

1313
# The `/i` flag covers case-insensitive matches on Windows / macOS-HFS+
1414
# where the on-disk path's case can differ from `SimpleCov.root`'s.
15+
def self.root_regex
16+
root = SimpleCov.root
17+
return @root_regex if root == @root_regex_root
18+
19+
@root_regex_root = root
20+
@root_regex = /\A#{Regexp.escape(root.chomp(File::SEPARATOR) + File::SEPARATOR)}/i
21+
end
22+
1523
def self.root_regx
16-
@root_regx ||= /\A#{Regexp.escape(SimpleCov.root.chomp(File::SEPARATOR) + File::SEPARATOR)}/i
24+
root_regex
1725
end
1826
end
1927
end

spec/command_guesser_spec.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,19 @@
6262

6363
it "appends parallel data" do
6464
guesser.original_run_command = "/some/path/spec/foo.rb"
65-
allow(ENV).to receive(:[]).and_call_original
66-
allow(ENV).to receive(:fetch).and_call_original
67-
allow(ENV).to receive(:[]).with("TEST_ENV_NUMBER").and_return("1")
68-
allow(ENV).to receive(:[]).with("PARALLEL_TEST_GROUPS").and_return("2")
69-
allow(ENV).to receive(:fetch).with("TEST_ENV_NUMBER", nil).and_return("1")
70-
allow(ENV).to receive(:fetch).with("PARALLEL_TEST_GROUPS", nil).and_return("2")
65+
allow(ENV).to receive(:values_at).and_call_original
66+
allow(ENV).to receive(:values_at)
67+
.with("PARALLEL_TEST_GROUPS", "TEST_ENV_NUMBER")
68+
.and_return(%w[2 1])
7169
expect(guesser.guess).to eq("RSpec (1/2)")
7270
end
7371

7472
it 'treats an empty TEST_ENV_NUMBER as worker "1"' do
7573
guesser.original_run_command = "/some/path/spec/foo.rb"
76-
allow(ENV).to receive(:[]).and_call_original
77-
allow(ENV).to receive(:fetch).and_call_original
78-
allow(ENV).to receive(:[]).with("TEST_ENV_NUMBER").and_return("")
79-
allow(ENV).to receive(:[]).with("PARALLEL_TEST_GROUPS").and_return("2")
80-
allow(ENV).to receive(:fetch).with("TEST_ENV_NUMBER", nil).and_return("")
81-
allow(ENV).to receive(:fetch).with("PARALLEL_TEST_GROUPS", nil).and_return("2")
74+
allow(ENV).to receive(:values_at).and_call_original
75+
allow(ENV).to receive(:values_at)
76+
.with("PARALLEL_TEST_GROUPS", "TEST_ENV_NUMBER")
77+
.and_return(["2", ""])
8278
expect(guesser.guess).to eq("RSpec (1/2)")
8379
end
8480
end

spec/useless_results_remover_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,19 @@
3333
expect(remover[source_path]["lines"]).to be_a(Array)
3434
end
3535

36+
it "keeps the legacy root_regx helper as an alias" do
37+
expect(described_class.root_regx).to eq(described_class.root_regex)
38+
end
39+
3640
context "when SimpleCov.root is the filesystem root" do
3741
around do |example|
3842
skip "filesystem root semantics are Unix-only" if Gem.win_platform?
3943

4044
previous_root = SimpleCov.root
41-
described_class.instance_variable_set(:@root_regx, nil)
4245
SimpleCov.root("/")
4346
example.run
4447
ensure
4548
SimpleCov.root(previous_root)
46-
described_class.instance_variable_set(:@root_regx, nil)
4749
end
4850

4951
let(:result_set) do

0 commit comments

Comments
 (0)