Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Unable to pass `[System.Collections.IDictionary]` for `.CodeCoverage.Path`

Open johlju opened this issue 3 years ago • 6 comments
trafficstars

Checklist

What is the issue?

According to the source it should be possible to pass an [System.Collections.IDictionary] so that it is possible to only calculate code coverage on part of a source file.

https://github.com/pester/Pester/blob/f22150b08e0c71bc9213b7f6d20985fbefcc915f/src/functions/Coverage.ps1#L125

But since the Pester configuration object only takes a string array for the property CodeCoverage.Path it is not possible to use this functionality,

Expected Behavior

Possible to pass an array of [System.Collections.IDictionary] for the property CodeCoverage.Path. I think it even is supposed to be able to mix both [System.Collections.IDictionary] and [String]? šŸ¤”

Steps To Reproduce

$coverage1 = [System.Collections.IDictionary] @{
    Path = '/Users/johlju/source/Sampler/output/Sampler/0.113.0/Sampler.psm1'
    StartLine = 1
    EndLine = 100
    Function = 'Add-Sample'
}

$pesterConfig = New-PesterConfiguration -Hashtable @{
    Run = @{
        Path = './tests/Unit/Public/Add-Sample.tests.ps1'
    }
    CodeCoverage = @{
        Enabled = $true
        Path = @($coverage1)
        UseBreakpoints = $true
    }
    Output = @{
        Verbosity = 'Detailed'
    }
}

Invoke-Pester -Configuration $pesterConfig

Since I can't get passed setting the property in the pester configuration, I'm not sure if IDictionary need more of the properties:

https://github.com/pester/Pester/blob/f22150b08e0c71bc9213b7f6d20985fbefcc915f/src/functions/Coverage.ps1#L143-L151

Describe your environment

Pester version     : 5.3.3 /Users/johlju/source/Sampler/output/RequiredModules/Pester/5.3.3/Pester.psm1                 
PowerShell version : 7.2.4
OS version         : Unix 12.4.0

Possible Solution?

The Pester configuration option .CodeCoverage.Path needs to be a Tuple to be able to pass both [String] and [System.Collections.IDictionary]?

johlju avatar Jun 20 '22 20:06 johlju

Advanced configuration for CodeCoverage isn't possible in Pester v5 atm. Docs are currently outdated on this topic, but it's mentioned in a few hidden places

https://github.com/pester/Pester/blob/f22150b08e0c71bc9213b7f6d20985fbefcc915f/src/Main.ps1#L782-L786

and as a breaking change

The compatibility is not 100%, neither -Script not -CodeCoverage take hashtables, they just take a collection of paths.

I'll let @nohwnd answer what the future might bring šŸ™‚ Related #2145

fflaten avatar Jun 20 '22 22:06 fflaten

I am not expecting nor need -CodeCoverage to work as it is a legacy parameter. I’m talking about [PesterConfiguration] object in the issue above.

johlju avatar Jun 21 '22 11:06 johlju

I understood that. The point was, poorly communicated by me, that it's intentionally not supported/implemented yet AFAIK. At least for now. šŸ™‚

fflaten avatar Jun 21 '22 13:06 fflaten

@johlju why do you need to get cc for only part of a file? Is it too expensive to calculate it otherwise? Or you don't care about the rest of the file?

nohwnd avatar Jun 30 '22 07:06 nohwnd

In our modules we test the built modules (built using the Sampler-project), so we have one big .psm1 file. During development of, for example a function, I want to run the test for just that function and also see that the tests cover all the code. Currently I need to specify the entire .psm1 file for the coverage resulting in several hundred (or thousand) of lines. To find if there were any lines that were not covered I have to scroll through the code coverage output until I find (or not find) any lines that were not covered.

When I saw the functionality above my thought was that I could make a wrapper that fetched the start line and end line of the (specified) function being tested and then told Pester to only cover those lines.

johlju avatar Jun 30 '22 10:06 johlju

Fair enough.

nohwnd avatar Jun 30 '22 16:06 nohwnd