-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathInstallPSResourceV2Server.Tests.ps1
More file actions
637 lines (529 loc) · 29.6 KB
/
InstallPSResourceV2Server.Tests.ps1
File metadata and controls
637 lines (529 loc) · 29.6 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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
Param()
$ProgressPreference = "SilentlyContinue"
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
Import-Module $modPath -Force -Verbose
$psmodulePaths = $env:PSModulePath -split ';'
Write-Verbose -Verbose -Message "Current module search paths: $psmodulePaths"
Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' {
BeforeAll {
$PSGalleryName = Get-PSGalleryName
$PSGalleryUri = Get-PSGalleryLocation
$NuGetGalleryName = Get-NuGetGalleryName
$testModuleName = "test_module"
$testModuleName2 = "TestModule99"
$testScriptName = "test_script"
$PackageManagement = "PackageManagement"
$clobberTestModule = "ClobberTestModule1"
$clobberTestModule2 = "ClobberTestModule2"
$RequiredResourceJSONFileName = "TestRequiredResourceFile.json"
$RequiredResourcePSD1FileName = "TestRequiredResourceFile.psd1"
Get-NewPSResourceRepositoryFile
Register-LocalRepos
}
AfterEach {
Uninstall-PSResource "test_module", "test_module2", "test_script", "TestModule99", "testModuleWithlicense", `
"TestFindModule", "ClobberTestModule1", "ClobberTestModule2", "PackageManagement", "TestTestScript", `
"TestModuleWithDependency", "TestModuleWithPrereleaseDep", "PrereleaseModule" -SkipDependencyCheck -ErrorAction SilentlyContinue
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
$testCases = [array](
@{Name = "*"; ErrorId = "NameContainsWildcard" },
@{Name = "Test_Module*"; ErrorId = "NameContainsWildcard" },
@{Name = "Test?Module", "Test[Module"; ErrorId = "ErrorFilteringNamesForUnsupportedWildcards" }
)
It "Should not install resource with wildcard in name" -TestCases $testCases {
param($Name, $ErrorId)
Install-PSResource -Name $Name -Repository $PSGalleryName -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "$ErrorId,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
}
It "Install specific module resource by name" {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0.0"
}
It "Install specific script resource by name" {
Install-PSResource -Name $testScriptName -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testScriptName
$pkg.Name | Should -Be $testScriptName
$pkg.Version | Should -Be "3.5.0.0"
}
It "Install multiple resources by name" {
$pkgNames = @($testModuleName, $testModuleName2)
Install-PSResource -Name $pkgNames -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $pkgNames
$pkg.Name | Should -Be $pkgNames
}
It "Should not install resource given nonexistant name" {
Install-PSResource -Name "NonExistantModule" -Repository $PSGalleryName -TrustRepository -ErrorVariable err -ErrorAction SilentlyContinue
$pkg = Get-InstalledPSResource "NonExistantModule"
$pkg.Name | Should -BeNullOrEmpty
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
}
# Do some version testing, but Find-PSResource should be doing thorough testing
It "Should install resource given name and exact version" {
Install-PSResource -Name $testModuleName -Version "1.0.0" -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "1.0.0.0"
}
It "Should install resource given name and exact version with bracket syntax" {
Install-PSResource -Name $testModuleName -Version "3.*" -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "3.0.0.0"
}
It "Should install resource given name and exact version with bracket syntax" {
Install-PSResource -Name $testModuleName -Version "[1.0.0]" -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "1.0.0.0"
}
It "Should install resource given name and exact range inclusive [1.0.0, 5.0.0]" {
Install-PSResource -Name $testModuleName -Version "[1.0.0, 5.0.0]" -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0.0"
}
It "Should install resource given name and exact range exclusive (1.0.0, 5.0.0)" {
Install-PSResource -Name $testModuleName -Version "(1.0.0, 5.0.0)" -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "3.0.0.0"
}
# TODO: Update this test and others like it that use try/catch blocks instead of Should -Throw
It "Should not install resource with incorrectly formatted version such as exclusive version (1.0.0.0)" {
$Version = "(1.0.0.0)"
try {
Install-PSResource -Name $testModuleName -Version $Version -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue
}
catch {
}
$Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
$res = Get-InstalledPSResource $testModuleName
$res | Should -BeNullOrEmpty
}
It "Install resource when given Name, Version '*', should install the latest version" {
Install-PSResource -Name $testModuleName -Version "*" -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0.0"
}
It "Install resource with latest (including prerelease) version given Prerelease parameter" {
Install-PSResource -Name $testModuleName -Prerelease -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.2.5"
$pkg.Prerelease | Should -Be "alpha001"
}
It "Install a module with a dependency" {
Uninstall-PSResource -Name "TestModuleWithDependency*" -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue
Install-PSResource -Name "TestModuleWithDependencyC" -Version "5.0.0.0" -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource "TestModuleWithDependencyC"
$pkg.Name | Should -Be "TestModuleWithDependencyC"
$pkg.Version | Should -Be "5.0"
$pkg = Get-InstalledPSResource "TestModuleWithDependencyB"
$pkg.Name | Should -Be "TestModuleWithDependencyB"
$pkg.Version | Should -Be "3.0"
$pkg = Get-InstalledPSResource "TestModuleWithDependencyD"
$pkg.Name | Should -Be "TestModuleWithDependencyD"
$pkg.Version | Should -Be "2.0"
}
It "Install a module with a prerelease dependency" {
Install-PSResource -Name "TestModuleWithPrereleaseDep" -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource "TestModuleWithPrereleaseDep"
$pkg.Name | Should -Be "TestModuleWithPrereleaseDep"
$pkg.Version | Should -Be "0.0.1"
$pkg = Get-InstalledPSResource "PrereleaseModule"
$pkg.Name | Should -Be "PrereleaseModule"
$pkg.Version | Should -Be "0.0.1"
$pkg.Prerelease | Should -Be "Prerelease"
}
It "Install a module with a dependency and skip installing the dependency" {
Uninstall-PSResource -Name "TestModuleWithDependency*" -Version "*" -SkipDependencyCheck
Install-PSResource -Name "TestModuleWithDependencyC" -Version "1.0.0.0" -SkipDependencyCheck -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource "TestModuleWithDependencyC"
$pkg.Name | Should -Be "TestModuleWithDependencyC"
$pkg.Version | Should -Be "1.0"
$pkg = Get-InstalledPSResource "TestModuleWithDependencyB", "TestModuleWithDependencyD"
$pkg | Should -BeNullOrEmpty
}
It "Install resource via InputObject by piping from Find-PSresource" {
Find-PSResource -Name $testModuleName -Repository $PSGalleryName | Install-PSResource -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0.0"
}
It "Install resource under specified in PSModulePath" {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
($env:PSModulePath).Contains($pkg.InstalledLocation)
}
It "Install resource with companyname, copyright and repository source location and validate" {
Install-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository PSGallery -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Version | Should -Be "5.2.5"
$pkg.Prerelease | Should -Be "alpha001"
$pkg.CompanyName | Should -Be "Anam"
$pkg.Copyright | Should -Be "(c) Anam Navied. All rights reserved."
$pkg.RepositorySourceLocation | Should -Be $PSGalleryUri
}
It "Install script with companyname, copyright, and repository source location and validate" {
Install-PSResource -Name "Install-VSCode" -Version "1.4.2" -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource "Install-VSCode" -Version "1.4.2"
$res.Name | Should -Be "Install-VSCode"
$res.Version | Should -Be "1.4.2"
$res.CompanyName | Should -Be "Microsoft Corporation"
$res.Copyright | Should -Be "(c) Microsoft Corporation"
$res.RepositorySourceLocation | Should -Be $PSGalleryUri
}
# Windows only
It "Install resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Scope CurrentUser
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains("Documents") | Should -Be $true
}
# Windows only
It "Install resource under AllUsers scope - Windows only" -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) {
Install-PSResource -Name "testmodule99" -Repository $PSGalleryName -TrustRepository -Scope AllUsers -Verbose
$pkg = Get-Module "testmodule99" -ListAvailable
$pkg.Name | Should -Be "testmodule99"
$pkg.Path.ToString().Contains("Program Files")
}
# Windows only
It "Install resource under no specified scope - Windows only" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains("Documents") | Should -Be $true
}
# Unix only
# Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules'
It "Install resource under CurrentUser scope - Unix only" -Skip:(Get-IsWindows) {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Scope CurrentUser
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains("$env:HOME/.local") | Should -Be $true
}
# Unix only
# Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules'
It "Install resource under no specified scope - Unix only" -Skip:(Get-IsWindows) {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains("$env:HOME/.local") | Should -Be $true
}
It "Should not install resource that is already installed" {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -WarningVariable WarningVar -WarningAction SilentlyContinue
$WarningVar | Should -Not -BeNullOrEmpty
}
It "Reinstall resource that is already installed with -Reinstall parameter" {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0.0"
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -Reinstall -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0.0"
}
# It "Restore resource after reinstall fails" {
# Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository
# $pkg = Get-InstalledPSResource $testModuleName
# $pkg.Name | Should -Contain $testModuleName
# $pkg.Version | Should -Contain "5.0.0.0"
# $resourcePath = Split-Path -Path $pkg.InstalledLocation -Parent
# $resourceFiles = Get-ChildItem -Path $resourcePath -Recurse
# # Lock resource file to prevent reinstall from succeeding.
# $fs = [System.IO.File]::Open($resourceFiles[0].FullName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
# try
# {
# # Reinstall of resource should fail with one of its files locked.
# Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -Reinstall -ErrorVariable ev -ErrorAction Silent
# $ev.FullyQualifiedErrorId | Should -BeExactly 'InstallPackageFailed,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource'
# }
# finally
# {
# $fs.Close()
# }
# # Verify that resource module has been restored.
# (Get-ChildItem -Path $resourcePath -Recurse).Count | Should -BeExactly $resourceFiles.Count
# }
# It "Install resource that requires accept license with -AcceptLicense flag" {
# Install-PSResource -Name "testModuleWithlicense" -Repository $TestGalleryName -AcceptLicense
# $pkg = Get-InstalledPSResource "testModuleWithlicense"
# $pkg.Name | Should -Be "testModuleWithlicense"
# $pkg.Version | Should -Be "0.0.3.0"
# }
It "Install resource with cmdlet names from a module already installed with -NoClobber (should not clobber)" {
Install-PSResource -Name $clobberTestModule -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $clobberTestModule
$pkg.Name | Should -Be $clobberTestModule
$pkg.Version | Should -Be "0.0.1"
Install-PSResource -Name $clobberTestModule2 -Repository $PSGalleryName -TrustRepository -NoClobber -ErrorVariable ev -ErrorAction SilentlyContinue
$pkg = Get-InstalledPSResource $clobberTestModule2
$pkg | Should -BeNullOrEmpty
$ev.Count | Should -Be 1
$ev[0] | Should -Be "'ClobberTestModule2' package could not be installed with error: The following commands are already available on this system: 'Test-Command2, Test-Command2'. This module 'ClobberTestModule2' may override the existing commands. If you still want to install this module 'ClobberTestModule2', remove the -NoClobber parameter."
}
It "Install resource with cmdlet names from a module already installed (should clobber)" {
Install-PSResource -Name $clobberTestModule -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $clobberTestModule
$pkg.Name | Should -Be $clobberTestModule
$pkg.Version | Should -Be "0.0.1"
Install-PSResource -Name $clobberTestModule2 -Repository $PSGalleryName -TrustRepository
$pkg = Get-InstalledPSResource $clobberTestModule2
$pkg.Name | Should -Be $clobberTestModule2
$pkg.Version | Should -Be "0.0.1"
}
It "Install resource with -NoClobber (should install)" {
Install-PSResource -Name $clobberTestModule -Repository $PSGalleryName -TrustRepository -NoClobber
$pkg = Get-InstalledPSResource $clobberTestModule
$pkg.Name | Should -Be $clobberTestModule
$pkg.Version | Should -Be "0.0.1"
}
It "Install module using -WhatIf, should not install the module" {
Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -WhatIf
$? | Should -BeTrue
$res = Get-InstalledPSResource $testModuleName
$res | Should -BeNullOrEmpty
}
It "Validates that a module with module-name script files (like Pester) installs under Modules path" {
Install-PSResource -Name "testModuleWithScript" -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource "testModuleWithScript"
$res.InstalledLocation.ToString().Contains("Modules") | Should -Be $true
}
# It "Install module using -NoClobber, should throw clobber error and not install the module" {
# Install-PSResource -Name "ClobberTestModule1" -Repository $PSGalleryName -TrustRepository
# $res = Get-InstalledPSResource "ClobberTestModule1"
# $res.Name | Should -Be "ClobberTestModule1"
# Install-PSResource -Name "ClobberTestModule2" -Repository $PSGalleryName -TrustRepository -NoClobber -ErrorAction SilentlyContinue
# $Error[0].FullyQualifiedErrorId | Should -be "CommandAlreadyExists,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
# }
It "Install PSResourceInfo object piped in" {
Find-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $PSGalleryName | Install-PSResource -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "1.0.0.0"
}
It "Install module using -PassThru" {
$res = Install-PSResource -Name $testModuleName -Repository $PSGalleryName -PassThru -TrustRepository
$res.Name | Should -Contain $testModuleName
}
# -RequiredResource
It "Install modules using -RequiredResource with hashtable" {
$rrHash = @{
test_module = @{
version = "[1.0.0,5.0.0)"
repository = $PSGalleryName
}
test_module2 = @{
version = "[1.0.0,3.0.0)"
repository = $PSGalleryName
prerelease = "true"
}
TestModule99 = @{}
}
Install-PSResource -RequiredResource $rrHash -TrustRepository
$res1 = Get-InstalledPSResource $testModuleName
$res1.Name | Should -Be $testModuleName
$res1.Version | Should -Be "3.0.0.0"
$res2 = Get-InstalledPSResource "test_module2" -Version "2.5.0-beta"
$res2.Name | Should -Be "test_module2"
$res2.Version | Should -Be "2.5.0"
$res2.Prerelease | Should -Be "beta"
$res3 = Get-InstalledPSResource $testModuleName2
$res3.Name | Should -Be $testModuleName2
$res3.Version | Should -Be "0.0.93"
}
It "Install module using -RequiredResource with hashtable, and prerelease is boolean true" {
Install-PSResource -TrustRepository -RequiredResource @{
'TestModule99' = @{
'repository' = 'PSGallery'
'prerelease' = $true
}
}
(Get-InstalledPSResource -Name 'TestModule99').'Prerelease' | Should -Be 'beta2'
}
It "Install modules using -RequiredResource with JSON string" {
$rrJSON = "{
'test_module': {
'version': '[1.0.0,5.0.0)',
'repository': 'PSGallery'
},
'test_module2': {
'version': '[1.0.0,3.0.0)',
'repository': 'PSGallery',
'prerelease': 'true'
},
'TestModule99': {
'repository': 'PSGallery'
}
}"
Install-PSResource -RequiredResource $rrJSON -TrustRepository
$res1 = Get-InstalledPSResource $testModuleName
$res1.Name | Should -Be $testModuleName
$res1.Version | Should -Be "3.0.0.0"
$res2 = Get-InstalledPSResource "test_module2" -Version "2.5.0-beta"
$res2.Name | Should -Be "test_module2"
$res2.Version | Should -Be "2.5.0"
$res2.Prerelease | Should -Be "beta"
$res3 = Get-InstalledPSResource $testModuleName2
$res3.Name | Should -Be $testModuleName2
$res3.Version | Should -Be "0.0.93"
}
It "Install modules using -RequiredResourceFile with PSD1 file" {
$rrFilePSD1 = "$psscriptroot/../$RequiredResourcePSD1FileName"
Install-PSResource -RequiredResourceFile $rrFilePSD1 -TrustRepository
$res1 = Get-InstalledPSResource $testModuleName
$res1.Name | Should -Be $testModuleName
$res1.Version | Should -Be "3.0.0.0"
$res2 = Get-InstalledPSResource "test_module2" -Version "2.5.0-beta"
$res2.Name | Should -Be "test_module2"
$res2.Version | Should -Be "2.5.0"
$res2.Prerelease | Should -Be "beta"
$res3 = Get-InstalledPSResource $testModuleName2
$res3.Name | Should -Be $testModuleName2
$res3.Version | Should -Be "0.0.93"
}
It "Install modules using -RequiredResourceFile with JSON file" {
$rrFileJSON = "$psscriptroot/../$RequiredResourceJSONFileName"
Install-PSResource -RequiredResourceFile $rrFileJSON -TrustRepository
$res1 = Get-InstalledPSResource $testModuleName
$res1.Name | Should -Be $testModuleName
$res1.Version | Should -Be "3.0.0.0"
$res2 = Get-InstalledPSResource "test_module2" -Version "2.5.0-beta"
$res2.Name | Should -Be "test_module2"
$res2.Version | Should -Be "2.5.0"
$res2.Prerelease | Should -Be "beta"
$res3 = Get-InstalledPSResource $testModuleName2
$res3.Name | Should -Be $testModuleName2
$res3.Version | Should -Be "0.0.93"
}
# Install module 1.4.3 (is authenticode signed and has catalog file)
# Should install successfully
It "Install modules with catalog file using publisher validation" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $PackageManagement -Version "1.4.3" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository
$res1 = Get-InstalledPSResource $PackageManagement -Version "1.4.3"
$res1.Name | Should -Be $PackageManagement
$res1.Version | Should -Be "1.4.3"
}
#TODO: update this test to use something other than PackageManagement.
# Install module 1.4.7 (is authenticode signed and has no catalog file)
# Should not install successfully
It "Install module with no catalog file" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $PackageManagement -Version "1.4.7" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository
$res1 = Get-InstalledPSResource $PackageManagement -Version "1.4.7"
$res1.Name | Should -Be $PackageManagement
$res1.Version | Should -Be "1.4.7"
}
# Install module that is not authenticode signed
# Should FAIL to install the module
It "Install module that is not authenticode signed" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "GetAuthenticodeSignatureError,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
}
# # Install 1.4.4.1 (with incorrect catalog file)
# # Should FAIL to install the module
# It "Install module with incorrect catalog file" -Skip:(!(Get-IsWindows)) {
# { Install-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository } | Should -Throw -ErrorId "TestFileCatalogError,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
# }
# Install script that is signed
# Should install successfully
It "Install script that is authenticode signed" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name "Install-VSCode" -Version "1.4.2" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository
$res1 = Get-InstalledPSResource "Install-VSCode" -Version "1.4.2"
$res1.Name | Should -Be "Install-VSCode"
$res1.Version | Should -Be "1.4.2"
}
# Install script that is not signed
# Should throw and fail to install
It "Install script that is not signed" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorVariable err -ErrorAction SilentlyContinue
Write-Information -InformationAction Continue -MessageData $err.Count
$err.Count | Should -HaveCount 1
Write-Information -InformationAction Continue -MessageData $err
Write-Information -InformationAction Continue -MessageData $err[0]
$err[0].FullyQualifiedErrorId | Should -BeExactly "GetAuthenticodeSignatureError,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
}
# Unix test for installing scripts
It "Install script resource - Unix only" -Skip:(Get-IsWindows) {
# previously installing pester on Unix was throwing an error due to how the environment PATH variable was being gotten.
Install-PSResource -Name "Pester" -Repository $PSGalleryName -Reinstall -TrustRepository
$res = Get-InstalledPSResource "Pester"
$res.Name | Should -Be "Pester"
}
It "not install resource and error handle when repository's ApiVersion is ApiVersion.unknown" {
Register-PSResourceRepository -Name "UnknownTypeRepo" -Uri "https://org.MyCompany.com/repository/shared-feed/" -Trusted
$repo = Get-PSResourceRepository -Name "UnknownTypeRepo"
$repo.ApiVersion | Should -Be "unknown"
$res = Install-PSResource -Name "MyPackage" -Repository "UnknownTypeRepo" -ErrorAction SilentlyContinue -ErrorVariable err -PassThru
$err[0].FullyQualifiedErrorId | Should -BeExactly "RepositoryApiVersionUnknown,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
$res | Should -BeNullOrEmpty
}
# This is testing FindVersionGlobbing when it creates the 'maxString' variable,
# specifically when the prerelease versions need to be accounted for since that's when PSResourceGet
# modifies the version range to also account for prerelease versions.
#
It "install resource when version has a nine in the digit and -Prerelease parameter is passed in" {
$moduleName = "TestModuleVersionWithNine"
$version = "1.9.9"
Install-PSResource -Name $moduleName -Repository $PSGalleryName -TrustRepository -Version "[$version, $version]" -Prerelease
$res = Get-InstalledPSResource $moduleName
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be $version
}
}
Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'ManualValidationOnly' {
BeforeAll {
$PSGalleryName = Get-PSGalleryName
$PSGalleryUri = Get-PSGalleryLocation
$NuGetGalleryName = Get-NuGetGalleryName
$testModuleName = "TestModule"
$testModuleName2 = "testModuleWithlicense"
Get-NewPSResourceRepositoryFile
Register-LocalRepos
}
AfterEach {
Uninstall-PSResource $testModuleName, $testModuleName2 -ErrorAction SilentlyContinue
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
# Unix only manual test
# Expected path should be similar to: '/usr/local/share/powershell/Modules'
It "Install resource under AllUsers scope - Unix only" -Skip:(Get-IsWindows) {
Install-PSResource -Name $testModuleName -Repository $TestGalleryName -Scope AllUsers
$pkg = Get-Module $testModuleName -ListAvailable
$pkg.Name | Should -Be $testModuleName
$pkg.Path.Contains("/usr/") | Should -Be $true
}
# This needs to be manually tested due to prompt
It "Install resource that requires accept license without -AcceptLicense flag" {
Install-PSResource -Name $testModuleName2 -Repository $TestGalleryName
$pkg = Get-InstalledPSResource $testModuleName2
$pkg.Name | Should -Be $testModuleName2
$pkg.Version | Should -Be "0.0.1.0"
}
# This needs to be manually tested due to prompt
It "Install resource should prompt 'trust repository' if repository is not trusted" {
Set-PSResourceRepository PoshTestGallery -Trusted:$false
Install-PSResource -Name $testModuleName -Repository $TestGalleryName -Confirm:$false
$pkg = Get-Module $testModuleName -ListAvailable
$pkg.Name | Should -Be $testModuleName
Set-PSResourceRepository PoshTestGallery -Trusted
}
}