Repository navigation
Synthesize branch and method coverage for unloaded files - #1194
Merged
Merged
Conversation
`SimulateCoverage` used to leave `branches` and `methods` as empty
hashes for files added via `cover` / `track_files` that were never
`require`'d during the run, on the rationale that "parsing source
ourselves" felt risky. That left those files invisible to the branch
and method denominators while their lines DID count — so a glob like
`cover "{app,lib}/**/*.rb"` over a project with files lacking specs
silently inflated branch% relative to line%. The OP's reproduction was
via SonarQube, which surfaces the asymmetry more visibly than the
SimpleCov HTML report.
A new `SimpleCov::StaticCoverageExtractor` walks the AST with Prism and
emits Coverage-shaped tuples without loading the file. Output matches
Ruby's own `Coverage.result` byte-for-byte for branches (every
`:if` / `:case` / `:while` / `:until` construct with its `:then` /
`:else` / `:when` / `:in` / `:body` arms, including the synthetic
`:else` that `ignore_branches :implicit_else` targets) and near-perfectly
for methods (the only difference is the class-name slot, which we
report as a string since we can't resolve the actual Class constant
without loading).
Prism is bundled with Ruby 3.3+. On older Rubies the require fails
gracefully — `StaticCoverageExtractor.available?` returns false and
SimulateCoverage falls back to the previous "empty hashes" behavior,
so the upgrade is a no-op for those Rubies (users who want the fix
can `gem install prism`).
The extractor is tested directly against Coverage's output shape for
each construct (`if`, `case/when`, `case/in`, `while`, postfix-if,
ternary) and the methods enumeration covers top-level, instance,
namespaced, and singleton-class shapes. SimulateCoverage's existing
behavior specs are updated to reflect the new hash-shape contract.
Resolves #1059.
sferik
force-pushed
the
synthesize-branches-methods-for-unloaded
branch
from
May 27, 2026 01:58
f06151b to
cea6def
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SimulateCoverageused to leavebranchesandmethodsas empty hashes for files added viacover/track_filesthat were neverrequire'd during the run, on the rationale that "parsing source ourselves" felt risky. That left those files invisible to the branch and method denominators while their lines did count — so a glob likecover "{app,lib}/**/*.rb"over a project with files lacking specs silently inflated branch% relative to line%. The OP's reproduction was via SonarQube, which surfaces the asymmetry more visibly than the SimpleCov HTML report.A new
SimpleCov::StaticCoverageExtractorwalks the AST with Prism and emits Coverage-shaped tuples without loading the file. Output matches Ruby's ownCoverage.resultbyte-for-byte for branches (every:if/:case/:while/:untilconstruct with its:then/:else/:when/:in/:bodyarms, including the synthetic:elsethatignore_branches :implicit_elsetargets) and near-perfectly for methods (the only difference is the class-name slot, which we report as a string since we can't resolve the actualClassconstant without loading).Prism is bundled with Ruby 3.3+. On older Rubies the require fails gracefully —
StaticCoverageExtractor.available?returnsfalseandSimulateCoveragefalls back to the previous "empty hashes" behavior, so the upgrade is a no-op for those Rubies (users who want the fix cangem install prism).The extractor is tested directly against Coverage's output shape for each construct (
if,case/when,case/in,while, postfix-if, ternary) and the methods enumeration covers top-level, instance, namespaced, and singleton-class shapes. SimulateCoverage's existing behavior specs are updated to reflect the new hash-shape contract.