-
Notifications
You must be signed in to change notification settings - Fork 6
/
ModuleFast.tests.ps1
433 lines (403 loc) · 15.3 KB
/
ModuleFast.tests.ps1
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
using namespace System.Management.Automation
using namespace Microsoft.PowerShell.Commands
using namespace System.Collections.Generic
using namespace System.Diagnostics.CodeAnalysis
using namespace NuGet.Versioning
. $PSScriptRoot/ModuleFast.ps1 -ImportNuGetVersioning
Import-Module $PSScriptRoot/ModuleFast.psm1 -Force
BeforeAll {
if ($env:MFURI) {
$PSDefaultParameterValues['Get-ModuleFastPlan:Source'] = $env:MFURI
}
}
InModuleScope 'ModuleFast' {
Describe 'ModuleFastSpec' {
Context 'Constructors' {
It 'Getters' {
$spec = [ModuleFastSpec]'Test'
'Name', 'Guid', 'Min', 'Max', 'Required' | ForEach-Object {
$spec.PSObject.Properties.name | Should -Contain $PSItem
}
}
It 'Name' {
$spec = [ModuleFastSpec]'Test'
$spec.Name | Should -Be 'Test'
$spec.Guid | Should -Be ([Guid]::Empty)
$spec.Min | Should -BeNull
$spec.Max | Should -BeNull
$spec.Required | Should -BeNull
}
It 'Has non-settable properties' {
$spec = [ModuleFastSpec]'Test'
{ $spec.Min = '1' } | Should -Throw
{ $spec.Max = '1' } | Should -Throw
{ $spec.Required = '1' } | Should -Throw
{ $spec.Name = 'fake' } | Should -Throw
{ $spec.Guid = New-Guid } | Should -Throw
}
It 'ModuleSpecification' {
$in = [ModuleSpecification]@{
ModuleName = 'Test'
ModuleVersion = '2.1.5'
}
$spec = [ModuleFastSpec]$in
$spec.Name | Should -Be 'Test'
$spec.Guid | Should -Be ([Guid]::Empty)
$spec.Min | Should -Be '2.1.5'
$spec.Max | Should -BeNull
$spec.Required | Should -BeNull
}
}
Context 'ModuleSpecification Conversion' {
It 'Name' {
$spec = [ModuleSpecification][ModuleFastSpec]'Test'
$spec.Name | Should -Be 'Test'
$spec.Version | Should -Be '0.0'
$spec.RequiredVersion | Should -BeNull
$spec.MaximumVersion | Should -BeNull
}
It 'RequiredVersion' {
$spec = [ModuleSpecification][ModuleFastSpec]::new('Test', '1.2.3')
$spec.Name | Should -Be 'Test'
$spec.RequiredVersion | Should -Be '1.2.3.0'
$spec.Version | Should -BeNull
$spec.MaximumVersion | Should -BeNull
}
}
}
}
Describe 'Get-ModuleFastPlan' -Tag 'E2E' {
BeforeAll {
$SCRIPT:__existingPSModulePath = $env:PSModulePath
$env:PSModulePath = $testDrive
$SCRIPT:__existingProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
}
AfterAll {
$env:PSModulePath = $SCRIPT:__existingPSModulePath
$ProgressPreference = $SCRIPT:__existingProgressPreference
}
Context 'Parameter Binding' {
#This is used for testcases
$SCRIPT:moduleName = 'Az.Accounts'
Context 'ModuleFastSpec' {
$moduleSpecTestCases = (
@{
Test = 'Name';
Spec = $SCRIPT:moduleName;
Check = {
$actual.ModuleVersion | Should -BeGreaterThan '2.7.3'
}
},
@{
Test = 'ModuleSpecification Name';
Spec = [ModuleSpecification]::new($SCRIPT:moduleName);
Check = {
$actual.ModuleVersion | Should -BeGreaterThan '2.7.3'
}
},
@{
Test = 'ModuleSpecification MinimumVersion';
Spec = [ModuleSpecification]::new(@{ ModuleName = $SCRIPT:moduleName; ModuleVersion = '0.0.0' })
Check = {
$actual.ModuleVersion | Should -BeGreaterThan '2.7.3'
}
},
@{
Test = 'ModuleSpecification RequiredVersion';
Spec = [ModuleSpecification]::new(@{ ModuleName = $SCRIPT:moduleName; RequiredVersion = '2.7.3' })
Check = {
$actual.ModuleVersion | Should -Be '2.7.3'
}
},
@{
Test = 'ModuleSpecification MaximumVersion';
Spec = [ModuleSpecification]::new(@{ ModuleName = $SCRIPT:moduleName; MaximumVersion = '2.7.3' })
Check = {
$actual.ModuleVersion | Should -Be '2.7.3'
}
}
)
It 'Gets Module with Parameter: <Test>' {
$actual = Get-ModuleFastPlan $Spec
$actual | Should -HaveCount 1
$ModuleName | Should -Be $actual.Name
$actual.ModuleVersion -as 'NuGet.Versioning.NuGetVersion' | Should -Not -BeNullOrEmpty
if ($Check) { . $Check }
} -TestCases $moduleSpecTestCases
It 'Gets Module with Pipeline: <Test>' {
$actual = $Spec | Get-ModuleFastPlan
$actual | Should -HaveCount 1
$ModuleName | Should -Be $actual.Name
$actual.ModuleVersion -as 'NuGet.Versioning.NuGetVersion' | Should -Not -BeNullOrEmpty
if ($Check) { . $Check }
} -TestCases $moduleSpecTestCases
}
Context 'ModuleFastSpec String' {
$stringTestCases = (
@{
Spec = 'Az.Accounts'
Check = {
$actual.ModuleVersion | Should -BeGreaterThan '2.7.3'
}
},
@{
Spec = 'Az.Accounts@2.7.3'
Check = {
$actual.ModuleVersion | Should -Be '2.7.3'
}
},
@{
Spec = 'Az.Accounts>2.7.3'
Check = {
$actual.ModuleVersion | Should -BeGreaterThan '2.7.3'
}
},
@{
Spec = 'Az.Accounts<2.7.3'
Check = {
$actual.ModuleVersion | Should -BeLessThan '2.7.3'
}
},
@{
Spec = 'Az.Accounts<=2.7.3'
Check = {
$actual.ModuleVersion | Should -Be '2.7.3'
}
},
@{
Spec = 'Az.Accounts>=2.7.3'
Check = {
$actual.ModuleVersion | Should -BeGreaterThan '2.7.3'
}
},
@{
Spec = 'Az.Accounts:2.7.3'
Check = {
$actual.ModuleVersion | Should -BeGreaterThan '2.7.3' -Because 'With NuGet syntax, a bare version is a minimum version, not a requiredversion'
}
},
@{
Spec = 'Az.Accounts:[2.7.3]'
Check = {
$actual.ModuleVersion | Should -Be '2.7.3'
}
},
@{
Spec = 'Az.Accounts:(,2.7.3)'
Check = {
$actual.ModuleVersion | Should -BeLessThan '2.7.3'
}
},
@{
Spec = '@{ModuleName = ''Az.Accounts''; ModuleVersion = ''2.7.3''}'
Check = {
$actual.ModuleVersion | Should -BeGreaterThan '2.7.3'
}
},
@{
Spec = '@{ModuleName = ''Az.Accounts''; RequiredVersion = ''2.7.3''}'
Check = {
$actual.ModuleVersion | Should -Be '2.7.3'
}
}
)
It 'Gets Module with String Parameter: <Spec>' {
$actual = Get-ModuleFastPlan $Spec
$actual | Should -HaveCount 1
$ModuleName | Should -Be $actual.Name
$actual.ModuleVersion -as 'NuGet.Versioning.NuGetVersion' | Should -Not -BeNullOrEmpty
if ($Check) { . $Check }
} -TestCases $stringTestCases
It 'Gets Module with String Pipeline: <Spec>' {
$actual = $Spec | Get-ModuleFastPlan
$actual | Should -HaveCount 1
$ModuleName | Should -Be $actual.Name
$actual.ModuleVersion -as 'NuGet.Versioning.NuGetVersion' | Should -Not -BeNullOrEmpty
if ($Check) { . $Check }
} -TestCases $stringTestCases
}
Context 'ModuleFastSpec Combinations' {
It 'Strings as Parameter' {
$actual = Get-ModuleFastPlan 'Az.Accounts', 'Az.Compute', 'ImportExcel'
$actual | Should -HaveCount 3
$actual | ForEach-Object {
$PSItem.Name | Should -BeIn 'Az.Accounts', 'Az.Compute', 'ImportExcel'
$PSItem.ModuleVersion -as 'NuGet.Versioning.NuGetVersion' | Should -BeGreaterThan '1.0'
}
}
It 'Strings as Pipeline' {
$actual = 'Az.Accounts', 'Az.Compute', 'ImportExcel' | Get-ModuleFastPlan
$actual | Should -HaveCount 3
$actual | ForEach-Object {
$PSItem.Name | Should -BeIn 'Az.Accounts', 'Az.Compute', 'ImportExcel'
$PSItem.ModuleVersion -as 'NuGet.Versioning.NuGetVersion' | Should -BeGreaterThan '1.0'
}
}
It 'ModuleSpecs as Parameter' {
$actual = Get-ModuleFastPlan 'Az.Accounts', '@{ModuleName = "Az.Compute"; ModuleVersion = "1.0.0" }', ([ModuleSpecification]::new('ImportExcel'))
$actual | Should -HaveCount 3
$actual | ForEach-Object {
$PSItem.Name | Should -BeIn 'Az.Accounts', 'Az.Compute', 'ImportExcel'
$PSItem.ModuleVersion -as 'NuGet.Versioning.NuGetVersion' | Should -BeGreaterThan '1.0'
}
}
It 'ModuleSpecs as Pipeline' {
$actual = 'Az.Accounts>1', '@{ModuleName = "Az.Compute"; ModuleVersion = "1.0.0" }', ([ModuleSpecification]::new('ImportExcel')) | Get-ModuleFastPlan
$actual | Should -HaveCount 3
$actual | ForEach-Object {
$PSItem.Name | Should -BeIn 'Az.Accounts', 'Az.Compute', 'ImportExcel'
$PSItem.ModuleVersion -as 'NuGet.Versioning.NuGetVersion' | Should -BeGreaterThan '1.0'
}
}
}
}
It 'Errors on Unsupported Object instead of Stringifying' {
{ Get-ModuleFastPlan [Tuple]::Create('Az.Accounts') -ErrorAction Stop }
| Should -Throw '*Cannot process argument transformation on parameter*'
}
It 'Gets Module with 1 dependency' {
Get-ModuleFastPlan 'Az.Compute' | Should -HaveCount 2
}
It 'Gets Module with lots of dependencies (Az)' {
Get-ModuleFastPlan @{ModuleName = 'Az'; ModuleVersion = '11.0' } | Should -HaveCount 86
}
It 'Gets Module with 4 section version number and a 4 section version number dependency (VMware.VimAutomation.Common)' {
Get-ModuleFastPlan 'VMware.VimAutomation.Common' | Should -HaveCount 2
}
It 'Gets multiple modules' {
Get-ModuleFastPlan @{ModuleName = 'Az'; ModuleVersion = '11.0' }, @{ModuleName = 'VMWare.PowerCli'; ModuleVersion = '13.2' }
| Should -HaveCount 168
}
It 'Casts to ModuleSpecification' {
$actual = (Get-ModuleFastPlan 'Az.Accounts') -as [Microsoft.PowerShell.Commands.ModuleSpecification]
$actual | Should -BeOfType [Microsoft.PowerShell.Commands.ModuleSpecification]
$actual.Name | Should -Be 'Az.Accounts'
$actual.RequiredVersion | Should -BeGreaterThan '2.7.3'
}
It 'Filters Prerelease Modules by Default' {
$actual = Get-ModuleFastPlan 'PrereleaseTest'
$actual.ModuleVersion | Should -Be '0.0.1'
}
It 'Shows Prerelease Modules if Prerelease is specified' {
$actual = Get-ModuleFastPlan 'PrereleaseTest' -PreRelease
$actual.ModuleVersion | Should -Be '0.0.2-newerversion'
}
It 'Detects Prerelease even if Prerelease not specified' {
$actual = Get-ModuleFastPlan 'PrereleaseTest@0.0.2-newerversion'
$actual.ModuleVersion | Should -Be '0.0.2-newerversion'
}
}
Describe 'Install-ModuleFast' -Tag 'E2E' {
BeforeAll {
$SCRIPT:__existingPSModulePath = $env:PSModulePath
filter Limit-ModulePath {
param(
[string]$path,
[Parameter(ValueFromPipeline)]
[Management.Automation.PSModuleInfo]$InputObject
)
if ($PSItem.Path.StartsWith($path)) {
return $PSItem
}
}
}
BeforeEach {
#Remove all PSModulePath to not affect existing environment
$installTempPath = Join-Path $testdrive $(New-Guid)
New-Item -ItemType Directory -Path $installTempPath -ErrorAction stop
$env:PSModulePath = $installTempPath
[SuppressMessageAttribute(
<#Category#>'PSUseDeclaredVarsMoreThanAssignments',
<#CheckId#>$null,
Justification = 'PSScriptAnalyzer doesnt see the connection between beforeeach and Describe/It'
)]
$imfParams = @{
Destination = $installTempPath
NoProfileUpdate = $true
NoPSModulePathUpdate = $true
Confirm = $false
}
}
AfterAll {
$env:PSModulePath = $SCRIPT:__existingPSModulePath
}
It 'Installs Module' {
#HACK: The testdrive mount is not available in the threadjob runspaces so we need to translate it
Install-ModuleFast @imfParams 'Az.Accounts'
Get-Item $installTempPath\Az.Accounts\*\Az.Accounts.psd1 | Should -Not -BeNullOrEmpty
}
It '4 section version numbers (VMware.PowerCLI)' {
Install-ModuleFast @imfParams 'VMware.VimAutomation.Common'
Get-Item $installTempPath\VMware*\*\*.psd1 | ForEach-Object {
$moduleFolderVersion = $_ | Split-Path | Split-Path -Leaf
Import-PowerShellDataFile -Path $_.FullName | Select-Object -ExpandProperty ModuleVersion | Should -Be $moduleFolderVersion
}
Get-Module VMWare* -ListAvailable
| Limit-ModulePath $installTempPath
| Should -HaveCount 2
}
It 'lots of dependencies (Az)' {
Install-ModuleFast @imfParams 'Az'
(Get-Module Az* -ListAvailable).count | Should -BeGreaterThan 10
}
It 'specific requiredVersion' {
Install-ModuleFast @imfParams @{ ModuleName = 'Az.Accounts'; RequiredVersion = '2.7.4' }
Get-Module Az.Accounts -ListAvailable
| Limit-ModulePath $installTempPath
| Select-Object -ExpandProperty Version
| Should -Be '2.7.4'
}
It 'specific requiredVersion when newer version is present' {
Install-ModuleFast @imfParams 'Az.Accounts'
Install-ModuleFast @imfParams @{ ModuleName = 'Az.Accounts'; RequiredVersion = '2.7.4' }
$installedVersions = Get-Module Az.Accounts -ListAvailable
| Limit-ModulePath $installTempPath
| Select-Object -ExpandProperty Version
$installedVersions | Should -HaveCount 2
$installedVersions | Should -Contain '2.7.4'
}
It 'Installs when Maximumversion is lower than currently installed' {
Install-ModuleFast @imfParams 'Az.Accounts'
Install-ModuleFast @imfParams @{ ModuleName = 'Az.Accounts'; MaximumVersion = '2.7.3' }
Get-Module Az.Accounts -ListAvailable
| Limit-ModulePath $installTempPath
| Select-Object -ExpandProperty Version
| Should -Contain '2.7.3'
}
It 'Only installs once when Update is specified and latest has not changed' {
Install-ModuleFast @imfParams 'Az.Accounts' -Update
#This will error if the file already exists
Install-ModuleFast @imfParams 'Az.Accounts' -Update
}
It 'Updates only dependent module that requires update' {
Install-ModuleFast @imfParams @{ ModuleName = 'Az.Accounts'; RequiredVersion = '2.10.2' }
Install-ModuleFast @imfParams @{ ModuleName = 'Az.Compute'; RequiredVersion = '5.0.0' }
Get-Module Az.Accounts -ListAvailable
| Limit-ModulePath $installTempPath
| Select-Object -ExpandProperty Version
| Sort-Object Version -Descending
| Select-Object -First 1
| Should -Be '2.10.2'
Install-ModuleFast @imfParams 'Az.Compute', 'Az.Accounts' #Should not update
Get-Module Az.Accounts -ListAvailable
| Limit-ModulePath $installTempPath
| Select-Object -ExpandProperty Version
| Sort-Object Version -Descending
| Select-Object -First 1
| Should -Be '2.10.2'
Install-ModuleFast @imfParams 'Az.Compute' -Update #Should disregard local install and update latest Az.Accounts
Get-Module Az.Accounts -ListAvailable
| Limit-ModulePath $installTempPath
| Select-Object -ExpandProperty Version
| Sort-Object Version -Descending
| Select-Object -First 1
| Should -BeGreaterThan ([version]'2.10.2')
Get-Module Az.Compute -ListAvailable
| Limit-ModulePath $installTempPath
| Select-Object -ExpandProperty Version
| Sort-Object Version -Descending
| Select-Object -First 1
| Should -BeGreaterThan ([version]'5.0.0')
}
}