-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathFindPSResourceLocal.Tests.ps1
More file actions
369 lines (317 loc) · 18.7 KB
/
FindPSResourceLocal.Tests.ps1
File metadata and controls
369 lines (317 loc) · 18.7 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
Import-Module $modPath -Force -Verbose
$psmodulePaths = $env:PSModulePath -split ';'
Write-Verbose -Verbose "Current module search paths: $psmodulePaths"
Describe 'Test Find-PSResource for local repositories' -tags 'CI' {
BeforeAll{
$localRepo = "psgettestlocal"
$localUNCRepo = 'psgettestlocal3'
$localPrivateRepo = "psgettestlocal5"
$testModuleName = "test_local_mod"
$testModuleName2 = "test_local_mod2"
$testModuleName3 = "Test_Local_Mod3"
$similarTestModuleName = "test_local_mod.similar"
$commandName = "cmd1"
$dscResourceName = "dsc1"
$prereleaseLabel = ""
$localNupkgRepo = "localNupkgRepo"
Get-NewPSResourceRepositoryFile
Register-LocalRepos
Register-LocalTestNupkgsRepo
$localRepoUriAddress = Join-Path -Path $TestDrive -ChildPath "testdir"
$tagsEscaped = @("'Test'", "'Tag2'", "'PSCommand_$cmdName'", "'PSDscResource_$dscName'")
$prereleaseLabel = "alpha001"
New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags @()
New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "3.0.0" -prereleaseLabel "" -tags @() -dscResourceToExport $dscResourceName -commandToExport $commandName
New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags $tagsEscaped
New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "5.2.5" -prereleaseLabel $prereleaseLabel -tags $tagsEscaped
New-TestModule -moduleName $testModuleName2 -repoName $localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags $tagsEscaped
New-TestModule -moduleName $testModuleName2 -repoName $localRepo -packageVersion "5.2.5" -prereleaseLabel $prereleaseLabel -tags $tagsEscaped
New-TestModule -moduleName $testModuleName3 -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags @()
New-TestModule -moduleName $similarTestModuleName -repoName $localRepo -packageVersion "4.0.0" -prereleaseLabel "" -tags $tagsEscaped
New-TestModule -moduleName $similarTestModuleName -repoName $localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags $tagsEscaped
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
It "find resource given specific Name, Version null (module)" {
# FindName()
$res = Find-PSResource -Name $testModuleName -Repository $localRepo
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0"
}
It "find resource given specific Name with incorrect casing (should return correct casing)" {
# FindName()
$res = Find-PSResource -Name "test_local_mod3" -Repository $localRepo
$res.Name | Should -Be $testModuleName3
$res.Version | Should -Be "1.0.0"
}
It "find resource given specific Name with incorrect casing and Version (should return correct casing)" {
# FindVersion()
$res = Find-PSResource -Name "test_local_mod3" -Version "1.0.0" -Repository $localRepo
$res.Name | Should -Be $testModuleName3
$res.Version | Should -Be "1.0.0"
}
It "find resource given specific Name, Version null (module) from a UNC-based local repository" {
# FindName()
$res = Find-PSResource -Name $testModuleName -Repository $localUNCRepo
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0"
}
It "find resource given Name, Version null (package containing nuspec only)" {
# FindName()
$pkgName = "PowerShell"
Save-PSResource -Name $pkgName -Repository "NuGetGallery" -Path $localRepoUriAddress -AsNupkg -TrustRepository
$res = Find-PSResource -Name $pkgName -Repository $localRepo
$res.Name | Should -Be $pkgName
$res.Repository | Should -Be $localRepo
}
It "find script without RequiredModules" {
# FindName()
$pkgName = "Required-Script1"
$requiredTag = "Tag1"
Save-PSResource -Name $pkgName -Repository "PSGallery" -Path $localRepoUriAddress -AsNupkg -TrustRepository
# $res = Find-PSResource -Name $pkgName -Repository $localRepo
# $res.Name | Should -Be $pkgName
# $res.Repository | Should -Be $localRepo
# $res.Tags | Should -Contain $requiredTag
}
It "should not find resource given nonexistent Name" {
# FindName()
$res = Find-PSResource -Name NonExistentModule -Repository $localRepo -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
$res | Should -BeNullOrEmpty
}
It "find resource given specific Name when another package with similar name (with period) exists" {
# FindName()
$res = Find-PSResource -Name $testModuleName -Repository $localRepo
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0"
$res = Find-PSResource -Name $similarTestModuleName -Repository $localRepo
$res.Name | Should -Be $similarTestModuleName
$res.Version | Should -Be "5.0.0"
}
It "find resource(s) given wildcard Name" {
# FindNameGlobbing
$res = Find-PSResource -Name "test_local_*" -Repository $localRepo
$res.Count | Should -BeGreaterThan 1
}
$testCases2 = @{Version="[5.0.0.0]"; ExpectedVersions=@("5.0.0"); Reason="validate version, exact match"},
@{Version="5.0.0.0"; ExpectedVersions=@("5.0.0"); Reason="validate version, exact match without bracket syntax"},
@{Version="[1.0.0.0, 5.0.0.0]"; ExpectedVersions=@("1.0.0", "3.0.0", "5.0.0"); Reason="validate version, exact range inclusive"},
@{Version="(1.0.0.0, 5.0.0.0)"; ExpectedVersions=@("3.0.0"); Reason="validate version, exact range exclusive"},
@{Version="(1.0.0.0,)"; ExpectedVersions=@("3.0.0", "5.0.0"); Reason="validate version, minimum version exclusive"},
@{Version="[1.0.0.0,)"; ExpectedVersions=@("1.0.0", "3.0.0", "5.0.0"); Reason="validate version, minimum version inclusive"},
@{Version="(,3.0.0.0)"; ExpectedVersions=@("1.0.0"); Reason="validate version, maximum version exclusive"},
@{Version="(,3.0.0.0]"; ExpectedVersions=@("1.0.0", "3.0.0"); Reason="validate version, maximum version inclusive"},
@{Version="[1.0.0.0, 5.0.0.0)"; ExpectedVersions=@("1.0.0", "3.0.0"); Reason="validate version, mixed inclusive minimum and exclusive maximum version"}
@{Version="(1.0.0.0, 5.0.0.0]"; ExpectedVersions=@("3.0.0", "5.0.0"); Reason="validate version, mixed exclusive minimum and inclusive maximum version"}
It "find resource when given Name to <Reason> <Version>" -TestCases $testCases2{
# FindVersionGlobbing()
param($Version, $ExpectedVersions)
$res = Find-PSResource -Name $testModuleName -Version $Version -Repository $localRepo
foreach ($item in $res) {
$item.Name | Should -Be $testModuleName
$ExpectedVersions | Should -Contain $item.Version
}
}
It "find all versions of resource when given specific Name, Version not null --> '*'" {
# FindVersionGlobbing()
$res = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo
$res | ForEach-Object {
$_.Name | Should -Be $testModuleName
}
$res.Count | Should -BeGreaterOrEqual 1
}
It "find resource with latest (including prerelease) version given Prerelease parameter" {
# FindName()
# test_module resource's latest version is a prerelease version, before that it has a non-prerelease version
$res = Find-PSResource -Name $testModuleName -Repository $localRepo
$res.Version | Should -Be "5.0.0"
$resPrerelease = Find-PSResource -Name $testModuleName -Prerelease -Repository $localRepo
$resPrerelease.Version | Should -Be "5.2.5"
$resPrerelease.Prerelease | Should -Be "alpha001"
}
It "find resource given specific Name when another package with similar name (with period) exists" {
# FindVersion()
# Package $testModuleName version 4.0.0 does not exist
# previously if Find-PSResource -Version against local repo did not find that package's version it kept looking at
# similar named packages and would fault. This test is to ensure only the specified package and its version is checked
$res = Find-PSResource -Name $testModuleName -Version "4.0.0" -Repository $localRepo -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
$res | Should -BeNullOrEmpty
$res = Find-PSResource -Name $similarTestModuleName -Version "4.0.0" -Repository $localRepo
$res.Name | Should -Be $similarTestModuleName
$res.Version | Should -Be "4.0.0"
}
It "find resources, including Prerelease version resources, when given Prerelease parameter" {
# FindVersionGlobbing()
$resWithoutPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo
$resWithPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo
$resWithPrerelease.Count | Should -BeGreaterOrEqual $resWithoutPrerelease.Count
}
It "find resource that satisfies given Name and Tag property (single tag)" {
# FindNameWithTag()
$requiredTag = "test"
$res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $localRepo
$res.Name | Should -Be $testModuleName
$res.Tags | Should -Contain $requiredTag
}
It "should not find resource if Name and Tag are not both satisfied (single tag)" {
# FindNameWithTag
$requiredTag = "Windows" # tag "windows" is not present for test_module package
$res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $localRepo -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
$res | Should -BeNullOrEmpty
}
It "find resource that satisfies given Name and Tag property (multiple tags)" {
# FindNameWithTag()
$requiredTags = @("test", "Tag2")
$res = Find-PSResource -Name $testModuleName -Tag $requiredTags -Repository $localRepo
$res.Name | Should -Be $testModuleName
$res.Tags | Should -Contain $requiredTags[0]
$res.Tags | Should -Contain $requiredTags[1]
}
It "find all resources that satisfy Name pattern and have specified Tag (single tag)" {
# FindNameGlobbingWithTag()
$requiredTag = "test"
$nameWithWildcard = "test_local_mod*"
$res = Find-PSResource -Name $nameWithWildcard -Tag $requiredTag -Repository $localRepo
$res.Count | Should -BeGreaterThan 1
foreach ($pkg in $res)
{
$pkg.Name | Should -BeLike $nameWithWildcard
$pkg.Tags | Should -Contain "$requiredTag"
}
}
It "should not find resources if both Name pattern and Tags are not satisfied (single tag)" {
# FindNameGlobbingWithTag()
$requiredTag = "windows" # tag "windows" is not present for test_module package
$res = Find-PSResource -Name "test_module*" -Tag $requiredTag -Repository $localRepo
$res | Should -BeNullOrEmpty
}
It "find all resources that satisfy Name pattern and have specified Tag (multiple tags)" {
# FindNameGlobbingWithTag()
$requiredTags = @("test", "Tag2")
$nameWithWildcard = "test_local_mod*"
$res = Find-PSResource -Name $nameWithWildcard -Tag $requiredTags -Repository $localRepo
$res.Count | Should -BeGreaterThan 1
foreach ($pkg in $res)
{
$pkg.Name | Should -BeLike $nameWithWildcard
$pkg.Tags | Should -Contain $requiredTags[0]
$pkg.Tags | Should -Contain $requiredTags[1]
}
}
It "find resource that satisfies given Name, Version and Tag property (single tag)" {
# FindVersionWithTag()
$requiredTag = "test"
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTag -Repository $localRepo
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0"
$res.Tags | Should -Contain $requiredTag
}
It "should not find resource if Name, Version and Tag property are not all satisfied (single tag)" {
# FindVersionWithTag()
$requiredTag = "windows" # tag "windows" is not present for test_module package
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTag -Repository $localRepo -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
$res | Should -BeNullOrEmpty
}
It "find resource that satisfies given Name, Version and Tag property (multiple tags)" {
# FindVersionWithTag()
$requiredTags = @("test", "Tag2")
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTags -Repository $localRepo
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0"
$res.Tags | Should -Contain $requiredTags[0]
$res.Tags | Should -Contain $requiredTags[1]
}
It "find scripts given -Type parameter" {
Get-ScriptResourcePublishedToLocalRepoTestDrive "testScriptName" $localRepo "1.0.0"
$res = Find-PSResource -Type Script -Repository $localRepo
$res | Should -Not -BeNullOrEmpty
$res.Count | Should -Be 2
$res.Type | Should -Be @("Script", "Script")
}
It "find modules given -Type parameter" {
Get-ScriptResourcePublishedToLocalRepoTestDrive "testScriptName" $localRepo "1.0.0"
$res = Find-PSResource -Type Module -Repository $localRepo
$res | Should -Not -BeNullOrEmpty
$res.Count | Should -BeGreaterOrEqual 1
foreach ($module in $res) {
$module.Type | Should -Be "Module"
}
}
It "find resource given CommandName" -Pending {
$res = Find-PSResource -CommandName $commandName -Repository $localRepo
$res | Should -Not -BeNullOrEmpty
foreach ($item in $res) {
$item.Names | Should -Be $commandName
$item.ParentResource.Includes.Command | Should -Contain $commandName
}
}
It "find resource given DscResourceName" -Pending {
$res = Find-PSResource -DscResourceName $dscResourceName -Repository $localRepo
$res | Should -Not -BeNullOrEmpty
foreach ($item in $res) {
$item.Names | Should -Be $dscResourceName
$item.ParentResource.Includes.DscResource | Should -Contain $dscResourceName
}
}
It "Get definition for alias 'fdres'" {
(Get-Alias fdres).Definition | Should -BeExactly 'Find-PSResource'
}
It "not find resource with tag value that is non-existent for the packages" {
# Since this is pattern matching based search no error should be written out.
$res = Find-PSResource -Tag "nonexistenttag" -Repository $localRepo -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -BeNullOrEmpty
$err.Count | Should -Not -Be 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "FindTagsPackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
}
It "find package where prerelease label includes digits and period (i.e prerelease label is not just words)" {
$nupkgName = "WebView2.Avalonia"
$nupkgVersion = "1.0.1518.46"
$prereleaseLabel = "preview.230207.17"
$res = Find-PSResource -Name $nupkgName -Prerelease -Repository $localNupkgRepo
$res.Name | Should -Be $nupkgName
$res.Version | Should -Be $nupkgVersion
$res.Prerelease | Should -Be $prereleaseLabel
}
It "find module that has multiple manifests and use exact name match one" {
# Az.KeyVault has 2 manifest files - Az.KeyVault.psd1 and Az.KeyVault.Extension.psd1
# this test was added because PSResourceGet would previously pick the .psd1 file by pattern matching the module name, not exact matching it
# this meant Az.KeyVault.Extension.psd1 and its metadata was being returned.
# The package is present on PSGallery but issue reproduces when looking at the package's file paths in local repo
$PSGalleryName = Get-PSGalleryName
Save-PSResource -Name 'Az.KeyVault' -Version '6.3.1' -Repository $PSGalleryName -AsNupkg -Path $localRepoUriAddress -TrustRepository
$res = Find-PSResource -Name 'Az.KeyVault' -Repository $localRepo
$res.Version | Should -Be "6.3.1"
}
It "Find 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 = Find-PSResource -Name $testModuleName -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 "Find 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 = Find-PSResource -Name $testModuleName -Version "1.0.0" -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"
}
}