Skip to content

Synthesize branch and method coverage for unloaded files - #1194

Merged
sferik merged 1 commit into
mainfrom
synthesize-branches-methods-for-unloaded
May 27, 2026
Merged

sferik merged 1 commit into
mainfrom
synthesize-branches-methods-for-unloaded

Conversation

@sferik

@sferik sferik commented May 27, 2026 •

Copy link
Copy Markdown
Collaborator

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.

This comment was marked as resolved.

`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
sferik force-pushed the synthesize-branches-methods-for-unloaded branch from f06151b to cea6def Compare May 27, 2026 01:58
@sferik
sferik merged commit 98a47e0 into main May 27, 2026
24 checks passed
@sferik
sferik deleted the synthesize-branches-methods-for-unloaded branch August 12, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants