Skip to content

Commit c2e23d1

Browse files
committed
fix(Publish-PSBuildModule, Test-PSBuildPester): ✨ Improve parameter handling and suppress warnings
* Added suppression attributes for unused parameters in `Publish-PSBuildModule` and `Test-PSBuildPester`. * Enhanced `CmdletBinding` attribute casing for consistency. * Refined parameter validation logic for better clarity and maintainability.
1 parent 7791597 commit c2e23d1

File tree

3 files changed

+44
-30
lines changed

3 files changed

+44
-30
lines changed

PowerShellBuild/Public/Publish-PSBuildModule.ps1

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
function Publish-PSBuildModule {
2+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute(
3+
'PSReviewUnusedParameter',
4+
'',
5+
Justification = 'Both Credential and NuGetApiKey are used just not via explicit variable call.'
6+
)]
27
<#
38
.SYNOPSIS
49
Publishes a module to the defined PowerShell repository.
@@ -27,18 +32,18 @@ function Publish-PSBuildModule {
2732
2833
Publish version 0.1.0 of the module at path .\Output\0.1.0\MyModule to the PSGallery repository using an API key and a PowerShell credential.
2934
#>
30-
[cmdletbinding(DefaultParameterSetName = 'ApiKey')]
35+
[CmdletBinding(DefaultParameterSetName = 'ApiKey')]
3136
param(
3237
[parameter(Mandatory)]
3338
[ValidateScript({
34-
if (-not (Test-Path -Path $_ )) {
35-
throw 'Folder does not exist'
36-
}
37-
if (-not (Test-Path -Path $_ -PathType Container)) {
38-
throw 'The Path argument must be a folder. File paths are not allowed.'
39-
}
40-
$true
41-
})]
39+
if (-not (Test-Path -Path $_ )) {
40+
throw 'Folder does not exist'
41+
}
42+
if (-not (Test-Path -Path $_ -PathType Container)) {
43+
throw 'The Path argument must be a folder. File paths are not allowed.'
44+
}
45+
$true
46+
})]
4247
[System.IO.FileInfo]$Path,
4348

4449
[parameter(Mandatory)]

PowerShellBuild/Public/Test-PSBuildPester.ps1

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
function Test-PSBuildPester {
2+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute(
3+
'PSReviewUnusedParameter',
4+
'CodeCoverageThreshold',
5+
Justification = 'Used inside a foreach method call.'
6+
)]
27
<#
38
.SYNOPSIS
49
Execute Pester tests for module.
@@ -27,11 +32,11 @@ function Test-PSBuildPester {
2732
.PARAMETER ImportModule
2833
Import module from OutDir prior to running Pester tests.
2934
.EXAMPLE
30-
PS> Test-PSBuildPester -Path ./tests -ModuleName Mymodule -OutputPath ./out/testResults.xml
35+
PS> Test-PSBuildPester -Path ./tests -ModuleName MyModule -OutputPath ./out/testResults.xml
3136
3237
Run Pester tests in ./tests and save results to ./out/testResults.xml
3338
#>
34-
[cmdletbinding()]
39+
[CmdletBinding()]
3540
param(
3641
[parameter(Mandatory)]
3742
[string]$Path,
@@ -76,18 +81,18 @@ function Test-PSBuildPester {
7681

7782
Import-Module Pester -MinimumVersion 5.0.0
7883
$configuration = [PesterConfiguration]::Default
79-
$configuration.Output.Verbosity = 'Detailed'
80-
$configuration.Run.PassThru = $true
81-
$configuration.TestResult.Enabled = -not [string]::IsNullOrEmpty($OutputPath)
82-
$configuration.TestResult.OutputPath = $OutputPath
84+
$configuration.Output.Verbosity = 'Detailed'
85+
$configuration.Run.PassThru = $true
86+
$configuration.TestResult.Enabled = -not [string]::IsNullOrEmpty($OutputPath)
87+
$configuration.TestResult.OutputPath = $OutputPath
8388
$configuration.TestResult.OutputFormat = $OutputFormat
8489

8590
if ($CodeCoverage.IsPresent) {
8691
$configuration.CodeCoverage.Enabled = $true
8792
if ($CodeCoverageFiles.Count -gt 0) {
8893
$configuration.CodeCoverage.Path = $CodeCoverageFiles
8994
}
90-
$configuration.CodeCoverage.OutputPath = $CodeCoverageOutputFile
95+
$configuration.CodeCoverage.OutputPath = $CodeCoverageOutputFile
9196
$configuration.CodeCoverage.OutputFormat = $CodeCoverageOutputFileFormat
9297
}
9398

@@ -103,25 +108,25 @@ function Test-PSBuildPester {
103108
$textInfo = (Get-Culture).TextInfo
104109
[xml]$testCoverage = Get-Content $CodeCoverageOutputFile
105110
$ccReport = $testCoverage.report.counter.ForEach({
106-
$total = [int]$_.missed + [int]$_.covered
107-
$perc = [Math]::Truncate([int]$_.covered / $total)
108-
[pscustomobject]@{
109-
name = $textInfo.ToTitleCase($_.Type.ToLower())
110-
percent = $perc
111-
}
112-
})
111+
$total = [int]$_.missed + [int]$_.covered
112+
$percent = [Math]::Truncate([int]$_.covered / $total)
113+
[PSCustomObject]@{
114+
name = $textInfo.ToTitleCase($_.Type.ToLower())
115+
percent = $percent
116+
}
117+
})
113118

114119
$ccFailMsgs = @()
115120
$ccReport.ForEach({
116-
'Type: [{0}]: {1:p}' -f $_.name, $_.percent
117-
if ($_.percent -lt $CodeCoverageThreshold) {
118-
$ccFailMsgs += ('Code coverage: [{0}] is [{1:p}], which is less than the threshold of [{2:p}]' -f $_.name, $_.percent, $CodeCoverageThreshold)
119-
}
120-
})
121+
'Type: [{0}]: {1:p}' -f $_.name, $_.percent
122+
if ($_.percent -lt $CodeCoverageThreshold) {
123+
$ccFailMsgs += ('Code coverage: [{0}] is [{1:p}], which is less than the threshold of [{2:p}]' -f $_.name, $_.percent, $CodeCoverageThreshold)
124+
}
125+
})
121126
Write-Host "`n"
122127
$ccFailMsgs.Foreach({
123-
Write-Error $_
124-
})
128+
Write-Error $_
129+
})
125130
} else {
126131
Write-Error "Code coverage file [$CodeCoverageOutputFile] not found."
127132
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ Install-Module -Name psake -RequiredVersion 4.8.0 -Repository PSGallery
2525
> [how to dot source tasks using PowerShell aliases](https://github.com/nightroman/Invoke-Build/blob/master/Tasks/Import/README.md#example-2-import-from-a-module-with-tasks)
2626
> example.
2727
28+
<!-- markdownlint-disable no-inline-html -->
2829
<p align="center">
2930
<img src="media/psaketaskmodule-256x256.png" alt="Logo">
3031
</p>
32+
<!-- markdownlint-enable no-inline-html -->
3133

3234
## Status - Work in progress
3335

@@ -195,3 +197,5 @@ $PSBPreference.Test.CodeCoverage.Enabled = $false
195197
[psgallery]: https://www.powershellgallery.com/packages/PowerShellBuild
196198
[license-badge]: https://img.shields.io/github/license/psake/PowerShellBuild.svg
197199
[license]: https://raw.githubusercontent.com/psake/PowerShellBuild/main/LICENSE
200+
201+
<!-- spell-checker:ignore PSGALLERY psaketaskmodule -->

0 commit comments

Comments
 (0)