This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathPSGetUnInstallScript.Tests.ps1
More file actions
340 lines (280 loc) · 14.3 KB
/
Copy pathPSGetUnInstallScript.Tests.ps1
File metadata and controls
340 lines (280 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<#####################################################################################
# File: PSGetUninstallScriptTests.ps1
# Tests for PSGet module functionality
#
# Copyright (c) Microsoft Corporation, 2015
#####################################################################################>
<#
Name: PowerShell.PSGet.UninstallScriptTests
Description: Tests for Uninstall-Script cmdlet functionality
Local PSGet Test Gallery (ex: http://localhost:8765/packages) is pre-populated with static scripts:
Fabrikam-ClientScript: versions 1.0, 1.5, 2.0, 2.5
Fabrikam-ServerScript: versions 1.0, 1.5, 2.0, 2.5
#>
function SuiteSetup {
Import-Module "$PSScriptRoot\PSGetTestUtils.psm1" -WarningAction SilentlyContinue
Import-Module "$PSScriptRoot\Asserts.psm1" -WarningAction SilentlyContinue
$script:PSGetLocalAppDataPath = Get-PSGetLocalAppDataPath
$script:TempPath = Get-TempPath
#Bootstrap NuGet binaries
Install-NuGetBinaries
$psgetModuleInfo = Import-Module PowerShellGet -Global -Force -Passthru
Import-LocalizedData script:LocalizedData -filename PSGet.Resource.psd1 -BaseDirectory $psgetModuleInfo.ModuleBase
$script:moduleSourcesFilePath= Join-Path $script:PSGetLocalAppDataPath "PSRepositories.xml"
$script:moduleSourcesBackupFilePath = Join-Path $script:PSGetLocalAppDataPath "PSRepositories.xml_$(get-random)_backup"
if(Test-Path $script:moduleSourcesFilePath)
{
Rename-Item $script:moduleSourcesFilePath $script:moduleSourcesBackupFilePath -Force
}
GetAndSet-PSGetTestGalleryDetails -IsScriptSuite -SetPSGallery
Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force
Get-InstalledScript -Name Fabrikam-ClientScript -ErrorAction SilentlyContinue | Uninstall-Script -Force
$script:AddedAllUsersInstallPath = Set-PATHVariableForScriptsInstallLocation -Scope AllUsers
$script:AddedCurrentUserInstallPath = Set-PATHVariableForScriptsInstallLocation -Scope CurrentUser
}
function SuiteCleanup {
if(Test-Path $script:moduleSourcesBackupFilePath)
{
Move-Item $script:moduleSourcesBackupFilePath $script:moduleSourcesFilePath -Force
}
else
{
RemoveItem $script:moduleSourcesFilePath
}
# Import the PowerShellGet provider to reload the repositories.
$null = Import-PackageProvider -Name PowerShellGet -Force
if($script:AddedAllUsersInstallPath)
{
Reset-PATHVariableForScriptsInstallLocation -Scope AllUsers
}
if($script:AddedCurrentUserInstallPath)
{
Reset-PATHVariableForScriptsInstallLocation -Scope CurrentUser
}
}
Describe PowerShell.PSGet.UninstallScriptTests -Tags 'BVT','InnerLoop' {
BeforeAll {
SuiteSetup
}
AfterAll {
SuiteCleanup
}
AfterEach {
Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force
Get-InstalledScript -Name Fabrikam-ClientScript -ErrorAction SilentlyContinue | Uninstall-Script -Force
}
It ValidateGetInstalledScriptAndUninstallScriptCmdletsWithMinimumVersion {
$ScriptName = 'Fabrikam-ServerScript'
$version = "2.0"
try
{
Install-Script -Name $ScriptName -RequiredVersion $version -Force
$script = Get-InstalledScript -Name $ScriptName -MinimumVersion 1.0
AssertEquals $script.Name $ScriptName "Get-InstalledScript is not working properly, $script"
AssertEquals $script.Version $Version "Get-InstalledScript is not working properly, $script"
}
finally
{
PowerShellGet\Uninstall-Script -Name $ScriptName -MinimumVersion $Version
$script = Get-InstalledScript -Name $ScriptName -ErrorAction SilentlyContinue
AssertNull $script "Script uninstallation is not working properly, $script"
}
}
It ValidateGetInstalledScriptAndUninstallScriptCmdletWithMinMaxRange {
$ScriptName = 'Fabrikam-ServerScript'
$version = "2.0"
try
{
Install-Script -Name $ScriptName -RequiredVersion $version -Force
$script = Get-InstalledScript -Name $ScriptName -MinimumVersion $Version -MaximumVersion $Version
AssertEquals $script.Name $ScriptName "Get-InstalledScript is not working properly, $script"
AssertEquals $script.Version $Version "Get-InstalledScript is not working properly, $script"
}
finally
{
PowerShellGet\Uninstall-Script -Name $ScriptName -MinimumVersion $Version -MaximumVersion $Version
$script = Get-InstalledScript -Name $ScriptName -ErrorAction SilentlyContinue
AssertNull $script "Script uninstallation is not working properly, $script"
}
}
It ValidateGetInstalledScriptAndUninstallScriptCmdletWithRequiredVersion {
$ScriptName = 'Fabrikam-ServerScript'
$version = "2.0"
try
{
Install-Script -Name $ScriptName -RequiredVersion $version -Force
$script = Get-InstalledScript -Name $ScriptName -RequiredVersion $Version
AssertEquals $script.Name $ScriptName "Get-InstalledScript is not working properly, $script"
AssertEquals $script.Version $Version "Get-InstalledScript is not working properly, $script"
}
finally
{
PowerShellGet\Uninstall-Script -Name $ScriptName -RequiredVersion $Version
$script = Get-InstalledScript -Name $ScriptName -ErrorAction SilentlyContinue
AssertNull $script "Script uninstallation is not working properly, $script"
}
}
It ValidateGetInstalledScriptAndUninstallScriptCmdletWithMiximumVersion {
$ScriptName = 'Fabrikam-ServerScript'
$version = "2.0"
try
{
Install-Script -Name $ScriptName -RequiredVersion $version -Force
$script = Get-InstalledScript -Name $ScriptName -MaximumVersion $Version
AssertEquals $script.Name $ScriptName "Get-InstalledScript is not working properly, $script"
AssertEquals $script.Version $Version "Get-InstalledScript is not working properly, $script"
}
finally
{
PowerShellGet\Uninstall-Script -Name $ScriptName -RequiredVersion $Version
$script = Get-InstalledScript -Name $ScriptName -ErrorAction SilentlyContinue
AssertNull $script "Script uninstallation is not working properly, $script"
}
}
# Purpose: UninstallScriptWithWhatIf
#
# Action: Find-Script Fabrikam-ServerScript | Install-Script | Uninstall-Script -WhatIf
#
# Expected Result: it should not uninstall the script
#
It "UninstallScriptWithWhatIf" {
$outputPath = $script:TempPath
$guid = [system.guid]::newguid().tostring()
$outputFilePath = Join-Path $outputPath "$guid"
$runspace = CreateRunSpace $outputFilePath 1
$content = $null
try
{
Find-Script Fabrikam-ServerScript | Install-Script
$result = ExecuteCommand $runspace 'PowerShellGet\Uninstall-Script Fabrikam-ServerScript -whatif'
}
finally
{
$fileName = "WriteLine-0.txt"
$path = join-path $outputFilePath $fileName
if(Test-Path $path)
{
$content = get-content $path
}
CloseRunSpace $runspace
RemoveItem $outputFilePath
}
$itemInfo = Find-Script Fabrikam-ServerScript -Repository PSGallery
$uninstallShouldProcessMessage = $script:LocalizedData.InstallScriptwhatIfMessage -f ($itemInfo.Name, $itemInfo.Version)
Assert ($content -and ($content -match $uninstallShouldProcessMessage)) "Uninstall script whatif message is missing, Expected:$uninstallShouldProcessMessage, Actual:$content"
$res = Get-InstalledScript Fabrikam-ServerScript
Assert ($res) "Uninstall-Script should not uninstall the script with -WhatIf option"
} `
-Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0'))
# Purpose: UninstallScriptWithConfirmAndNoToPrompt
#
# Action: Uninstall-Script Fabrikam-ServerScript -Confirm
#
# Expected Result: script should not be uninstalled after confirming NO
#
It "UninstallScriptWithConfirmAndNoToPrompt" {
$outputPath = $script:TempPath
$guid = [system.guid]::newguid().tostring()
$outputFilePath = Join-Path $outputPath "$guid"
$runspace = CreateRunSpace $outputFilePath 1
# 2 is mapped to NO in ShouldProcess prompt
$Global:proxy.UI.ChoiceToMake=2
$content = $null
try
{
Install-Script Fabrikam-ServerScript -Repository PSGallery -force
$result = ExecuteCommand $runspace 'PowerShellGet\Uninstall-Script Fabrikam-ServerScript -Confirm'
}
finally
{
$fileName = "PromptForChoice-0.txt"
$path = join-path $outputFilePath $fileName
if(Test-Path $path)
{
$content = get-content $path
}
CloseRunSpace $runspace
RemoveItem $outputFilePath
}
$itemInfo = Find-Script Fabrikam-ServerScript -Repository PSGallery
$UninstallShouldProcessMessage = $script:LocalizedData.InstallScriptwhatIfMessage -f ($itemInfo.Name, $itemInfo.Version)
Assert ($content -and ($content -match $UninstallShouldProcessMessage)) "Uninstall script confirm prompt is not working, Expected:$UninstallShouldProcessMessage, Actual:$content"
$res = Get-InstalledScript Fabrikam-ServerScript
Assert ($res) "Uninstall-Script should not uninstall the script if confirm is not accepted"
} `
-Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0'))
# Purpose: UninstallScriptWithConfirmAndYesToPrompt
#
# Action: Find-Script Fabrikam-ServerScript | Install-Script | UninstallScript-Confirm
#
# Expected Result: script should be uninstalled after confirming YES
#
It "UninstallScriptWithConfirmAndYesToPrompt" {
$outputPath = $script:TempPath
$guid = [system.guid]::newguid().tostring()
$outputFilePath = Join-Path $outputPath "$guid"
$runspace = CreateRunSpace $outputFilePath 1
# 0 is mapped to YES in ShouldProcess prompt
$Global:proxy.UI.ChoiceToMake=0
$content = $null
try
{
Find-Script Fabrikam-ServerScript | Install-Script
$result = ExecuteCommand $runspace 'PowerShellGet\Uninstall-Script Fabrikam-ServerScript -Confirm'
}
finally
{
$fileName = "PromptForChoice-0.txt"
$path = join-path $outputFilePath $fileName
if(Test-Path $path)
{
$content = get-content $path
}
CloseRunSpace $runspace
RemoveItem $outputFilePath
}
$itemInfo = Find-Script Fabrikam-ServerScript -Repository PSGallery
$UninstallShouldProcessMessage = $script:LocalizedData.InstallScriptwhatIfMessage -f ($itemInfo.Name, $itemInfo.Version)
Assert ($content -and ($content -match $UninstallShouldProcessMessage)) "Uninstall script confirm prompt is not working, Expected:$UninstallShouldProcessMessage, Actual:$content"
$res = Get-InstalledScript Fabrikam-ServerScript -ErrorAction SilentlyContinue
AssertNull $res "Uninstall-Script should uninstall a script if Confirm is not accepted"
} `
-Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0'))
}
Describe PowerShell.PSGet.UninstallScriptTests.ErrorCases -Tags 'P1','InnerLoop','RI' {
BeforeAll {
SuiteSetup
}
AfterAll {
SuiteCleanup
}
# Uninstall-Script error cases
It ValidateUninstallScriptWithMultiNamesAndRequiredVersion {
AssertFullyQualifiedErrorIdEquals -scriptblock {PowerShellGet\Uninstall-Script -Name Fabrikam-ClientScript,Fabrikam-ServerScript -RequiredVersion 3.0 } `
-expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Uninstall-Script"
}
It ValidateUninstallScriptWithMultiNamesAndMinVersion {
AssertFullyQualifiedErrorIdEquals -scriptblock {PowerShellGet\Uninstall-Script -Name Fabrikam-ClientScript,Fabrikam-ServerScript -MinimumVersion 3.0 } `
-expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Uninstall-Script"
}
It ValidateUninstallScriptWithMultiNamesAndMaxVersion {
AssertFullyQualifiedErrorIdEquals -scriptblock {PowerShellGet\Uninstall-Script -Name Fabrikam-ClientScript,Fabrikam-ServerScript -MaximumVersion 3.0 } `
-expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Uninstall-Script"
}
It ValidateUninstallScriptWithSingleWildcardName {
AssertFullyQualifiedErrorIdEquals -scriptblock {PowerShellGet\Uninstall-Script -Name Fabrikam-Client*ipt} `
-expectedFullyQualifiedErrorId "NameShouldNotContainWildcardCharacters,Uninstall-Script"
}
It ValidateUninstallScriptWithSingleNameRequiredandMinVersion {
AssertFullyQualifiedErrorIdEquals -scriptblock {PowerShellGet\Uninstall-Script -Name Fabrikam-ClientScript -RequiredVersion 3.0 -MinimumVersion 1.0 } `
-expectedFullyQualifiedErrorId "VersionRangeAndRequiredVersionCannotBeSpecifiedTogether,Uninstall-Script"
}
It ValidateUninstallScriptWithSingleNameRequiredandMaxVersion {
AssertFullyQualifiedErrorIdEquals -scriptblock {PowerShellGet\Uninstall-Script -Name Fabrikam-ClientScript -RequiredVersion 3.0 -MaximumVersion 1.0 } `
-expectedFullyQualifiedErrorId "VersionRangeAndRequiredVersionCannotBeSpecifiedTogether,Uninstall-Script"
}
It ValidateUninstallScriptWithSingleNameInvalidMinMaxRange {
AssertFullyQualifiedErrorIdEquals -scriptblock {PowerShellGet\Uninstall-Script -Name Fabrikam-ClientScript -MinimumVersion 3.0 -MaximumVersion 1.0 } `
-expectedFullyQualifiedErrorId "MinimumVersionIsGreaterThanMaximumVersion,Uninstall-Script"
}
}