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

Include more statements in code coverage #2554

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/functions/Coverage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ function Get-CoverageBreakpoints {
[ScriptBlock]$Logger
)

$fileGroups = @($CoverageInfo | & $SafeCommands['Group-Object'] -Property Path)
# PowerShell 6.1+ sorts by default in Group-Object. We need to sort for consistent output in Windows PowerShell
$fileGroups = @($CoverageInfo | & $SafeCommands['Group-Object'] -Property Path | & $SafeCommands['Sort-Object'] -Property Name)
foreach ($fileGroup in $fileGroups) {
if ($null -ne $Logger) {
$sw = [System.Diagnostics.Stopwatch]::StartNew()
Expand Down Expand Up @@ -287,9 +288,16 @@ function Get-CommandsInFile {
# In PowerShell 5.0, dynamic keywords for DSC configurations are represented by the DynamicKeywordStatementAst
# class. They still trigger breakpoints, but are not a child class of CommandBaseAst anymore.

# 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
$predicate = {
$args[0] -is [System.Management.Automation.Language.DynamicKeywordStatementAst] -or
$args[0] -is [System.Management.Automation.Language.CommandBaseAst]
$args[0] -is [System.Management.Automation.Language.CommandBaseAst] -or
$args[0] -is [System.Management.Automation.Language.BreakStatementAst] -or
$args[0] -is [System.Management.Automation.Language.ContinueStatementAst] -or
$args[0] -is [System.Management.Automation.Language.ExitStatementAst] -or
$args[0] -is [System.Management.Automation.Language.ThrowStatementAst]
}
Comment on lines 294 to 301
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only updated PSv5+ predicate as we might remove the redundant predicate for v6.
A potential backport would need to extend both predicates.

}
else {
Expand Down Expand Up @@ -551,6 +559,7 @@ function Get-CoverageCommandText {

$reportParentExtentTypes = @(
[System.Management.Automation.Language.ReturnStatementAst]
[System.Management.Automation.Language.ExitStatementAst]
[System.Management.Automation.Language.ThrowStatementAst]
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
[System.Management.Automation.Language.AssignmentStatementAst]
[System.Management.Automation.Language.IfStatementAst]
Expand Down Expand Up @@ -818,9 +827,10 @@ function Get-JaCoCoReportXml {
[long] $endTime = [System.DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
[long] $startTime = [math]::Floor($endTime - $TotalMilliseconds)

# PowerShell 6.1+ sorts by default in Group-Object. We need to sort for consistent output in Windows PowerShell
$folderGroups = $CommandCoverage | & $SafeCommands["Group-Object"] -Property {
& $SafeCommands["Split-Path"] $_.File -Parent
}
} | & $SafeCommands["Sort-Object"] -Property Name

$packageList = [System.Collections.Generic.List[psobject]]@()

Expand Down
1 change: 1 addition & 0 deletions src/functions/TestResults.NUnit25.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function Write-NUnitTestSuiteElements {

$suites = @(
# Tests only have GroupId if parameterized. All other tests are put in group with '' value
# PowerShell 6.1+ sorts by default in Group-Object. We need to sort for consistent output in Windows PowerShell
Copy link
Member

Choose a reason for hiding this comment

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

This says we sort but the code below does not use sort? Is this a mistake?

Copy link
Collaborator Author

@fflaten fflaten Nov 12, 2024

Choose a reason for hiding this comment

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

Very likely. Good catch, will fix. :)

$Node.Tests | & $SafeCommands['Group-Object'] -Property GroupId
)

Expand Down
1 change: 1 addition & 0 deletions src/functions/TestResults.NUnit3.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function Write-NUnit3TestSuiteElement {

$blockGroups = @(
# Blocks only have GroupId if parameterized (using -ForEach). All other blocks are put in group with '' value
# PowerShell 6.1+ sorts by default in Group-Object. We need to sort for consistent output in Windows PowerShell
$Node.Blocks | & $SafeCommands['Group-Object'] -Property GroupId
)

Expand Down
Loading