From 451c36a9b12da1acd0fe94a717b60257ad37bd80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Fri, 17 May 2024 22:39:40 +0200 Subject: [PATCH] Set non-bp coverage as default and fix tests --- src/Pester.RSpec.ps1 | 6 ++-- .../Pester/CodeCoverageConfiguration.cs | 4 +-- src/functions/Coverage.ps1 | 2 +- tst/functions/Coverage.Tests.ps1 | 28 ++++++++++++------- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/Pester.RSpec.ps1 b/src/Pester.RSpec.ps1 index 339f8a691..bf83c9d18 100644 --- a/src/Pester.RSpec.ps1 +++ b/src/Pester.RSpec.ps1 @@ -363,10 +363,10 @@ function New-PesterConfiguration { CoveragePercentTarget: Target percent of code coverage that you want to achieve, default 75%. Default value: 75 - UseBreakpoints: EXPERIMENTAL: When false, use Profiler based tracer to do CodeCoverage instead of using breakpoints. - Default value: $true + UseBreakpoints: When false, use Profiler based tracer to do CodeCoverage instead of using breakpoints. + Default value: $false - SingleHitBreakpoints: Remove breakpoint when it is hit. + SingleHitBreakpoints: Remove breakpoint when it is hit. This increases performance of breakpoint based CodeCoverage. Default value: $true TestResult: diff --git a/src/csharp/Pester/CodeCoverageConfiguration.cs b/src/csharp/Pester/CodeCoverageConfiguration.cs index ffcc3e10f..04ccb6078 100644 --- a/src/csharp/Pester/CodeCoverageConfiguration.cs +++ b/src/csharp/Pester/CodeCoverageConfiguration.cs @@ -47,9 +47,9 @@ public CodeCoverageConfiguration() : base("CodeCoverage configuration.") Path = new StringArrayOption("Directories or files to be used for code coverage, by default the Path(s) from general settings are used, unless overridden here.", new string[0]); ExcludeTests = new BoolOption("Exclude tests from code coverage. This uses the TestFilter from general configuration.", true); RecursePaths = new BoolOption("Will recurse through directories in the Path option.", true); - UseBreakpoints = new BoolOption("EXPERIMENTAL: When false, use Profiler based tracer to do CodeCoverage instead of using breakpoints.", true); + UseBreakpoints = new BoolOption("When false, use Profiler based tracer to do CodeCoverage instead of using breakpoints.", false); CoveragePercentTarget = new DecimalOption("Target percent of code coverage that you want to achieve, default 75%.", 75m); - SingleHitBreakpoints = new BoolOption("Remove breakpoint when it is hit.", true); + SingleHitBreakpoints = new BoolOption("Remove breakpoint when it is hit. This increases performance of breakpoint based CodeCoverage.", true); } public CodeCoverageConfiguration(IDictionary configuration) : this() diff --git a/src/functions/Coverage.ps1 b/src/functions/Coverage.ps1 index 275aca881..439ec650a 100644 --- a/src/functions/Coverage.ps1 +++ b/src/functions/Coverage.ps1 @@ -4,7 +4,7 @@ [object[]] $CodeCoverage, [ScriptBlock] $Logger, [bool] $UseSingleHitBreakpoints = $true, - [bool] $UseBreakpoints = $true + [bool] $UseBreakpoints = $false ) if ($null -ne $logger) { diff --git a/tst/functions/Coverage.Tests.ps1 b/tst/functions/Coverage.Tests.ps1 index 3a41303a6..ae6b9edd3 100644 --- a/tst/functions/Coverage.Tests.ps1 +++ b/tst/functions/Coverage.Tests.ps1 @@ -495,7 +495,7 @@ InPesterModuleScope { @{ UseBreakpoints = $false; Description = "Profiler based cc" } ) { BeforeAll { - $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; Function = 'FunctionTwo' } + $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; Function = 'FunctionTwo' } -UseBreakpoints $UseBreakpoints @($breakpoints).Count | Should -Be 1 -Because "it has the proper number of breakpoints defined" if ($UseBreakpoints) { @@ -543,7 +543,7 @@ InPesterModuleScope { ) { BeforeAll { - $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; Function = 'FunctionOne' } + $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; Function = 'FunctionOne' } -UseBreakpoints $UseBreakpoints @($breakpoints).Count | Should -Be 9 -Because "it has the proper number of breakpoints defined" @@ -592,7 +592,7 @@ InPesterModuleScope { ) { BeforeAll { - $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; StartLine = 11; EndLine = 12 } + $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; StartLine = 11; EndLine = 12 } -UseBreakpoints $UseBreakpoints @($breakpoints).Count | Should -Be 2 -Because 'it has the proper number of breakpoints defined' @@ -640,7 +640,7 @@ InPesterModuleScope { @{ UseBreakpoints = $false; Description = "Profiler based cc" } ) { BeforeAll { - $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = "$(Join-Path -Path $root -ChildPath *.ps1)"; Function = '*' } + $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = "$(Join-Path -Path $root -ChildPath *.ps1)"; Function = '*' } -UseBreakpoints $UseBreakpoints @($breakpoints).Count | Should -Be 13 -Because 'it has the proper number of breakpoints defined' @@ -700,7 +700,7 @@ InPesterModuleScope { ) { BeforeAll { - $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; Class = 'MyClass' } + $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; Class = 'MyClass' } -UseBreakpoints $UseBreakpoints @($breakpoints).Count | Should -Be 3 -Because 'it has the proper number of breakpoints defined' @@ -745,7 +745,7 @@ InPesterModuleScope { ) { BeforeAll { - $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; Class = '*' } + $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; Class = '*' } -UseBreakpoints $UseBreakpoints @($breakpoints).Count | Should -Be 3 -Because 'it has the proper number of breakpoints defined' @@ -790,12 +790,20 @@ InPesterModuleScope { ) { BeforeAll { - $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; Class = 'MyClass'; Function = 'MethodTwo' } + $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{Path = $testScriptPath; Class = 'MyClass'; Function = 'MethodTwo' } -UseBreakpoints $UseBreakpoints @($breakpoints).Count | Should -Be 1 -Because 'it has the proper number of breakpoints defined' - $null = & $testScriptPath - $coverageReport = Get-CoverageReport -CommandCoverage $breakpoints + if ($UseBreakpoints) { + & $testScriptPath + } + else { + $patched, $tracer = Start-TraceScript $breakpoints + try { & $testScriptPath } finally { Stop-TraceScript -Patched $patched } + $measure = $tracer.Hits + } + + $coverageReport = Get-CoverageReport -CommandCoverage $breakpoints -Measure $measure } It 'Reports the proper number of executed commands' { @@ -828,7 +836,7 @@ InPesterModuleScope { ) { BeforeAll { - $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{ Path = $testScriptPath; Class = 'MyClass' } + $breakpoints = Enter-CoverageAnalysis -CodeCoverage @{ Path = $testScriptPath; Class = 'MyClass' } -UseBreakpoints $UseBreakpoints @($breakpoints).Count | Should -Be 0 -Because 'it has the proper number of breakpoints defined'