Skip to content
Merged
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: 15 additions & 1 deletion Functions/Coverage.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ InModuleScope Pester {
$null = New-Item -Path $(Join-Path -Path $root -ChildPath TestScript2.ps1) -ItemType File -ErrorAction SilentlyContinue

Set-Content -Path $(Join-Path -Path $root -ChildPath TestScript2.ps1) -Value @'
'Some other file'
'Some {0} file' `
-f `
'other'

'@

Expand Down Expand Up @@ -96,6 +98,18 @@ InModuleScope Pester {
$jaCoCoReportXml = $jaCoCoReportXml.Replace($root.Replace('\', '/'), '')
$jaCoCoReportXml | should -be '<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.1//EN" "report.dtd"><report name="Pester (date)"><sessioninfo id="this" start="" dump="" /><package name="PowerShell"><class name="TestScript.ps1" sourcefilename="/TestScript.ps1"><method name="NestedFunction" desc="()" line="5"><counter type="INSTRUCTION" missed="0" covered="2" /><counter type="LINE" missed="0" covered="2" /><counter type="METHOD" missed="0" covered="1" /></method><method name="FunctionOne" desc="()" line="9"><counter type="INSTRUCTION" missed="1" covered="6" /><counter type="LINE" missed="0" covered="5" /><counter type="METHOD" missed="0" covered="1" /></method><method name="FunctionTwo" desc="()" line="22"><counter type="INSTRUCTION" missed="1" covered="0" /><counter type="LINE" missed="1" covered="0" /><counter type="METHOD" missed="1" covered="0" /></method><method name="&lt;script&gt;" desc="()" line="25"><counter type="INSTRUCTION" missed="0" covered="1" /><counter type="LINE" missed="0" covered="1" /><counter type="METHOD" missed="0" covered="1" /></method><counter type="INSTRUCTION" missed="2" covered="9" /><counter type="LINE" missed="1" covered="8" /><counter type="METHOD" missed="1" covered="3" /><counter type="CLASS" missed="0" covered="1" /></class><class name="TestScript2.ps1" sourcefilename="/TestScript2.ps1"><method name="&lt;script&gt;" desc="()" line="1"><counter type="INSTRUCTION" missed="0" covered="1" /><counter type="LINE" missed="0" covered="1" /><counter type="METHOD" missed="0" covered="1" /></method><counter type="INSTRUCTION" missed="0" covered="1" /><counter type="LINE" missed="0" covered="1" /><counter type="METHOD" missed="0" covered="1" /><counter type="CLASS" missed="0" covered="1" /></class><sourcefile name="/TestScript.ps1"><line nr="5" mi="0" ci="1" /><line nr="6" mi="0" ci="1" /><line nr="9" mi="0" ci="1" /><line nr="11" mi="0" ci="1" /><line nr="12" mi="0" ci="1" /><line nr="15" mi="1" ci="1" /><line nr="17" mi="0" ci="2" /><line nr="22" mi="1" ci="0" /><line nr="25" mi="0" ci="1" /><counter type="INSTRUCTION" missed="2" covered="9" /><counter type="LINE" missed="1" covered="8" /><counter type="METHOD" missed="1" covered="3" /><counter type="CLASS" missed="0" covered="1" /></sourcefile><sourcefile name="/TestScript2.ps1"><line nr="1" mi="0" ci="1" /><counter type="INSTRUCTION" missed="0" covered="1" /><counter type="LINE" missed="0" covered="1" /><counter type="METHOD" missed="0" covered="1" /><counter type="CLASS" missed="0" covered="1" /></sourcefile><counter type="INSTRUCTION" missed="2" covered="10" /><counter type="LINE" missed="1" covered="9" /><counter type="METHOD" missed="1" covered="4" /><counter type="CLASS" missed="0" covered="2" /></package><counter type="INSTRUCTION" missed="2" covered="10" /><counter type="LINE" missed="1" covered="9" /><counter type="METHOD" missed="1" covered="4" /><counter type="CLASS" missed="0" covered="2" /></report>'
}

It 'Reports the right line numbers' {
$coverageReport.HitCommands[$coverageReport.NumberOfCommandsExecuted-1].Line | Should -Be 1
$coverageReport.HitCommands[$coverageReport.NumberOfCommandsExecuted-1].StartLine | Should -Be 1
$coverageReport.HitCommands[$coverageReport.NumberOfCommandsExecuted-1].EndLine | Should -Be 3
}

It 'Reports the right column numbers' {
$coverageReport.HitCommands[$coverageReport.NumberOfCommandsExecuted-1].StartColumn | Should -Be 13
$coverageReport.HitCommands[$coverageReport.NumberOfCommandsExecuted-1].EndColumn | Should -Be 24
}

Exit-CoverageAnalysis -PesterState $testState
}

Expand Down
43 changes: 25 additions & 18 deletions Functions/Coverage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,14 @@ function New-CoverageBreakpoint
$breakpoint = & $SafeCommands['Set-PSBreakpoint'] @params

[pscustomobject] @{
File = $Command.Extent.File
Function = Get-ParentFunctionName -Ast $Command
Line = $Command.Extent.StartLineNumber
Command = Get-CoverageCommandText -Ast $Command
Breakpoint = $breakpoint
File = $Command.Extent.File
Function = Get-ParentFunctionName -Ast $Command
StartLine = $Command.Extent.StartLineNumber
EndLine = $Command.Extent.EndLineNumber
StartColumn = $Command.Extent.StartColumnNumber
EndColumn = $Command.Extent.EndColumnNumber
Command = Get-CoverageCommandText -Ast $Command
Breakpoint = $breakpoint
}
}

Expand Down Expand Up @@ -484,24 +487,28 @@ function Get-CoverageReport
{
param ([object] $PesterState)

$totalCommandCount = $PesterState.CommandCoverage.Count

$missedCommands = @(Get-CoverageMissedCommands -CommandCoverage $PesterState.CommandCoverage | & $SafeCommands['Select-Object'] File, Line, Function, Command)
$hitCommands = @(Get-CoverageHitCommands -CommandCoverage $PesterState.CommandCoverage | & $SafeCommands['Select-Object'] File, Line, Function, Command)
$allCommands = @($PesterState.CommandCoverage | & $SafeCommands['Select-Object'] File, Line, Function, Command, Breakpoint)
$properties = @(
'File'
@{ Name = 'Line'; Expression = { $_.StartLine } }
'StartLine'
'EndLine'
'StartColumn'
'EndColumn'
'Function'
'Command'
@{ Name = 'HitCount'; Expression = { $_.Breakpoint.HitCount } }
)
$missedCommands = @(Get-CoverageMissedCommands -CommandCoverage $PesterState.CommandCoverage | & $SafeCommands['Select-Object'] $properties)
$hitCommands = @(Get-CoverageHitCommands -CommandCoverage $PesterState.CommandCoverage | & $SafeCommands['Select-Object'] $properties)
$analyzedFiles = @($PesterState.CommandCoverage | & $SafeCommands['Select-Object'] -ExpandProperty File -Unique)
$fileCount = $analyzedFiles.Count

$executedCommandCount = $totalCommandCount - $missedCommands.Count

[pscustomobject] @{
NumberOfCommandsAnalyzed = $totalCommandCount
NumberOfFilesAnalyzed = $fileCount
NumberOfCommandsExecuted = $executedCommandCount
NumberOfCommandsAnalyzed = $PesterState.CommandCoverage.Count
NumberOfFilesAnalyzed = $analyzedFiles.Count
NumberOfCommandsExecuted = $hitCommands.Count
NumberOfCommandsMissed = $missedCommands.Count
MissedCommands = $missedCommands
HitCommands = $hitCommands
AllCommands = $allCommands
AnalyzedFiles = $analyzedFiles
}
}
Expand Down Expand Up @@ -610,7 +617,7 @@ function Get-JaCoCoReportXml {
$file = $command.File
$function = $command.Function
if (!$function) { $function = '<script>' }
$line = $command.Line.ToString()
$line = $command.StartLine.ToString()

$missed = if ($command.Breakpoint.HitCount) { 0 } else { 1 }
$covered = if ($command.Breakpoint.HitCount) { 1 } else { 0 }
Expand Down