1+ Describe ' Expand-ChocolateyArchive helper function tests' - Tags ExpandChocolateyArchive, Cmdlets {
2+ BeforeAll {
3+ Initialize-ChocolateyTestInstall
4+
5+ $testLocation = Get-ChocolateyTestLocation
6+ }
7+
8+ Context ' Unit tests' - Tags WhatIf {
9+ BeforeAll {
10+ $Guid = New-Guid
11+ $Path = " $env: TEMP \$Guid .zip"
12+ $Path64 = " $env: TEMP \$Guid -x64.zip"
13+ $Destination = " $env: TEMP \$Guid "
14+ $LogPath = " $env: TEMP \$Guid -packagefolder"
15+ $PackageName = " $Guid "
16+
17+ $tempFile = New-Item - Path $Path
18+ $tempFile64 = New-Item - Path $Path64
19+ }
20+
21+ AfterAll {
22+ $tempFile , $tempFile64 | Remove-Item - Force
23+ }
24+
25+ It ' extracts the target zip file specified by <_> to the expected location' - TestCases @ (' -Path' , ' -Path64' ) {
26+ $Preamble = [scriptblock ]::Create(" Import-Module '$testLocation \helpers\chocolateyInstaller.psm1'" )
27+ $Command = [scriptblock ]::Create(" Expand-ChocolateyArchive $_ '$Path ' -Destination '$Destination ' -WhatIf" )
28+
29+ $expectedResults = @ (
30+ " What if: Performing the operation `" Create Directory`" on target `" $Destination `" ."
31+ " What if: Performing the operation `" Extract zip file contents to '$Destination ' with 7-Zip`" on target `" $Path `" ."
32+ )
33+
34+ $results = Get-WhatIfResult - Preamble $Preamble - Command $Command
35+ $results.WhatIf | Should - BeExactly $expectedResults
36+ }
37+
38+ It ' always uses -Path if the chocolateyForceX86 environment variable is set' {
39+ $Preamble = [scriptblock ]::Create(@"
40+ `$ env:chocolateyForceX86 = 'true'
41+ Import-Module '$testLocation \helpers\chocolateyInstaller.psm1'
42+ "@ )
43+ $Command = [scriptblock ]::Create(" Expand-ChocolateyArchive -Path '$Path ' -Path64 '$Path64 ' -Destination '$Destination ' -WhatIf" )
44+ $results = Get-WhatIfResult - Preamble $Preamble - Command $Command
45+
46+ $expectedResults = @ (
47+ " What if: Performing the operation `" Create Directory`" on target `" $Destination `" ."
48+ " What if: Performing the operation `" Extract zip file contents to '$Destination ' with 7-Zip`" on target `" $Path `" ."
49+ )
50+ $results.WhatIf | Should - BeExactly $expectedResults
51+ }
52+
53+ It ' creates a lib folder to store the log files if needed when -PackageName is set' {
54+ $Preamble = [scriptblock ]::Create(@"
55+ Import-Module '$testLocation \helpers\chocolateyInstaller.psm1'
56+ `$ env:chocolateyPackageFolder = '$LogPath '
57+ "@ )
58+ $Command = [scriptblock ]::Create(" Expand-ChocolateyArchive -Path '$Path ' -Destination '$Destination ' -PackageName $PackageName -WhatIf" )
59+ $results = Get-WhatIfResult - Preamble $Preamble - Command $Command
60+
61+ $expectedResult = " What if: Performing the operation `" Create Directory`" on target `" $LogPath `" ."
62+ $results.WhatIf | Should - Contain $expectedResult
63+ }
64+
65+ It ' creates a lib folder to store the log files if needed when chocolateyPackageName environment variable is set' {
66+ $Preamble = [scriptblock ]::Create(@"
67+ Import-Module '$testLocation \helpers\chocolateyInstaller.psm1'
68+ `$ env:chocolateyPackageFolder = '$LogPath '
69+ `$ env:chocolateyPackageName = '$PackageName '
70+ "@ )
71+ $Command = [scriptblock ]::Create(" Expand-ChocolateyArchive -Path '$Path ' -Destination '$Destination ' -WhatIf" )
72+ $results = Get-WhatIfResult - Preamble $Preamble - Command $Command
73+
74+ $expectedResult = " What if: Performing the operation `" Create Directory`" on target `" $LogPath `" ."
75+ $results.WhatIf | Should - Contain $expectedResult
76+ }
77+
78+ It ' uses 7zip to decompress by default' {
79+ $Preamble = [scriptblock ]::Create(" Import-Module '$testLocation \helpers\chocolateyInstaller.psm1'" )
80+ $Command = [scriptblock ]::Create(" Expand-ChocolateyArchive -Path '$Path ' -Destination '$Destination ' -WhatIf" )
81+ $results = Get-WhatIfResult - Preamble $Preamble - Command $Command
82+
83+ $expectedResult = " What if: Performing the operation `" Extract zip file contents to '$Destination ' with 7-Zip`" on target `" $Path `" ."
84+ $results.WhatIf | Should - Contain $expectedResult
85+ }
86+
87+ It ' will use the fallback builtin extraction method if using -UseBuiltinCompression' {
88+ $Preamble = [scriptblock ]::Create(" Import-Module '$testLocation \helpers\chocolateyInstaller.psm1'" )
89+ $Command = [scriptblock ]::Create(" Expand-ChocolateyArchive -Path '$Path ' -Destination '$Destination ' -UseBuiltInCompression -WhatIf" )
90+ $results = Get-WhatIfResult - Preamble $Preamble - Command $Command
91+
92+ $expectedResult = " What if: Performing the operation `" Extract zip file contents to '$Destination ' with built-in decompression`" on target `" $Path `" ."
93+ $results.WhatIf | Should - Contain $expectedResult
94+ }
95+
96+ It ' will use the fallback builtin extraction method if specified by chocolateyUseBuiltinCompression environment variable' {
97+ $Preamble = [scriptblock ]::Create(@"
98+ Import-Module '$testLocation \helpers\chocolateyInstaller.psm1'
99+ `$ env:chocolateyUseBuiltinCompression = 'true'
100+ "@ )
101+ $Command = [scriptblock ]::Create(" Expand-ChocolateyArchive -Path '$Path ' -Destination '$Destination ' -WhatIf" )
102+ $results = Get-WhatIfResult - Preamble $Preamble - Command $Command
103+
104+ $expectedResult = " What if: Performing the operation `" Extract zip file contents to '$Destination ' with built-in decompression`" on target `" $Path `" ."
105+ $results.WhatIf | Should - Contain $expectedResult
106+ }
107+
108+ It ' applies a -FilesToExtract filter when provided' {
109+ $Preamble = [scriptblock ]::Create(" Import-Module '$testLocation \helpers\chocolateyInstaller.psm1'" )
110+ $Command = [scriptblock ]::Create(" Expand-ChocolateyArchive -Path '$Path ' -Destination '$Destination ' -WhatIf -FilesToExtract '*.exe'" )
111+
112+ $expectedResults = @ (
113+ " What if: Performing the operation `" Create Directory`" on target `" $Destination `" ."
114+ " What if: Performing the operation `" Extract zip file contents matching pattern *.exe to '$Destination ' with 7-Zip`" on target `" $Path `" ."
115+ )
116+
117+ $results = Get-WhatIfResult - Preamble $Preamble - Command $Command
118+ $results.WhatIf | Should - BeExactly $expectedResults
119+ }
120+ }
121+
122+ Context ' Integration tests' {
123+ BeforeAll {
124+ Import-Module " $testLocation \helpers\chocolateyInstaller.psm1" - Force
125+
126+ $Guid = New-Guid
127+ $Path = " $env: TEMP \$Guid .zip"
128+ $Path64 = " $env: TEMP \$Guid -x64.zip"
129+ $Destination = " $env: TEMP \$Guid "
130+
131+ $LogPath = " $env: TEMP \$Guid -packagefolder"
132+ $PackageName = " $Guid "
133+
134+ $TempFiles = 1 .. 10 | ForEach-Object { @ { Id = $_ ; File = New-TemporaryFile } }
135+ foreach ($file in $TempFiles ) {
136+ # Populate the file with random data
137+ $file.Content = 1 .. 100 | ForEach-Object { (New-Guid ).ToString() }
138+ $file.Content | Set-Content - Path $file.File
139+ }
140+
141+ Compress-Archive - Path $TempFiles.File.FullName - DestinationPath $Path
142+ New-Item - ItemType Directory - Path $LogPath > $null
143+
144+ $env: chocolateyPackageName = $PackageName
145+ $env: chocolateyPackageFolder = $LogPath
146+ }
147+
148+ AfterAll {
149+ $cleanupItems = @ (
150+ $TempFiles.File.FullName
151+ $Path
152+ $Destination
153+ $LogPath
154+ )
155+ Remove-Item - Path $cleanupItems - Force - Recurse - ErrorAction Ignore
156+
157+ $env: chocolateyPackageName = ' '
158+ $env: chocolateyPackageFolder = ' '
159+ }
160+
161+ Describe ' Invalid parameters' {
162+ BeforeAll {
163+ $env: chocolateyForceX86 = ' true'
164+ }
165+
166+ AfterAll {
167+ $env: chocolateyForceX86 = ' '
168+ }
169+
170+ It ' throws an error if using only -Path64 with the chocolateyForceX86 environment variable set' {
171+ { Expand-ChocolateyArchive - Path64 $Path64 - Destination $Destination } |
172+ Should - Throw - ExpectedMessage " 32-bit archive is not supported for $PackageName " - ExceptionType ' System.NotSupportedException'
173+ }
174+ }
175+
176+ Describe ' Using <Mode> decompression' - ForEach @ (
177+ @ { Mode = ' 7zip' }
178+ @ { Mode = ' fallback' }
179+ ) {
180+ Context ' No filter' {
181+ It ' completes successfully and returns the destination path' {
182+ $params = @ {
183+ Path = $Path
184+ Destination = $Destination
185+ UseBuiltinCompression = $Mode -eq ' fallback'
186+ }
187+ $result = Expand-ChocolateyArchive @params
188+
189+ $result | Should - BeExactly $Destination
190+ }
191+
192+ It ' extracts the files from the archive' {
193+ $extractedFiles = Get-ChildItem - Path $Destination
194+ $extractedFiles.Count | Should - Be $TempFiles.Count
195+
196+ foreach ($file in $extractedFiles ) {
197+ $expectedFile = $TempFiles | Where-Object { $_.File.Name -eq $file.Name }
198+ $expectedFile | Should -Not - BeNullOrEmpty - Because ' we should not have any unexpected files extracted'
199+
200+ $content = Get-Content - Path $file.FullName
201+ $content | Should - BeExactly $expectedFile.Content - Because " $ ( $file.Name ) should have the same content as it originally did"
202+ }
203+ }
204+
205+ It ' writes a log file to the package folder' {
206+ $log = Get-ChildItem - Path $LogPath - File
207+
208+ $log | Should -Not - BeNullOrEmpty - Because ' the command should have written an extraction log'
209+
210+ $expectedContent = $TempFiles.File.Name | ForEach-Object { Join-Path $Destination - ChildPath $_ }
211+ Get-Content - Path $log | Should - BeExactly $expectedContent
212+ }
213+ }
214+
215+ Context ' Filter for specific files' {
216+ BeforeAll {
217+ $TargetFile = $TempFiles | Select-Object - First 1
218+ $SpecificDestination = " $Destination -test"
219+ }
220+
221+ AfterAll {
222+ Remove-Item - Path $SpecificDestination - Force - Recurse - ErrorAction Ignore
223+ }
224+
225+ It ' completes successfully and returns the destination path' {
226+ $params = @ {
227+ Path = $Path
228+ Destination = $SpecificDestination
229+ UseBuiltinCompression = $Mode -eq ' fallback'
230+ FilesToExtract = $TargetFile.File.Name
231+ }
232+ $result = Expand-ChocolateyArchive @params
233+
234+ $result | Should - BeExactly $SpecificDestination
235+ }
236+
237+ It ' extracts the file from the archive' {
238+ $extractedFiles = Get-ChildItem - Path $SpecificDestination
239+ @ ($extractedFiles ).Count | Should - Be 1
240+
241+ $extractedFiles.Name | Should - BeExactly $TargetFile.File.Name
242+
243+ $extractedContent = Get-Content - Path $extractedFiles.FullName
244+ $extractedContent | Should - BeExactly $TargetFile.Content - Because " $ ( $extractedFiles.Name ) should have the same content as it originally did"
245+ }
246+
247+ It ' writes a log file to the package folder' {
248+ $log = Get-ChildItem - Path $LogPath - File
249+
250+ $log | Should -Not - BeNullOrEmpty - Because ' the command should have written an extraction log'
251+
252+ $expectedContent = Join-Path $SpecificDestination - ChildPath $TargetFile.File.Name
253+ Get-Content - Path $log | Should - BeExactly $expectedContent
254+ }
255+ }
256+ }
257+ }
258+ }
0 commit comments