From 6c7ea9502f14fd0a0ac86e4b3b678ce3dbe37aa9 Mon Sep 17 00:00:00 2001 From: Trent Blackburn Date: Mon, 11 May 2026 23:49:00 -0400 Subject: [PATCH] style: align test scaffolding with canonical PSMTplt Apply the three style rules from PowerShellModuleTemplate#23 to the 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) - Quoted string literals where they were left bareword (one -ErrorAction Stop -> -ErrorAction 'Stop') - ValidateNotNull / ValidateNotNullOrEmpty validators on mandatory param entries in tests/MetaFixers.psm1 Tests/MetaFixers.psm1 was also missing the named -FileInfo parameter on the Test-FileUnicode call inside Get-UnicodeFilesList; aligned with canonical. Behavior is unchanged; this is the first-party-files compliance work PR #23 brought to the template, applied here. --- tests/Help.tests.ps1 | 16 ++++++++-------- tests/Manifest.tests.ps1 | 20 ++++++++++---------- tests/Meta.tests.ps1 | 6 +++--- tests/MetaFixers.psm1 | 6 +++++- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/tests/Help.tests.ps1 b/tests/Help.tests.ps1 index 2c6469e..a7ccdbb 100644 --- a/tests/Help.tests.ps1 +++ b/tests/Help.tests.ps1 @@ -52,7 +52,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' @@ -62,10 +62,10 @@ BeforeDiscovery { } # PowerShellBuild outputs to Output///, override BHBuildOutput - $projectRoot = Split-Path -Parent $PSScriptRoot - $sourceManifest = Join-Path $projectRoot "$Env:BHProjectName/$Env:BHProjectName.psd1" + $projectRoot = Split-Path -Path $PSScriptRoot -Parent + $sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1" $moduleVersion = (Import-PowerShellDataFile -Path $sourceManifest).ModuleVersion - $Env:BHBuildOutput = Join-Path $projectRoot "Output/$Env:BHProjectName/$moduleVersion" + $Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion" # Define the path to the module manifest $moduleManifestFilename = $Env:BHProjectName + '.psd1' @@ -77,18 +77,18 @@ BeforeDiscovery { 'Classes' ) | ForEach-Object { $path = Join-Path -Path $Env:BHBuildOutput -ChildPath $_ - if (Test-Path $path) { + if (Test-Path -Path $path) { $global:CustomTypes += (Get-ChildItem -Path $path -Recurse -ErrorAction 'SilentlyContinue').BaseName } } # Remove all versions of the module from the session. Pester can't handle multiple versions. - Get-Module $Env:BHProjectName | Remove-Module -Force -ErrorAction 'Ignore' + Get-Module -Name $Env:BHProjectName | Remove-Module -Force -ErrorAction 'Ignore' Import-Module -Name $moduleManifestPath -Verbose:$false -ErrorAction 'Stop' # Get module commands $getCommandParameters = @{ - Module = (Get-Module $Env:BHProjectName) + Module = (Get-Module -Name $Env:BHProjectName) CommandType = [System.Management.Automation.CommandTypes[]]'Cmdlet, Function' # Not alias } if ($PSVersionTable.PSVersion.Major -lt 6) { @@ -108,7 +108,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' diff --git a/tests/Manifest.tests.ps1 b/tests/Manifest.tests.ps1 index 718bd9c..aecf1ab 100644 --- a/tests/Manifest.tests.ps1 +++ b/tests/Manifest.tests.ps1 @@ -64,7 +64,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' @@ -74,10 +74,10 @@ BeforeDiscovery { } # PowerShellBuild outputs to Output///, override BHBuildOutput - $projectRoot = Split-Path -Parent $PSScriptRoot - $sourceManifest = Join-Path $projectRoot "$Env:BHProjectName/$Env:BHProjectName.psd1" + $projectRoot = Split-Path -Path $PSScriptRoot -Parent + $sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1" $moduleVersion = (Import-PowerShellDataFile -Path $sourceManifest).ModuleVersion - $Env:BHBuildOutput = Join-Path $projectRoot "Output/$Env:BHProjectName/$moduleVersion" + $Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion" # Define the path to the module manifest $moduleManifestFilename = $Env:BHProjectName + '.psd1' @@ -108,7 +108,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' @@ -118,10 +118,10 @@ BeforeAll { } # PowerShellBuild outputs to Output///, override BHBuildOutput - $projectRoot = Split-Path -Parent $PSScriptRoot - $sourceManifest = Join-Path $projectRoot "$Env:BHProjectName/$Env:BHProjectName.psd1" + $projectRoot = Split-Path -Path $PSScriptRoot -Parent + $sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1" $moduleVersion = (Import-PowerShellDataFile -Path $sourceManifest).ModuleVersion - $Env:BHBuildOutput = Join-Path $projectRoot "Output/$Env:BHProjectName/$moduleVersion" + $Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion" # Define the path to the module manifest $moduleManifestFilename = $Env:BHProjectName + '.psd1' @@ -145,12 +145,12 @@ BeforeAll { Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'ManifestHelpers.psm1') -Verbose:$false -Force $requirementsPath = Join-Path -Path $env:BHProjectPath -ChildPath 'requirements.psd1' - $requirements = Import-PowerShellDataFile -Path $requirementsPath -ErrorAction Stop + $requirements = Import-PowerShellDataFile -Path $requirementsPath -ErrorAction 'Stop' # Parse the version from the changelog $changelogPath = Join-Path -Path $Env:BHProjectPath -ChildPath 'CHANGELOG.md' $changelogVersionPattern = '^##\s\\?\[(?(\d+\.){1,3}\d+)\\?\]' # Matches on a line that starts with '## [Version]' or '## \[Version\]' - $changelogVersion = Get-Content $changelogPath | ForEach-Object { + $changelogVersion = Get-Content -Path $changelogPath | ForEach-Object { if ($_ -match $changelogVersionPattern) { $changelogVersion = $matches.Version break diff --git a/tests/Meta.tests.ps1 b/tests/Meta.tests.ps1 index 68298f0..c1813ec 100644 --- a/tests/Meta.tests.ps1 +++ b/tests/Meta.tests.ps1 @@ -9,11 +9,11 @@ BeforeAll { $projectRoot = $PSScriptRoot } - $allTextFiles = Get-TextFilesList $projectRoot + $allTextFiles = Get-TextFilesList -Root $projectRoot $unicodeFilesCount = 0 $totalTabsCount = 0 foreach ($textFile in $allTextFiles) { - if (Test-FileUnicode $textFile) { + if (Test-FileUnicode -FileInfo $textFile) { $unicodeFilesCount++ Write-Warning ( "File $($textFile.FullName) contains 0x00 bytes." + @@ -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 -Pattern "`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..43da225 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,10 +162,11 @@ function Get-UnicodeFilesList { [OutputType([System.IO.FileInfo])] param( [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] [string]$Root ) $root | Get-TextFilesList | Where-Object { - Test-FileUnicode $_ + Test-FileUnicode -FileInfo $_ } }