diff --git a/tests/Help.tests.ps1 b/tests/Help.tests.ps1 index a6f3f46..836f1c9 100644 --- a/tests/Help.tests.ps1 +++ b/tests/Help.tests.ps1 @@ -72,7 +72,7 @@ BeforeDiscovery { # the values it needs (BHPSModuleManifest, BHProjectName) — when running # via ./build.ps1 this happens before psake; running tests in isolation # bypasses that, so we do it here. - Set-BuildEnvironment -Path (Split-Path -Parent $PSScriptRoot) -Force + Set-BuildEnvironment -Path (Split-Path -Path $PSScriptRoot -Parent) -Force $buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1' $invokePsakeParameters = @{ TaskList = 'Build' @@ -82,10 +82,10 @@ BeforeDiscovery { } # PowerShellBuild outputs to Output///, override BHBuildOutput - $projectRoot = Split-Path -Parent $PSScriptRoot - $sourceManifest = Join-Path $projectRoot "$Env:BHProjectName/$Env:BHProjectName.psd1" - $moduleVersion = (Import-PowerShellDataFile -Path $sourceManifest).ModuleVersion - $Env:BHBuildOutput = Join-Path $projectRoot "Output/$Env:BHProjectName/$moduleVersion" + $projectRoot = Split-Path -Path $PSScriptRoot -Parent + $sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1" + $moduleVersion = (Import-PowerShellDataFile $sourceManifest).ModuleVersion + $Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion" # Define the path to the module manifest $moduleManifestFilename = $Env:BHProjectName + '.psd1' @@ -104,7 +104,7 @@ BeforeDiscovery { # Remove all versions of the module from the session. Pester can't handle multiple versions. Get-Module $Env:BHProjectName | Remove-Module -Force -ErrorAction 'Ignore' - Import-Module -Name $moduleManifestPath -Verbose:$false -ErrorAction 'Stop' + Import-Module $moduleManifestPath -Verbose:$false -ErrorAction 'Stop' # Get module commands $getCommandParameters = @{ @@ -130,7 +130,7 @@ BeforeAll { # the values it needs (BHPSModuleManifest, BHProjectName) — when running # via ./build.ps1 this happens before psake; running tests in isolation # bypasses that, so we do it here. - Set-BuildEnvironment -Path (Split-Path -Parent $PSScriptRoot) -Force + Set-BuildEnvironment -Path (Split-Path -Path $PSScriptRoot -Parent) -Force $buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1' $invokePsakeParameters = @{ TaskList = 'Build' @@ -148,10 +148,10 @@ Describe "Test help for <_.Name>" -ForEach $commands { # -ForEach, which Pester evaluates during discovery (before BeforeAll runs). $command = $_ $commandName = $command.Name - $commandHelp = Get-Help -Name $command.Name -ErrorAction 'SilentlyContinue' - $commandParameters = global:FilterOutCommonParameters -Parameters $command.ParameterSets.Parameters + $commandHelp = Get-Help $command.Name -ErrorAction 'SilentlyContinue' + $commandParameters = global:FilterOutCommonParameters $command.ParameterSets.Parameters $commandParameterNames = $commandParameters.Name - $helpParameters = global:FilterOutCommonParameters -Parameters $commandHelp.Parameters.Parameter + $helpParameters = global:FilterOutCommonParameters $commandHelp.Parameters.Parameter $helpParameterNames = $helpParameters.Name $helpLinks = $commandHelp.relatedLinks.navigationLink.uri | Where-Object { $_ -match '^https?://' } } @@ -160,10 +160,10 @@ Describe "Test help for <_.Name>" -ForEach $commands { # These variables are needed in both discovery and test phases so we need to duplicate them here $command = $_ $commandName = $_.Name - $commandHelp = Get-Help -Name $command.Name -ErrorAction 'SilentlyContinue' - $commandParameters = global:FilterOutCommonParameters -Parameters $command.ParameterSets.Parameters + $commandHelp = Get-Help $command.Name -ErrorAction 'SilentlyContinue' + $commandParameters = global:FilterOutCommonParameters $command.ParameterSets.Parameters $commandParameterNames = $commandParameters.Name - $helpParameters = global:FilterOutCommonParameters -Parameters $commandHelp.Parameters.Parameter + $helpParameters = global:FilterOutCommonParameters $commandHelp.Parameters.Parameter $helpParameterNames = $helpParameters.Name } @@ -217,8 +217,20 @@ Describe "Test help for <_.Name>" -ForEach $commands { # Required value in Help should match IsMandatory property of parameter It 'Has correct [mandatory] value' { - $codeMandatory = $_.IsMandatory.toString() - $parameterHelp.Required | Should -Be $codeMandatory + # Skip parameters that have different mandatory status across parameter sets + $parameterSetsWithParam = $command.ParameterSets | Where-Object { $_.Parameters.Name -contains $parameterName } + $mandatoryValues = $parameterSetsWithParam | ForEach-Object { + ($_.Parameters | Where-Object { $_.Name -eq $parameterName }).IsMandatory + } | Sort-Object -Unique + + if ($mandatoryValues.Count -gt 1) { + Set-ItResult -Skipped -Because "Parameter '$parameterName' has varying mandatory status across parameter sets" + return + } + + $codeMandatory = $_.IsMandatory.toString().ToLower() + $helpRequired = $parameterHelp.Required.ToLower() + $helpRequired | Should -Be $codeMandatory } # Parameter type in help should match code diff --git a/tests/Meta.tests.ps1 b/tests/Meta.tests.ps1 index 68298f0..c3addf4 100644 --- a/tests/Meta.tests.ps1 +++ b/tests/Meta.tests.ps1 @@ -2,7 +2,7 @@ BeforeAll { Set-StrictMode -Version 'Latest' # Make sure MetaFixers.psm1 is loaded - it contains Get-TextFilesList - Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'MetaFixers.psm1') -Verbose:$false -Force + Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'MetaFixers.psm1') -Verbose:$false -Force $projectRoot = $Env:BHProjectPath if (-not $projectRoot) { @@ -24,7 +24,7 @@ BeforeAll { $unicodeFilesCount | Should -Be 0 $fileName = $textFile.FullName - (Get-Content $fileName -Raw) | Select-String "`t" | Foreach-Object { + (Get-Content -Path $fileName -Raw) | Select-String "`t" | Foreach-Object { Write-Warning ( "There are tabs in $fileName." + ' Use Fixer "Get-TextFilesList `$pwd | ConvertTo-SpaceIndentation".' diff --git a/tests/MetaFixers.psm1 b/tests/MetaFixers.psm1 index 7dec0c8..5c614df 100644 --- a/tests/MetaFixers.psm1 +++ b/tests/MetaFixers.psm1 @@ -27,6 +27,7 @@ function ConvertTo-UTF8 { [OutputType([void])] param( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [ValidateNotNull()] [System.IO.FileInfo]$FileInfo ) @@ -56,6 +57,7 @@ function ConvertTo-SpaceIndentation { [OutputType([void])] param( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [ValidateNotNull()] [System.IO.FileInfo]$FileInfo ) @@ -85,6 +87,7 @@ function Get-TextFilesList { [OutputType([System.IO.FileInfo])] param( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [ValidateNotNullOrEmpty()] [string]$Root ) @@ -159,6 +162,7 @@ function Get-UnicodeFilesList { [OutputType([System.IO.FileInfo])] param( [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] [string]$Root )