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 pathPSGetFindScript.Tests.ps1
More file actions
440 lines (389 loc) · 18.2 KB
/
Copy pathPSGetFindScript.Tests.ps1
File metadata and controls
440 lines (389 loc) · 18.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
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
<#####################################################################################
# File: PSGetFindScriptTests.ps1
# Tests for PSGet script functionality
#
# Copyright (c) Microsoft Corporation, 2015
#####################################################################################>
<#
Name: PowerShell.PSGet.FindScriptTests
Description: Tests for Find-Script 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:DscTestScript = "DscTestScript"
#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
}
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
}
Describe PowerShell.PSGet.FindScriptTests -Tags 'BVT', 'InnerLoop' {
BeforeAll {
SuiteSetup
}
AfterAll {
SuiteCleanup
}
# Purpose:
# Test Find-Script cmdlet without any parameters
#
# Action:
# Find-Script
#
# Expected Result:
# Should find few scripts
#
It "FindScriptWithoutAnyParameterValues" {
$psgetItemInfo = Find-Script
Assert ($psgetItemInfo.Count -ge 1) "Find-Script did not return any scripts."
}
# Purpose: FindASpecificScript
#
# Action: Find-Script Fabrikam-ServerScript
#
# Expected Result: Should find Fabrikam-ServerScript script
#
It "FindASpecificScript" {
$res = Find-Script Fabrikam-ServerScript
Assert ($res -and ($res.Name -eq "Fabrikam-ServerScript")) "Find-Script failed to find a specific script"
}
# Purpose: FindScriptWithRangeWildCards
#
# Action: Find-Script "Co[nN]t?soS[a-z]r?er"
#
# Expected Result: Should find Fabrikam-ServerScript script
#
It "FindScriptWithRangeWildCards" {
$res = Find-Script -Name "Fab[rR]ikam?Ser[a-z]erScr?pt"
Assert ($res -and ($res.Name -eq "Fabrikam-ServerScript")) "Find-Script failed to get a script with wild card in script name"
}
# Purpose: FindNotAvaialableScriptWithWildCards
#
# Action: Find-Script "Fab[rR]ikam?Ser[a-z]erScr?ptW"
#
# Expected Result: Should not find any script
#
It "FindNotAvaialableScriptWithWildCards" {
$res = Find-Script -Name "Fab[rR]ikam?Ser[a-z]erScr?ptW"
Assert (-not $res) "Find-Script should not find a not available script with wild card in script name"
}
# Purpose: FindScriptNonExistentScript
#
# Action: Find-Script NonExistentScript
#
# Expected Result: Should fail
#
It "FindScriptNonExistentScript" {
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script NonExistentScript} `
-expectedFullyQualifiedErrorId 'NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.FindPackage'
}
# Purpose: FindModuleNotScript
#
# Action: Find-Script ContosoServer
#
# Expected Result: Should fail
#
It "FindModuleNotScript" {
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script ContosoServer} `
-expectedFullyQualifiedErrorId 'MatchInvalidType,Find-Script'
}
# Purpose: FindModuleNotScriptWildcard
#
# Action: Find-Script ContosoServer
#
# Expected Result: Should not return anything
#
It "FindModuleNotScriptWildcard" {
$res = Find-Script ContosoServer*
Assert (-not $res) "Find-Script returned a module"
}
# Purpose: Find a script using MinimumVersion
#
# Action: Find-Script Fabrikam-ServerScript -MinimumVersion 1.0
#
# Expected Result: Should find the Fabrikam-ServerScript script
#
It "FindScriptWithMinVersion" {
$res = Find-Script Fabrikam-ServerScript -MinimumVersion 1.0
Assert ($res.Name -eq "Fabrikam-ServerScript" -and $res.Version -ge [Version]"1.0" ) "Find-Script failed to find a script using MinimumVersion"
}
# Purpose: FindScriptWithRequiredVersion
#
# Action: Find-Script Fabrikam-ServerScript -RequiredVersion 2.0
#
# Expected Result: Should find the Fabrikam-ServerScript script with version 2.0
#
It "FindScriptWithRequiredVersion" {
$res = Find-Script Fabrikam-ServerScript -RequiredVersion 2.0
Assert ($res -and ($res.Name -eq "Fabrikam-ServerScript") -and $res.Version -eq [Version]"2.0") "Find-Script failed to find a script using RequiredVersion, $res"
}
# Purpose: FindScriptWithMultiNames
#
# Action: Find-Script Fabrikam-ClientScript,Fabrikam-ServerScript
#
# Expected Result: should find Fabrikam-ClientScript and Fabrikam-ServerScript scripts
#
It "FindScriptWithMultiNames" {
$res = Find-Script Fabrikam-ClientScript, Fabrikam-ServerScript -Repository PSGallery
Assert ($res.Count -eq 2) "Find-Script with multiple names should not fail, $res"
}
# Purpose: FindScriptWithAllVersions
#
# Action: Find-Script Fabrikam-ClientScript -AllVersions
#
# Expected Result: should find more than one version of the Fabrikam-ClientScript script
#
It FindScriptWithAllVersions {
$res = Find-Script Fabrikam-ClientScript -Repository PSGallery -AllVersions
Assert ($res.Count -gt 1) "Find-Script with -AllVersions should return more than one version, $res"
}
# Purpose: Validate Find-Script -Filter KeyWord1
#
# Action: Find-Script -Filter KeyWord1
#
# Expected Result: Find-Script should work and it should have valid metadata
#
It FindScriptUsingFilter {
$psgetItemInfo = Find-Script -Filter Fabrikam-ClientScript
AssertEquals $psgetItemInfo.Name Fabrikam-ClientScript "Find-Script with filter is not working, $psgetItemInfo"
}
# Purpose: Validate Find-Script -Includes Workflow
#
# Action: Find-Script -Includes Workflow
#
# Expected Result: Find-Script should work and it should have valid metadata
#
It FindScriptUsingIncludesWorkflow {
$psgetScriptInfo = Find-Script -Includes Workflow | Where-Object {$_.Name -eq 'Fabrikam-ClientScript'}
AssertNotNull $psgetScriptInfo.Includes "Includes is missing on PSGetScriptInfo, $($psgetScriptInfo.Includes)"
Assert (-not $psgetScriptInfo.Includes.DscResource.Count) "Script should not have any DscResources, $($psgetScriptInfo.Includes.DscResource)"
Assert $psgetScriptInfo.Includes.Workflow.Count "Workflows are missing on PSGetScriptInfo, $($psgetScriptInfo.Includes.Workflow)"
AssertEquals $psgetScriptInfo.Includes.Workflow 'Test-WorkflowFromScript_Fabrikam-ClientScript' "Test-WorkflowFromScript_Fabrikam-ClientScript Workflow is missing on PSGetScriptInfo, $($psgetScriptInfo.Includes.Workflow)"
Assert $psgetScriptInfo.Includes.Command.Count "Commands are missing on PSGetScriptInfo, $($psgetScriptInfo.Includes.Command)"
Assert $psgetScriptInfo.Includes.Function.Count "Functions are missing on PSGetScriptInfo, $($psgetScriptInfo.Includes.Function)"
Assert (-not $psgetScriptInfo.Includes.Cmdlet.Count) "Script should not have any cmdlets, $($psgetScriptInfo.Includes.Cmdlet)"
}
# Purpose: Validate Find-Script -Includes Function
#
# Action: Find-Script -Includes Function
#
# Expected Result: Find-Script should work and it should have valid metadata
#
It FindScriptUsingIncludesFunction {
$psgetScriptInfo = Find-Script -Includes Function -Tag Tag1 | Where-Object {$_.Name -eq 'Fabrikam-ServerScript'}
AssertNotNull $psgetScriptInfo.Includes "Includes is missing on PSGetScriptInfo, $($psgetScriptInfo.Includes)"
Assert (-not $psgetScriptInfo.Includes.DscResource.Count) "Script should not have any DscResources, $($psgetScriptInfo.Includes.DscResource)"
Assert $psgetScriptInfo.Includes.Workflow.Count "Workflows are missing on PSGetScriptInfo, $($psgetScriptInfo.Includes.Workflow)"
Assert $psgetScriptInfo.Includes.Command.Count "Commands are missing on PSGetScriptInfo, $($psgetScriptInfo.Includes.Command)"
Assert $psgetScriptInfo.Includes.Function.Count "Functions are missing on PSGetScriptInfo, $($psgetScriptInfo.Includes.Function)"
AssertEquals $psgetScriptInfo.Includes.Function 'Test-FunctionFromScript_Fabrikam-ServerScript' "Test-FunctionFromScript_Fabrikam-ServerScript function is missing on PSGetScriptInfo, $($psgetScriptInfo.Includes.Function)"
Assert (-not $psgetScriptInfo.Includes.Cmdlet.Count) "Script should not have any cmdlets, $($psgetScriptInfo.Includes.Cmdlet)"
}
# Purpose: Validate Find-Script with Tag parameter
#
# Action: Find-Script -Tag Function
#
# Expected Result: Find-Script should work and it should have valid metadata
#
It FindScriptUsingTag {
$tagValue = 'Tag-Fabrikam-Script-2.5'
$psgetScriptInfo = Find-Script -Tag $tagValue
AssertNotNull $psgetScriptInfo.Tags "Tags is missing on PSGetScriptInfo, $($psgetScriptInfo.Tags)"
Assert ($psgetScriptInfo.Tags -contains $tagValue) "$tagValue is missing in Tags, $($psgetScriptInfo.Tags)"
}
# Purpose: Validate Find-Script with Command parameter
#
# Action: Find-Script -Command Test-FunctionFromScript_Fabrikam-Script
#
# Expected Result: Find-Script should work and it should have valid metadata
#
It FindScriptUsingCommand {
$commandName = 'Test-FunctionFromScript_Fabrikam-Script'
$psgetScriptInfo = Find-Script -Command $commandName
AssertNotNull $psgetScriptInfo.Includes.Command "Command list is missing on PSGetScriptInfo, $($psgetScriptInfo.Includes.Command)"
Assert ($psgetScriptInfo.Includes.Command -contains $commandName) "$commandName is missing in Command, $($psgetScriptInfo.Includes.Command)"
}
}
Describe PowerShell.PSGet.FindScriptTests.P1 -Tags 'P1', 'OuterLoop' {
BeforeAll {
SuiteSetup
}
AfterAll {
SuiteCleanup
}
# Purpose: FindScriptWithPostfixWildcard
#
# Action: Find-Script Fabrikam-Serve*
#
# Expected Result: Should find Fabrikam-ServerScript script
#
It "FindScriptWithPostfixWildcard" {
$res = Find-Script Fabrikam-Serve*
Assert ($res -and ($res.Name -eq "Fabrikam-ServerScript")) "Find-Script failed to get a script with postfix wild card search"
}
# Purpose: FindScriptWithPrefixWildcard
#
# Action: Find-Script *ontosoServer
#
# Expected Result: Should find Fabrikam-ServerScript script
#
It "FindScriptWithPrefixWildcard" {
$res = Find-Script *ServerScript
Assert ($res -and ($res.Name -eq "Fabrikam-ServerScript")) "Find-Script failed to get a script with wild card"
}
# Purpose: FindMultipleScriptsWithWildcard
#
# Action: Find-Script Fabrikam*
#
# Expected Result: Should find atleast 3 scripts
#
It "FindMultipleScriptsWithWildcard" {
$res = Find-Script Fabrikam*
Assert ($res.Count -ge 3) "Find-Script failed to multiple scripts with wild card"
}
# Purpose: FindScriptWithWildcards
#
# Action: Find-Script *ontosoServe*
#
# Expected Result: Should find Fabrikam-ServerScript script
#
It "FindScriptWithWildcards" {
$res = Find-Script *rikam-ServerScr*
Assert ($res -and ($res.Name -eq "Fabrikam-ServerScript")) "Find-Script failed to find script using wild cards"
}
# Purpose: FindScriptWithVersionParams
#
# Action: Find-Script Fabrikam-ServerScript -MinimumVersion 1.0 -RequiredVersion 5.0
#
# Expected Result: Should fail with error id
#
It "FindScriptWithVersionParams" {
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script Fabrikam-ServerScript -MinimumVersion 1.0 -RequiredVersion 5.0} `
-expectedFullyQualifiedErrorId "VersionRangeAndRequiredVersionCannotBeSpecifiedTogether,Find-Script"
}
# Purpose: Find a script with not available MinimumVersion
#
# Action: Find-Script Fabrikam-ServerScript -MinimumVersion 10.0
#
# Expected Result: Should not find the Fabrikam-ServerScript script
#
It "FindScriptWithMinVersionNotAvailable" {
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script Fabrikam-ServerScript -MinimumVersion 10.0} `
-expectedFullyQualifiedErrorId "NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.FindPackage"
}
# Purpose: FindScriptWithReqVersionNotAvailable
#
# Action: Find-Script Fabrikam-ServerScript -RequiredVersion 10.0
#
# Expected Result: Should not find the Fabrikam-ServerScript script
#
It "FindScriptWithReqVersionNotAvailable" {
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script Fabrikam-ServerScript -RequiredVersion 10.0} `
-expectedFullyQualifiedErrorId "NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.FindPackage"
}
# Purpose: FindScriptWithMultipleScriptNamesAndReqVersion
#
# Action: Find-Script Fabrikam-ServerScript,Fabrikam-ClientScript -RequiredVersion 1.0
#
# Expected Result: Should fail with error id
#
It "FindScriptWithMultipleScriptNamesAndReqVersion" {
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script Fabrikam-ServerScript, Fabrikam-ClientScript -RequiredVersion 1.0} `
-expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Find-Script"
}
# Purpose: FindScriptWithMultipleScriptNamesAndMinVersion
#
# Action: Find-Script Fabrikam-ServerScript,Fabrikam-ClientScript -MinimumVersion 1.0
#
# Expected Result: Should fail with error id
#
It "FindScriptWithMultipleScriptNamesAndMinVersion" {
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script Fabrikam-ServerScript, Fabrikam-ClientScript -MinimumVersion 1.0} `
-expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Find-Script"
}
# Purpose: FindScriptWithWildcardNameAndReqVersion
#
# Action: Find-Script Fabrikam-Ser*er -RequiredVersion 1.0
#
# Expected Result: Should fail with error id
#
It "FindScriptWithWildcardNameAndReqVersion" {
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script Fabrikam-Ser*er -RequiredVersion 1.0} `
-expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Find-Script"
}
# Purpose: FindScriptWithWildcardNameAndMinVersion
#
# Action: Find-Script Fabrikam-Ser*er -MinimumVersion 1.0
#
# Expected Result: Should fail with error id
#
It "FindScriptWithWildcardNameAndMinVersion" {
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script Fabrikam-Ser*er -MinimumVersion 1.0} `
-expectedFullyQualifiedErrorId "VersionParametersAreAllowedOnlyWithSingleName,Find-Script"
}
# Purpose: FindScriptWithAllVersionsAndMinimumVersion
#
# Action: Find-Script Fabrikam-ClientScript -AllVersions -MinimumVersion 2.0
#
# Expected Result: should fail with an error id
#
It FindScriptWithAllVersionsAndMinimumVersion {
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script Fabrikam-ClientScript -MinimumVersion 2.0 -Repository PSGallery -AllVersions} `
-expectedFullyQualifiedErrorId 'AllVersionsCannotBeUsedWithOtherVersionParameters,Find-Script'
}
# Purpose: FindScriptWithAllVersionsAndRequiredVersion
#
# Action: Find-Script Fabrikam-ClientScript -AllVersions -RequiredVersion 2.0
#
# Expected Result: should fail with an error id
#
It FindScriptWithAllVersionsAndRequiredVersion {
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Script Fabrikam-ClientScript -RequiredVersion 2.0 -Repository PSGallery -AllVersions} `
-expectedFullyQualifiedErrorId 'AllVersionsCannotBeUsedWithOtherVersionParameters,Find-Script'
}
# Purpose: Validate Find-Script -Filter KeyWordNotExists
#
# Action: Find-Script -Filter KeyWordNotExists
#
# Expected Result: Find-Script should not return any results
#
It FindScriptUsingFilterKeyWordNotExists {
$psgetItemInfo = Find-Script -Filter KeyWordNotExists
AssertNull $psgetItemInfo "Find-Script with filter is not working for KeyWordNotExists, $psgetItemInfo"
}
# Purpose: Validate Find-Script cmdlet with -IncludeDependencies for a script with dependencies
#
# Action: Find-Script -Name ScriptWithDependencies1 -IncludeDependencies
#
# Expected Result: Should return the script with its dependencies
#
It FindScriptWithIncludeDependencies {
$ScriptName = "Script-WithDependencies1"
$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"
$DepencyScriptNames = $res1.Dependencies.Name
$res2 = Find-Script -Name $ScriptName -IncludeDependencies -MaximumVersion "1.0" -MinimumVersion "0.1"
Assert ($res2.Count -ge ($DepencyScriptNames.Count + 1)) "Find-Script with -IncludeDependencies returned wrong results, $res2"
$DepencyScriptNames | ForEach-Object { Assert ($res2.Name -Contains $_) "Find-Script with -IncludeDependencies didn't return the $_ script, $($res2.Name)"}
}
}