Skip to content

Commit 1fd99de

Browse files
authored
Merge pull request #21 from pwshdevs/feat/update-chocolatey-to-be-in-parity
setting up parity with other providers adjusted multiple test cases for chocolatey refactored code chocolatey code to be more efficient with less duplication added a helper function to find the choco binary rather then relying on just Get-Command or hard coding 'choco.exe' for those times when its not in the path
2 parents b9d7e16 + 31e1201 commit 1fd99de

26 files changed

Lines changed: 4449 additions & 906 deletions
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
BeforeAll {
2+
. $PSScriptRoot\Find-Chocolatey.ps1
3+
. $PSScriptRoot\..\..\..\..\DevSetup\Private\Utils\Write-StatusMessage.ps1
4+
. $PSScriptRoot\..\..\..\..\DevSetup\Private\Utils\Get-EnvironmentVariable.ps1
5+
}
6+
7+
Describe "Find-Chocolatey" {
8+
9+
Context "When Chocolatey is found via Get-Command" {
10+
It "Should return the path from Get-Command when choco is in PATH" {
11+
$expectedPath = Join-Path $TestDrive "chocolatey" "bin" "choco.exe"
12+
Mock Get-Command {
13+
return @{ Path = $expectedPath }
14+
} -ParameterFilter { $Name -eq "choco" }
15+
Mock Write-StatusMessage { }
16+
17+
$result = Find-Chocolatey
18+
19+
$result | Should -Be $expectedPath
20+
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
21+
$Message -match "Found Chocolatey at: $([regex]::Escape($expectedPath))" -and $Verbosity -eq "Debug"
22+
}
23+
}
24+
}
25+
26+
Context "When Get-Command fails but ChocolateyInstall environment variable exists" {
27+
It "Should return path from ChocolateyInstall environment variable" {
28+
$chocolateyInstallPath = Join-Path $TestDrive "Chocolatey"
29+
$expectedPath = Join-Path $chocolateyInstallPath "bin" "choco.exe"
30+
31+
Mock Get-Command { return $null } -ParameterFilter { $Name -eq "choco" }
32+
Mock Get-EnvironmentVariable {
33+
return $chocolateyInstallPath
34+
} -ParameterFilter { $Name -eq "ChocolateyInstall" }
35+
Mock Test-Path { $true } -ParameterFilter { $Path -eq $expectedPath }
36+
Mock Write-StatusMessage { }
37+
38+
$result = Find-Chocolatey
39+
40+
$result | Should -Be $expectedPath
41+
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
42+
$Message -match "Found Chocolatey at: $([regex]::Escape($expectedPath))" -and $Verbosity -eq "Debug"
43+
}
44+
}
45+
}
46+
47+
Context "When ChocolateyInstall environment variable is not set" {
48+
It "Should return null and log debug message" {
49+
Mock Get-Command { return $null } -ParameterFilter { $Name -eq "choco" }
50+
Mock Get-EnvironmentVariable { return $null } -ParameterFilter { $Name -eq "ChocolateyInstall" }
51+
Mock Write-StatusMessage { }
52+
53+
$result = Find-Chocolatey
54+
55+
$result | Should -BeNullOrEmpty
56+
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
57+
$Message -eq "ChocolateyInstall environment variable is not set." -and $Verbosity -eq "Debug"
58+
}
59+
}
60+
}
61+
62+
Context "When ChocolateyInstall path exists but choco.exe does not exist" {
63+
It "Should return null and log debug message about missing executable" {
64+
$chocolateyInstallPath = Join-Path $TestDrive "Chocolatey"
65+
$expectedPath = Join-Path $chocolateyInstallPath "bin" "choco.exe"
66+
67+
Mock Get-Command { return $null } -ParameterFilter { $Name -eq "choco" }
68+
Mock Get-EnvironmentVariable {
69+
return $chocolateyInstallPath
70+
} -ParameterFilter { $Name -eq "ChocolateyInstall" }
71+
Mock Test-Path { $false } -ParameterFilter { $Path -eq $expectedPath }
72+
Mock Write-StatusMessage { }
73+
74+
$result = Find-Chocolatey
75+
76+
$result | Should -BeNullOrEmpty
77+
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
78+
$Message -eq "Chocolatey executable not found at expected path: $expectedPath" -and $Verbosity -eq "Debug"
79+
}
80+
}
81+
}
82+
83+
Context "When Get-Command throws an exception" {
84+
It "Should handle Get-Command exception and continue with environment variable lookup" {
85+
$chocolateyInstallPath = Join-Path $TestDrive "Chocolatey"
86+
$expectedPath = Join-Path $chocolateyInstallPath "bin" "choco.exe"
87+
88+
Mock Get-Command { throw "Command not found error" } -ParameterFilter { $Name -eq "choco" }
89+
Mock Get-EnvironmentVariable {
90+
return $chocolateyInstallPath
91+
} -ParameterFilter { $Name -eq "ChocolateyInstall" }
92+
Mock Test-Path { $true } -ParameterFilter { $Path -eq $expectedPath }
93+
Mock Write-StatusMessage { }
94+
95+
$result = Find-Chocolatey
96+
97+
$result | Should -Be $expectedPath
98+
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
99+
$Message -match "Error finding Chocolatey command:" -and $Verbosity -eq "Error"
100+
}
101+
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
102+
$Message -match "Found Chocolatey at: $([regex]::Escape($expectedPath))" -and $Verbosity -eq "Debug"
103+
}
104+
}
105+
}
106+
107+
Context "When Get-EnvironmentVariable throws an exception" {
108+
It "Should handle Get-EnvironmentVariable exception and return null" {
109+
Mock Get-Command { return $null } -ParameterFilter { $Name -eq "choco" }
110+
Mock Get-EnvironmentVariable { throw "Environment variable access error" } -ParameterFilter { $Name -eq "ChocolateyInstall" }
111+
Mock Write-StatusMessage { }
112+
113+
$result = Find-Chocolatey
114+
115+
$result | Should -BeNullOrEmpty
116+
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
117+
$Message -match "Error retrieving ChocolateyInstall environment variable:" -and $Verbosity -eq "Error"
118+
}
119+
}
120+
}
121+
122+
Context "When Join-Path throws an exception" {
123+
It "Should handle Join-Path exception and return null" {
124+
$chocolateyInstallPath = "InvalidPath:"
125+
126+
Mock Get-Command { return $null } -ParameterFilter { $Name -eq "choco" }
127+
Mock Get-EnvironmentVariable {
128+
return $chocolateyInstallPath
129+
} -ParameterFilter { $Name -eq "ChocolateyInstall" }
130+
Mock Join-Path { throw "Invalid path error" }
131+
Mock Write-StatusMessage { }
132+
133+
$result = Find-Chocolatey
134+
135+
$result | Should -BeNullOrEmpty
136+
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
137+
$Message -match "Error constructing Chocolatey path:" -and $Verbosity -eq "Error"
138+
}
139+
}
140+
}
141+
142+
Context "When all operations succeed via Get-Command" {
143+
It "Should not attempt environment variable lookup when Get-Command succeeds" {
144+
$expectedPath = Join-Path $TestDrive "chocolatey" "bin" "choco.exe"
145+
Mock Get-Command {
146+
return @{ Path = $expectedPath }
147+
} -ParameterFilter { $Name -eq "choco" }
148+
Mock Get-EnvironmentVariable { throw "Should not be called" }
149+
Mock Write-StatusMessage { }
150+
151+
$result = Find-Chocolatey
152+
153+
$result | Should -Be $expectedPath
154+
Assert-MockCalled Get-EnvironmentVariable -Times 0 -Scope It
155+
}
156+
}
157+
158+
Context "Integration scenarios" {
159+
It "Should return path when both methods would work but Get-Command takes precedence" {
160+
$commandPath = Join-Path $TestDrive "system" "choco.exe"
161+
$envInstallPath = Join-Path $TestDrive "custom" "chocolatey"
162+
$envPath = Join-Path $envInstallPath "bin" "choco.exe"
163+
164+
Mock Get-Command {
165+
return @{ Path = $commandPath }
166+
} -ParameterFilter { $Name -eq "choco" }
167+
Mock Get-EnvironmentVariable {
168+
return $envInstallPath
169+
} -ParameterFilter { $Name -eq "ChocolateyInstall" }
170+
Mock Test-Path { $true }
171+
Mock Write-StatusMessage { }
172+
173+
$result = Find-Chocolatey
174+
175+
# Should return the Get-Command path, not the environment variable path
176+
$result | Should -Be $commandPath
177+
# Environment variable should not be called since Get-Command succeeded
178+
Assert-MockCalled Get-EnvironmentVariable -Times 0 -Scope It
179+
}
180+
}
181+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Function Find-Chocolatey {
2+
[CmdletBinding()]
3+
[OutputType([string])]
4+
Param(
5+
)
6+
7+
# Check if Chocolatey is installed
8+
try {
9+
$Path = (Get-Command "choco" -ErrorAction SilentlyContinue).Path
10+
} catch {
11+
Write-StatusMessage "Error finding Chocolatey command: $_" -Verbosity Error
12+
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
13+
}
14+
15+
if ($Path) {
16+
Write-StatusMessage "Found Chocolatey at: $Path" -Verbosity Debug
17+
return $Path
18+
} else {
19+
try {
20+
$ChocolateyInstallEnvPath = Get-EnvironmentVariable ChocolateyInstall
21+
} catch {
22+
Write-StatusMessage "Error retrieving ChocolateyInstall environment variable: $_" -Verbosity Error
23+
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
24+
return $null
25+
}
26+
if (-not $ChocolateyInstallEnvPath) {
27+
Write-StatusMessage "ChocolateyInstall environment variable is not set." -Verbosity Debug
28+
return $null
29+
} else {
30+
try {
31+
$Path = Join-Path $ChocolateyInstallEnvPath "bin\choco.exe"
32+
} catch {
33+
Write-StatusMessage "Error constructing Chocolatey path: $_" -Verbosity Error
34+
Write-StatusMessage $_.ScriptStackTrace -Verbosity Error
35+
return $null
36+
}
37+
if (Test-Path $Path) {
38+
Write-StatusMessage "Found Chocolatey at: $Path" -Verbosity Debug
39+
return $Path
40+
} else {
41+
Write-StatusMessage "Chocolatey executable not found at expected path: $Path" -Verbosity Debug
42+
return $null
43+
}
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)