-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathModuleFast.tests.ps1
More file actions
1192 lines (1115 loc) · 45.4 KB
/
Copy pathModuleFast.tests.ps1
File metadata and controls
1192 lines (1115 loc) · 45.4 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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using namespace System.Management.Automation
using namespace Microsoft.PowerShell.Commands
using namespace System.Collections.Generic
using namespace System.Diagnostics.CodeAnalysis
using namespace NuGet.Versioning
using namespace ModuleFast
$env:MODULEFASTDEBUG = $true
Import-Module $PSScriptRoot/ModuleFast.psd1 -Force
BeforeAll {
if ($env:MFURI) {
$PSDefaultParameterValues['*-ModuleFast*:Source'] = $env:MFURI
}
}
# ModuleFastSpec is a public C# class — no InModuleScope needed
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 'Install-ModuleFast -Plan' -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 = Install-ModuleFast $Spec -Plan
$actual | Should -HaveCount 1
$ModuleName | Should -Be $actual.Name
$actual.ModuleVersion | Should -Not -BeNullOrEmpty
if ($Check) { . $Check }
} -TestCases $moduleSpecTestCases
It 'Gets Module with Pipeline: <Test>' {
$actual = $Spec | Install-ModuleFast -Plan
$actual | Should -HaveCount 1
$ModuleName | Should -Be $actual.Name
$actual.ModuleVersion | Should -Not -BeNullOrEmpty
if ($Check) { . $Check }
} -TestCases $moduleSpecTestCases
}
Context 'StrictSemVer Parameter' {
It 'StrictSemVer matches prereleases with exclusive upper bound' {
$actual = Install-ModuleFast 'PrereleaseTest!<0.0.2' -StrictSemVer -Plan
$actual | Should -HaveCount 1
$actual.ModuleVersion.IsPrerelease | Should -Be $true
$actual.ModuleVersion.Patch | Should -Be 2
}
It 'StrictSemVer not specified does not match prereleases with exclusive upper bound' {
$actual = Install-ModuleFast 'PrereleaseTest!<0.0.2' -Plan
$actual | Should -HaveCount 1
$actual.ModuleVersion.IsPrerelease | Should -Be $false
$actual.ModuleVersion.Patch | Should -Be 1
}
}
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'
}
},
@{
Spec = 'PrereleaseTest'
Check = {
$actual.Name | Should -Be 'PrereleaseTest'
$actual.ModuleVersion | Should -Be '0.0.1'
}
ModuleName = 'PrereleaseTest'
},
@{
Spec = 'PrereleaseTest!'
Check = {
$actual.Name | Should -Be 'PrereleaseTest'
$actual.ModuleVersion | Should -Be '0.0.2-prerelease'
}
ModuleName = 'PrereleaseTest'
},
@{
Spec = '!PrereleaseTest'
Check = {
$actual.Name | Should -Be 'PrereleaseTest'
$actual.ModuleVersion | Should -Be '0.0.2-prerelease'
}
ModuleName = 'PrereleaseTest'
},
@{
Spec = 'PrereleaseTest!<=0.0.1'
Check = {
$actual.Name | Should -Be 'PrereleaseTest'
$actual.ModuleVersion | Should -Be '0.0.1'
}
ModuleName = 'PrereleaseTest'
},
@{
Spec = 'PrereleaseTest>=0.0.1-prerelease'
Check = {
$actual.Name | Should -Be 'PrereleaseTest'
$actual.ModuleVersion | Should -Be '0.0.2-prerelease'
}
ModuleName = 'PrereleaseTest'
},
@{
Spec = 'PrereleaseTest:*'
ModuleName = 'PrereleaseTest'
},
@{
Spec = 'ImportExcel<2'
ModuleName = 'ImportExcel'
Check = {
$actual.ModuleVersion | Should -Be '1.99.0' #Nuget changes this to 1.99
}
},
# Special cases where the upper bound is specified as exclusive, ignore prereleases
@{
Spec = 'PrereleaseTest!<0.0.2'
ModuleName = 'PrereleaseTest'
Check = {
$actual.ModuleVersion | Should -Be '0.0.1'
}
},
@{
Spec = 'PrereleaseTest!<=0.0.2'
ModuleName = 'PrereleaseTest'
Check = {
$actual.ModuleVersion | Should -Be '0.0.2-prerelease'
}
},
@{
# Test lexical matching. This should not match the 'prerelease' version because r is after p
Spec = 'PrereleaseTest!:(0.0.2-rIsAfterP,0.0.2)'
ModuleName = 'PrereleaseTest'
Check = {
[string]$actual | Should -Match 'a matching module was not found'
}
},
@{
Spec = 'PrereleaseTest!<0.0.2-prerelease'
ModuleName = 'PrereleaseTest'
Check = {
[string]$actual.ModuleVersion | Should -Be '0.0.2-newerversion'
}
},
@{
Spec = 'PrereleaseTest!:(0.0.2-alpha,0.0.2)'
ModuleName = 'PrereleaseTest'
Check = {
[string]$actual.ModuleVersion | Should -Be '0.0.2-prerelease'
}
},
@{
Spec = 'PrereleaseTest!:(0.0.2-alpha,0.0.2-prerelease)'
ModuleName = 'PrereleaseTest'
Check = {
[string]$actual.ModuleVersion | Should -Be '0.0.2-newerversion'
}
},
#End special cases
@{
Spec = 'PnP.PowerShell:2.2.*'
ModuleName = 'PnP.PowerShell'
Check = {
$actual.ModuleVersion | Should -Be '2.2.0'
}
},
@{
Spec = 'ConnectWiseManageAPI=0.4.15.0'
ModuleName = 'ConnectWiseManageAPI'
Check = {
$actual.ModuleVersion | Should -Be '0.4.15' #Install module version is 0.4.15.0 but nuget truncates this
}
}
)
It 'Fails if hashtable-style string parameter is not a modulespec' {
{ Install-ModuleFast '@{ModuleName = ''Az.Accounts''; ModuleVersion = ''2.7.3''; InvalidParameter = ''ThisShouldNotBeValid''}' -Plan -ErrorAction Stop }
| Should -Throw '*not valid ModuleSpecification syntax*'
}
It 'Gets Module with String Parameter: <Spec>' {
try {
$actual = Install-ModuleFast $Spec -Plan
$actual | Should -HaveCount 1
$ModuleName | Should -Be $actual.Name
$actual.ModuleVersion | Should -Not -BeNullOrEmpty
} catch {
$actual = $PSItem
}
if ($Check) { . $Check }
} -TestCases $stringTestCases
It 'Gets Module with String Pipeline: <Spec>' {
try {
$actual = $Spec | Install-ModuleFast -Plan
$actual | Should -HaveCount 1
$ModuleName | Should -Be $actual.Name
$actual.ModuleVersion | Should -Not -BeNullOrEmpty
} catch {
$actual = $PSItem
}
if ($Check) { . $Check }
} -TestCases $stringTestCases
}
Context 'ModuleFastSpec Combinations' {
It 'Strings as Parameter' {
$actual = Install-ModuleFast 'Az.Accounts', 'Az.Compute', 'ImportExcel' -Plan
$actual | Should -HaveCount 3
$actual | ForEach-Object {
$PSItem.Name | Should -BeIn 'Az.Accounts', 'Az.Compute', 'ImportExcel'
$PSItem.ModuleVersion | Should -BeGreaterThan '1.0'
}
}
It 'Strings as Pipeline' {
$actual = 'Az.Accounts', 'Az.Compute', 'ImportExcel' | Install-ModuleFast -Plan
$actual | Should -HaveCount 3
$actual | ForEach-Object {
$PSItem.Name | Should -BeIn 'Az.Accounts', 'Az.Compute', 'ImportExcel'
$PSItem.ModuleVersion | Should -BeGreaterThan '1.0'
}
}
It 'Plan output is sorted alphabetically by module name' {
$actual = Install-ModuleFast 'Az.Compute', 'Az.Accounts' -Plan
$actual | Should -HaveCount 2
($actual | Select-Object -ExpandProperty Name) | Should -Be @('Az.Accounts', 'Az.Compute')
}
It 'ModuleSpecs as Parameter' {
$actual = Install-ModuleFast 'Az.Accounts', '@{ModuleName = "Az.Compute"; ModuleVersion = "1.0.0" }', ([ModuleSpecification]::new('ImportExcel')) -Plan
$actual | Should -HaveCount 3
$actual | ForEach-Object {
$PSItem.Name | Should -BeIn 'Az.Accounts', 'Az.Compute', 'ImportExcel'
$PSItem.ModuleVersion | Should -BeGreaterThan '1.0'
}
}
It 'ModuleSpecs as Pipeline' {
$actual = 'Az.Accounts>1', '@{ModuleName = "Az.Compute"; ModuleVersion = "1.0.0" }', ([ModuleSpecification]::new('ImportExcel')) | Install-ModuleFast -Plan
$actual | Should -HaveCount 3
$actual | ForEach-Object {
$PSItem.Name | Should -BeIn 'Az.Accounts', 'Az.Compute', 'ImportExcel'
$PSItem.ModuleVersion | Should -BeGreaterThan '1.0'
}
}
It 'Prerelease does not affect non-prerelease' {
#The prerelease flag on az.accounts should not trigger prerelease on PrereleaseTest
$actual = 'Az.Accounts!', 'PrereleaseTest' | Install-ModuleFast -Plan
$actual | Should -HaveCount 2
$actual | Where-Object Name -EQ 'PrereleaseTest' | ForEach-Object {
$PSItem.ModuleVersion | Should -Be '0.0.1'
}
}
It '-Prerelease overrides even if prerelease is not specified' {
#The prerelease flag on az.accounts should not trigger prerelease on PrereleaseTest
$actual = 'Az.Accounts!', 'PrereleaseTest' | Install-ModuleFast -Prerelease -Plan
$actual | Should -HaveCount 2
$actual | Where-Object Name -EQ 'PrereleaseTest' | ForEach-Object {
$PSItem.ModuleVersion | Should -Be '0.0.2-prerelease'
}
}
}
}
It 'Errors on Unsupported Object instead of Stringifying' {
{ Install-ModuleFast [Tuple]::Create('Az.Accounts') -Plan -ErrorAction Stop }
| Should -Throw '*Cannot bind parameter*'
}
It 'Gets Module with 1 dependency' {
Install-ModuleFast 'Az.Compute' -Plan | Should -HaveCount 2
}
It 'Gets all dependencies for a Module with lots of dependencies (Az)' {
Install-ModuleFast @{ModuleName = 'Az'; RequiredVersion = '11.1.0' } -Plan | Should -HaveCount 86
}
It 'Gets Module with 4 section version number and a 4 section version number dependency (VMware.VimAutomation.Common)' {
Install-ModuleFast 'VMware.VimAutomation.Common' -Plan | Should -HaveCount 2
}
It 'Gets multiple modules' {
Install-ModuleFast @{ModuleName = 'Az'; RequiredVersion = '11.1.0' }, @{ModuleName = 'VMWare.PowerCli'; RequiredVersion = '13.2.0.22746353' } -Plan
| Should -HaveCount 122
}
It 'Casts to ModuleSpecification' {
$actual = (Install-ModuleFast 'Az.Accounts' -Plan) -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 = Install-ModuleFast 'PrereleaseTest' -Plan
$actual.ModuleVersion | Should -Be '0.0.1'
}
It 'Shows Prerelease Modules if Prerelease is specified' {
$actual = Install-ModuleFast 'PrereleaseTest' -Prerelease -Plan
$actual.ModuleVersion | Should -Be '0.0.2-prerelease'
}
It 'Detects Prerelease even if Prerelease not specified' {
$actual = Install-ModuleFast 'PrereleaseTest=0.0.2-prerelease' -Plan
$actual.ModuleVersion | Should -Be '0.0.2-prerelease'
}
}
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)' {
$actual = Install-ModuleFast @imfParams 'VMware.VimAutomation.Common=13.2.0.22643733' -PassThru
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 '4 section version numbers with repeated zeroes' {
$actual = Install-ModuleFast @imfParams 'xDSCResourceDesigner=1.13.0.0' -PassThru
$resolvedPath = Resolve-Path $actual.Location.LocalPath
Split-Path $resolvedPath -Leaf | Should -Be '1.13.0.0'
Get-Module xDSCResourceDesigner -ListAvailable
| Limit-ModulePath $installTempPath
| Should -HaveCount 1
}
It '4 section version numbers with single trailing zero' {
$actual = Install-ModuleFast @imfParams 'ConnectWiseManageApi=0.4.15.0' -PassThru
# Path should exist in the new location
$resolvedPath = $actual.Location.LocalPath
Split-Path $resolvedPath -Leaf | Should -Be '0.4.15.0'
}
It 'lots of dependencies (Az)' {
Install-ModuleFast @imfParams 'Az=11.1.0'
(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
$debugpreference = 'continue'
Install-ModuleFast @imfParams 'Az.Accounts' -Update *>&1
| Select-String 'best remote candidate matches what is locally installed'
| Should -BeLike '*best remote candidate matches what is locally installed*'
$debugpreference = 'silentlycontinue'
}
It 'Only installs once when Update is specified and latest has not changed for multiple modules' {
Install-ModuleFast @imfParams 'Az.Compute', 'Az.CosmosDB' -Update
Install-ModuleFast @imfParams 'Az.Compute', 'Az.CosmosDB' -Update -Plan
| Should -BeNullOrEmpty
}
It 'Updates if multiple local versions installed' {
Install-ModuleFast @imfParams 'Plaster=1.1.1'
Install-ModuleFast @imfParams 'Plaster=1.1.3'
$actual = Install-ModuleFast @imfParams 'Plaster' -Update -PassThru
$actual.ModuleVersion | Should -BeGreaterThan '1.1.3'
}
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 -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 -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 -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 -Descending
| Select-Object -First 1
| Should -BeGreaterThan ([version]'5.0.0')
}
It 'Detects module in other psmodulePath' {
$installPath2 = Join-Path $testdrive $(New-Guid)
New-Item -ItemType Directory $installPath2 | Out-Null
$env:PSModulePath = "$installPath2"
Install-ModuleFast @imfParams -Destination $installPath2 'PreReleaseTest'
Install-ModuleFast @imfParams 'PreReleaseTest' -PassThru | Should -BeNullOrEmpty
}
It 'Only considers destination modules if -DestinationOnly is specified' {
$installPath2 = Join-Path $testdrive $(New-Guid)
New-Item -ItemType Directory $installPath2 | Out-Null
$env:PSModulePath = "$installPath2"
Install-ModuleFast @imfParams -Destination $installPath2 'PreReleaseTest'
Install-ModuleFast @imfParams 'PreReleaseTest' -DestinationOnly -PassThru | Should -HaveCount 1
Install-ModuleFast @imfParams 'PreReleaseTest' -DestinationOnly -PassThru | Should -BeNullOrEmpty
}
#TODO: Possibly mock this so we don't touch the testing system documents directory
It 'Destination CurrentUser installs to $HOME\Documents\PowerShell\Modules' {
if (-not $profile) {
Set-ItResult -Skipped -Because 'This test is not supported when $profile is not set'
}
$winConfigPath = Join-Path (Split-Path ($profile.CurrentUserAllHosts)) 'powershell.config.json'
if (-not $IsWindows -or (Test-Path $winConfigPath)) {
Set-ItResult -Skipped -Because 'This test is not supported on non-Windows or when powershell.config.json is present'
}
try {
Remove-Item $HOME\Documents\PowerShell\Modules\PrereleaseTest -Recurse -Force -ErrorAction SilentlyContinue
Install-ModuleFast @imfParams 'PrereleaseTest' -Destination CurrentUser
Resolve-Path $HOME\Documents\PowerShell\Modules\PrereleaseTest -EA Stop
} finally {
Remove-Item $HOME\Documents\PowerShell\Modules\PrereleaseTest -Recurse -Force -ErrorAction SilentlyContinue
}
}
It '-DestinationOnly works on modules with dependencies' {
Install-ModuleFast @imfParams 'Az.Compute' -DestinationOnly -PassThru | Should -HaveCount 2
}
It 'Errors trying to install prerelease over regular module' {
Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1'
{ Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1-prerelease' }
| Should -Throw '*is newer than the requested version*'
}
It 'Errors trying to install older prerelease over regular module' {
Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1'
{ Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1-prerelease' }
| Should -Throw '*is newer than the requested version*'
}
It 'Installs regular module over prerelease module with warning' {
Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1-prerelease'
Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1' -WarningVariable actual *>&1 | Out-Null
$actual | Should -BeLike '*is newer than existing version*'
}
It 'Installs newer prerelease with warning' {
Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1-aprerelease'
Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1-bprerelease' -WarningVariable actual *>&1 | Out-Null
$actual | Should -BeLike '*is newer than existing version*'
}
It 'Doesnt install prerelease if same-version Prerelease already installed' {
Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1-prerelease'
$plan = Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1-prerelease' -Plan
$plan | Should -BeNullOrEmpty
}
It 'Installs from <Name> SpecFile' {
$SCRIPT:Mocks = Resolve-Path "$PSScriptRoot/Test/Mocks"
$specFilePath = Join-Path $Mocks $File
$modulesToInstall = Install-ModuleFast @imfParams -Path $specFilePath -Plan
#TODO: Verify individual modules and versions
$modulesToInstall | Should -Not -BeNullOrEmpty
if ($modules) {
foreach ($module in $modules) {
$module | Should -BeIn $modulesToInstall
$modulesToInstall.Remove($module)
}
#All modules should be removed at this point
$modulesToInstall | Should -BeNullOrEmpty
}
} -TestCases @(
@{
Name = 'PowerShell Data File'
File = 'ModuleFast.requires.psd1'
},
@{
Name = 'JSON'
File = 'ModuleFast.requires.json'
},
@{
Name = 'JSONArray'
File = 'ModuleFastArray.requires.json'
},
@{
Name = 'ScriptRequires'
File = 'RequiresScript.ps1'
},
@{
Name = 'ScriptModule'
File = 'RequiresModule.psm1'
},
@{
Name = 'DynamicManifest'
File = 'Dynamic.psd1'
},
@{
Name = 'ModuleBuilder'
File = 'ModuleBuilder-RequiredModules.psd1'
},
@{
Name = 'PSDepend-Implicit'
File = 'PSDepend.psd1'
}
)
It 'Fails if PSDepend File has PSDependOptions DependencyType set' {
$SCRIPT:Mocks = Resolve-Path "$PSScriptRoot/Test/Mocks"
$specFilePath = Join-Path $Mocks 'PSDepend-NotSupported.psd1'
{ Install-ModuleFast @imfParams -Path $specFilePath -Plan }
| Should -Throw '*DependencyType*'
}
It 'Fails for script if #Requires is not Present' {
$scriptPath = Join-Path $testDrive 'norequires.ps1'
{
'There is no requires here!'
} | Out-File $scriptPath
{ Install-ModuleFast @imfParams -Path $scriptPath }
| Should -Throw 'The script does not have a #Requires*'
}
It 'Fails for module if #Requires is not Present' {
$scriptPath = Join-Path $testDrive 'norequires.psm1'
{
'There is no requires here!'
} | Out-File $scriptPath
{ Install-ModuleFast @imfParams -Path $scriptPath }
| Should -Throw 'The script does not have a #Requires*'
}
It 'Fails if Module Manifest and RequiredModules is missing' {
$scriptPath = Join-Path $testDrive 'testmanifestnorequires.psd1'
"@{
'ModuleVersion' = '1.0.0'
}" | Out-File $scriptPath
{ Install-ModuleFast @imfParams -Path $scriptPath }
| Should -Throw 'The manifest does not have a RequiredMOdules key*'
}
It 'Resolves Module Manifest RequiredModules' {
$scriptPath = Join-Path $testDrive 'testmanifest.psd1'
"@{
'ModuleVersion' = '1.0.0'
'RequiredModules' = @(
'PreReleaseTest'
@{
ModuleName = 'Az.Accounts'
ModuleVersion = '2.7.0'
}
)
}" | Out-File $scriptPath
$modules = Install-ModuleFast @imfParams -Path $scriptPath -Plan
$modules.count | Should -Be 2
}
It 'Resolves GUID with version range' {
$scriptPath = Join-Path $testDrive 'testscript.ps1'
"#requires -Module @{ModuleName='PreReleaseTest';Guid='7c279caf-00bc-40ae-a1ed-184ad07be1b0';ModuleVersion='0.0.1';MaximumVersion='0.0.2'}" | Out-File $scriptPath
$actual = Install-ModuleFast @imfParams -WarningAction SilentlyContinue -Path $scriptPath -PassThru
$actual.Name | Should -Be 'PrereleaseTest'
$actual.ModuleVersion | Should -Be '0.0.1'
}
It 'Errors if GUID spec is different than installed module' {
{ Install-ModuleFast @imfParams -WarningAction SilentlyContinue -Specification "@{ModuleName='PreReleaseTest';Guid='3cb1a381-5d96-4b56-843e-dd97cf4c6545';ModuleVersion='0.0.1';MaximumVersion='0.0.2'}" -PassThru }
| Should -Throw '*Expected 3cb1a381-5d96-4b56-843e-dd97cf4c6545 but found 7c279caf-00bc-40ae-a1ed-184ad07be1b0*'
}
It 'Writes a CI File' {
Set-Location $testDrive
Install-ModuleFast @imfParams -CI -Specification 'PreReleaseTest'
Get-Item 'requires.lock.json' | Should -Not -BeNullOrEmpty
#TODO: CI Content
}
It 'Installs from CI File and Installs CI Pinned Version' {
Set-Location $testDrive
Install-ModuleFast @imfParams -CI -Specification 'PreReleaseTest=0.0.1-prerelease'
Get-Item 'requires.lock.json' | Should -Not -BeNullOrEmpty
Remove-Item $imfParams.Destination -Recurse -Force
New-Item -ItemType Directory -Path $imfParams.Destination -ErrorAction stop
Install-ModuleFast @imfParams -CI
$PreReleaseManifest = "$($imfParams.Destination)\PrereleaseTest\0.0.1\PrereleaseTest.psd1"
Resolve-Path $PreReleaseManifest
(Import-PowerShellDataFile $PreReleaseManifest).PrivateData.PSData.Prerelease
| Should -Be 'prerelease' -Because 'CI lock file should have 0.1 prerelease even if 0.2 is available'
#TODO: CI Content
}
It 'Handles an incomplete installation' {
$incompleteItemPath = "$installTempPath\PrereleaseTest\0.0.1\.incomplete"
Install-ModuleFast @imfParams -Specification 'PreReleaseTest=0.0.1'
New-Item -ItemType File -Path $incompleteItemPath
Install-ModuleFast @imfParams -Specification 'PreReleaseTest=0.0.1' -Update -WarningVariable actual 3>$null
$actual | Should -BeLike '*incomplete installation detected*'
Test-Path $incompleteItemPath | Should -BeFalse
}
It 'PassThru has proper matching output' {
$actual = Install-ModuleFast @imfParams -Specification 'PrereleaseTest=0.0.1', 'ImportExcel=7.8.6', 'PendingReboot=0.9.0.6' -PassThru
($actual | Where-Object Name -EQ 'ImportExcel').ModuleVersion | Should -Be '7.8.6'
($actual | Where-Object Name -EQ 'PrereleaseTest').ModuleVersion | Should -Be '0.0.1'
($actual | Where-Object Name -EQ 'PendingReboot').ModuleVersion | Should -Be '0.9.0.6'
}
It 'PassThru only reports on installed modules' {
Install-ModuleFast @imfParams -Specification 'Pester=5.4.0', 'Pester=5.4.1' -PassThru | Should -HaveCount 2
Install-ModuleFast @imfParams -Specification 'Pester=5.4.0', 'Pester=5.4.1' -PassThru | Should -HaveCount 0
}
Describe 'GitHub Packages' {
It 'Gets Specific Module' {
if (-not (Get-Command Get-Secret -ea 0)) {
Set-ItResult -Skipped -Because 'SecretManagement Not Present'
return
}
$credential = [PSCredential]::new('Pester', (Get-Secret -Name 'ReadOnlyPackagesGithubPAT'))
if (-not $credential) {
Set-ItResult -Skipped -Because 'No ReadOnlyPackagesGithubPAT Credential'
return
}
$actual = Install-ModuleFast @imfParams -Specification 'PrereleaseTest=0.0.1' -Source 'https://nuget.pkg.github.com/justingrote/index.json' -Credential $credential -Plan
$actual.Name | Should -Be 'PrereleaseTest'
$actual.ModuleVersion | Should -Be '0.0.1'
}
}
Describe 'Plan Parameter' {
It 'Does not install if Plan is specified' {
Install-ModuleFast @imfParams -Specification 'PrereleaseTest' -Plan | Should -Match 'PreReleaseTest'
Test-Path $installTempPath\PrereleaseTest | Should -BeFalse
}
}
Describe 'LocalModuleFinder' {
It 'Skips and deletes .incomplete folders' {
# Install a module, then mark it incomplete and try to re-install
Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1'
$moduleDir = Get-ChildItem $installTempPath\PrereleaseTest -Directory | Select-Object -First 1
New-Item -Path (Join-Path $moduleDir.FullName '.incomplete') -ItemType File | Out-Null
# Should ignore the incomplete install and re-download
$plan = Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1' -Plan
$plan | Should -Not -BeNullOrEmpty
# The incomplete folder should have been deleted
Test-Path $moduleDir.FullName | Should -BeFalse
}
It 'Detects module with 3-part version folder' {
Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1'
# The folder should be 3-part (0.0.1) not 4-part
$versionFolder = Get-ChildItem $installTempPath\PrereleaseTest -Directory | Select-Object -First 1
$versionFolder.Name | Should -Be '0.0.1'
# Second install should detect it and produce no plan
$plan = Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1' -Plan
$plan | Should -BeNullOrEmpty
}
It 'Handles missing manifest gracefully' {
Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1'
$moduleDir = Get-ChildItem $installTempPath\PrereleaseTest -Directory | Select-Object -First 1
# Remove the manifest to simulate corruption
Remove-Item (Join-Path $moduleDir.FullName '*.psd1')
# Should produce a plan since the local module is unreadable
$plan = Install-ModuleFast @imfParams 'PrereleaseTest=0.0.1' -Plan
$plan | Should -Not -BeNullOrEmpty
}
}
Describe 'Duplicate Specification Warning' {
It 'Warns when same module is specified twice' {
$actual = Install-ModuleFast @imfParams 'PrereleaseTest', 'PrereleaseTest' -Plan -WarningVariable warnings 3>$null
$warnings | Should -BeLike '*specified twice*'
}
}
Describe 'Prerelease Parameter' {
It 'Installs prerelease when -Prerelease is specified globally' {
$actual = Install-ModuleFast @imfParams 'PrereleaseTest' -Prerelease -PassThru
$actual | Should -HaveCount 1
$actual.ModuleVersion | Should -Be '0.0.2-prerelease'
}
}
Describe 'StrictSemVer Parameter' {
It 'StrictSemVer includes prereleases within exclusive upper bound' {
$actual = Install-ModuleFast @imfParams 'PrereleaseTest!<0.0.2' -StrictSemVer -Plan
$actual | Should -HaveCount 1
$actual.ModuleVersion.IsPrerelease | Should -Be $true
}
It 'Without StrictSemVer excludes prereleases from exclusive upper bound' {
$actual = Install-ModuleFast @imfParams 'PrereleaseTest!<0.0.2' -Plan
$actual | Should -HaveCount 1
$actual.ModuleVersion.IsPrerelease | Should -Be $false
}
}
Describe 'ModuleFastInfo Pipeline' {
It 'Installs modules from piped Install-ModuleFast -Plan output' {
$plan = Install-ModuleFast 'PrereleaseTest=0.0.1' -Plan
$actual = $plan | Install-ModuleFast @imfParams -PassThru
$actual | Should -HaveCount 1
$actual.Name | Should -Be 'PrereleaseTest'
Get-Item $installTempPath\PrereleaseTest\*\PrereleaseTest.psd1 | Should -Not -BeNullOrEmpty
}
}
Describe 'Auto-Detect Spec Files' {
It 'Detects spec files in current directory when no args given' {
$specDir = Join-Path $testDrive 'autodetect'
New-Item -ItemType Directory $specDir | Out-Null
"@{ 'PrereleaseTest' = 'latest' }" | Out-File (Join-Path $specDir 'ModuleFast.requires.psd1')
Push-Location $specDir
try {
$actual = Install-ModuleFast @imfParams -Plan
$actual | Should -Not -BeNullOrEmpty
$actual.Name | Should -Contain 'PrereleaseTest'
} finally {
Pop-Location
}
}
}
Describe 'Directory Path' {
It 'Installs from directory containing spec files' {
$SCRIPT:Mocks = Resolve-Path "$PSScriptRoot/Test/Mocks"
$actual = Install-ModuleFast @imfParams -Path $Mocks -Plan
$actual | Should -Not -BeNullOrEmpty
}
}
}
Describe 'Clear-ModuleFastCache' {
It 'Runs without error' {
{ Clear-ModuleFastCache } | Should -Not -Throw
}
It 'Clears cached entries so subsequent plans re-fetch' {
# Prime the cache
Install-ModuleFast 'PrereleaseTest=0.0.1' -Plan | Out-Null
Clear-ModuleFastCache
# Should still return a valid plan after cache flush
$actual = Install-ModuleFast 'PrereleaseTest=0.0.1' -Plan
$actual | Should -HaveCount 1
}
}
Describe 'ModuleFastSpec' {
Context 'Hashtable Constructor' {
It 'Creates from hashtable' {
$spec = [ModuleFastSpec]@{ ModuleName = 'Test'; ModuleVersion = '1.0.0' }
$spec.Name | Should -Be 'Test'
$spec.Min | Should -Be '1.0.0'
}
It 'Creates from hashtable with RequiredVersion' {
$spec = [ModuleFastSpec]@{ ModuleName = 'Test'; RequiredVersion = '2.0.0' }
$spec.Name | Should -Be 'Test'
$spec.Required | Should -Be '2.0.0'
}
It 'Creates from hashtable with Guid' {
$guid = [Guid]::NewGuid()
$spec = [ModuleFastSpec]@{ ModuleName = 'Test'; Guid = $guid; ModuleVersion = '1.0.0' }
$spec.Name | Should -Be 'Test'
$spec.Guid | Should -Be $guid
}
}
Context 'SatisfiedBy' {
It 'Exact version satisfies required version spec' {
$spec = [ModuleFastSpec]'Test=1.0.0'