Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using same data with two or more containers fail #2073

Closed
johlju opened this issue Sep 5, 2021 · 3 comments · Fixed by #2302
Closed

Using same data with two or more containers fail #2073

johlju opened this issue Sep 5, 2021 · 3 comments · Fixed by #2302
Assignees
Milestone

Comments

@johlju
Copy link
Contributor

johlju commented Sep 5, 2021

General summary of the issue

Using the same data hashtable for tests script files that contains parameters, and is using a parameter with [Parameter(ValueFromRemainingArguments = $true)], fails.

This might have been introduced in Pester v5.3.0 because we are seeing this in the pipeline today, and have not seen it before.
I wondering if this is a bug in Pester (or can be fixed in Pester), or if this is by design, then I must use the workaround (see possible solution below) in our pipeline.

The error is thrown here:

& $sb $Path $Data

Describe your environment

Pester version     : 5.3.0 C:\source\SqlServerDsc\output\RequiredModules\Pester\5.3.0\Pester.psm1
PowerShell version : 7.1.4
OS version         : Microsoft Windows NT 10.0.19043.0

Steps to reproduce

1. Create test script file

Save the test script file ScriptParameterIssue.Tests.ps1:

param
(
    $Param1,
    $Param2,

    [Parameter(ValueFromRemainingArguments = $true)]
    $Args
)

Describe 'Test script parameter' {
    It 'Should do something' {
        $true | Should -BeTrue
    }
}

2. Run the test script

Run the following code, it will throw an error:

$pesterData = @{
    Param1        = '1'
    Param2        = '2'
    Param3        = '3'
}

$pesterConfiguration = [PesterConfiguration]::Default
$pesterConfiguration.Run.Container = @(
    New-PesterContainer -Path ScriptParameterIssue.Tests.ps1 -Data $pesterData
    New-PesterContainer -Path ScriptParameterIssue.Tests.ps1 -Data $pesterData
)

Invoke-Pester -Configuration $pesterConfiguration

$pesterConfiguration.Run.Container.Value[0].Data

Expected Behavior

Should not throw an error.

Current Behavior

Throws an error saying:

[-] Discovery in C:\source\SqlServerDsc\ScriptParameterIssue.Tests.ps1 failed with:
System.Management.Automation.ParameterBindingException: A parameter cannot be found that matches parameter name 'Param3'

Possible Solution? (optional)

The problem seems to be that Pester is changing the content of the container data hashtable, adding a new property Args, when discovery run on the first test. Then the second script file gets the wrong data since the data property for all containers point to the same hashtable.

A workaround is to make sure to clone the hashtable containing the data when passing it to New-PesterContainer.

$pesterConfiguration.Run.Container = @(
    New-PesterContainer -Path ScriptParameterIssue.Tests.ps1 -Data $pesterData.Clone()
    New-PesterContainer -Path ScriptParameterIssue.Tests.ps1 -Data $pesterData.Clone()
)
@nohwnd
Copy link
Member

nohwnd commented Sep 6, 2021

Ah cool. I thought we are cloning the data on input.

@fflaten
Copy link
Collaborator

fflaten commented Sep 6, 2021

We do when processing foreach/testcases, but I probably forgot with container data in #1986.

@nohwnd nohwnd self-assigned this Sep 13, 2021
@nohwnd
Copy link
Member

nohwnd commented Sep 13, 2021

I am fixing this, the same way we fixed the others. Shallow copy, to avoid the backwrites into the hashtable. But not full clone to avoid unnecessary perf hit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants