Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Code Coverage Incorrect for Parameter Default When Using New Profiler

Open wethreetrees opened this issue 2 years ago • 3 comments

General summary of the issue

When using the new code coverage profiler ($PesterPreference.CodeCoverage.UseBreakpoints = $false), Pester is reporting missing commands for parameter default value expressions.

ex:

[Int]$Temperature = (Get-Random -Maximum 100 -Minimum 0)

This is working, as expected, when using breakpoints ($PesterPreference.CodeCoverage.UseBreakpoints = $true)

Describe your environment

Pester version : 5.3.1 C:\Client\PowerShell\Modules\Pester\5.3.1\Pester.psm1 PowerShell version : 7.2.1 OS version : Microsoft Windows NT 10.0.19042.0

Steps to reproduce

  1. Download and extract the PesterCCProfilerIssue.zip
  2. From the target directory, run:
.\Invoke-Tests.ps1

Expected Behavior

Should return no missing commands with 100% code coverage.

Current Behavior

Returns:

Covered 75% / 75%. 4 analyzed Commands in 1 File.
 Missed command:
 
File            Class Function        Line Command
----            ----- --------        ---- -------
TestModule.psm1       Get-Temperature    6 Get-Random -Maximum 100 -Minimum 0

Possible Solution? (optional)

😕

wethreetrees avatar Dec 20 '21 19:12 wethreetrees

Thanks for the report. I can confirm the issue.

The following lines might indicate it's a known limitation because we couldn't register a hit for parameter values either way. https://github.com/pester/Pester/blob/13d65958ae6dc33bb18824b6acb48509f11b6784/src/functions/Coverage.ps1#L1213-L1218

CC debug of Get-Temperature (notice that the param block isn't hit).

PS /workspaces/Pester/Samples/PesterCCProfilerIssue> 
DBG: 9+     end  >>>> {
EXP: /workspaces/Pester/Samples/PesterCCProfilerIssue/TestModule.psm1:9:9:{
DBG: 10+         if ( >>>> $Temperature -gt 50) {
EXP: /workspaces/Pester/Samples/PesterCCProfilerIssue/TestModule.psm1:10:13:$Temperature -gt 50
DBG: 13+         return  >>>> 'Thats Cold'
EXP: /workspaces/Pester/Samples/PesterCCProfilerIssue/TestModule.psm1:13:16:'Thats Cold'
DBG: 14+      >>>> }
EXP: /workspaces/Pester/Samples/PesterCCProfilerIssue/TestModule.psm1:14:5:}

However if I use Set-PSDebug -Trace 1 (the same events the profiler hooks into) it shows line 6 being called when default parameter value is used:

> Get-Temperature -Temperature 1
DEBUG:    1+  >>>> Get-Temperature -Temperature 1
DEBUG:    9+     end  >>>> {
DEBUG:   10+         if ( >>>> $Temperature -gt 50) {
DEBUG:   13+         return  >>>> 'Thats Cold'
Thats Cold
DEBUG:   14+      >>>> }

> Get-Temperature
DEBUG:    1+  >>>> Get-Temperature
** DEBUG:    6+         [Int]$Temperature =  >>>> (Get-Random -Maximum 100 -Minimum 0) **
DEBUG:    9+     end  >>>> {**
DEBUG:   10+         if ( >>>> $Temperature -gt 50) {
DEBUG:   13+         return  >>>> 'Thats Cold'
Thats Cold
DEBUG:   14+      >>>> }

@nohwnd, do you remember any of this? If powershell traces line 6, shouldn't we get it too? Are we excluding it for some reason?

fflaten avatar Dec 21 '21 00:12 fflaten

Oh, I see it. The message show the parameter value being calculated, but the extent we're able to get at that point is just the reference/call to the function.

image

fflaten avatar Dec 21 '21 11:12 fflaten

Yeah I think this was one of the limitations, there is a test that compares old cc with new, and comments on what we can do better than the previous CC and what we can do worse. This is probably one of the (few) things we do worse.

https://github.com/pester/Pester/blob/main/tst/CoverageTestFile.ps1 https://github.com/pester/Pester/blob/main/tst/Pester.RSpec.Coverage.ts.ps1#L30

nohwnd avatar Apr 22 '22 20:04 nohwnd