Skip to content

Commit 557de2c

Browse files
committed
(#3051) Add tests for Get-ChocolateyPath
Test all the things!
1 parent accd2f0 commit 557de2c

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
Describe 'Get-ChocolateyPath helper function tests' -Tags Cmdlets, GetChocolateyPath {
2+
BeforeAll {
3+
Initialize-ChocolateyTestInstall
4+
5+
$testLocation = Get-ChocolateyTestLocation
6+
Import-Module "$testLocation\helpers\chocolateyInstaller.psm1"
7+
8+
if (-not $env:ChocolateyInstall) {
9+
$env:ChocolateyInstall = $testLocation
10+
}
11+
}
12+
13+
Context '-PathType InstallPath (Path exists)' {
14+
BeforeAll {
15+
$programData = $env:ProgramData
16+
$systemDrive = $env:SystemDrive
17+
}
18+
19+
AfterAll {
20+
$env:ChocolateyInstall = $testLocation
21+
$env:ProgramData = $programData
22+
$env:SystemDrive = $systemDrive
23+
}
24+
25+
It 'Returns the value of $env:ChocolateyInstall if set' {
26+
$env:ChocolateyInstall | Should -Not -BeNullOrEmpty -Because 'it should be set for this test'
27+
Get-ChocolateyPath -PathType InstallPath | Should -BeExactly $env:ChocolateyInstall
28+
}
29+
30+
# Skip this if the system choco install path does not exist, it will return null in that case, and we shouldn't be creating paths
31+
# at this level for a test.
32+
It 'Falls back to $env:ProgramData\chocolatey if $env:ChocolateyInstall is not set' -Skip:(-not (Test-Path "$env:ProgramData\chocolatey")) {
33+
$env:ChocolateyInstall = ''
34+
Get-ChocolateyPath -PathType InstallPath | Should -Be "$env:ProgramData\chocolatey"
35+
}
36+
37+
# Skip this if the system choco install path does not exist, it will return null in that case, and we shouldn't be creating paths
38+
# at this level for a test.
39+
It 'Falls back to $env:SystemDrive\ProgramData\chocolatey if $env:ChocolateyInstall and $env:ProgramData are not set' -Skip:(-not (Test-Path "$env:SystemDrive\ProgramData\chocolatey")) {
40+
$env:ChocolateyInstall = ''
41+
$env:ProgramData = ''
42+
Get-ChocolateyPath -PathType InstallPath | Should -Be "$env:SystemDrive\ProgramData\chocolatey"
43+
}
44+
45+
It 'Falls back to a path relative to the DLL location if none of the environment variables are set' {
46+
$env:SystemDrive = ''
47+
$env:ChocolateyInstall = ''
48+
$env:ProgramData = ''
49+
$expectedPath = [Chocolatey.PowerShell.Helpers.PSHelper].Assembly.Location | Split-Path -Parent | Split-Path -Parent
50+
Get-ChocolateyPath -PathType InstallPath | Should -Be $expectedPath
51+
}
52+
}
53+
54+
Context '-PathType InstallPath (Path does not exist)' {
55+
BeforeAll {
56+
$programData = $env:ProgramData
57+
$systemDrive = $env:SystemDrive
58+
}
59+
60+
AfterAll {
61+
$env:ChocolateyInstall = $testLocation
62+
$env:ProgramData = $programData
63+
$env:SystemDrive = $systemDrive
64+
}
65+
66+
It 'Returns null if $env:ChocolateyInstall path does not exist' {
67+
$env:ChocolateyInstall = "$env:TEMP\DummyFolderDoesNotExist"
68+
Get-ChocolateyPath -PathType InstallPath | Should -BeNullOrEmpty
69+
}
70+
71+
It 'Returns null if $env:ProgramData\chocolatey does not exist and $env:ChocolateyInstall is not set' {
72+
$env:ChocolateyInstall = ''
73+
$env:ProgramData = "$env:TEMP\DummyFolderDoesNotExist"
74+
Get-ChocolateyPath -PathType InstallPath | Should -BeNullOrEmpty
75+
}
76+
}
77+
78+
Context '-PathType PackagePath (Path exists)' {
79+
BeforeAll {
80+
$TestPackageFolder = "$env:TEMP\test"
81+
New-Item -ItemType Directory -Path $TestPackageFolder -Force > $null
82+
83+
$env:ChocolateyPackageName = 'test'
84+
85+
$installPath = Get-ChocolateyPath -PathType InstallPath
86+
$expectedLibPath = Join-Path -Path $installPath -ChildPath "lib\$env:ChocolateyPackageName"
87+
New-Item -ItemType Directory -Path $expectedLibPath > $null
88+
}
89+
90+
AfterEach {
91+
$env:ChocolateyPackageFolder = ''
92+
$env:PackageFolder = ''
93+
}
94+
95+
AfterAll {
96+
$env:ChocolateyPackageName = ''
97+
Remove-Item -Path $expectedLibPath -Force -Recurse
98+
}
99+
100+
It 'Returns the value of $env:ChocolateyPackageFolder if set' {
101+
$env:ChocolateyPackageFolder = $TestPackageFolder
102+
Get-ChocolateyPath -PathType PackagePath | Should -BeExactly $env:ChocolateyPackageFolder
103+
}
104+
105+
It 'Returns the value of $env:PackageFolder if set and $env:ChocolateyPackageFolder is not set' {
106+
$env:PackageFolder = $TestPackageFolder
107+
Get-ChocolateyPath -PathType PackagePath | Should -BeExactly $env:PackageFolder
108+
}
109+
110+
It 'Falls back to "{InstallPath}\lib\$env:ChocolateyPackageName" if neither of the PackageFolder variables are set' {
111+
Get-ChocolateyPath -PathType PackagePath | Should -Be $expectedLibPath
112+
}
113+
}
114+
115+
Context '-PathType PackagePath (Path does not exist)' {
116+
AfterEach {
117+
$env:ChocolateyPackageFolder = ''
118+
$env:PackageFolder = ''
119+
$env:ChocolateyPackageName = ''
120+
}
121+
122+
It 'Returns null if $env:ChocolateyPackageFolder does not exist' {
123+
$env:ChocolateyPackageFolder = "$env:TEMP\DummyFolderDoesNotExist"
124+
Get-ChocolateyPath -PathType PackagePath | Should -BeNullOrEmpty
125+
}
126+
127+
It 'Returns null if $env:PackageFolder does not exist and $env:ChocolateyPackageFolder is not set' {
128+
$env:PackageFolder = 'C:\DummyFolderDoesNotExist'
129+
Get-ChocolateyPath -PathType PackagePath | Should -BeNullOrEmpty
130+
}
131+
132+
It 'Returns null if "{InstallPath}\lib\$env:ChocolateyPackageName" does not exist and neither of the PackageFolder variables are set' {
133+
$env:ChocolateyPackageName = 'test'
134+
$installPath = Get-ChocolateyPath -PathType InstallPath
135+
136+
$expectedPath = Join-Path -Path $installPath -ChildPath "lib\$env:ChocolateyPackageName"
137+
Get-ChocolateyPath -PathType PackagePath | Should -BeNullOrEmpty
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)