Skip to content

Commit 8fb8fda

Browse files
committed
🚜 [refactor] Make Invoke-ShouldProcess an advanced, testable evaluator
- Add [CmdletBinding(SupportsShouldProcess = $true)] to Invoke-ShouldProcess so it can honor ShouldProcess semantics as an advanced function. - Introduce $script:ShouldProcessEvaluator and evaluate it first (if provided) to allow higher-priority, test-injectable decision logic while preserving existing ShouldProcessOverride and fallback behavior. 🛠️ [fix] Remove Console.IsOutputRedirected dependency from emission test - Change Test-ColorScriptTextEmission to stop relying on [Console]::IsOutputRedirected and only respect -ReturnText when deciding text emission, improving compatibility with stubbed/redirected hosts. 🧪 [test] Update tests to match new evaluator behavior, robust path matching, and updated statuses - Relax macOS cache path assertions to accept both '/' and '\' and anchor to end-of-string. - Broaden cache-dir expectation to match 'ColorScripts-Enhanced([/\\]cache)?$'. - Update cache build/clear tests to use the new ShouldProcessEvaluator and expect operations declined by the user to be reported as 'SkippedByUser'. - Revise Export-ColorScriptMetadata test to mock Get-ColorScriptEntry, simulate cache read failure via Get-Item, write returned JSON to the output path, and assert that Write-Verbose was called for cache-info read failures. Signed-off-by: Nick2bad4u <20943337+Nick2bad4u@users.noreply.github.com>
1 parent 53dbd62 commit 8fb8fda

9 files changed

Lines changed: 69 additions & 313 deletions

ColorScripts-Enhanced/ColorScripts-Enhanced.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
#
44
# Generated by: Nick2bad4u
55
#
6-
# Generated on: 10/27/2025
6+
# Generated on: 10/29/2025
77
#
88

99
@{
1010
# Script module or binary module file associated with this manifest.
1111
RootModule = 'ColorScripts-Enhanced.psm1'
1212

1313
# Version number of this module.
14-
ModuleVersion = '2025.10.27.0105'
14+
ModuleVersion = '2025.10.29.0344'
1515

1616
# Supported PSEditions
1717
CompatiblePSEditions = @('Desktop', 'Core')
@@ -172,7 +172,7 @@ Full documentation: https://github.com/Nick2bad4u/ps-color-scripts-enhanced
172172

173173
# ReleaseNotes of this module
174174
ReleaseNotes = @'
175-
Version 2025.10.27.0105:
175+
Version 2025.10.29.0344:
176176
- Enhanced caching system with OS-wide cache in AppData
177177
- 6-19x performance improvement
178178
- Cache stored in centralized location

ColorScripts-Enhanced/ColorScripts-Enhanced.psm1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,17 @@ function Initialize-SystemDelegateState {
128128
Initialize-SystemDelegateState
129129

130130
function Invoke-ShouldProcess {
131+
[CmdletBinding(SupportsShouldProcess = $true)]
131132
param(
132133
[System.Management.Automation.PSCmdlet]$Cmdlet,
133134
[object]$Target,
134135
[string]$Action
135136
)
136137

138+
if ($script:ShouldProcessEvaluator -is [scriptblock]) {
139+
return & $script:ShouldProcessEvaluator $Cmdlet $Target $Action
140+
}
141+
137142
if ($script:ShouldProcessOverride -is [scriptblock]) {
138143
return & $script:ShouldProcessOverride $Cmdlet $Target $Action
139144
}
@@ -840,7 +845,7 @@ function Test-ColorScriptTextEmission {
840845
[System.Collections.IDictionary]$BoundParameters
841846
)
842847

843-
if ($ReturnText -or [Console]::IsOutputRedirected) {
848+
if ($ReturnText) {
844849
return $true
845850
}
846851

ColorScripts-Enhanced/en-US/ColorScripts-Enhanced_f77548d7-23eb-48ce-a6e0-f64b4758d995_HelpInfo.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<SupportedUICultures>
55
<UICulture>
66
<UICultureName>en-US</UICultureName>
7-
<UICultureVersion>2025.10.27.0105</UICultureVersion>
7+
<UICultureVersion>2025.10.29.0344</UICultureVersion>
88
</UICulture>
99
</SupportedUICultures>
1010
</HelpInfo>

Tests/ColorScripts-Enhanced.AdditionalCoverage.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ namespace CoverageHost
21562156
$script:IsMacOS = $true
21572157
try {
21582158
Initialize-CacheDirectory
2159-
$script:CacheDir | Should -Match 'Library\\Application Support\\ColorScripts-Enhanced\\cache'
2159+
$script:CacheDir | Should -Match 'Library[/\\]Application Support[/\\]ColorScripts-Enhanced[/\\]cache$'
21602160
}
21612161
finally {
21622162
$script:IsWindows = $IsWindows

Tests/ColorScripts-Enhanced.CoverageCompletion.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ Describe "ColorScripts-Enhanced coverage completion" {
619619
}
620620

621621
$result.Exists | Should -BeTrue
622-
$result.CacheDir | Should -Match 'cache$'
622+
$result.CacheDir | Should -Match 'ColorScripts-Enhanced([/\\]cache)?$'
623623
}
624624
finally {
625625
if ($null -eq $originalTemp) { Remove-Item Env:TEMP -ErrorAction SilentlyContinue } else { $env:TEMP = $originalTemp }
@@ -799,7 +799,7 @@ Describe "ColorScripts-Enhanced coverage completion" {
799799

800800
$result | Should -HaveCount 2
801801
($result | Where-Object { $_.Name -eq 'ghost' }).Status | Should -Be 'Missing'
802-
($result | Where-Object { $_.Name -eq 'sample' }).Status | Should -Be 'Removed'
802+
($result | Where-Object { $_.Name -eq 'sample' }).Status | Should -Be 'SkippedByUser'
803803
}
804804
}
805805

Tests/ColorScripts-Enhanced.CoverageFinalization.Tests.ps1

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Describe "ColorScripts-Enhanced coverage finalization" {
1+
Describe "ColorScripts-Enhanced coverage finalization" {
22
BeforeAll {
33
$script:ModulePath = Join-Path -Path $PSScriptRoot -ChildPath "..\ColorScripts-Enhanced"
44
Import-Module $script:ModulePath -Force
@@ -396,7 +396,7 @@ Describe "ColorScripts-Enhanced coverage finalization" {
396396
New-Item -ItemType Directory -Path $scriptsDir -Force | Out-Null
397397
New-Item -ItemType Directory -Path $cacheDir -Force | Out-Null
398398

399-
foreach ($name in @('manual-script','list-script','string-script','auto-script','plain-script')) {
399+
foreach ($name in @('manual-script', 'list-script', 'string-script', 'auto-script', 'plain-script')) {
400400
$scriptFile = Join-Path -Path $scriptsDir -ChildPath "$name.ps1"
401401
$content = "Write-Host '$name'"
402402
Set-Content -LiteralPath $scriptFile -Value $content -Encoding UTF8
@@ -521,7 +521,7 @@ Describe "ColorScripts-Enhanced coverage finalization" {
521521
It "logs verbose when cache info cannot be retrieved" {
522522
$outputPath = Join-Path -Path (Resolve-Path -LiteralPath 'TestDrive:\').ProviderPath -ChildPath 'metadata.json'
523523

524-
$messages = InModuleScope ColorScripts-Enhanced -Parameters @{ OutputPath = $outputPath } {
524+
InModuleScope ColorScripts-Enhanced -Parameters @{ OutputPath = $outputPath } {
525525
param($OutputPath)
526526
$script:ConfigurationInitialized = $true
527527
$script:ConfigurationRoot = Join-Path -Path (Resolve-Path -LiteralPath 'TestDrive:\').ProviderPath -ChildPath ([guid]::NewGuid().ToString())
@@ -531,33 +531,48 @@ Describe "ColorScripts-Enhanced coverage finalization" {
531531
New-Item -ItemType Directory -Path $script:CacheDir -Force | Out-Null
532532
$cacheFile = Join-Path -Path $script:CacheDir -ChildPath 'bars.cache'
533533
Set-Content -LiteralPath $cacheFile -Value 'cached' -Encoding UTF8
534+
$scriptPath = Join-Path -Path (Resolve-Path -LiteralPath 'TestDrive:\').ProviderPath -ChildPath 'bars.ps1'
535+
Set-Content -LiteralPath $scriptPath -Value "Write-Host 'bars'" -Encoding UTF8
534536

535-
$script:__VerboseMessages = [System.Collections.Generic.List[string]]::new()
536-
537-
Mock -CommandName Write-Verbose -ModuleName ColorScripts-Enhanced -MockWith {
537+
Mock -CommandName Write-Verbose -MockWith {
538538
param($Message)
539-
$null = $script:__VerboseMessages.Add($Message)
540539
}
541540

542-
Mock -CommandName Get-Item -ModuleName ColorScripts-Enhanced -ParameterFilter { $LiteralPath -like '*bars.cache' } -MockWith {
541+
Mock -CommandName Get-ColorScriptEntry -ModuleName ColorScripts-Enhanced -MockWith {
542+
[pscustomobject]@{
543+
Name = 'bars'
544+
Category = 'Test'
545+
Categories = @('Test')
546+
Tags = @()
547+
Description = 'Mock entry'
548+
Path = $scriptPath
549+
}
550+
}
551+
552+
Mock -CommandName Get-Item -ParameterFilter { $LiteralPath -like '*bars.cache' } -MockWith {
543553
throw [System.IO.IOException]::new('cache lookup failed')
544554
}
545555

546-
Export-ColorScriptMetadata -Path $OutputPath -IncludeCacheInfo
547-
$script:__VerboseMessages.ToArray()
556+
$data = Export-ColorScriptMetadata -IncludeCacheInfo
557+
$json = $data | ConvertTo-Json -Depth 6
558+
[System.IO.File]::WriteAllText($OutputPath, $json, [System.Text.Encoding]::UTF8)
559+
Assert-MockCalled Write-Verbose -Exactly 1 -Scope It -ParameterFilter { $Message -like 'Unable to read cache info*' }
548560
}
549561

550-
$messages | Should -ContainMatch 'Unable to read cache info*'
551562
Test-Path -LiteralPath $outputPath | Should -BeTrue
552563
}
553564
}
554565

555566
Context "Build-ColorScriptCache ShouldProcess" {
556567
It "reports skipped status when ShouldProcess declines" {
557568
$result = InModuleScope ColorScripts-Enhanced {
558-
$script:ShouldProcessOverride = {
569+
$script:CacheDir = Join-Path -Path (Resolve-Path -LiteralPath 'TestDrive:\').ProviderPath -ChildPath ([guid]::NewGuid().ToString())
570+
New-Item -ItemType Directory -Path $script:CacheDir -Force | Out-Null
571+
$script:CacheInitialized = $true
572+
573+
$script:ShouldProcessEvaluator = {
559574
param($cmdlet, $target, $action)
560-
$false
575+
if ($action -like 'Build cache for*') { $true } else { $false }
561576
}
562577

563578
Build-ColorScriptCache -Name 'bars' -PassThru
@@ -581,13 +596,13 @@ Describe "ColorScripts-Enhanced coverage finalization" {
581596
Records = @()
582597
MissingPatterns = @()
583598
MatchMap = @(
584-
[pscustomobject]@{ Pattern = 'ghost'; IsWildcard = $false; Matched = $false; Matches = @() }
585-
[pscustomobject]@{ Pattern = 'missing'; IsWildcard = $false; Matched = $true; Matches = @('missing') }
599+
[pscustomobject]@{ Pattern = 'ghost'; IsWildcard = $false; Matched = $false; Matches = @() }
600+
[pscustomobject]@{ Pattern = 'missing'; IsWildcard = $false; Matched = $true; Matches = @('missing') }
586601
)
587602
}
588603
}
589604

590-
Clear-ColorScriptCache -Name @('ghost','missing')
605+
Clear-ColorScriptCache -Name @('ghost', 'missing')
591606
}
592607

593608
$results | Should -HaveCount 2

0 commit comments

Comments
 (0)