Skip to content

Instantly share code, notes, and snippets.

@juneb
Last active June 16, 2016 15:46
Show Gist options
  • Save juneb/1d248d773576f5ec5fd21d901c0f8736 to your computer and use it in GitHub Desktop.
Save juneb/1d248d773576f5ec5fd21d901c0f8736 to your computer and use it in GitHub Desktop.
Tests the Path hashtable feature of Pester 3.4.0. This script was used as a bug repro.
<#
.SYNOPSIS
Tests the Path hashtable feature of Pester 3.4.0.
.DESCRIPTION
Demonstrates that Invoke-Pester with a Path hashtable:
Invoke-Pester -Script @{Path=<path>}
does not get default values set for script parameters.
In this case, with a $ModuleName parameter, the error is generated for line:
-- ValidationScript that runs Get-Module (Line 59)
-or-
-- Get-Module call (Line 69)
If both of these lines are commented out, the script runs w/o error,
but the test for the parameter value fails.
Error:
[-] Error occurred in test script 'C:\ps-test\PesterHelp\InvokePesterPathHashtable.Tests.ps1' 206ms
ValidationMetadataException: The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
ParameterBindingValidationException: Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the
command again.
at <ScriptBlock>, C:\ps-test\PesterHelp\InvokePesterPathHashtable.Tests.ps1: line 26
at <ScriptBlock>, C:\Users\JuneBlender\Documents\WindowsPowerShell\Modules\Pester\3.4.0\Pester.psm1: line 279
at Invoke-Pester, C:\Users\JuneBlender\Documents\WindowsPowerShell\Modules\Pester\3.4.0\Pester.psm1: line 292
at <ScriptBlock>, C:\ps-test\Test-PesterScriptParameter.ps1: line 22
at <ScriptBlock>, <No file>: line 1
The error occurs because this invocation:
& $Path @Parameters @Arguments
contains the Path that is specified in the hashtable, but does not get the default parameter value.
This script runs without error and all tests pass in the following conditions:
-- Run it as a .ps1
.\InvokePesterPathHashtable.Tests.ps1
-- Use a path string as the value of the Script parameter:
Invoke-Pester -Script .\InvokePesterPathHashtable.Tests.ps1
But, the error occurs when you run it with a Path hashtable.
Invoke-Pester -Script @{Path = ".\InvokePesterPathHashtable.Tests.ps1"}
#>
Param
(
[Parameter(Mandatory = $false)]
[ValidateScript({ Get-Module -ListAvailable -Name $_ })]
[string]
$ModuleName = 'PSScriptAnalyzer',
[Parameter(Mandatory = $false)]
[System.Version]
$RequiredVersion
)
Write-Host "Module name is: $ModuleName"
$null = Get-Module -ListAvailable -Name $ModuleName
Describe "Testing Invoke-Pester with path hashtable" {
It "verifies that the moduleName isn't null" {
$ModuleName | Should Not BeNullOrEmpty
}
}
@dlwyatt
Copy link

dlwyatt commented May 26, 2016

Hrm, weird. The bug was that a variable that was supposed to be a hashtable would up being $null in this case, and apparently if you try to splat $Null to a function, it just gets passed in as a positional argument instead of splatting nothing.

Easily fixed, but I had no idea that @null behaved that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment