Skip to content

Commit

Permalink
Include more statements in code coverage (#2554)
Browse files Browse the repository at this point in the history
* Include more statements in CodeCoverage

* Update tests

* Sort files in Coverage

* Extend sort to all Group-Object

Saving future headaches

* Update tests after rebase

* Add missing sorts
  • Loading branch information
fflaten authored Dec 13, 2024
1 parent d40aaa1 commit 422ef99
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 35 deletions.
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]
}
}
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]
[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
3 changes: 2 additions & 1 deletion src/functions/TestResults.NUnit25.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ function Write-NUnitTestSuiteElements {

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

foreach ($suite in $suites) {
Expand Down
6 changes: 4 additions & 2 deletions src/functions/TestResults.NUnit3.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ function Write-NUnit3TestSuiteElement {

$blockGroups = @(
# Blocks only have GroupId if parameterized (using -ForEach). All other blocks are put in group with '' value
$Node.Blocks | & $SafeCommands['Group-Object'] -Property GroupId
# 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 | & $SafeCommands["Sort-Object"] -Property Name
)

foreach ($group in $blockGroups) {
Expand Down Expand Up @@ -170,7 +171,8 @@ function Write-NUnit3TestSuiteElement {

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

foreach ($group in $testGroups) {
Expand Down
Loading

0 comments on commit 422ef99

Please sign in to comment.