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 133
Expand file tree
/
Copy pathPSGetUpdateScript.Tests.ps1
More file actions
608 lines (502 loc) · 26.1 KB
/
Copy pathPSGetUpdateScript.Tests.ps1
File metadata and controls
608 lines (502 loc) · 26.1 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<#####################################################################################
# File: PSGetUpdateScriptTests.ps1
# Tests for PSGet Script functionality
#
# Copyright (c) Microsoft Corporation, 2015
#####################################################################################>
<#
Name: PowerShell.PSGet.UpdateScriptTests
Description: Tests for Update-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:ProgramFilesScriptsPath = Get-AllUsersScriptsPath
$script:MyDocumentsScriptsPath = Get-CurrentUserScriptsPath
$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
# Create temp folder for saving the scripts
$script:TempSavePath = Join-Path -Path $script:TempPath -ChildPath "PSGet_$(Get-Random)"
$null = New-Item -Path $script:TempSavePath -ItemType Directory -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
RemoveItem $script:TempSavePath
if ($script:AddedAllUsersInstallPath) {
Reset-PATHVariableForScriptsInstallLocation -Scope AllUsers
}
if ($script:AddedCurrentUserInstallPath) {
Reset-PATHVariableForScriptsInstallLocation -Scope CurrentUser
}
}
Describe PowerShell.PSGet.UpdateScriptTests -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
}
# Purpose: UpdateScriptWithConfirmAndNoToPrompt
#
# Action: Update-Script Fabrikam-ServerScript -Confirm
#
# Expected Result: script should not be updated after confirming NO
#
It "UpdateScriptWithConfirmAndNoToPrompt" {
$installedVersion = "1.0"
Install-Script Fabrikam-ServerScript -RequiredVersion $installedVersion
$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 {
$result = ExecuteCommand $runspace 'Update-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
$shouldProcessMessage = ($LocalizedData.UpdateScriptwhatIfMessage -replace "__OLDVERSION__", $installedVersion)
$shouldProcessMessage = ($shouldProcessMessage -f ($itemInfo.Name, $itemInfo.Version))
Assert ($content -and ($content -match $shouldProcessMessage)) "update script confirm prompt is not working, Expected:$shouldProcessMessage, Actual:$content"
$res = Get-InstalledScript Fabrikam-ServerScript
AssertEquals $res.Name 'Fabrikam-ServerScript' "Update-Script should not update the Fabrikam-ServerScript script when pressed NO to Confirm."
AssertEquals $res.Version $installedVersion "Update-Script should not update the Fabrikam-ServerScript script when pressed NO to Confirm."
} `
-Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0'))
# Purpose: UpdateScriptWithConfirmAndYesToPrompt
#
# Action: Update-Script Fabrikam-ServerScript -Confirm
#
# Expected Result: script should be updated after confirming YES
#
It "UpdateScriptWithConfirmAndYesToPrompt" {
$installedVersion = '1.0'
Install-Script Fabrikam-ServerScript -RequiredVersion $installedVersion
$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 {
$result = ExecuteCommand $runspace 'Update-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
$shouldProcessMessage = ($LocalizedData.UpdateScriptwhatIfMessage -replace "__OLDVERSION__", $installedVersion)
$shouldProcessMessage = ($shouldProcessMessage -f ($itemInfo.Name, $itemInfo.Version))
Assert ($content -and ($content -match $shouldProcessMessage)) "update script confirm prompt is not working, Expected:$shouldProcessMessage, Actual:$content"
$res = Get-InstalledScript Fabrikam-ServerScript
AssertEquals $res.Name 'Fabrikam-ServerScript' "Update-Script should not update the Fabrikam-ServerScript script when pressed NO to Confirm, $res"
Assert ($res.Version -gt [Version]"1.0") "Update-Script should not update the Fabrikam-ServerScript script when pressed NO to Confirm, $res"
} `
-Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0'))
# Purpose: UpdateScriptWithWhatIf
#
# Action: Update-Script Fabrikam-ServerScript -WhatIf
#
# Expected Result: script should not be updated -WhatIf
#
It "UpdateScriptWithWhatIf" {
$installedVersion = "1.0"
Install-Script Fabrikam-ServerScript -RequiredVersion $installedVersion
$outputPath = $script:TempPath
$guid = [system.guid]::newguid().tostring()
$outputFilePath = Join-Path $outputPath "$guid"
$runspace = CreateRunSpace $outputFilePath 1
$content = $null
try {
$result = ExecuteCommand $runspace 'Update-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
$shouldProcessMessage = ($LocalizedData.UpdateScriptwhatIfMessage -replace "__OLDVERSION__", $installedVersion)
$shouldProcessMessage = ($shouldProcessMessage -f ($itemInfo.Name, $itemInfo.Version))
Assert ($content -and ($content -match $shouldProcessMessage)) "update script whatif message is missing, Expected:$shouldProcessMessage, Actual:$content"
$res = Get-InstalledScript Fabrikam-ServerScript
AssertEquals $res.Name 'Fabrikam-ServerScript' "Update-Script should not update the script with -WhatIf option, $res"
Assert ($res.Version -eq [Version]"1.0") "Update-Script should not update the script with -WhatIf option, $res"
} `
-Skip:$(($PSEdition -eq 'Core') -or ($PSCulture -ne 'en-US') -or ([System.Environment]::OSVersion.Version -lt '6.2.9200.0'))
# Purpose: UpdateScriptWithFalseConfirm
#
# Action: Update-Script Fabrikam-ServerScript -Confirm:$false
#
# Expected Result: Script should be updated
#
It "UpdateScriptWithFalseConfirm" {
$scriptName = 'Fabrikam-ServerScript'
Install-Script -Name $scriptName -RequiredVersion 1.0
Update-Script $scriptName -Confirm:$false
$res = Get-InstalledScript $scriptName
AssertEquals $res.Name $scriptName "Update-Script should update the script if -Confirm option is false, $res"
Assert ($res.Version -gt [Version]"1.0") "Update-Script should update the script if -Confirm option is false, $res"
}
# Purpose: UpdateScriptWithForce
#
# Action: Install latest version then update it with force option
#
# Expected Result: Script should be updated without any error
#
It "UpdateScriptWithForce" {
$scriptName = 'Fabrikam-ServerScript'
Install-Script -Name $scriptName
$res1 = Get-InstalledScript $scriptName
$MyError = $null
Update-Script $scriptName -Force -ErrorVariable MyError
Assert ($MyError.Count -eq 0) "There should not be any error from force update, $MyError"
$res2 = Get-InstalledScript $scriptName
Assert (($res1.Name -eq $res2.Name) -and ($res1.Version -eq $res2.Version)) "Update-Script with force should not change the version"
}
# Purpose: UpdateMultipleScriptsWithReqVersion
#
# Action: Update-Script Fabrikam-ClientScript,Fabrikam-ServerScript -RequiredVersion 3.0
#
# Expected Result: Should fail with error id
#
It "UpdateMultipleScriptsWithReqVersion" {
Install-Script Fabrikam-ClientScript, Fabrikam-ServerScript
AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Script Fabrikam-ClientScript, Fabrikam-ServerScript -RequiredVersion 3.0 } `
-expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Update-Script"
}
# Purpose: UpdateScriptsWithReqVersionAndWildcard
#
# Action: Update-Script Fabrikam-*rScript -RequiredVersion 3.0
#
# Expected Result: Should fail with error id
#
It "UpdateScriptsWithReqVersionAndWildcard" {
Install-Script Fabrikam-ServerScript
AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Script Fabrikam-*rScript -RequiredVersion 3.0 } `
-expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Update-Script"
}
# Purpose: UpdateScriptWithWildcardName
#
# Action: Update-Script "Fab[rR]ikam?Ser[a-z]erScr?pt"
#
# Expected Result: Should update Fabrikam-ServerScript
#
It "UpdateScriptWithWildcardName" {
Install-Script Fabrikam-ServerScript -RequiredVersion 1.0
Update-Script Fab[rR]ikam?Ser[a-z]erScr?pt
$res = Get-InstalledScript Fabrikam-ServerScript
Assert ($res.Name -eq "Fabrikam-ServerScript" -and $res.Version -gt [Version]"1.0") "Update-Script with wildcard name should update the script, $res"
}
# Purpose: UpdateMultipleScriptsWithWildcard
#
# Action: Install two scripts Fabrikam-* then update them with wildcard in name
#
# Expected Result: both scripts should be refreshed
#
It "UpdateMultipleScriptsWithWildcard" {
Install-Script Fabrikam-ClientScript -RequiredVersion 1.0
$ClientScriptDetails = Get-InstalledScript -Name Fabrikam-ClientScript
Install-Script Fabrikam-ServerScript -RequiredVersion 1.0
$MyError = $null
$DateTimeBeforeUpdate = Get-Date
Update-Script Fabrikam-* -Force -ErrorVariable MyError
Assert ($MyError.Count -eq 0) "There should not be any error when updating multiple scripts with wildcard in name, $MyError"
$res = Get-InstalledScript -Name Fabrikam-ServerScript -MinimumVersion '1.1'
Assert ($res -and ($res.Name -eq "Fabrikam-ServerScript") -and ($res.Version -gt [Version]"1.0")) "Update-Script should update when wildcard specified in name"
$res = Get-InstalledScript -Name Fabrikam-ClientScript -MinimumVersion '1.1'
Assert ($res -and ($res.Name -eq 'Fabrikam-ClientScript') -and ($res.Version -gt [Version]"1.0")) "Update-Script should update when wildcard specified in name"
AssertEquals $res.InstalledDate $ClientScriptDetails.InstalledDate "InstalledDate should be same for the updated version"
Assert ($res.UpdatedDate.AddSeconds(1) -ge $DateTimeBeforeUpdate) "Get-InstalledScript results are not expected, UpdatedDate $($res.UpdatedDate.Ticks) should be after $($DateTimeBeforeUpdate.Ticks)"
}
# Purpose: UpdateNotInstalledScript
#
# Action: Update-Script ScriptNotInstalled
#
# Expected Result: Should fail
#
It "UpdateNotInstalledScript" {
AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Script ScriptNotInstalled } `
-expectedFullyQualifiedErrorId "ScriptNotInstalledOnThisMachine,Update-Script"
}
# Purpose: Update a script silently
#
# Action: Update-Script Fabrikam-ServerScript
#
# Expected Result: Update-Script should update the script silently
#
It "Update-Script should be silent" {
$scriptName = 'Fabrikam-ServerScript'
Install-Script $scriptName -RequiredVersion 1.0
$result = Update-Script $scriptName
$result | Should -BeNullOrEmpty
}
# Purpose: Update a script and return output
#
# Action: Update-Script Fabrikam-ServerScript -PassThru
#
# Expected Result: Update-Script should update the script and return output
#
It "Update-Script should return output" {
$scriptName = 'Fabrikam-ServerScript'
Install-Script $scriptName -RequiredVersion 1.0
$result = Update-Script $scriptName -PassThru
$result | Should -Not -BeNullOrEmpty
}
# Purpose: Update a script with requiredversion
#
# Action: Update-Script Fabrikam-ServerScript -RequiredVersion 2.0
#
# Expected Result: Update-Script should update the script with RequiredVersion
#
It "UpdateScriptWithReqVersion" {
$scriptName = 'Fabrikam-ServerScript'
Install-Script $scriptName -RequiredVersion 1.0
Update-Script $scriptName -RequiredVersion 2.0
$res = Get-InstalledScript $scriptName
Assert ($res.Name -eq "Fabrikam-ServerScript" -and $res.Version -eq [Version]"2.0") "Update-Script should update the script with RequiredVersion, $res"
}
# Purpose: UpdateScriptWithNotAvailableReqVersion
#
# Action: Update-Script Fabrikam-ServerScript -RequiredVersion 10.0
#
# Expected Result: Update-Script should fail
#
It "UpdateScriptWithNotAvailableReqVersion" {
Install-Script Fabrikam-ServerScript -RequiredVersion 1.0
$expectedFullyQualifiedErrorId = 'NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage'
AssertFullyQualifiedErrorIdEquals -scriptblock { Update-Script Fabrikam-ServerScript -RequiredVersion 10.0 } `
-expectedFullyQualifiedErrorId $expectedFullyQualifiedErrorId
}
# Purpose: UpdateScriptWithReqVersionAndForceToDowngradeVersion
#
# Action: Update-Script Fabrikam-ServerScript -RequiredVersion 1.0
#
# Expected Result: Script should be downgraded to 1.0
#
It "UpdateScriptWithReqVersionAndForceToDowngradeVersion" {
$scriptName = 'Fabrikam-ServerScript'
Install-Script $scriptName
Update-Script $scriptName -RequiredVersion 1.0 -Force
$res = Get-InstalledScript $scriptName
Assert ($res.Name -eq $scriptName -and $res.Version -eq [Version]"1.0") "Update-Script should downgrade the script version with -RequiredVersion and -Force, $res"
}
# Purpose: AdminPrivilegesAreRequiredForUpdatingAllUsersScript
#
# Action: Install a script as admin and try to update it as non-admin user
#
# Expected Result: should pass, installing the update in currentuser scope
#
It "AdminPrivilegesAreNotRequiredForUpdatingAllUsersScript" {
Install-Script -Name Fabrikam-ServerScript -RequiredVersion 1.0 -Scope AllUsers
$content = Invoke-WithoutAdminPrivileges (@'
Import-Module "{0}\PowerShellGet.psd1" -Force -Passthru | select ModuleBase
Update-Script -Name Fabrikam-ServerScript
'@ -f (Get-Module PowerShellGet).ModuleBase)
$updatedScript = Get-InstalledScript Fabrikam-ServerScript
Assert ($updatedScript.Version -gt 1.0) "Update-Script failed to updated script running as non-admin: $content"
} `
-Skip:$(
$whoamiValue = (whoami)
($whoamiValue -eq "NT AUTHORITY\SYSTEM") -or
($whoamiValue -eq "NT AUTHORITY\LOCAL SERVICE") -or
($whoamiValue -eq "NT AUTHORITY\NETWORK SERVICE") -or
($env:APPVEYOR_TEST_PASS -eq 'True') -or
($PSVersionTable.PSVersion -lt '4.0.0')
)
# Purpose: UpdateScriptWithLowerReqVersionShouldNotUpdate
#
# Action: Install higher version and try to update with lower version as requiredversion
#
# Expected Result: Script should not be downgraded to 1.0
#
It "UpdateScriptWithLowerReqVersionShouldNotUpdate" {
Install-Script Fabrikam-ServerScript -Force
Update-Script Fabrikam-ServerScript -RequiredVersion 1.0
$res = Get-InstalledScript Fabrikam-ServerScript
Assert ($res.Name -eq "Fabrikam-ServerScript" -and $res.Version -gt [Version]"1.0") "Update-Script should not downgrade the script version with -RequiredVersion, Name: $($res.Name), Version: $($res.Version)"
}
# Purpose: UpdateMultipleScripts
#
# Action: Install two scripts with lowerversions then update them
#
# Expected Result: both should be updated
#
It "UpdateMultipleScripts" {
Install-Script Fabrikam-ClientScript -RequiredVersion 1.0
Install-Script Fabrikam-ServerScript -RequiredVersion 1.0
Update-Script Fabrikam-ClientScript, Fabrikam-ServerScript
$res = Get-InstalledScript -Name Fabrikam-ServerScript, Fabrikam-ClientScript
Assert (($res.Count -eq 2) -and ($res[0].Version -gt [Version]"1.0") -and ($res[1].Version -gt [Version]"1.0")) "Multiple script should be updated"
}
# Purpose: UpdateAllScripts
#
# Action: Install two scripts with lowerversions then update them
# Update-Script
#
# Expected Result: both should be updated when no name is specified to Update-Script
#
It "UpdateAllScripts" {
Install-Script Fabrikam-ClientScript -RequiredVersion 1.0
Install-Script Fabrikam-ServerScript -RequiredVersion 1.0
Update-Script -ErrorAction SilentlyContinue -ErrorVariable err
$err | ? { $_.FullyQualifiedErrorId -notmatch "NoMatchFoundForCriteria" } | % { Write-Error $_ }
$res = Get-InstalledScript -Name Fabrikam-ServerScript, Fabrikam-ClientScript
Assert (($res.Count -eq 2) -and ($res[0].Version -gt [Version]"1.0") -and ($res[1].Version -gt [Version]"1.0")) "Multiple script should be updated"
}
# Purpose: UpdateMultipleScriptsWithForce
#
# Action: Install two scripts then update them with force option
#
# Expected Result: both scripts should be refreshed
#
It "UpdateMultipleScriptsWithForce" {
Install-Script Fabrikam-ClientScript, Fabrikam-ServerScript
$MyError = $null
Update-Script Fabrikam-ClientScript, Fabrikam-ServerScript -Force -ErrorVariable MyError
Assert ($MyError.Count -eq 0) "There should not be any error from force update for multiple scripts, $MyError"
$res = Get-InstalledScript Fabrikam-ServerScript
Assert (($res.Name -eq 'Fabrikam-ServerScript') -and ($res.Version -gt [Version]"1.0")) "Update-Script should update when multiple scripts are specified"
$res = Get-InstalledScript Fabrikam-ClientScript
Assert (($res.Name -eq 'Fabrikam-ClientScript') -and ($res.Version -gt [Version]"1.0")) "Update-Script should update when multiple scripts are specified"
}
# Purpose: UpdateScriptUnderCurrentUserScope
#
# Action: Install a script with CurrentUser scope then update it
#
# Expected Result: updated script should be under current user's windows powershell scripts folder
#
It "UpdateScriptUnderCurrentUserScope" {
$scriptName = 'Fabrikam-ServerScript'
Install-Script $scriptName -Scope CurrentUser -RequiredVersion 1.0
Update-Script $scriptName
$res = Get-InstalledScript $scriptName
Assert (($res.Name -eq $scriptName) -and ($res.Version -gt [Version]"1.0")) "Update-Script should update the script installed to current user scope, $res"
AssertEquals $res.InstalledLocation $script:MyDocumentsScriptsPath "Update-Script should update the script installed to current user scope, updated script base: $($res.InstalledLocation)"
}
# Purpose: UpdateScriptUnderAllUsersScope
#
# Action: Install a script with AllUsers scope then update it
#
# Expected Result: updated script should be under AllUsers windows powershell scripts folder for an admin on Windows Powershell, currentuser otherwise
#
It "UpdateScriptUnderAllUsersScope" {
$scriptName = 'Fabrikam-ServerScript'
$shouldBeInAllUsers = ($PSVersionTable.PSVersion -lt "5.0" -or $PSEdition -eq 'Desktop') # when running these tests we always need to be an admin
Install-Script $scriptName -Scope AllUsers -RequiredVersion 1.0
Update-Script $scriptName
$res = Get-InstalledScript $scriptName
Assert (($res.Name -eq $scriptName) -and ($res.Version -gt [Version]"1.0")) "Update-Script should update the script, $res"
if ($shouldBeInAllUsers) {
AssertEquals $res.InstalledLocation $script:ProgramFilesScriptsPath "Update-Script should put update in all users scope, but updated script base: $($res.InstalledLocation)"
}
else {
AssertEquals $res.InstalledLocation $script:MyDocumentsScriptsPath "Update-Script should put update in current user scope, updated script base: $($res.InstalledLocation)"
}
}
}
Describe PowerShell.PSGet.UpdateScriptTests.P1 -Tags 'P1', 'OuterLoop' {
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
}
# Purpose: Validate Update-Script cmdlet with a script with dependencies
#
# Action: Update-Script -Name Script-WithDependencies2
#
# Expected Result: Should update the script along with its dependencies
#
It UpdateScriptWithIncludeDependencies {
$ScriptName = 'Script-WithDependencies2'
$NamesToUninstall = @()
try {
$res1 = Find-Script -Name $ScriptName -MaximumVersion "1.0" -MinimumVersion "0.1"
AssertEquals $res1.Name $ScriptName "Find-Script didn't find the exact script which has dependencies, $res1"
$DepencyNames = $res1.Dependencies.Name
$res2 = Find-Script -Name $ScriptName -IncludeDependencies -MaximumVersion "1.0" -MinimumVersion "0.1"
Assert ($res2.Count -ge ($DepencyNames.Count + 1)) "Find-Script with -IncludeDependencies returned wrong results, $res2"
Install-Script -Name $ScriptName -MaximumVersion "1.0" -MinimumVersion "0.1"
$ActualScriptDetails = Get-InstalledScript -Name $ScriptName -RequiredVersion $res1.Version
AssertNotNull $ActualScriptDetails "$ScriptName script with dependencies is not installed properly"
$NamesToUninstall += $res2.Name
$res2 | ForEach-Object {
if (-not (Get-InstalledScript -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue) -and
-not (Get-InstalledModule -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue)) {
Assert $false "Script dependency $_ is not installed"
}
}
# Find the latest available version
$res3 = Find-Script -Name $ScriptName -IncludeDependencies
Update-Script -Name $ScriptName
$NamesToUninstall += $res3.Name
$res3 | ForEach-Object {
if (-not (Get-InstalledScript -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue) -and
-not (Get-InstalledModule -Name $_.Name -MaximumVersion $_.Version -ErrorAction SilentlyContinue)) {
Assert $false "Script dependency $_ is not updated properly"
}
}
}
finally {
# Uninstall the installed artifacts
$NamesToUninstall | ForEach-Object {
PowerShellGet\Uninstall-Script $_ -Force -ErrorAction SilentlyContinue
PowerShellGet\Uninstall-Module $_ -Force -ErrorAction SilentlyContinue
}
}
} -Skip:$($PSVersionTable.PSVersion -lt '5.0.0')
}