Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Call operator not detected for code coverage

Open KreizIT opened this issue 6 years ago • 2 comments

1. General summary of the issue

Line where call operator is used is reported as not covered by Pester's codeCoverage capabilty. ie : $scriptCmd = {& $wrappedCmd @PSBoundParameters } Code coverage report : File Function Line Command


ProxyCmd.ps1 Write-TrueHost 29 $scriptCmd = {& $wrappedCmd @PSBoundParameters } ProxyCmd.ps1 Write-TrueHost 29 & $wrappedCmd @PSBoundParameters

2. Describe Your Environment

Pester version : 4.4.2 C:\Program Files\WindowsPowerShell\Modules\Pester\4.4.2\Pester.psm1 PowerShell version : 5.1.14409.1005 OS version : Microsoft Windows NT 6.1.7601 Service Pack 1

3. Expected Behavior

Report this line as covered

4.Current Behavior

Missing coverage

5. Possible Solution

Tag or mark a line as "excluded" from coverage report ?

6. Context

I'm trying to get a 100% covered code but got only 98% cause of this line :-/

Thanks for help or advise ;)

KreizIT avatar Nov 05 '18 15:11 KreizIT

Full code :

function Write-TrueHost {
	[CmdletBinding(HelpUri='http://go.microsoft.com/fwlink/?LinkID=113426', RemotingCapability='None')]
	param(
		[Parameter(Position=0, ValueFromPipeline=$true, ValueFromRemainingArguments=$true)]
		[System.Object]
		${Object},

		[switch]
		${NoNewline},

		[System.Object]
		${Separator},

		[System.ConsoleColor]
		${ForegroundColor},

		[System.ConsoleColor]
		${BackgroundColor})

	begin
	{
		try {
			$outBuffer = $null
			if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
			{
				$PSBoundParameters['OutBuffer'] = 1
			}
			$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Write-Host', [System.Management.Automation.CommandTypes]::Cmdlet)
			$scriptCmd = {& $wrappedCmd @PSBoundParameters }
			$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
			$steppablePipeline.Begin($PSCmdlet)
		} catch {
			throw
		}
	}

	process
	{
		try {
			$steppablePipeline.Process($_)
		} catch {
			throw
		}
	}

	end
	{
		try {
			$steppablePipeline.End()
		} catch {
			throw
		}
	}
}

Describe "Write-TrueHost" {

    It "Sould write to host" {
        $message = Write-TrueHost "Mocked" 6>&1
        $message | Should -BeExactly "Mocked"
    }

    It "Sould buffering to host" {
        $res= (0..10| Write-TrueHost -OutBuffer 5 6>&1)
        $res.count | Should -be 11
    }

}

KreizIT avatar Nov 05 '18 15:11 KreizIT

@KreizIT Thans for the issue, the code coverage works by setting breakpoints, so it's possible that we either missed it or the breakpoint cannot be set there. Will review this.

nohwnd avatar Jan 08 '19 08:01 nohwnd