@@ -42,6 +42,46 @@ function script:Clear-ChocolateyTestEnvironment {
4242 }
4343}
4444
45+ function script :Disable-ChocolateyDefaultLocations {
46+ <#
47+ . SYNOPSIS
48+ Isolate detection from a host Chocolatey install (ProgramData + hardcoded fallback).
49+ #>
50+ $emptyProgramData = Join-Path $script :TempDir ' empty-programdata'
51+ if (-not (Test-Path - LiteralPath $emptyProgramData )) {
52+ New-Item - ItemType Directory - Path $emptyProgramData - Force | Out-Null
53+ }
54+ Mock- EnvironmentVariable - Name ' ProgramData' - Value $emptyProgramData
55+
56+ Mock Get-Command {
57+ $cmdName = $Name
58+ if ([string ]::IsNullOrWhiteSpace($cmdName ) -and $args.Count -gt 0 ) {
59+ $cmdName = [string ]$args [0 ]
60+ }
61+ if ($cmdName -eq ' Test-ValidPath' ) {
62+ return $null
63+ }
64+ return Microsoft.PowerShell.Core\Get-Command @PSBoundParameters
65+ } - ModuleName ChocolateyDetection
66+
67+ Mock Test-Path {
68+ $target = $null
69+ if ($PSBoundParameters.ContainsKey (' LiteralPath' ) -and $LiteralPath ) {
70+ $target = [string ]$LiteralPath
71+ }
72+ elseif ($PSBoundParameters.ContainsKey (' Path' ) -and $Path ) {
73+ $target = [string ]$Path
74+ }
75+ elseif ($args.Count -gt 0 ) {
76+ $target = [string ]$args [0 ]
77+ }
78+ if ($target -eq ' C:\ProgramData\chocolatey' ) {
79+ return $false
80+ }
81+ return Microsoft.PowerShell.Management\Test-Path @PSBoundParameters
82+ } - ModuleName ChocolateyDetection
83+ }
84+
4585AfterAll {
4686 Clear-ChocolateyTestEnvironment
4787 if ($script :TempDir -and (Test-Path - LiteralPath $script :TempDir )) {
@@ -99,6 +139,7 @@ Describe 'ChocolateyDetection extended scenarios' {
99139 $filePath = Join-Path $script :TempDir ' missing-choco.txt'
100140 New-Item - ItemType File - Path $filePath - Force | Out-Null
101141 Mock- EnvironmentVariable - Name ' ChocolateyInstall' - Value $filePath
142+ Disable-ChocolateyDefaultLocations
102143
103144 Get-ChocolateyRoot | Should - BeNullOrEmpty
104145 }
@@ -154,6 +195,7 @@ Describe 'ChocolateyDetection extended scenarios' {
154195
155196 It ' Logs verbose output when no Chocolatey root is found at debug level 2' {
156197 Mock- EnvironmentVariable - Name ' ChocolateyInstall' - Value $null
198+ Disable-ChocolateyDefaultLocations
157199 $env: PS_PROFILE_DEBUG = ' 2'
158200
159201 Get-ChocolateyRoot | Should - BeNullOrEmpty
0 commit comments