Skip to content

Commit e3c2cb4

Browse files
committed
🧪 [test] Harden cache-directory tests: pre-create macOS/XDG caches and localize env/state
- Pre-create macOS and XDG cache directories in tests so Initialize-CacheDirectory can resolve real candidates. - Inject test paths into the module scope (InModuleScope -Parameters @{ TestHomePath = ... } / @{ TestXdgPath = ... }) and set HOME via Set-Variable inside the module scope to avoid leaking global HOME. - Null, clear and restore APPDATA/XDG_CACHE_HOME within module scope; wrap Initialize-CacheDirectory calls in try/finally to guarantee environment restoration. - Initialize module state (ConfigurationData, ConfigurationInitialized, CacheInitialized, CacheDir, IsWindows/IsMacOS) for deterministic behavior during tests. - Remove direct env modifications outside InModuleScope and adjust mocks/assertions to use the injected test paths to reduce test pollution and flakiness. Signed-off-by: Nick2bad4u <20943337+Nick2bad4u@users.noreply.github.com>
1 parent 451f3e2 commit e3c2cb4

2 files changed

Lines changed: 43 additions & 17 deletions

File tree

‎Tests/ColorScripts-Enhanced.AdditionalCoverage.Tests.ps1‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,64 +2242,88 @@ namespace CoverageHost
22422242
}
22432243

22442244
It "uses macOS cache locations when applicable" {
2245-
$originalHome = $env:HOME
22462245
$testRoot = (Resolve-Path -LiteralPath 'TestDrive:\').ProviderPath
22472246
$homePath = Join-Path -Path $testRoot -ChildPath ([guid]::NewGuid().ToString())
22482247
New-Item -ItemType Directory -Path $homePath -Force | Out-Null
2249-
$env:HOME = $homePath
2248+
2249+
# Pre-create the macOS directory structure
2250+
$macCachePath = Join-Path -Path $homePath -ChildPath 'Library\Application Support\ColorScripts-Enhanced\cache'
2251+
New-Item -ItemType Directory -Path $macCachePath -Force | Out-Null
22502252

22512253
Mock -CommandName Resolve-CachePath -ModuleName ColorScripts-Enhanced -MockWith {
22522254
param($Path)
22532255
$Path
22542256
}
22552257

2256-
InModuleScope ColorScripts-Enhanced {
2258+
InModuleScope ColorScripts-Enhanced -Parameters @{ TestHomePath = $homePath } {
2259+
param($TestHomePath)
2260+
$originalHome = Get-Variable -Name HOME -Scope Global -ValueOnly
2261+
$originalAppData = $env:APPDATA
2262+
$originalXdg = $env:XDG_CACHE_HOME
22572263
$script:CacheInitialized = $false
22582264
$script:CacheDir = $null
2265+
$script:ConfigurationData = @{ Cache = @{} ; Startup = @{} }
2266+
$script:ConfigurationInitialized = $false
22592267
$script:IsWindows = $false
22602268
$script:IsMacOS = $true
2269+
Set-Variable -Name HOME -Scope Global -Value $TestHomePath -Force
2270+
$env:APPDATA = $null
2271+
$env:XDG_CACHE_HOME = $null
22612272
try {
22622273
Initialize-CacheDirectory
22632274
$script:CacheDir | Should -Match 'Library[/\\]Application Support[/\\]ColorScripts-Enhanced[/\\]cache$'
22642275
}
22652276
finally {
22662277
$script:IsWindows = $IsWindows
22672278
$script:IsMacOS = $IsMacOS
2279+
Set-Variable -Name HOME -Scope Global -Value $originalHome -Force
2280+
$env:APPDATA = $originalAppData
2281+
$env:XDG_CACHE_HOME = $originalXdg
22682282
}
22692283
}
2270-
2271-
$env:HOME = $originalHome
22722284
}
22732285

22742286
It "uses XDG cache home on non-windows platforms" {
2275-
$originalXdg = $env:XDG_CACHE_HOME
22762287
$testRoot = (Resolve-Path -LiteralPath 'TestDrive:\').ProviderPath
22772288
$xdgPath = Join-Path -Path $testRoot -ChildPath ([guid]::NewGuid().ToString())
22782289
New-Item -ItemType Directory -Path $xdgPath -Force | Out-Null
2279-
$env:XDG_CACHE_HOME = $xdgPath
2290+
2291+
# Pre-create the XDG cache directory
2292+
$xdgCachePath = Join-Path -Path $xdgPath -ChildPath 'ColorScripts-Enhanced'
2293+
New-Item -ItemType Directory -Path $xdgCachePath -Force | Out-Null
22802294

22812295
Mock -CommandName Resolve-CachePath -ModuleName ColorScripts-Enhanced -MockWith {
22822296
param($Path)
22832297
$Path
22842298
}
22852299

2286-
InModuleScope ColorScripts-Enhanced {
2300+
InModuleScope ColorScripts-Enhanced -Parameters @{ TestXdgPath = $xdgPath } {
2301+
param($TestXdgPath)
2302+
$originalHome = Get-Variable -Name HOME -Scope Global -ValueOnly
2303+
$originalXdg = $env:XDG_CACHE_HOME
2304+
$originalAppData = $env:APPDATA
22872305
$script:CacheInitialized = $false
22882306
$script:CacheDir = $null
2307+
$script:ConfigurationData = @{ Cache = @{} ; Startup = @{} }
2308+
$script:ConfigurationInitialized = $false
22892309
$script:IsWindows = $false
22902310
$script:IsMacOS = $false
2311+
Set-Variable -Name HOME -Scope Global -Value (Join-Path -Path $TestXdgPath -ChildPath 'fakehome') -Force
2312+
$env:XDG_CACHE_HOME = $TestXdgPath
2313+
$env:APPDATA = $null
22912314
try {
22922315
Initialize-CacheDirectory
22932316
$script:CacheDir | Should -Match 'ColorScripts-Enhanced$'
2294-
$script:CacheDir | Should -Match ([regex]::Escape($xdgPath))
2317+
$script:CacheDir | Should -Match ([regex]::Escape($TestXdgPath))
22952318
}
22962319
finally {
22972320
$script:IsWindows = $IsWindows
22982321
$script:IsMacOS = $IsMacOS
2322+
Set-Variable -Name HOME -Scope Global -Value $originalHome -Force
2323+
$env:XDG_CACHE_HOME = $originalXdg
2324+
$env:APPDATA = $originalAppData
22992325
}
23002326
}
2301-
2302-
$env:XDG_CACHE_HOME = $originalXdg
23032327
}
23042328

23052329
It "skips unresolved candidate paths" {

‎Tests/ColorScripts-Enhanced.CoverageExpansion.Tests.ps1‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,18 +1184,20 @@ Describe "ColorScripts-Enhanced extended coverage" {
11841184
}
11851185
11861186
It "skips unresolved candidate paths" {
1187-
$originalAppData = $env:APPDATA
1188-
$env:APPDATA = 'ZZ:\MissingAppData'
1189-
11901187
Mock -CommandName Write-Verbose -ModuleName ColorScripts-Enhanced -MockWith { param($Message) }
11911188
11921189
InModuleScope ColorScripts-Enhanced {
1193-
Initialize-CacheDirectory
1190+
$originalAppData = $env:APPDATA
1191+
$env:APPDATA = 'ZZ:\MissingAppData'
1192+
try {
1193+
Initialize-CacheDirectory
1194+
}
1195+
finally {
1196+
$env:APPDATA = $originalAppData
1197+
}
11941198
}
11951199
11961200
Assert-MockCalled -CommandName Write-Verbose -ModuleName ColorScripts-Enhanced -ParameterFilter { $Message -like 'Skipping cache candidate*' } -Times 1
1197-
1198-
$env:APPDATA = $originalAppData
11991201
}
12001202
12011203
It "warns when candidate directory creation fails" {

0 commit comments

Comments
 (0)