-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathInstallPSResourceLocal.Tests.ps1
More file actions
318 lines (269 loc) · 16.2 KB
/
InstallPSResourceLocal.Tests.ps1
File metadata and controls
318 lines (269 loc) · 16.2 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
# 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 local repositories' -tags 'CI' {
BeforeAll {
$localRepo = "psgettestlocal"
$localUNCRepo = "psgettestlocal3"
$localPrivateRepo = "psgettestlocal5"
$localNupkgRepo = "localNupkgRepo"
$testModuleName = "test_local_mod"
$testModuleName2 = "test_local_mod2"
$testModuleClobber = "testModuleClobber"
$testModuleClobber2 = "testModuleClobber2"
Get-NewPSResourceRepositoryFile
Register-LocalRepos
Register-LocalTestNupkgsRepo
$prereleaseLabel = "Alpha001"
$tags = @()
New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags $tags
New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "3.0.0" -prereleaseLabel "" -tags $tags
New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags $tags
New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "5.2.5" -prereleaseLabel $prereleaseLabel -tags $tags
New-TestModule -moduleName $testModuleName2 -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags $tags
New-TestModule -moduleName $testModuleName2 -repoName $localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags $tags
New-TestModule -moduleName $testModuleClobber -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -cmdletToExport 'Test-Cmdlet1' -cmdletToExport2 'Test-Cmdlet2'
New-TestModule -moduleName $testModuleClobber2 -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -cmdletToExport 'Test-Cmdlet1'
}
AfterEach {
Uninstall-PSResource $testModuleName, $testModuleName2, "test_script", "RequiredModule*", $testModuleClobber, $testModuleClobber2 -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
It "Install resource given Name parameter" {
Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0"
}
It "Install resource given Name parameter from UNC repository" {
Install-PSResource -Name $testModuleName -Repository $localUNCRepo -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0"
}
It "Install resource given Name and Version (specific) parameters" {
Install-PSResource -Name $testModuleName -Version "3.0.0" -Repository $localRepo -TrustRepository
$res = Get-InstalledPSResource -Name $testModuleName
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "3.0.0"
}
It "Install multiple resources by name" {
$pkgNames = @($testModuleName, $testModuleName2)
Install-PSResource -Name $pkgNames -Repository $localRepo -TrustRepository
$pkg = Get-InstalledPSResource $pkgNames
$pkg.Name | Should -Be $pkgNames
}
It "Should not install resource given nonexistent name" {
$res = Install-PSResource -Name "NonExistentModule" -Repository $localRepo -TrustRepository -PassThru -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
}
It "Should install resource given name and exact version with bracket syntax" {
Install-PSResource -Name $testModuleName -Version "[1.0.0.0]" -Repository $localRepo -TrustRepository
$res = Get-InstalledPSResource $testModuleName
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "1.0.0"
}
It "Should install resource given name and exact range inclusive [1.0.0.0, 5.0.0.0]" {
Install-PSResource -Name $testModuleName -Version "[1.0.0.0, 5.0.0.0]" -Repository $localRepo -TrustRepository
$res = Get-InstalledPSResource $testModuleName
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0"
}
It "Should install resource given name and exact range exclusive (1.0.0.0, 5.0.0.0)" {
Install-PSResource -Name $testModuleName -Version "(1.0.0.0, 5.0.0.0)" -Repository $localRepo -TrustRepository
$res = Get-InstalledPSResource $testModuleName
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "3.0.0"
}
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 $localRepo -TrustRepository -ErrorAction SilentlyContinue
}
catch
{}
$Error[0].FullyQualifiedErrorId | Should -Be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
$res = Get-InstalledPSResource $testModuleName -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
}
It "Install resource when given Name, Version '*', should install the latest version" {
Install-PSResource -Name $testModuleName -Version "*" -Repository $localRepo -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0"
}
It "Install resource when given Name, Version '3.*', should install the appropriate version" {
Install-PSResource -Name $testModuleName -Version "3.*" -Repository $localRepo -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "3.0.0"
}
It "Install resource with latest (including prerelease) version given Prerelease parameter (prerelease casing should be correct)" {
Install-PSResource -Name $testModuleName -Prerelease -Repository $localRepo -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.2.5"
$pkg.Prerelease | Should -Be "Alpha001"
}
It "Install resource with cmdlet names from a module already installed with -NoClobber (should not clobber)" {
Install-PSResource -Name $testModuleClobber -Repository $localRepo -TrustRepository
$pkg = Get-InstalledPSResource $testModuleClobber
$pkg.Name | Should -Be $testModuleClobber
$pkg.Version | Should -Be "1.0.0"
Install-PSResource -Name $testModuleClobber2 -Repository $localRepo -TrustRepository -NoClobber -ErrorVariable ev -ErrorAction SilentlyContinue
$pkg = Get-InstalledPSResource $testModuleClobber2 -ErrorAction SilentlyContinue
$pkg | Should -BeNullOrEmpty
$ev.Count | Should -Be 1
$ev[0] | Should -Be "'testModuleClobber2' package could not be installed with error: The following commands are already available on this system: 'Test-Cmdlet1, Test-Cmdlet1'. This module 'testModuleClobber2' may override the existing commands. If you still want to install this module 'testModuleClobber2', remove the -NoClobber parameter."
}
It "Install resource with cmdlet names from a module already installed (should clobber)" {
Install-PSResource -Name $testModuleClobber -Repository $localRepo -TrustRepository
$pkg = Get-InstalledPSResource $testModuleClobber
$pkg.Name | Should -Be $testModuleClobber
$pkg.Version | Should -Be "1.0.0"
Install-PSResource -Name $testModuleClobber2 -Repository $localRepo -TrustRepository
$pkg = Get-InstalledPSResource $testModuleClobber2
$pkg.Name | Should -Be $testModuleClobber2
$pkg.Version | Should -Be "1.0.0"
}
It "Install resource with -NoClobber (should install)" {
Install-PSResource -Name $testModuleClobber -Repository $localRepo -TrustRepository -NoClobber
$pkg = Get-InstalledPSResource $testModuleClobber
$pkg.Name | Should -Be $testModuleClobber
$pkg.Version | Should -Be "1.0.0"
}
It "Install resource via InputObject by piping from Find-PSresource" {
Find-PSResource -Name $testModuleName -Repository $localRepo | Install-PSResource -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0"
}
It "Install resource via InputObject by piping from Find-PSResource" {
$modules = Find-PSResource -Name "*" -Repository $localRepo
$modules.Count | Should -BeGreaterThan 1
Install-PSResource -TrustRepository -InputObject $modules
$pkg = Get-InstalledPSResource $modules.Name
$pkg.Count | Should -BeGreaterThan 1
}
It "Install resource under location specified in PSModulePath" {
Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
($env:PSModulePath).Contains($pkg.InstalledLocation)
}
# Windows only
It "Install resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $testModuleName -Repository $localRepo -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 $testModuleName -Repository $localRepo -TrustRepository -Scope AllUsers
$pkg = Get-InstalledPSResource $testModuleName -Scope AllUsers
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains("Program Files") | Should -Be $true
}
# Windows only
It "Install resource under no specified scope - Windows only" -Skip:(!(Get-IsWindows)) {
Install-PSResource -Name $testModuleName -Repository $localRepo -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 $localRepo -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 $localRepo -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 $localRepo -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository -WarningVariable WarningVar -WarningAction SilentlyContinue
$WarningVar | Should -Not -BeNullOrEmpty
}
It "Reinstall resource that is already installed with -Reinstall parameter" {
Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0"
Install-PSResource -Name $testModuleName -Repository $localRepo -Reinstall -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.0.0"
}
It "Install module using -WhatIf, should not install the module" {
Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $localRepo -TrustRepository -WhatIf
$? | Should -BeTrue
$res = Get-InstalledPSResource -Name $testModuleName -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
}
It "Install resource given -Name and -PassThru parameters" {
$res = Install-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $localRepo -TrustRepository -PassThru
$res.Name | Should -Contain $testModuleName
$res.Version | Should -Be "1.0.0"
}
It "Get definition for alias 'isres'" {
(Get-Alias isres).Definition | Should -BeExactly 'Install-PSResource'
}
It "Not install resource that lists dependency packages which cannot be found" {
$localRepoUri = Join-Path -Path $TestDrive -ChildPath "testdir"
Save-PSResource -Name "test_script" -Repository "PSGallery" -TrustRepository -Path $localRepoUri -AsNupkg -SkipDependencyCheck
Write-Information -InformationAction Continue -MessageData $localRepoUri
$res = Install-PSResource -Name "test_script" -Repository $localRepo -TrustRepository -PassThru -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -Not -Be 0
for ($i = 0; $i -lt $err.Count; $i++) {
$err[$i].FullyQualifiedErrorId | Should -Not -Be "System.NullReferenceException,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
}
}
It "Install .nupkg that contains directories (specific package throws errors when accessed by ZipFile.OpenRead)" {
$nupkgName = "Microsoft.Web.Webview2"
$nupkgVersion = "1.0.2792.45"
$repoPath = Get-PSResourceRepository $localNupkgRepo
$searchPkg = Find-PSResource -Name $nupkgName -Version $nupkgVersion -Repository $localNupkgRepo
Install-PSResource -Name $nupkgName -Version $nupkgVersion -Repository $localNupkgRepo -TrustRepository
$pkg = Get-InstalledPSResource $nupkgName
$pkg.Name | Should -Be $nupkgName
$pkg.Version | Should -Be $nupkgVersion
}
It "Install should not silently fail if network connection to local private repository cannot be established and remainder repositories should be searched" {
$privateRepo = Get-PSResourceRepository $localPrivateRepo
$res = Install-PSResource -Name $testModuleName -TrustRepository -PassThru -WarningVariable WarningVar -WarningAction SilentlyContinue
$WarningVar | Should -Not -BeNullOrEmpty
$WarningVar[0] | Should -Match "$($privateRepo.Uri.LocalPath)"
$res.Name | Should -Contain $testModuleName
$res.Version | Should -Be "1.0.0"
}
It "Install should not silently fail if network connection to local private repository cannot be established and package version was provided and remainder repositories should be searched" {
$privateRepo = Get-PSResourceRepository $localPrivateRepo
$res = Install-PSResource -Name $testModuleName -Version "1.0.0" -TrustRepository -PassThru -WarningVariable WarningVar -WarningAction SilentlyContinue
$WarningVar | Should -Not -BeNullOrEmpty
$WarningVar[0] | Should -Match "$($privateRepo.Uri.LocalPath)"
$res.Name | Should -Contain $testModuleName
$res.Version | Should -Be "1.0.0"
}
}