Skip to content

Commit 5449ffa

Browse files
committed
Tighten Ruby idioms across several files
Switch the project-root path check in UselessResultsRemover from `=~` to `match?` (real boolean, skips `$~` capture) and rebuild the prefix regex with `chomp + append` rather than a mutating `+= unless`. Rewrite `ResultAdapter#adapt` as a `to_h` block over the input hash with an `adapt_one` helper, separating the pre-0.18 array-shape path from the modern hash-shape path. Replace `!ENV.fetch("NAME", nil).nil?` with `ENV.key?("NAME")` across the parallel-adapter detection paths, fold the double `key?` check in `Profiles#fetch_proc` into one, read each env var once in `CommandGuesser#parallel_data`, and replace the manual `each` plus early `return` in `cli/coverage.rb`'s `lookup` with `Hash#find`. All behavior-preserving.
1 parent e1536b9 commit 5449ffa

7 files changed

Lines changed: 35 additions & 33 deletions

File tree

lib/simplecov/cli/coverage.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ def locate_match(opts, stderr)
5959
def lookup(coverage_hash, path)
6060
absolute = File.expand_path(path)
6161
suffix = "/#{path}"
62-
coverage_hash.each do |fname, payload|
63-
return [fname, payload] if fname == absolute || fname == path || fname.end_with?(suffix)
64-
end
65-
nil
62+
coverage_hash.find { |fname, _| fname == absolute || fname == path || fname.end_with?(suffix) }
6663
end
6764

6865
def emit(match, opts, stdout)

lib/simplecov/command_guesser.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ def guess
1919

2020
private
2121

22+
# When parallel_tests (or a compatible runner) is driving the suite,
23+
# tag the command name with this worker's position in the pool.
2224
def parallel_data
23-
# If being run from inside parallel_tests set the command name according to the process number
24-
return unless ENV["PARALLEL_TEST_GROUPS"] && ENV["TEST_ENV_NUMBER"]
25-
25+
groups = ENV.fetch("PARALLEL_TEST_GROUPS", nil)
2626
number = ENV.fetch("TEST_ENV_NUMBER", nil)
27+
return unless groups && number
28+
29+
# parallel_tests sets the first worker's TEST_ENV_NUMBER to "" rather
30+
# than "1"; restore the position so the rendered label reads cleanly.
2731
number = "1" if number.empty?
28-
"(#{number}/#{ENV.fetch('PARALLEL_TEST_GROUPS', nil)})"
32+
"(#{number}/#{groups})"
2933
end
3034

3135
COMMAND_LINE_FRAMEWORKS = {

lib/simplecov/parallel_adapters/generic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module ParallelAdapters
2323
class GenericAdapter < Base
2424
class << self
2525
def active?
26-
!ENV.fetch("TEST_ENV_NUMBER", nil).nil?
26+
ENV.key?("TEST_ENV_NUMBER")
2727
end
2828

2929
# parallel_tests sets the first worker's TEST_ENV_NUMBER to "";

lib/simplecov/parallel_adapters/parallel_tests.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class << self
1616
def active?
1717
ensure_loaded
1818
# !! to coerce `defined?` (returns nil or "constant") to a proper bool.
19-
!!(defined?(::ParallelTests) && !ENV.fetch("TEST_ENV_NUMBER", nil).nil?)
19+
!!(defined?(::ParallelTests) && ENV.key?("TEST_ENV_NUMBER"))
2020
end
2121

2222
# Pick the *first* started process to do the final-result work,
@@ -69,7 +69,7 @@ def ensure_loaded
6969
end
7070

7171
def env_suggests_parallel_tests?
72-
!ENV.fetch("TEST_ENV_NUMBER", nil).nil? && !ENV.fetch("PARALLEL_TEST_GROUPS", nil).nil?
72+
ENV.key?("TEST_ENV_NUMBER") && ENV.key?("PARALLEL_TEST_GROUPS")
7373
end
7474
end
7575
end

lib/simplecov/profiles.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ def load(name)
4242
def fetch_proc(name)
4343
name = name.to_sym
4444
autoload_profile(name) unless key?(name)
45-
raise SimpleCov::ConfigurationError, "Could not find SimpleCov Profile called '#{name}'" unless key?(name)
46-
47-
self[name]
45+
self[name] || raise(SimpleCov::ConfigurationError, "Could not find SimpleCov Profile called '#{name}'")
4846
end
4947

5048
private

lib/simplecov/result_adapter.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,25 @@ def self.call(*args)
1818
def adapt
1919
return unless result
2020

21-
result.each_with_object({}) do |(file_name, cover_statistic), adapted_result|
22-
if cover_statistic.is_a?(Array)
23-
adapted_result.merge!(file_name => {"lines" => cover_statistic})
24-
else
25-
adapt_oneshot_lines_if_needed(file_name, cover_statistic)
26-
normalize_method_keys(cover_statistic)
27-
adapted_result.merge!(file_name => cover_statistic)
28-
end
21+
result.to_h do |file_name, cover_statistic|
22+
[file_name, adapt_one(file_name, cover_statistic)]
2923
end
3024
end
3125

3226
private
3327

28+
# Pre-0.18 resultsets pointed each filename straight at a line-coverage
29+
# array; everything since uses the `{lines:, branches:, methods:}`
30+
# shape. Newer entries also need their methods table massaged before
31+
# downstream code merges across processes.
32+
def adapt_one(file_name, cover_statistic)
33+
return {"lines" => cover_statistic} if cover_statistic.is_a?(Array)
34+
35+
adapt_oneshot_lines_if_needed(file_name, cover_statistic)
36+
normalize_method_keys(cover_statistic)
37+
cover_statistic
38+
end
39+
3440
# Normalize memory addresses in method coverage keys so that results
3541
# from different processes can be merged. Anonymous class names like
3642
# "#<Class:0x00007ff19ab24790>" get inconsistent addresses across runs.
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
# frozen_string_literal: true
22

33
module SimpleCov
4-
#
5-
# Select the files that related to working scope directory of SimpleCov
6-
#
4+
# Drop coverage entries whose paths live outside `SimpleCov.root` so the
5+
# report only reflects the project's own source. Vendored gems, stdlib
6+
# files, and anything else that happens to have been touched during the
7+
# run never make it into the formatted result.
78
module UselessResultsRemover
89
def self.call(coverage_result)
9-
coverage_result.select do |path, _coverage|
10-
path =~ root_regx
11-
end
10+
coverage_result.select { |path, _coverage| path.match?(root_regx) }
1211
end
1312

13+
# The `/i` flag covers case-insensitive matches on Windows / macOS-HFS+
14+
# where the on-disk path's case can differ from `SimpleCov.root`'s.
1415
def self.root_regx
15-
@root_regx ||= begin
16-
prefix = SimpleCov.root
17-
prefix += File::SEPARATOR unless prefix.end_with?(File::SEPARATOR)
18-
/\A#{Regexp.escape(prefix)}/i
19-
end
16+
@root_regx ||= /\A#{Regexp.escape(SimpleCov.root.chomp(File::SEPARATOR) + File::SEPARATOR)}/i
2017
end
2118
end
2219
end

0 commit comments

Comments
 (0)