-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for excluding functions from coverage with attribute (#2593)
* Add support for excluding functions with [ExcludeFromCodeCoverageAttribute()] attribute * Refactor coverage exclusion logic * Improving Test-ContainsAttribute and minor review improvements * Replacing FindAll with AstVisitor * Updating tests * Adding description to new class and high level test
- Loading branch information
1 parent
eee7d45
commit 648f71c
Showing
3 changed files
with
208 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Management.Automation.Language; | ||
|
||
namespace Pester | ||
{ | ||
/// <summary> | ||
/// A visitor class for traversing the PowerShell AST to collect coverage-relevant locations. | ||
/// This replaces predicate-based filtering with a centralized, extensible approach. | ||
/// | ||
/// Advantages: | ||
/// - Efficiently skips nodes with attributes like [ExcludeFromCodeCoverage]. | ||
/// - Simplifies logic by handling each AST type in dedicated methods. | ||
/// </summary> | ||
public class CoverageLocationVisitor : AstVisitor2 | ||
{ | ||
public readonly List<Ast> CoverageLocations = new(); | ||
|
||
public override AstVisitAction VisitScriptBlock(ScriptBlockAst scriptBlockAst) | ||
{ | ||
if (scriptBlockAst.ParamBlock?.Attributes != null) | ||
{ | ||
foreach (var attribute in scriptBlockAst.ParamBlock.Attributes) | ||
{ | ||
if (attribute.TypeName.GetReflectionType() == typeof(ExcludeFromCodeCoverageAttribute)) | ||
{ | ||
return AstVisitAction.SkipChildren; | ||
} | ||
} | ||
} | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitCommand(CommandAst commandAst) | ||
{ | ||
CoverageLocations.Add(commandAst); | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitCommandExpression(CommandExpressionAst commandExpressionAst) | ||
{ | ||
CoverageLocations.Add(commandExpressionAst); | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitDynamicKeywordStatement(DynamicKeywordStatementAst dynamicKeywordStatementAst) | ||
{ | ||
CoverageLocations.Add(dynamicKeywordStatementAst); | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitBreakStatement(BreakStatementAst breakStatementAst) | ||
{ | ||
CoverageLocations.Add(breakStatementAst); | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitContinueStatement(ContinueStatementAst continueStatementAst) | ||
{ | ||
CoverageLocations.Add(continueStatementAst); | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitExitStatement(ExitStatementAst exitStatementAst) | ||
{ | ||
CoverageLocations.Add(exitStatementAst); | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
public override AstVisitAction VisitThrowStatement(ThrowStatementAst throwStatementAst) | ||
{ | ||
CoverageLocations.Add(throwStatementAst); | ||
return AstVisitAction.Continue; | ||
} | ||
|
||
// ReturnStatementAst is excluded as it's not behaving consistent. | ||
// "return" is not hit in 5.1 but fixed in a later version. Using "return 123" we get hit on 123 but not return. | ||
// See https://github.com/pester/Pester/issues/1465#issuecomment-604323645 | ||
} | ||
} |
This file contains 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
This file contains 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