Skip to content

Commit

Permalink
More comprehensive JaCoCo support (#1120)
Browse files Browse the repository at this point in the history
* Added detailed code coverage. Per sourcefile a line counter and each line covered or missed numbers.

* Added CodeCoverage parameter to Describe. If the -DetailedCodeCoverage switch is on then the CodeCoverage parameters of the Describe parts will be used for code coverage.

* A compatibility with PSCore 6.x correction and merging upstream error correction

* More comprehensive JaCoCo support

* Fix failing tests
  • Loading branch information
Thilas authored and nohwnd committed Dec 12, 2018
1 parent ac0ccb1 commit ec44a1f
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 153 deletions.
72 changes: 26 additions & 46 deletions Functions/Coverage.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ InModuleScope Pester {
'I am functionOne'
NestedFunction
}
if ($false) { 'I cannot get called.' }
Invoke-Command { 'I get called.' }
}
function FunctionTwo
Expand All @@ -47,75 +51,50 @@ InModuleScope Pester {
Enter-CoverageAnalysis -CodeCoverage "$(Join-Path -Path $root -ChildPath TestScript.ps1)", "$(Join-Path -Path $root -ChildPath TestScript.ps1)", "$(Join-Path -Path $root -ChildPath TestScript2.ps1)" -PesterState $testState

It 'Has the proper number of breakpoints defined' {
$testState.CommandCoverage.Count | Should -Be 8
$testState.CommandCoverage.Count | Should -Be 12
}

$null = & "$(Join-Path -Path $root -ChildPath TestScript.ps1)"
$null = & "$(Join-Path -Path $root -ChildPath TestScript2.ps1)"
$coverageReport = Get-CoverageReport -PesterState $testState

It 'Reports the proper number of executed commands' {
$coverageReport.NumberOfCommandsExecuted | Should -Be 7
$coverageReport.NumberOfCommandsExecuted | Should -Be 10
}

It 'Reports the proper number of analyzed commands' {
$coverageReport.NumberOfCommandsAnalyzed | Should -Be 8
$coverageReport.NumberOfCommandsAnalyzed | Should -Be 12
}

It 'Reports the proper number of analyzed files' {
$coverageReport.NumberOfFilesAnalyzed | Should -Be 2
}

It 'Reports the proper number of missed commands' {
$coverageReport.MissedCommands.Count | Should -Be 1
$coverageReport.MissedCommands.Count | Should -Be 2
}

It 'Reports the correct missed command' {
$coverageReport.MissedCommands[0].Command | Should -Be "'I am function two. I never get called.'"
$coverageReport.MissedCommands[0].Command | Should -Be "'I cannot get called.'"
$coverageReport.MissedCommands[1].Command | Should -Be "'I am function two. I never get called.'"
}

It 'Reports the proper number of hit commands' {
$coverageReport.HitCommands.Count | Should -Be 7
$coverageReport.HitCommands.Count | Should -Be 10
}

It 'Reports the correct hit command' {
$coverageReport.HitCommands[0].Command | Should -Be "'I am the nested function.'"
}

It 'JaCoCo report must be correct'{
[String]$jaCoCoReportXml = Get-JaCoCoReportXml -PesterState $testState -CoverageReport $coverageReport
$jaCoCoReportXml = $jaCoCoReportXml -replace 'Pester \([^\)]*','Pester (date'
$jaCoCoReportXml = $jaCoCoReportXml -replace 'start="[0-9]*"','start=""'
$jaCoCoReportXml = $jaCoCoReportXml -replace 'dump="[0-9]*"','dump=""'
$jaCoCoReportXml = $jaCoCoReportXml -replace "$([System.Environment]::NewLine)",''
[String]$ReferenceReport = [String]'<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN" "report.dtd"><report name="Pester (date)"><sessioninfo id="this" start="" dump="" /><counter type="INSTRUCTION" missed="1" covered="7" /><counter type="LINE" missed="1" covered="7" /><counter type="METHOD" missed="1" covered="4" /><counter type="CLASS" missed="0" covered="2" /></report>'
$jaCoCoReportXml | should -Be $ReferenceReport
}
Exit-CoverageAnalysis -PesterState $testState
}

Context 'Entire file detailed coverage' {
$testState = New-PesterState -Path $root

# Path deliberately duplicated to make sure the code doesn't produce multiple breakpoints for the same commands
Enter-CoverageAnalysis -CodeCoverage "$root\TestScript.ps1", "$root\TestScript.ps1" -PesterState $testState

It 'Has the proper number of breakpoints defined' {
$testState.CommandCoverage.Count | Should -Be 7
}

$null = & "$root\TestScript.ps1"
$coverageReport = Get-CoverageReport -PesterState $testState

It 'JaCoCo report must be correct'{
[String]$jaCoCoReportXml = Get-JaCoCoReportXml -PesterState $testState -CoverageReport $coverageReport -DetailedCodeCoverage
$jaCoCoReportXml = $jaCoCoReportXml -replace 'Pester \([^\)]*','Pester (date'
$jaCoCoReportXml = $jaCoCoReportXml -replace 'start="[0-9]*"','start=""'
$jaCoCoReportXml = $jaCoCoReportXml -replace 'dump="[0-9]*"','dump=""'
$jaCoCoReportXml = $jaCoCoReportXml -replace "$([System.Environment]::NewLine)",''
$jaCoCoReportXml = $jaCoCoReportXml.Replace($root,'')
[String]$ReferenceReport = "{0}{1}{2}" -f [String]'<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN" "report.dtd"><report name="Pester (date)"><sessioninfo id="this" start="" dump="" /><package name="Powershell"><sourcefile name="', $([System.IO.Path]::DirectorySeparatorChar).ToString(), [String]'TestScript.ps1"><line nr="5" ci="1" mi="0" /><line nr="6" ci="1" mi="0" /><line nr="9" ci="1" mi="0" /><line nr="11" ci="1" mi="0" /><line nr="12" ci="1" mi="0" /><line nr="18" ci="0" mi="1" /><line nr="21" ci="1" mi="0" /><counter type="LINE" missed="1" covered="6" /></sourcefile></package><counter type="INSTRUCTION" missed="1" covered="6" /><counter type="LINE" missed="1" covered="6" /><counter type="METHOD" missed="1" covered="3" /><counter type="CLASS" missed="0" covered="1" /></report>'
$jaCoCoReportXml | should -Be $ReferenceReport
$jaCoCoReportXml = $jaCoCoReportXml -replace "$([System.Environment]::NewLine)",''
$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>'
}
Exit-CoverageAnalysis -PesterState $testState
}
Expand Down Expand Up @@ -161,26 +140,26 @@ InModuleScope Pester {
Enter-CoverageAnalysis -CodeCoverage @{Path = "$(Join-Path -Path $root -ChildPath TestScript.ps1)"; Function = 'FunctionOne'} -PesterState $testState

It 'Has the proper number of breakpoints defined' {
$testState.CommandCoverage.Count | Should -Be 5
$testState.CommandCoverage.Count | Should -Be 9
}

$null = & "$(Join-Path -Path $root -ChildPath TestScript.ps1)"
$coverageReport = Get-CoverageReport -PesterState $testState

It 'Reports the proper number of executed commands' {
$coverageReport.NumberOfCommandsExecuted | Should -Be 5
$coverageReport.NumberOfCommandsExecuted | Should -Be 8
}

It 'Reports the proper number of analyzed commands' {
$coverageReport.NumberOfCommandsAnalyzed | Should -Be 5
$coverageReport.NumberOfCommandsAnalyzed | Should -Be 9
}

It 'Reports the proper number of missed commands' {
$coverageReport.MissedCommands.Count | Should -Be 0
$coverageReport.MissedCommands.Count | Should -Be 1
}

It 'Reports the proper number of hit commands' {
$coverageReport.HitCommands.Count | Should -Be 5
$coverageReport.HitCommands.Count | Should -Be 8
}

It 'Reports the correct hit command' {
Expand Down Expand Up @@ -231,34 +210,35 @@ InModuleScope Pester {
Enter-CoverageAnalysis -CodeCoverage @{Path = "$(Join-Path -Path $root -ChildPath *.ps1)"; Function = '*' } -PesterState $testState

It 'Has the proper number of breakpoints defined' {
$testState.CommandCoverage.Count | Should -Be 6
$testState.CommandCoverage.Count | Should -Be 10
}

$null = & "$(Join-Path -Path $root -ChildPath TestScript.ps1)"
$coverageReport = Get-CoverageReport -PesterState $testState

It 'Reports the proper number of executed commands' {
$coverageReport.NumberOfCommandsExecuted | Should -Be 5
$coverageReport.NumberOfCommandsExecuted | Should -Be 8
}

It 'Reports the proper number of analyzed commands' {
$coverageReport.NumberOfCommandsAnalyzed | Should -Be 6
$coverageReport.NumberOfCommandsAnalyzed | Should -Be 10
}

It 'Reports the proper number of analyzed files' {
$coverageReport.NumberOfFilesAnalyzed | Should -Be 1
}

It 'Reports the proper number of missed commands' {
$coverageReport.MissedCommands.Count | Should -Be 1
$coverageReport.MissedCommands.Count | Should -Be 2
}

It 'Reports the correct missed command' {
$coverageReport.MissedCommands[0].Command | Should -Be "'I am function two. I never get called.'"
$coverageReport.MissedCommands[0].Command | Should -Be "'I cannot get called.'"
$coverageReport.MissedCommands[1].Command | Should -Be "'I am function two. I never get called.'"
}

It 'Reports the proper number of hit commands' {
$coverageReport.HitCommands.Count | Should -Be 5
$coverageReport.HitCommands.Count | Should -Be 8
}

It 'Reports the correct hit command' {
Expand Down
Loading

0 comments on commit ec44a1f

Please sign in to comment.