From 65cc39d1b91e31ac10311ead61f9192f2e385330 Mon Sep 17 00:00:00 2001 From: OpenCode Date: Tue, 14 Jul 2026 10:49:31 +0530 Subject: [PATCH 1/2] fix: make PS 5.1 CI green (array returns, menu header, test mocks, fail-fast) - Guard Get-Printer calls in Get-Printers/Get-PrinterShareStatus/ Get-PrintComplianceReport so they return an empty array (not $null) when the Print Spooler is unavailable (terminating exception on CI). - Guard Clear-Host/ReadKey in Write-MenuHeader/Wait-Menu for non-interactive hosts (invalid CursorPosition handle). - Fix Test-Administrator test (was passing -is as a parameter) and scope Assert-Elevated mocks with -ModuleName so they apply inside the module. - Set fail-fast: false on the analyze matrix so both PS versions report. --- .github/workflows/ci.yml | 1 + Modules/Core/PrinterToolkit.Core.psm1 | 6 +++++- Modules/Reporting/PrinterToolkit.Reporting.psm1 | 6 +++++- Modules/Sharing/PrinterToolkit.Sharing.psm1 | 6 +++++- Modules/Utilities/PrinterToolkit.Utilities.psm1 | 4 ++-- Tests/PrinterToolkit.Tests.ps1 | 7 ++++--- 6 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4ce7871..f206162 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,6 +14,7 @@ jobs: name: Analyze & Test runs-on: windows-latest strategy: + fail-fast: false matrix: psversion: ['5.1', '7.4'] steps: diff --git a/Modules/Core/PrinterToolkit.Core.psm1 b/Modules/Core/PrinterToolkit.Core.psm1 index a6b84a5..6a4aa3c 100644 --- a/Modules/Core/PrinterToolkit.Core.psm1 +++ b/Modules/Core/PrinterToolkit.Core.psm1 @@ -144,7 +144,11 @@ function Get-Printers { [CmdletBinding()] [OutputType([array])] param() - return @(Get-Printer -ErrorAction SilentlyContinue) + try { + return @(Get-Printer -ErrorAction SilentlyContinue) + } catch { + return @() + } } function Set-DefaultPrinter { diff --git a/Modules/Reporting/PrinterToolkit.Reporting.psm1 b/Modules/Reporting/PrinterToolkit.Reporting.psm1 index 48d882e..4517bd9 100644 --- a/Modules/Reporting/PrinterToolkit.Reporting.psm1 +++ b/Modules/Reporting/PrinterToolkit.Reporting.psm1 @@ -216,7 +216,11 @@ function Get-PrintComplianceReport { [CmdletBinding()] [OutputType([array])] param() - $printers = @(Get-Printer -ErrorAction SilentlyContinue) + try { + $printers = @(Get-Printer -ErrorAction SilentlyContinue) + } catch { + $printers = @() + } $drivers = @(Get-PrinterDriver -ErrorAction SilentlyContinue) $results = foreach ($p in $printers) { diff --git a/Modules/Sharing/PrinterToolkit.Sharing.psm1 b/Modules/Sharing/PrinterToolkit.Sharing.psm1 index 941e22a..c971ee8 100644 --- a/Modules/Sharing/PrinterToolkit.Sharing.psm1 +++ b/Modules/Sharing/PrinterToolkit.Sharing.psm1 @@ -16,7 +16,11 @@ function Get-PrinterShareStatus { [CmdletBinding()] [OutputType([array])] param() - $printers = @(Get-Printer -ErrorAction SilentlyContinue) + try { + $printers = @(Get-Printer -ErrorAction SilentlyContinue) + } catch { + $printers = @() + } $results = foreach ($p in $printers) { $driver = Get-PrinterDriver -Name $p.DriverName -ErrorAction SilentlyContinue $isIPP = if ($p.PortName -match '^(http|ipp|wsd|wsdprint)') { $true } else { $false } diff --git a/Modules/Utilities/PrinterToolkit.Utilities.psm1 b/Modules/Utilities/PrinterToolkit.Utilities.psm1 index 759916e..04283ab 100644 --- a/Modules/Utilities/PrinterToolkit.Utilities.psm1 +++ b/Modules/Utilities/PrinterToolkit.Utilities.psm1 @@ -123,7 +123,7 @@ function Write-MenuHeader { [string]$Subtitle = '' ) - Clear-Host + try { Clear-Host } catch { } $line = '=' * 74 Write-Host $line -ForegroundColor Cyan Write-Host " $Title" -ForegroundColor White @@ -140,7 +140,7 @@ function Wait-Menu { param() Write-Host '' Write-Host 'Press any key to continue...' -ForegroundColor DarkGray - $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') + try { $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') } catch { } } Export-ModuleMember -Function Test-Administrator, Test-Elevated, Assert-Elevated, Confirm-DestructiveAction, Get-SystemInfo, Write-MenuHeader, Wait-Menu diff --git a/Tests/PrinterToolkit.Tests.ps1 b/Tests/PrinterToolkit.Tests.ps1 index c261015..ef802eb 100644 --- a/Tests/PrinterToolkit.Tests.ps1 +++ b/Tests/PrinterToolkit.Tests.ps1 @@ -97,7 +97,8 @@ Describe 'Core Module' { Describe 'Utilities Module' { It 'Test-Administrator should return boolean' { - Test-Administrator -is [bool] | Should -Be $true + $result = Test-Administrator + $result -is [bool] | Should -Be $true } It 'Test-Elevated calls Test-Administrator' { @@ -110,12 +111,12 @@ Describe 'Utilities Module' { } It 'Assert-Elevated should not throw when admin' { - Mock Test-Administrator { return $true } + Mock Test-Administrator { return $true } -ModuleName 'PrinterToolkit.Utilities' { Assert-Elevated } | Should -Not -Throw } It 'Assert-Elevated should throw when not admin' { - Mock Test-Administrator { return $false } + Mock Test-Administrator { return $false } -ModuleName 'PrinterToolkit.Utilities' { Assert-Elevated } | Should -Throw } } From 2a70dac6cae2d2de7c8f86de5777af69538eb317 Mon Sep 17 00:00:00 2001 From: OpenCode Date: Tue, 14 Jul 2026 11:03:04 +0530 Subject: [PATCH 2/2] fix: force array returns via comma-array idiom (return ,@(...)) PowerShell enumerates a returned array, so functions returning @(...) yield a scalar when there are 0/1 elements. Wrap with ,@(...) so Get-Printers, Get-PrinterShareStatus, Get-PrinterSharingCompatibility and Get-PrintComplianceReport always return a real array regardless of printer count. --- Modules/Core/PrinterToolkit.Core.psm1 | 4 ++-- Modules/Reporting/PrinterToolkit.Reporting.psm1 | 2 +- Modules/Sharing/PrinterToolkit.Sharing.psm1 | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Modules/Core/PrinterToolkit.Core.psm1 b/Modules/Core/PrinterToolkit.Core.psm1 index 6a4aa3c..86a0914 100644 --- a/Modules/Core/PrinterToolkit.Core.psm1 +++ b/Modules/Core/PrinterToolkit.Core.psm1 @@ -145,9 +145,9 @@ function Get-Printers { [OutputType([array])] param() try { - return @(Get-Printer -ErrorAction SilentlyContinue) + return ,@(Get-Printer -ErrorAction SilentlyContinue) } catch { - return @() + return ,@() } } diff --git a/Modules/Reporting/PrinterToolkit.Reporting.psm1 b/Modules/Reporting/PrinterToolkit.Reporting.psm1 index 4517bd9..6fdc1b9 100644 --- a/Modules/Reporting/PrinterToolkit.Reporting.psm1 +++ b/Modules/Reporting/PrinterToolkit.Reporting.psm1 @@ -251,7 +251,7 @@ function Get-PrintComplianceReport { } } - return ,$results + return ,@($results) } Export-ModuleMember -Function New-PrinterReport, Get-PrintComplianceReport diff --git a/Modules/Sharing/PrinterToolkit.Sharing.psm1 b/Modules/Sharing/PrinterToolkit.Sharing.psm1 index c971ee8..efe16bb 100644 --- a/Modules/Sharing/PrinterToolkit.Sharing.psm1 +++ b/Modules/Sharing/PrinterToolkit.Sharing.psm1 @@ -40,7 +40,7 @@ function Get-PrinterShareStatus { } } - return ,$results + return ,@($results) } function Enable-PrinterSharing { @@ -125,7 +125,7 @@ function Get-SmbSharePermissions { } } - return ,$results + return ,@($results) } function Set-PrinterSharePermission { @@ -234,7 +234,7 @@ function Get-PrinterSharingCompatibility { } } - return ,$results + return ,@($results) } Export-ModuleMember -Function Get-PrinterShareStatus, Enable-PrinterSharing, Disable-PrinterSharing, Get-SmbSharePermissions, Set-PrinterSharePermission, Set-PrinterSharingTransport, Get-PrinterSharingCompatibility