Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for excluding functions from coverage with attribute #2593

Merged
merged 6 commits into from
Jan 16, 2025

Conversation

benjaminfuchs
Copy link
Contributor

@benjaminfuchs benjaminfuchs commented Dec 21, 2024

PR Summary

Fixes #2268

This PR adds support for excluding specific code constructs from coverage analysis using [ExcludeFromCodeCoverageAttribute()].

Changes:

  • New CoverageLocationVisitor: Skips AST nodes in script blocks marked with [ExcludeFromCodeCoverageAttribute()].
  • Get-CommandsInFile updated: Replaced predicate-based filtering with CoverageLocationVisitor.
  • Updated Tests: Ensures nodes with [ExcludeFromCodeCoverageAttribute()] are skipped.
    Verifies inclusion of unmarked nodes and relevant control flow statements.

PR Checklist

  • PR has meaningful title
  • Summary describes changes
  • PR is ready to be merged
    • If not, use the arrow next to Create Pull Request to mark it as a draft. PR can be marked Ready for review when it's ready.
  • Tests are added/update (if required)
  • Documentation is updated/added (if required)

@benjaminfuchs
Copy link
Contributor Author

If the approach is valid, I'll refine the tests and add proper documentation. Feedback is welcome!

@benjaminfuchs benjaminfuchs force-pushed the feature/exclude-attribute-coverage branch 3 times, most recently from 99eb66a to 1c9dd96 Compare December 21, 2024 22:37
@benjaminfuchs benjaminfuchs force-pushed the feature/exclude-attribute-coverage branch from 1c9dd96 to d1c84df Compare December 21, 2024 22:58
@benjaminfuchs benjaminfuchs force-pushed the feature/exclude-attribute-coverage branch from 53c13ac to 8608926 Compare December 29, 2024 12:48
Copy link
Collaborator

@fflaten fflaten left a comment

Choose a reason for hiding this comment

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

Thank you for looking into this! It works as intended, though I've suggested some improvements.

While reviewing I realized we could make this more efficient, see comment about FindAll. So I'd like to discuss an alternative approach. See draft PR #2598 which should produce the same results.

src/functions/Coverage.ps1 Outdated Show resolved Hide resolved
src/functions/Coverage.ps1 Outdated Show resolved Hide resolved
src/functions/Coverage.ps1 Outdated Show resolved Hide resolved
src/functions/Coverage.ps1 Outdated Show resolved Hide resolved
src/functions/Coverage.ps1 Outdated Show resolved Hide resolved
src/functions/Coverage.ps1 Outdated Show resolved Hide resolved
@benjaminfuchs
Copy link
Contributor Author

@fflaten Thanks for the feedback! I agree with your point about improving efficiency by replacing FindAll with Visit. The approach in draft PR #2598 looks good to me. My check for the attribute also seems a bit over-engineered. If you'd like, I can take over your implementation and finalize the PR with tests and documentation updates, or let me know how you'd like to proceed.

@fflaten
Copy link
Collaborator

fflaten commented Jan 12, 2025

Looks like you've simplified yours as well. Feel free the use and complete the Visitor-based code. Was waiting on @nohwnd's thoughts but personally I'd prefer it as it's more extensible.

@benjaminfuchs benjaminfuchs force-pushed the feature/exclude-attribute-coverage branch 2 times, most recently from c1acbcd to e800b90 Compare January 12, 2025 21:26
@benjaminfuchs benjaminfuchs force-pushed the feature/exclude-attribute-coverage branch 2 times, most recently from 9865db2 to efd3c4c Compare January 12, 2025 23:29
@benjaminfuchs benjaminfuchs force-pushed the feature/exclude-attribute-coverage branch from efd3c4c to 82ef868 Compare January 12, 2025 23:32
@nohwnd
Copy link
Member

nohwnd commented Jan 13, 2025

Will try to have a look today in the evening.

@nohwnd
Copy link
Member

nohwnd commented Jan 13, 2025

Great job here,thank you! Some small nitpicks on the code :)

@fflaten
Copy link
Collaborator

fflaten commented Jan 14, 2025

Thanks. @benjaminfuchs Let me know if you'd like me to address any of the comments.

@nohwnd
Copy link
Member

nohwnd commented Jan 14, 2025 via email

@benjaminfuchs
Copy link
Contributor Author

@fflaten @nohwnd Thank you both for your feedback! I wanted to give it a shot, so I’ve pushed my updates to address the comments. Feel free to finalize any remaining points. I really enjoyed helping out, thanks for great project!

@nohwnd nohwnd merged commit 648f71c into pester:main Jan 16, 2025
9 checks passed
@nohwnd
Copy link
Member

nohwnd commented Jan 16, 2025

Thank you for your contribution!

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.

Allow [ExcludeFromCodeCoverage()] on the param() block to exclude a function from code coverage
3 participants