Skip to content

Fix misleading 100% branch/method coverage for unloaded tracked files - #1163

Merged
sferik merged 1 commit into
simplecov-ruby:mainfrom
55728:fix/misleading-coverage-for-unloaded-tracked-files
Apr 3, 2026
Merged

sferik merged 1 commit into
simplecov-ruby:mainfrom
55728:fix/misleading-coverage-for-unloaded-tracked-files

Conversation

@55728

@55728 55728 commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #902

Problem

Files added via track_files that are never loaded/required report 100% branch coverage because SimulateCoverage returns empty branch data, resulting in 0 covered / 0 missed = 100%.

Solution

Add a not_loaded flag to SimulateCoverage output and check it in SourceFile#branch_coverage_statistics (and method_coverage_statistics) to report 0% instead of misleading 100% for files that were never loaded.

This follows the approach suggested by @PragTob in #902:

"0/unknown = 0%"

@sferik sferik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a bit of feedback but I like the idea!

Comment thread lib/simplecov/source_file.rb Resolved
Comment thread lib/simplecov/simulate_coverage.rb Resolved
Files added via `track_files` that are never loaded/required reported 100% branch coverage because `SimulateCoverage` returns empty branch data, resulting in 0 covered / 0 missed = 100%.

Add a `loaded` flag to `SourceFile` (passed via `add_not_loaded_files`) and use a `not_applicable` state in `CoverageStatistics` to report 0% instead of misleading 100% for files that were never loaded. This avoids faking raw counts while still producing the correct percentage.

Fixes #902
@55728

55728 commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

Addressed your feedback — thanks @sferik!

  • Moved the flag from the coverage data hash to a loaded keyword arg on SourceFile#initialize
  • Replaced the missed = 1 hack with a percent: override on CoverageStatistics, so raw counts stay accurate (0 covered / 0 missed)
  • add_not_loaded_files now returns the set of not-loaded paths, which flows through Result → SourceFile

Force-pushed as a single commit.

Known limitation: The loaded state is not persisted via Result#to_hash / Result.from_hash, so files that are never loaded across any test suite could revert to 100% after result merging or restoration from disk. Happy to address this in a follow-up if needed.

@55728
55728 requested a review from sferik April 3, 2026 11:50

@sferik sferik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better! I’m going to merge this. Thanks again!

@sferik
sferik merged commit c27a0b2 into simplecov-ruby:main Apr 3, 2026
10 checks passed
@55728
55728 deleted the fix/misleading-coverage-for-unloaded-tracked-files branch April 3, 2026 17:33
@55728

55728 commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the thorough review and quick merge, @sferik! Your feedback on avoiding faked counts and using instance-level flags made the solution much cleaner. Happy to follow up on the serialization limitation if needed.

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.

Don't mark non required files as "100%" branch coverage

2 participants