Skip to content

Commit fdc19ff

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 fixes a stale $parameterNames reference in the last Context block of Help.tests.ps1 (should be $commandParameterNames). Bug 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.
1 parent b7cfa16 commit fdc19ff

3 files changed

Lines changed: 17 additions & 13 deletions

File tree

tests/Help.tests.ps1

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

8585
# PowerShellBuild outputs to Output/<ModuleName>/<Version>/, override BHBuildOutput
86-
$projectRoot = Split-Path -Parent $PSScriptRoot
87-
$sourceManifest = Join-Path $projectRoot "$Env:BHProjectName/$Env:BHProjectName.psd1"
86+
$projectRoot = Split-Path -Path $PSScriptRoot -Parent
87+
$sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1"
8888
$moduleVersion = (Import-PowerShellDataFile -Path $sourceManifest).ModuleVersion
89-
$Env:BHBuildOutput = Join-Path $projectRoot "Output/$Env:BHProjectName/$moduleVersion"
89+
$Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion"
9090

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

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

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

249249
# Shouldn't find extra parameters in help
250250
It 'finds help parameter in code: <_>' {
251-
$_ -in $parameterNames | Should -Be $true
251+
$_ -in $commandParameterNames | Should -Be $true
252252
}
253253
}
254254
}

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)