1+ BeforeAll {
2+ $global :LASTEXITCODE = 0
3+ Function Expand-Archive {
4+ param (
5+ [string ]$Path ,
6+ [string ]$DestinationPath ,
7+ [switch ]$Force
8+ )
9+ }
10+
11+ Mock Expand-Archive {
12+ switch ($Path ) {
13+ (Join-Path $TestDrive " test.zip" ) {
14+ # Simulate successful expansion
15+ # Write-Output "test.zip expanded successfully"
16+ $global :LASTEXITCODE = 0
17+ return
18+ }
19+ (Join-Path $TestDrive " bad.zip" ) {
20+ # Write-Output "bad.zip encountered an error"
21+ # Simulate failed expansion
22+ throw " Simulated bad zip"
23+ return
24+ }
25+ (Join-Path $TestDrive " testdest.zip" ) {
26+ switch ($DestinationPath ) {
27+ (Join-Path $TestDrive " extracted" ) {
28+ # Write-Output "testdest.zip expanded successfully"
29+ # Simulate successful extraction
30+ $global :LASTEXITCODE = 0
31+ return
32+ }
33+ (Join-Path $TestDrive " badextract" ) {
34+ # Write-Output "testdest.zip encountered an error"
35+ # Simulate failed extraction
36+ throw " Simulated bad destination"
37+ return
38+ }
39+ default {
40+ # Write-Output "Invalid destination: $DestinationPath"
41+ # Simulate invalid destination
42+ $global :LASTEXITCODE = 1
43+ throw " Invalid destination: $DestinationPath "
44+ }
45+ }
46+ }
47+ default {
48+ Write-Error " File not found: $Path "
49+ # Simulate file not found
50+ $global :LASTEXITCODE = 1
51+ throw " File not found: $Path "
52+ }
53+ }
54+ # Simulate successful expansion
55+ $global :LASTEXITCODE = 1
56+ }
57+ . $PSScriptRoot \Expand-DevSetupUpdateArchive.ps1
58+ . $PSScriptRoot \..\Utils\Write-StatusMessage.ps1
59+ }
60+
61+ Describe " Expand-DevSetupUpdateArchive" {
62+
63+ Context " When the archive file does not exist" {
64+ It " Should return false and log an error" {
65+ Mock Write-StatusMessage { }
66+ $Archive = (Join-Path $TestDrive " nonexistent.zip" )
67+ $result = Expand-DevSetupUpdateArchive - Path $Archive - DestinationPath (Join-Path $TestDrive " temp" )
68+ $result | Should - Be $false
69+ Assert-MockCalled Write-StatusMessage - Exactly 1 - Scope It - ParameterFilter {
70+ $Message -match " Archive file not found at path: $ ( [regex ]::Escape($Archive )) " -and $Verbosity -eq " Error"
71+ }
72+ }
73+ }
74+
75+ Context " When the archive expansion fails" {
76+ It " Should return false and log an error" {
77+ Mock Write-StatusMessage { }
78+ Mock Test-Path { $true } - ParameterFilter { $Path -eq (Join-Path $TestDrive " bad.zip" ) }
79+ $badArchive = (Join-Path $TestDrive " bad.zip" )
80+ $goodDestination = (Join-Path $TestDrive " extracted" )
81+ $result = Expand-DevSetupUpdateArchive - Path $badArchive - DestinationPath $goodDestination
82+ $result | Should - Be $false
83+ Assert-MockCalled Expand-Archive - Exactly 1 - Scope It - ParameterFilter {
84+ $Path -eq $badArchive -and $DestinationPath -eq $goodDestination -and $Force
85+ }
86+ Assert-MockCalled Write-StatusMessage - Scope It - ParameterFilter {
87+ $Message -match " Expanding archive file from $ ( [regex ]::Escape($badArchive )) to $ ( [regex ]::Escape($goodDestination )) " -and $Verbosity -eq " Debug"
88+ } - Exactly 1
89+ }
90+ }
91+
92+ Context " When the archive expansion fails with bad destination" {
93+ It " Should return false and log an error" {
94+ Mock Write-StatusMessage { }
95+ Mock Test-Path { $true } - ParameterFilter { $Path -eq (Join-Path $TestDrive " testdest.zip" ) }
96+ $goodArchive = (Join-Path $TestDrive " testdest.zip" )
97+ $badDestination = (Join-Path $TestDrive " badextract" )
98+ $result = Expand-DevSetupUpdateArchive - Path $goodArchive - DestinationPath $badDestination
99+ $result | Should - Be $false
100+ Assert-MockCalled Expand-Archive - Exactly 1 - Scope It - ParameterFilter {
101+ $Path -eq $goodArchive -and $DestinationPath -eq $badDestination -and $Force
102+ }
103+ Assert-MockCalled Write-StatusMessage - Scope It - ParameterFilter {
104+ $Message -match " Expanding archive file from $ ( [regex ]::Escape($goodArchive )) to $ ( [regex ]::Escape($badDestination )) " -and $Verbosity -eq " Debug"
105+ } - Exactly 1
106+ }
107+ }
108+
109+ Context " When the archive expansion fails with invalid destination" {
110+ It " Should return false and log an error" {
111+ Mock Write-StatusMessage { }
112+ Mock Test-Path { $true } - ParameterFilter { $Path -eq (Join-Path $TestDrive " testdest.zip" ) }
113+ $goodArchive = (Join-Path $TestDrive " testdest.zip" )
114+ $invalidDestination = (Join-Path $TestDrive " invalid\path" )
115+ $result = Expand-DevSetupUpdateArchive - Path $goodArchive - DestinationPath $invalidDestination
116+ $result | Should - Be $false
117+ Assert-MockCalled Expand-Archive - Exactly 1 - Scope It - ParameterFilter {
118+ $Path -eq $goodArchive -and $DestinationPath -eq $invalidDestination -and $Force
119+ }
120+ Assert-MockCalled Write-StatusMessage - Scope It - ParameterFilter {
121+ $Message -match " Expanding archive file from $ ( [regex ]::Escape($goodArchive )) to $ ( [regex ]::Escape($invalidDestination )) " -and $Verbosity -eq " Debug"
122+ } - Exactly 1
123+ Assert-MockCalled Write-StatusMessage - Scope It - ParameterFilter {
124+ $Message -match " Failed to expand archive:" -and $Verbosity -eq " Error"
125+ } - Exactly 1
126+ Assert-MockCalled Write-StatusMessage - Scope It - ParameterFilter {
127+ $Verbosity -eq " Error"
128+ } - Exactly 2
129+ }
130+ }
131+
132+ Context " When the archive expansion succeeds" {
133+ It " Should return true and log debug messages" {
134+ Mock Write-StatusMessage { }
135+ Mock Test-Path { $true } - ParameterFilter { $Path -eq (Join-Path $TestDrive " test.zip" ) }
136+ $goodArchive = (Join-Path $TestDrive " test.zip" )
137+ $goodDestination = (Join-Path $TestDrive " extracted" )
138+ $result = Expand-DevSetupUpdateArchive - Path $goodArchive - DestinationPath $goodDestination
139+ $result | Should - Be $true
140+ Assert-MockCalled Expand-Archive - Exactly 1 - Scope It - ParameterFilter {
141+ $Path -eq $goodArchive -and $DestinationPath -eq $goodDestination -and $Force
142+ }
143+ Assert-MockCalled Write-StatusMessage - Scope It - ParameterFilter {
144+ $Message -match " Expanding archive file from $ ( [regex ]::Escape($goodArchive )) to $ ( [regex ]::Escape($goodDestination )) " -and $Verbosity -eq " Debug"
145+ } - Exactly 1
146+ Assert-MockCalled Write-StatusMessage - Scope It - ParameterFilter {
147+ $Message -match " Expansion completed successfully." -and $Verbosity -eq " Debug"
148+ } - Exactly 1
149+ }
150+ }
151+ }
0 commit comments