Skip to content

Commit 025e417

Browse files
committed
style: align test scaffolding with canonical PSMTplt
Apply the three style rules from PowerShellModuleTemplate#23 to the Help/Meta/MetaFixers test files this repo inherited from the template: - Named parameters on multi-arg cmdlet calls (Split-Path, Join-Path, Test-Path, Get-Module, Get-Content, Select-String, Get-TextFilesList, Test-FileUnicode) - ValidateNotNull / ValidateNotNullOrEmpty validators on mandatory param entries in tests/MetaFixers.psm1 - Test-FileUnicode call inside Get-UnicodeFilesList now uses named -FileInfo parameter Also restores the PR #23-era multi-paramset mandatory-skip block on the 'Has correct [mandatory] value' It in Help.tests.ps1 (this repo was carrying the pre-#23 version that doesn't handle parameters with varying IsMandatory status across parameter sets), and fixes a stale $parameterNames reference in the last Context block (should be $commandParameterNames). Both bundled in per the cross-repo audit. tests/Manifest.tests.ps1 is the older Module Dependency variant (no Test-VersionConstraint helper, -Child typo on Join-Path) and is deferred to a separate uplift PR. Behavior is unchanged for parameters with consistent IsMandatory across sets; previously-failing-or-flaky tests on multi-paramset parameters will now skip with a clear reason.
1 parent a0611f7 commit 025e417

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

tests/Help.tests.ps1

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ BeforeDiscovery {
7272
# the values it needs (BHPSModuleManifest, BHProjectName) — when running
7373
# via ./build.ps1 this happens before psake; running tests in isolation
7474
# bypasses that, so we do it here.
75-
Set-BuildEnvironment -Path (Split-Path -Parent $PSScriptRoot) -Force
75+
Set-BuildEnvironment -Path (Split-Path -Path $PSScriptRoot -Parent) -Force
7676
$buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1'
7777
$invokePsakeParameters = @{
7878
TaskList = 'Build'
@@ -82,10 +82,10 @@ BeforeDiscovery {
8282
}
8383

8484
# PowerShellBuild outputs to Output/<ModuleName>/<Version>/, override BHBuildOutput
85-
$projectRoot = Split-Path -Parent $PSScriptRoot
86-
$sourceManifest = Join-Path $projectRoot "$Env:BHProjectName/$Env:BHProjectName.psd1"
85+
$projectRoot = Split-Path -Path $PSScriptRoot -Parent
86+
$sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1"
8787
$moduleVersion = (Import-PowerShellDataFile -Path $sourceManifest).ModuleVersion
88-
$Env:BHBuildOutput = Join-Path $projectRoot "Output/$Env:BHProjectName/$moduleVersion"
88+
$Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion"
8989

9090
# Define the path to the module manifest
9191
$moduleManifestFilename = $Env:BHProjectName + '.psd1'
@@ -97,18 +97,18 @@ BeforeDiscovery {
9797
'Classes'
9898
) | ForEach-Object {
9999
$path = Join-Path -Path $Env:BHBuildOutput -ChildPath $_
100-
if (Test-Path $path) {
100+
if (Test-Path -Path $path) {
101101
$global:CustomTypes += (Get-ChildItem -Path $path -Recurse -ErrorAction 'SilentlyContinue').BaseName
102102
}
103103
}
104104

105105
# Remove all versions of the module from the session. Pester can't handle multiple versions.
106-
Get-Module $Env:BHProjectName | Remove-Module -Force -ErrorAction 'Ignore'
106+
Get-Module -Name $Env:BHProjectName | Remove-Module -Force -ErrorAction 'Ignore'
107107
Import-Module -Name $moduleManifestPath -Verbose:$false -ErrorAction 'Stop'
108108

109109
# Get module commands
110110
$getCommandParameters = @{
111-
Module = (Get-Module $Env:BHProjectName)
111+
Module = (Get-Module -Name $Env:BHProjectName)
112112
CommandType = [System.Management.Automation.CommandTypes[]]'Cmdlet, Function' # Not alias
113113
}
114114
if ($PSVersionTable.PSVersion.Major -lt 6) {
@@ -130,7 +130,7 @@ BeforeAll {
130130
# the values it needs (BHPSModuleManifest, BHProjectName) — when running
131131
# via ./build.ps1 this happens before psake; running tests in isolation
132132
# bypasses that, so we do it here.
133-
Set-BuildEnvironment -Path (Split-Path -Parent $PSScriptRoot) -Force
133+
Set-BuildEnvironment -Path (Split-Path -Path $PSScriptRoot -Parent) -Force
134134
$buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1'
135135
$invokePsakeParameters = @{
136136
TaskList = 'Build'
@@ -212,8 +212,20 @@ Describe "Test help for <_.Name>" -ForEach $commands {
212212

213213
# Required value in Help should match IsMandatory property of parameter
214214
It 'Has correct [mandatory] value' {
215-
$codeMandatory = $_.IsMandatory.toString()
216-
$parameterHelp.Required | Should -Be $codeMandatory
215+
# Skip parameters that have different mandatory status across parameter sets
216+
$parameterSetsWithParam = $command.ParameterSets | Where-Object { $_.Parameters.Name -contains $parameterName }
217+
$mandatoryValues = $parameterSetsWithParam | ForEach-Object {
218+
($_.Parameters | Where-Object { $_.Name -eq $parameterName }).IsMandatory
219+
} | Sort-Object -Unique
220+
221+
if ($mandatoryValues.Count -gt 1) {
222+
Set-ItResult -Skipped -Because "Parameter '$parameterName' has varying mandatory status across parameter sets"
223+
return
224+
}
225+
226+
$codeMandatory = $_.IsMandatory.toString().ToLower()
227+
$helpRequired = $parameterHelp.Required.ToLower()
228+
$helpRequired | Should -Be $codeMandatory
217229
}
218230

219231
# Parameter type in help should match code
@@ -233,7 +245,7 @@ Describe "Test help for <_.Name>" -ForEach $commands {
233245

234246
# Shouldn't find extra parameters in help
235247
It 'finds help parameter in code: <_>' {
236-
$_ -in $parameterNames | Should -Be $true
248+
$_ -in $commandParameterNames | Should -Be $true
237249
}
238250
}
239251
}

tests/Meta.tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ BeforeAll {
99
$projectRoot = $PSScriptRoot
1010
}
1111

12-
$allTextFiles = Get-TextFilesList $projectRoot
12+
$allTextFiles = Get-TextFilesList -Root $projectRoot
1313
$unicodeFilesCount = 0
1414
$totalTabsCount = 0
1515
foreach ($textFile in $allTextFiles) {
16-
if (Test-FileUnicode $textFile) {
16+
if (Test-FileUnicode -FileInfo $textFile) {
1717
$unicodeFilesCount++
1818
Write-Warning (
1919
"File $($textFile.FullName) contains 0x00 bytes." +
@@ -24,7 +24,7 @@ BeforeAll {
2424
$unicodeFilesCount | Should -Be 0
2525

2626
$fileName = $textFile.FullName
27-
(Get-Content $fileName -Raw) | Select-String "`t" | Foreach-Object {
27+
(Get-Content -Path $fileName -Raw) | Select-String -Pattern "`t" | Foreach-Object {
2828
Write-Warning (
2929
"There are tabs in $fileName." +
3030
' Use Fixer "Get-TextFilesList `$pwd | ConvertTo-SpaceIndentation".'

tests/MetaFixers.psm1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function ConvertTo-UTF8 {
2727
[OutputType([void])]
2828
param(
2929
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
30+
[ValidateNotNull()]
3031
[System.IO.FileInfo]$FileInfo
3132
)
3233

@@ -56,6 +57,7 @@ function ConvertTo-SpaceIndentation {
5657
[OutputType([void])]
5758
param(
5859
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
60+
[ValidateNotNull()]
5961
[System.IO.FileInfo]$FileInfo
6062
)
6163

@@ -85,6 +87,7 @@ function Get-TextFilesList {
8587
[OutputType([System.IO.FileInfo])]
8688
param(
8789
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
90+
[ValidateNotNullOrEmpty()]
8891
[string]$Root
8992
)
9093

@@ -159,10 +162,11 @@ function Get-UnicodeFilesList {
159162
[OutputType([System.IO.FileInfo])]
160163
param(
161164
[Parameter(Mandatory = $true)]
165+
[ValidateNotNullOrEmpty()]
162166
[string]$Root
163167
)
164168

165169
$root | Get-TextFilesList | Where-Object {
166-
Test-FileUnicode $_
170+
Test-FileUnicode -FileInfo $_
167171
}
168172
}

0 commit comments

Comments
 (0)