Add :eval_generated filter for ignore_branches and ignore_methods#1196
Merged
Conversation
Rails delegate (and other module_eval / class_eval / instance_eval macros that pass __FILE__ and __LINE__) inject method and branch entries into a file's coverage data even when the static source has nothing at that line. Ruby's Coverage library attributes the eval'd code to the caller's position, so a single delegate call shows up as a missed def and a missed if branch wherever the macro fires. The previous workaround was to disable eval coverage entirely, which loses the legitimate signal. Adds the :eval_generated token to SimpleCov.ignore_branches and adds a new SimpleCov.ignore_methods with the same token. Both filters walk the static source through SimpleCov::StaticCoverageExtractor (Prism) and drop any Coverage tuple whose start line lacks a real def keyword (for methods) or branch construct (for branches). Line presence is the matcher, so a real branch or def that shares a line with an eval-generated entry survives. Prism ships with Ruby 3.3+, and on older Rubies gem install prism enables the filter, otherwise the setting is a no-op. The filters are opt-in and stored regardless of which coverage criteria are enabled at call time, matching the existing ignore_branches lifecycle. Unknown tokens raise SimpleCov::ConfigurationError to catch typos. README and CHANGELOG updated, with specs covering the DSL, the new StaticCoverageExtractor.real_source_positions helper, and SourceFile integration. Resolves #1046.
78b81a9 to
ad7b76c
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.
Rails delegate (and other module_eval / class_eval / instance_eval macros that pass
__FILE__and__LINE__) inject method and branch entries into a file's coverage data even when the static source has nothing at that line. Ruby's Coverage library attributes the eval'd code to the caller's position, so a single delegate call shows up as a missed def and a missed if branch wherever the macro fires. The previous workaround was to disable eval coverage entirely, which loses the legitimate signal.Adds the
:eval_generatedtoken toSimpleCov.ignore_branchesand adds a newSimpleCov.ignore_methodswith the same token. Both filters walk the static source throughSimpleCov::StaticCoverageExtractor(Prism) and drop any Coverage tuple whose start line lacks a real def keyword (for methods) or branch construct (for branches). Line presence is the matcher, so a real branch or def that shares a line with an eval-generated entry survives. Prism ships with Ruby 3.3+, and on older Rubies gem install prism enables the filter, otherwise the setting is a no-op.The filters are opt-in and stored regardless of which coverage criteria are enabled at call time, matching the existing
ignore_brancheslifecycle. Unknown tokens raiseSimpleCov::ConfigurationErrorto catch typos.READMEandCHANGELOGupdated, with specs covering the DSL, the newStaticCoverageExtractor.real_source_positionshelper, andSourceFileintegration.