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 133
Expand file tree
/
Copy pathPSGetPublishModule.Tests.ps1
More file actions
2210 lines (1872 loc) · 115 KB
/
PSGetPublishModule.Tests.ps1
File metadata and controls
2210 lines (1872 loc) · 115 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
<#####################################################################################
# File: PSGetPublishModuleTests.ps1
# Tests for PSGet module functionality
#
# Copyright (c) Microsoft Corporation, 2014
#####################################################################################>
<#
Name: PowerShell.PSGet.PublishModuleTests
Description: Tests for Publish-Module functionality
The local directory based NuGet repository is used for publishing the modules.
#>
# Not executing these tests on MacOS as
# the total execution time is exceeding allowed 50 min in TravisCI daily builds.
if($IsMacOS) {
return
}
function SuiteSetup {
Import-Module "$PSScriptRoot\PSGetTestUtils.psm1" -WarningAction SilentlyContinue
Import-Module "$PSScriptRoot\Asserts.psm1" -WarningAction SilentlyContinue
$script:ProgramFilesModulesPath = Get-AllUsersModulesPath
$script:MyDocumentsModulesPath = Get-CurrentUserModulesPath
$script:PSGetLocalAppDataPath = Get-PSGetLocalAppDataPath
$script:TempPath = Get-TempPath
$script:CurrentPSGetFormatVersion = "1.0"
$script:OutdatedNuGetExeVersion = [System.Version]"2.8.60717.93"
#Bootstrap NuGet binaries
Install-NuGetBinaries
$script:psgetModuleInfo = Import-Module PowerShellGet -Global -Force -Passthru
Import-LocalizedData script:LocalizedData -filename PSGet.Resource.psd1 -BaseDirectory $script:psgetModuleInfo.ModuleBase
$script:PSGalleryRepoPath = Join-Path -Path $script:TempPath -ChildPath 'PSGalleryRepo'
RemoveItem $script:PSGalleryRepoPath
$null = New-Item -Path $script:PSGalleryRepoPath -ItemType Directory -Force
$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
}
Set-PSGallerySourceLocation -Location $script:PSGalleryRepoPath -PublishLocation $script:PSGalleryRepoPath
$modSource = Get-PSRepository -Name "PSGallery"
AssertEquals $modSource.SourceLocation $script:PSGalleryRepoPath "Test repository's SourceLocation is not set properly"
AssertEquals $modSource.PublishLocation $script:PSGalleryRepoPath "Test repository's PublishLocation is not set properly"
$script:ApiKey="TestPSGalleryApiKey"
# Create temp module to be published
$script:TempModulesPath = Join-Path -Path $script:TempPath -ChildPath "PSGet_$(Get-Random)"
$null = New-Item -Path $script:TempModulesPath -ItemType Directory -Force
$script:PublishModuleName = "ContosoPublishModule"
$script:PublishModuleBase = Join-Path $script:TempModulesPath $script:PublishModuleName
$null = New-Item -Path $script:PublishModuleBase -ItemType Directory -Force
}
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
RemoveItem $script:PSGalleryRepoPath
RemoveItem $script:TempModulesPath
}
Describe PowerShell.PSGet.PublishModuleTests -Tags 'BVT','InnerLoop' {
BeforeAll {
SuiteSetup
}
AfterAll {
SuiteCleanup
}
BeforeEach {
Set-Content (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psm1") -Value "function Get-$script:PublishModuleName { Get-Date }"
$version = "1.0"
$script:PublishModuleBase = Join-Path $script:TempModulesPath $script:PublishModuleName
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
# Copy module to $script:ProgramFilesModulesPath
Copy-Item $script:PublishModuleBase $script:ProgramFilesModulesPath -Recurse -Force
}
AfterEach {
RemoveItem "$script:PSGalleryRepoPath\*"
RemoveItem "$script:ProgramFilesModulesPath\$script:PublishModuleName\*"
RemoveItem "$script:ProgramFilesModulesPath\$script:PublishModuleName"
RemoveItem "$script:PublishModuleBase\*"
}
# Purpose: Publish a module with -Name
#
# Action: Publish-Module -Name ContosoPublishModule -NuGetApiKey <ApiKey>
#
# Expected Result: should be able to publish a module
#
It "PublishModuleWithName" {
$version = "1.0"
$semanticVersion = '1.0.0'
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
#Copy module to $script:ProgramFilesModulesPath
Copy-Item $script:PublishModuleBase $script:ProgramFilesModulesPath -Recurse -Force
Publish-Module -Name $script:PublishModuleName -ReleaseNotes "$script:PublishModuleName release notes" -Tags PSGet -LicenseUri "https://$script:PublishModuleName.com/license" -ProjectUri "https://$script:PublishModuleName.com" -WarningAction SilentlyContinue
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
# This version can be 1.0 or 1.0.0, depending on the version of NuGet.exe or dotnet command.
Assert (($psgetItemInfo.Name -eq $script:PublishModuleName) -and (($psgetItemInfo.Version.ToString() -eq $version) -or ($psgetItemInfo.Version.ToString() -eq $semanticVersion))) "Publish-Module should publish a module with valid module name, $($psgetItemInfo.Name)"
}
# Purpose: Publish a module with -Name and Module is created with SxS multi version support
#
# Action: Publish-Module -Name ContosoPublishModule -NuGetApiKey <ApiKey>
#
# Expected Result: should be able to publish a module
#
It "PublishModuleWithNameForSxSVersion" {
$version = "2.0.0.0"
$semanticVersion = '2.0.0'
RemoveItem "$script:PublishModuleBase\*"
RemoveItem "$script:ProgramFilesModulesPath\$script:PublishModuleName\*"
$moduleBaseWithVersion = (Join-Path -Path $script:PublishModuleBase -ChildPath "$version")
$null = New-Item -Path $moduleBaseWithVersion -ItemType Directory -Force
Set-Content (Join-Path $moduleBaseWithVersion "$script:PublishModuleName.psm1") -Value "function Get-$script:PublishModuleName { Get-Date }"
New-ModuleManifest -Path (Join-Path $moduleBaseWithVersion "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
#Copy module to $script:ProgramFilesModulesPath
Copy-Item $script:PublishModuleBase $script:ProgramFilesModulesPath -Recurse -Force
Publish-Module -Name $script:PublishModuleName -NuGetApiKey $script:ApiKey -ReleaseNotes "$script:PublishModuleName release notes" -Tags PSGet -LicenseUri "https://$script:PublishModuleName.com/license" -ProjectUri "https://$script:PublishModuleName.com" -WarningAction SilentlyContinue
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
Assert (($psgetItemInfo.Name -eq $script:PublishModuleName) -and (($psgetItemInfo.Version.ToString() -eq $version) -or ($psgetItemInfo.Version.ToString() -eq $semanticVersion))) "Publish-Module should publish a module with valid module name, $($psgetItemInfo.Name)"
} `
-Skip:$(-not (Test-ModuleSxSVersionSupport))
# Purpose: Publish a module with -Name & -RequiredVersion and Module is created with SxS multi version support
#
# Action: Publish-Module -Name ContosoPublishModule -RequiredVersion 2.0 -NuGetApiKey <ApiKey>
#
# Expected Result: should be able to publish a module
#
It PublishModuleWithNameRequiredVersionForSxSVersion {
$version = "2.0.0"
$moduleBaseWithVersion = (Join-Path -Path $script:PublishModuleBase -ChildPath "$version")
$null = New-Item -Path $moduleBaseWithVersion -ItemType Directory -Force
Set-Content (Join-Path -Path $moduleBaseWithVersion -ChildPath "$script:PublishModuleName.psm1") -Value "function Get-$script:PublishModuleName { Get-Date }"
New-ModuleManifest -Path (Join-Path -Path $moduleBaseWithVersion -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
#Copy module to $script:ProgramFilesModulesPath
Copy-Item $script:PublishModuleBase $script:ProgramFilesModulesPath -Recurse -Force
Publish-Module -Name $script:PublishModuleName `
-RequiredVersion $version `
-NuGetApiKey $script:ApiKey `
-ReleaseNotes "$script:PublishModuleName release notes" `
-Tags 'PSGet' `
-LicenseUri "https://$script:PublishModuleName.com/license" `
-ProjectUri "https://$script:PublishModuleName.com" `
-WarningAction SilentlyContinue
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
Assert (($psgetItemInfo.Name -eq $script:PublishModuleName) -and ($psgetItemInfo.Version.ToString() -eq $version)) "Publish-Module should publish a module with valid module name, $($psgetItemInfo.Name)"
} `
-Skip:$(-not (Test-ModuleSxSVersionSupport))
# Purpose: Publish a module with -Path
#
# Action: Publish-Module -Path <ModulePath> -NuGetApiKey <ApiKey>
#
# Expected Result: should be able to publish a module
#
It "PublishModuleWithPath" {
$version = "1.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
Publish-Module -Path $script:PublishModuleBase -NuGetApiKey $script:ApiKey -ReleaseNotes "$script:PublishModuleName release notes" -Tags PSGet -LicenseUri "https://$script:PublishModuleName.com/license" -ProjectUri "https://$script:PublishModuleName.com" -WarningAction SilentlyContinue
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
Assert ($psgetItemInfo.Name -eq $script:PublishModuleName) "Publish-Module should publish a module with valid module path, $($psgetItemInfo.Name)"
}
# Purpose: Check that files with hidden attributes (like .git dirs) are not published
#
# Action: Publish-Module -Path <ModulePath> -NuGetApiKey <ApiKey>
#
# Expected Result: published module should not contain these files
#
It "PublishModuleLeavesOutHiddenFiles" {
# create module dir containing a hidden dir with a normal file inside, and a normal file
$version = "1.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
New-Item -Type Directory -Path $script:PublishModuleBase -Name ".git"
New-Item -Type File -Path $script:PublishModuleBase\.git\dummy
New-Item -Type File -Path $script:PublishModuleBase -Name "normal"
$hiddenDir = Get-Item $script:PublishModuleBase\.git -force
$hiddenDir.Attributes = "hidden"
# publish it, then save the published one and inspect it
Publish-Module -Path $script:PublishModuleBase -NuGetApiKey $script:ApiKey -ReleaseNotes "$script:PublishModuleName release notes" -Tags PSGet -LicenseUri "https://$script:PublishModuleName.com/license" -ProjectUri "https://$script:PublishModuleName.com" -WarningAction SilentlyContinue
New-Item -type directory -path $script:PublishModuleBase -Name saved
Save-Module $script:PublishModuleName -RequiredVersion $version -Path $script:PublishModuleBase\saved
# it should contain the normal file, but not the hidden dir
$contents = (dir -rec -force $script:PublishModuleBase | Out-String)
$basedir = "$script:PublishModuleBase\saved\$script:PublishModuleName"
if($PSVersionTable.PSVersion -gt '5.0.0') {
$basedir = join-path $basedir "1.0.0"
}
Assert ((Test-Path $basedir\.git) -eq $false) ".git dir shouldn't be included ($contents)"
Assert ((Test-Path $basedir\normal) -eq $true) "normal file should be included ($contents)"
}
# Purpose: Publish a module with -Path
#
# Action: Publish-Module -Path <ModulePath> -NuGetApiKey <ApiKey>
#
# Expected Result: should be able to publish a module
#
It "PublishModuleWithRelativePath" {
$version = "1.0"
$moduleBase = $script:PublishModuleBase
if($PSVersionTable.PSVersion -gt '5.0.0')
{
$moduleBase = (Join-Path -Path $script:PublishModuleBase -ChildPath "$version")
$null = New-Item -ItemType Directory -Path $moduleBase -Force
}
New-ModuleManifest -Path (Join-Path -Path $moduleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
$currentLocation = Get-Location
try
{
Set-Location -Path $moduleBase
Publish-Module -Path .\ -NuGetApiKey $script:ApiKey -ReleaseNotes "$script:PublishModuleName release notes" -Tags PSGet -LicenseUri "https://$script:PublishModuleName.com/license" -ProjectUri "https://$script:PublishModuleName.com" -WarningAction SilentlyContinue
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
Assert ($psgetItemInfo.Name -eq $script:PublishModuleName) "Publish-Module should publish a module with valid module relative path, $($psgetItemInfo.Name)"
}
finally
{
$currentLocation | Set-Location
}
}
# Purpose: Publish a module with -Path
#
# Action: Publish-Module -Path <ModulePath> -NuGetApiKey <ApiKey>
#
# Expected Result: should be able to publish a module
#
It "PublishModulePathWithoutVersion" {
$version = "2.0"
$Name = "TestModule_$(Get-Random)"
# Create temp module to be published
$moduleBase = Join-Path -Path $script:TempModulesPath -ChildPath $Name
$moduleBaseWithoutVersion = $moduleBase
if($PSVersionTable.PSVersion -gt '5.0.0')
{
$moduleBase = Join-Path -Path $moduleBase -ChildPath $version
}
$null = New-Item -ItemType Directory -Path $moduleBase -Force
New-ModuleManifest -Path (Join-Path -Path $moduleBase -ChildPath "$Name.psd1") -ModuleVersion $version -Description "$Name module"
Publish-Module -Path $moduleBaseWithoutVersion -NuGetApiKey $script:ApiKey -WarningAction SilentlyContinue
$psgetItemInfo = Find-Module $Name -RequiredVersion $version
Assert ($psgetItemInfo.Name -eq $Name) "Publish-Module should publish a module path without version, $($psgetItemInfo.Name)"
}
# Purpose: Publish a module with -Path
#
# Action: Publish-Module -Path <ModulePath> -NuGetApiKey <ApiKey>
#
# Expected Result: should fail with AmbiguousModulePathToPublish error id
#
It "PublishModuleWithAmbiguousPathWithoutVersion" {
$version1 = "1.0"
$version2 = "2.0"
$moduleBase = $script:PublishModuleBase
$moduleBaseWithoutVersion = $script:PublishModuleBase
$moduleBase = Join-Path -Path $script:PublishModuleBase -ChildPath $version1
$null = New-Item -ItemType Directory -Path $moduleBase -Force
New-ModuleManifest -Path (Join-Path -Path $moduleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version1 -Description "$script:PublishModuleName module"
$moduleBase = Join-Path -Path $script:PublishModuleBase -ChildPath $version2
$null = New-Item -ItemType Directory -Path $moduleBase -Force
New-ModuleManifest -Path (Join-Path -Path $moduleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version2 -Description "$script:PublishModuleName module"
AssertFullyQualifiedErrorIdEquals -scriptblock {Publish-Module -Path $moduleBaseWithoutVersion -WarningAction SilentlyContinue}`
-expectedFullyQualifiedErrorId 'AmbiguousModulePathToPublish,Publish-Module'
} `
-Skip:$(-not (Test-ModuleSxSVersionSupport))
# Purpose: Publish a module with -Path and Module is created with SxS multi version support
#
# Action: Publish-Module -Name ContosoPublishModule -NuGetApiKey <ApiKey>
#
# Expected Result: should be able to publish a module
#
It PublishModuleWithPathForSxSVersion {
$version = "2.0.0"
$moduleBaseWithVersion = (Join-Path -Path $script:PublishModuleBase -ChildPath "$version")
$null = New-Item -Path $moduleBaseWithVersion -ItemType Directory -Force
Set-Content (Join-Path -Path $moduleBaseWithVersion -ChildPath "$script:PublishModuleName.psm1") -Value "function Get-$script:PublishModuleName { Get-Date }"
New-ModuleManifest -Path (Join-Path -Path $moduleBaseWithVersion -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
Publish-Module -Path $moduleBaseWithVersion `
-NuGetApiKey $script:ApiKey `
-ReleaseNotes "$script:PublishModuleName release notes" `
-Tags 'PSGet' `
-LicenseUri "https://$script:PublishModuleName.com/license" `
-ProjectUri "https://$script:PublishModuleName.com" `
-WarningAction SilentlyContinue
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
Assert (($psgetItemInfo.Name -eq $script:PublishModuleName) -and ($psgetItemInfo.Version.ToString() -eq $version)) "Publish-Module should publish a module with valid module name, $($psgetItemInfo.Name)"
} `
-Skip:$(-not (Test-ModuleSxSVersionSupport))
# Purpose: PublishModuleWithConfirmAndNoToPrompt
#
# Action: Publish-Module -Name ContosoPublishModule -NuGetApiKey apikey -Confirm
#
# Expected Result: module should not be published after confirming NO
#
It "PublishModuleWithConfirmAndNoToPrompt" {
$outputPath = $script:TempPath
$guid = [system.guid]::newguid().tostring()
$outputFilePath = Join-Path $outputPath "$guid"
$runspace = CreateRunSpace $outputFilePath 1
# 2 is mapped to No in ShouldProcess prompt
$Global:proxy.UI.ChoiceToMake=2
$content = $null
$version = "1.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
#Copy module to $script:ProgramFilesModulesPath
Copy-Item $script:PublishModuleBase $script:ProgramFilesModulesPath -Recurse -Force
try
{
$result = ExecuteCommand $runspace "Publish-Module -Name $script:PublishModuleName -NuGetApiKey $script:ApiKey -Confirm"
}
finally
{
$fileName = "PromptForChoice-0.txt"
$path = join-path $outputFilePath $fileName
if(Test-Path $path)
{
$content = get-content $path
}
CloseRunSpace $runspace
RemoveItem $outputFilePath
}
$shouldProcessMessage = $script:LocalizedData.PublishModulewhatIfMessage -f ($version, $script:PublishModuleName)
Assert ($content -and ($content -match $shouldProcessMessage)) "publish module confirm prompt is not working, $content"
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Module $script:PublishModuleName -RequiredVersion $version}`
-expectedFullyQualifiedErrorId "NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.FindPackage"
} `
-Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US'))
# Purpose: PublishModuleWithConfirmAndYesToPrompt
#
# Action: Publish-Module -Name ContosoPublishModule -NuGetApiKey apikey -Confirm
#
# Expected Result: module should be published after confirming YES
#
It "PublishModuleWithConfirmAndYesToPrompt" {
$outputPath = $script:TempPath
$guid = [system.guid]::newguid().tostring()
$outputFilePath = Join-Path $outputPath "$guid"
$runspace = CreateRunSpace $outputFilePath 1
# 0 is mapped to YES in ShouldProcess prompt
$Global:proxy.UI.ChoiceToMake=0
$content = $null
$version = "2.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
# Copy module to $script:ProgramFilesModulesPath
Copy-Item $script:PublishModuleBase $script:ProgramFilesModulesPath -Recurse -Force
try
{
$result = ExecuteCommand $runspace 'Import-Module PowerShellGet -Global -Force; $PSGallerySourceUri="$script:TempPath\PSGalleryRepo"; $PSGalleryPublishUri="$script:TempPath\PSGalleryRepo"; Publish-Module -Name ContosoPublishModule -NuGetApiKey TestPSGalleryApiKey -Confirm'
}
finally
{
$fileName = "PromptForChoice-0.txt"
$path = join-path $outputFilePath $fileName
if(Test-Path $path)
{
$content = get-content $path
}
CloseRunSpace $runspace
RemoveItem $outputFilePath
}
$shouldProcessMessage = $script:LocalizedData.PublishModulewhatIfMessage -f ($version, $script:PublishModuleName)
Assert ($content -and ($content -match $shouldProcessMessage)) "publish module confirm prompt is not working, $content"
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
Assert (($psgetItemInfo.Name -eq $script:PublishModuleName) -or (($psgetItemInfo.Version.ToString() -eq $version))) "Publish-Module should publish a module with valid module name after confirming YES, $($psgetItemInfo.Name)"
} `
-Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US'))
# Purpose: PublishModuleWithWhatIf
#
# Action: Publish-Module -Name ContosoPublishModule -NuGetApiKey apikey -WhatIf
#
# Expected Result: module should not be published with -WhatIf
#
It "PublishModuleWithWhatIf" {
$version = "3.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
#Copy module to $script:ProgramFilesModulesPath
Copy-Item $script:PublishModuleBase $script:ProgramFilesModulesPath -Recurse -Force
$outputPath = $script:TempPath
$guid = [system.guid]::newguid().tostring()
$outputFilePath = Join-Path $outputPath "$guid"
$runspace = CreateRunSpace $outputFilePath 1
$content = $null
try
{
$result = ExecuteCommand $runspace 'Import-Module PowerShellGet -Global -Force; $PSGallerySourceUri="$script:TempPath\PSGalleryRepo"; $PSGalleryPublishUri="$script:TempPath\PSGalleryRepo"; Publish-Module -Name ContosoPublishModule -NuGetApiKey TestPSGalleryApiKey -WhatIf'
}
finally
{
$fileName = "WriteLine-0.txt"
$path = join-path $outputFilePath $fileName
if(Test-Path $path)
{
$content = get-content $path
}
CloseRunSpace $runspace
RemoveItem $outputFilePath
}
$shouldProcessMessage = $script:LocalizedData.PublishModulewhatIfMessage -f ($version, $script:PublishModuleName)
Assert ($content -and ($content -match $shouldProcessMessage)) "publish module whatif message is missing, $content"
AssertFullyQualifiedErrorIdEquals -scriptblock {Find-Module $script:PublishModuleName -RequiredVersion $version}`
-expectedFullyQualifiedErrorId "NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.FindPackage"
} `
-Skip:$(($PSEdition -eq 'Core') -or ([System.Environment]::OSVersion.Version -lt "6.2.9200.0") -or ($PSCulture -ne 'en-US'))
# Purpose: Publish multiple versions of a module
#
# Action: Publish-Module -Name modulename
#
# Expected Result: should be able to publish multiple versions
#
It "PublishModuleMultipleVersions" {
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
#Copy module to $script:ProgramFilesModulesPath
Copy-Item $script:PublishModuleBase $script:ProgramFilesModulesPath -Recurse -Force
Publish-Module -Name $script:PublishModuleName -NuGetApiKey $script:ApiKey -ReleaseNotes "$script:PublishModuleName release notes" -Tags PSGet -LicenseUri "https://$script:PublishModuleName.com/license" -ProjectUri "https://$script:PublishModuleName.com" -WarningAction SilentlyContinue
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
Assert (($psgetItemInfo.Name -eq $script:PublishModuleName) -and ($psgetItemInfo.Version.ToString() -eq $version)) "Publish-Module should publish a module with valid module name, $($psgetItemInfo.Name)"
$version = "2.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
#Copy module to $script:ProgramFilesModulesPath
Copy-Item $script:PublishModuleBase $script:ProgramFilesModulesPath -Recurse -Force
Publish-Module -Name $script:PublishModuleName -NuGetApiKey $script:ApiKey -ReleaseNotes "$script:PublishModuleName release notes" -Tags PSGet -LicenseUri "https://$script:PublishModuleName.com/license" -ProjectUri "https://$script:PublishModuleName.com" -WarningAction SilentlyContinue
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
Assert (($psgetItemInfo.Name -eq $script:PublishModuleName) -and ($psgetItemInfo.Version.ToString() -eq $version)) "Publish-Module should publish a module with valid module name, $($psgetItemInfo.Name)"
}
# Purpose: Publish a module with non-existing nested module
#
# Action: Publish-Module -Path <ModulePath>
#
# Expected Result: should fail with an error id
#
It "PublishModuleWithNonExistingNestedModule" {
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion '1.0' -Description "$script:PublishModuleName module" -NestedModules "NonExistingNestedModule"
AssertFullyQualifiedErrorIdEquals -scriptblock {Publish-Module -Path $script:PublishModuleBase -WarningAction SilentlyContinue}`
-expectedFullyQualifiedErrorId 'UnableToResolveModuleDependency,Publish-PSArtifactUtility'
}
# Purpose: Publish a module with invalid key in PSD1 file
#
# Action: Publish-Module -Path <ModulePath>
#
# Expected Result: should fail with an error id
#
It "PublishModuleWithInvalidEntryInPSD1" {
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion '1.0' -Description "$script:PublishModuleName module" -NestedModules "NonExistingNestedModule"
Set-Content -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -Value @'
@{
ModuleVersion = 1.0
Guid = '680e031b-f318-4534-bdc9-10f1787b2400'
Copyright = '(c) 2015 manikb. All rights reserved.'
Description = 'Test module description'
Author = 'Contoso Author (author@contoso.com)'
FunctionsToExport = '*'
CmdletsToExport = '*'
VariablesToExport = '*'
AliasesToExport = '*'
FileList2 = @()
}
'@
AssertFullyQualifiedErrorIdEquals -scriptblock {Publish-Module -Path $script:PublishModuleBase -WarningAction SilentlyContinue}`
-expectedFullyQualifiedErrorId 'Modules_InvalidManifestMember,Microsoft.PowerShell.Commands.TestModuleManifestCommand'
}
It "PublishModuleWithPSEditionVariableInPSD1" {
$Psd1FilePath = (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1")
New-ModuleManifest -Path $Psd1FilePath
Set-Content -Path $Psd1FilePath -Force -Value @'
@{
ModuleVersion = '1.0'
Guid = '680e031b-f318-4534-bdc9-10f1787b2400'
Copyright = '(c) 2015 manikb. All rights reserved.'
Description = 'Test module description'
Author = 'Contoso Author (author@contoso.com)'
FunctionsToExport = if($PSEdition -eq 'Desktop'){@('A','B')}elseif($PSEdition -eq 'Core'){@('C','D')};
CmdletsToExport = '*'
VariablesToExport = '*'
AliasesToExport = '*'
}
'@
if($PSVersionTable.PSVersion -ge '5.1.0')
{
Publish-Module -Path $script:PublishModuleBase -WarningAction SilentlyContinue
$res = Find-Module -Name $script:PublishModuleName
AssertEquals $res.Name $script:PublishModuleName "Module should be published with PSEdition variable"
}
else
{
AssertFullyQualifiedErrorIdEquals -scriptblock {Publish-Module -Path $script:PublishModuleBase -WarningAction SilentlyContinue}`
-expectedFullyQualifiedErrorId 'Modules_InvalidManifest,Microsoft.PowerShell.Commands.TestModuleManifestCommand'
}
}
It "PublishModuleWithPSScriptRootVariableInPSD1" {
$Psd1FilePath = (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1")
New-ModuleManifest -Path $Psd1FilePath
Set-Content -Path $Psd1FilePath -Force -Value @'
@{
ModuleVersion = '1.0'
Guid = '680e031b-f318-4534-bdc9-10f1787b2400'
Copyright = '(c) 2015 manikb. All rights reserved.'
Description = "Test module description; $PSScriptRoot\ReleaseNotes.md has full description."
Author = 'Contoso Author (author@contoso.com)'
FunctionsToExport = '*'
CmdletsToExport = '*'
VariablesToExport = '*'
AliasesToExport = '*'
}
'@
Publish-Module -Path $script:PublishModuleBase -WarningAction SilentlyContinue
$res = Find-Module -Name $script:PublishModuleName
AssertEquals $res.Name $script:PublishModuleName "Module should be published with PSScriptRoot variable"
}
It "PublishModuleWithInvalidVariableInPSD1" {
$Psd1FilePath = (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1")
New-ModuleManifest -Path $Psd1FilePath
Set-Content -Path $Psd1FilePath -Force -Value @'
@{
ModuleVersion = '1.0'
Guid = '680e031b-f318-4534-bdc9-10f1787b2400'
Copyright = '(c) 2015 manikb. All rights reserved.'
Description = 'Test module description'
Author = 'Contoso Author (author@contoso.com)'
FunctionsToExport = '*'
CmdletsToExport = '*'
VariablesToExport = '*'
AliasesToExport = '*'
FileList = "$InvalidVariable\TestFile.psm1"
}
'@
AssertFullyQualifiedErrorIdEquals -scriptblock {Publish-Module -Path $script:PublishModuleBase -WarningAction SilentlyContinue}`
-expectedFullyQualifiedErrorId 'Modules_InvalidManifest,Microsoft.PowerShell.Commands.TestModuleManifestCommand'
}
It "GetManifestHashTableWithInvalidVariableInPSD1" {
$Psd1FilePath = (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1")
New-ModuleManifest -Path $Psd1FilePath
Set-Content -Path $Psd1FilePath -Force -Value @'
@{
ModuleVersion = '1.0'
Guid = '680e031b-f318-4534-bdc9-10f1787b2400'
Copyright = '(c) 2015 manikb. All rights reserved.'
Description = 'Test module description'
Author = 'Contoso Author (author@contoso.com)'
FunctionsToExport = '*'
CmdletsToExport = '*'
VariablesToExport = '*'
AliasesToExport = '*'
FileList = "$InvalidVariable\TestFile.psm1"
}
'@
function Get-ManifestHashTableCaller
{
[CmdletBinding()]
param($Psd1FilePath)
& $script:psgetModuleInfo Get-ManifestHashTable -Path $Psd1FilePath -CallerPSCmdlet $PSCmdlet
}
AssertFullyQualifiedErrorIdEquals -scriptblock { Get-ManifestHashTableCaller -Psd1FilePath $Psd1FilePath }`
-expectedFullyQualifiedErrorId 'ParseException,Get-ManifestHashTableCaller'
}
# Purpose: Test xml special characters are escaped when publishing a module
#
# Action: Create a module, try to upload it with XML special characters in ReleaseNotes, Tag, LicenseUri, IconUri, ProjectUri, Description
#
# Expected Result: Publish operation should succeed and Find-Module should get the details with same special characters
#
It PublishModuleWithXMLSpecialCharacters {
$ModuleName = "ModuleWithSpecialChars"
$ModuleBase = Join-Path $script:TempModulesPath $ModuleName
$null = New-Item -Path $ModuleBase -ItemType Directory -Force
$version = "1.0.0"
$Description = "$ModuleName module <TestElement> $&*!()[]{}@#"
$ReleaseNotes = @("$ModuleName release notes", " <TestElement> $&*!()[]{}@#")
$Tags = "PSGet","Special$&*!()[]{}@#<TestElement>"
$ProjectUri = "https://$ModuleName.com/Project"
$IconUri = "https://$ModuleName.com/Icon"
$LicenseUri = "https://$ModuleName.com/license"
$Author = "Author#@<TestElement>$&*!()[]{}@#"
$CompanyName = "CompanyName <TestElement>$&*!()[]{}@#"
$CopyRight = "CopyRight <TestElement>$&*!()[]{}@#"
New-ModuleManifest -Path (Join-Path -Path $ModuleBase -ChildPath "$ModuleName.psd1") -ModuleVersion $version -Description $Description -Author $Author -CompanyName $CompanyName -Copyright $CopyRight
Publish-Module -Path $ModuleBase -NuGetApiKey $script:ApiKey -ReleaseNotes $ReleaseNotes -Tags $Tags -LicenseUri $LicenseUri -ProjectUri $ProjectUri -WarningAction SilentlyContinue
RemoveItem -path $ModuleBase
$psgetItemInfo = Find-Module $ModuleName -RequiredVersion $version
AssertEqualsCaseInsensitive $psgetItemInfo.Name $ModuleName "ModuleName should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.version $version "version should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.Description $Description "Description should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.ReleaseNotes "$($ReleaseNotes -join "`n")" "ReleaseNotes should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.ProjectUri $ProjectUri "ProjectUri should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.Author $Author "Author should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.CompanyName $CompanyName "CompanyName should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.CopyRight $CopyRight "CopyRight should be same as the published one"
Assert ($psgetItemInfo.Tags -contains $($Tags[0])) "Tags ($($psgetItemInfo.Tags)) should contain the published one ($($Tags[0]))"
Assert ($psgetItemInfo.Tags -contains $($Tags[1])) "Tags ($($psgetItemInfo.Tags)) should contain the published one ($($Tags[1]))"
AssertEqualsCaseInsensitive $psgetItemInfo.LicenseUri $LicenseUri "LicenseUri should be same as the published one"
}
# Purpose: Test Publish-Module cmdlet gets the PSData properties from the module manifest file
#
# Action: Create a module manifest with PrivateData\PSData hashtable, try to publish it
#
# Expected Result: Publish operation should succeed and Find-Module should get the details as provided in PSData.
#
It PublishModulePSDataInManifestFile {
$version = "1.0.0"
$Description = "$script:PublishModuleName module"
$ReleaseNotes = "$script:PublishModuleName release notes"
$Tags = "PSGet","DSC"
$ProjectUri = "https://$script:PublishModuleName.com/Project"
$IconUri = "https://$script:PublishModuleName.com/Icon"
$LicenseUri = "https://$script:PublishModuleName.com/license"
$Author = "AuthorName"
$CompanyName = "CompanyName"
$CopyRight = "CopyRight"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") `
-ModuleVersion $version `
-Description "$script:PublishModuleName module" `
-NestedModules "$script:PublishModuleName.psm1" `
-Author $Author `
-CompanyName $CompanyName `
-Copyright $CopyRight `
-Tags $Tags `
-IconUri $IconUri `
-ProjectUri $ProjectUri `
-LicenseUri $LicenseUri `
-ReleaseNotes $ReleaseNotes `
-WarningAction SilentlyContinue
Publish-Module -Path $script:PublishModuleBase -NuGetApiKey $script:ApiKey -WarningAction SilentlyContinue
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
AssertEqualsCaseInsensitive $psgetItemInfo.Name $script:PublishModuleName "ModuleName should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.version $version "version should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.Description $Description "Description should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.ReleaseNotes $ReleaseNotes "ReleaseNotes should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.ProjectUri $ProjectUri "ProjectUri should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.Author $Author "Author should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.CompanyName $CompanyName "CompanyName should be same as the published one"
AssertEqualsCaseInsensitive $psgetItemInfo.CopyRight $CopyRight "CopyRight should be same as the published one"
Assert ($psgetItemInfo.Tags -contains $Tags[0]) "Tags ($($psgetItemInfo.Tags)) should contain the published one ($($Tags[0]))"
Assert ($psgetItemInfo.Tags -contains $Tags[1]) "Tags ($($psgetItemInfo.Tags)) should contain the published one ($($Tags[1]))"
AssertEqualsCaseInsensitive $psgetItemInfo.LicenseUri $LicenseUri "LicenseUri should be same as the published one"
} `
-Skip:$($PSVersionTable.PSVersion -lt '5.0.0')
# Purpose: Test Publish-Module cmdlet warns when tag length is greater than 4000
#
# Action: Create a module manifest with PrivateData\PSData hashtable, try to publish
#
# Expected Result: Publish operation should succeed and should have warned about tag length.
#
It PublishModuleFunctionsAsTagsWarnWithoutSkipAutomaticTags {
$version = "1.0.0"
$Description = "$script:PublishModuleName module"
$Tags = "PSGet","DSC"
$Author = "AuthorName"
$CompanyName = "CompanyName"
$CopyRight = "CopyRight"
$functionNames = 1..250 | Foreach-Object { "Get-TestFunction$($_)" }
$tempFunctions = 1..250 | Foreach-Object { "function Get-TestFunction$($_) { Get-Date }" + [Environment]::NewLine }
Set-Content (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psm1") -Value $tempFunctions
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") `
-ModuleVersion $version `
-Description "$script:PublishModuleName module" `
-NestedModules "$script:PublishModuleName.psm1" `
-Author $Author `
-CompanyName $CompanyName `
-Copyright $CopyRight `
-Tags $Tags `
-FunctionsToExport $functionNames
Publish-Module -Path $script:PublishModuleBase `
-NuGetApiKey $script:ApiKey `
-Repository "PSGallery" `
-WarningAction SilentlyContinue `
-WarningVariable wa
Assert ("$wa".Contains('4000 characters')) "Warning messages should include 'Tag list exceeded 4000 characters and may not be accepted by some Nuget feeds.'"
} `
-Skip:$($PSVersionTable.PSVersion -lt '5.0.0')
# Purpose: Test Publish-Module cmdlet excludes functions from tags when using SkipAutomaticTags parameter
#
# Action: Create a module manifest with PrivateData\PSData hashtable, try to publish with SkipAutomaticTags parameter
#
# Expected Result: Publish operation should succeed and should not have warned about tag length
#
It PublishModuleFunctionsAsTagsWithSkipAutomaticTags {
$version = "1.0.0"
$Description = "$script:PublishModuleName module"
$Tags = "PSGet","DSC"
$Author = "AuthorName"
$CompanyName = "CompanyName"
$CopyRight = "CopyRight"
$functionNames = 1..250 | Foreach-Object { "Get-TestFunction$($_)" }
$tempFunctions = 1..250 | Foreach-Object { "function Get-TestFunction$($_) { Get-Date }" + [Environment]::NewLine }
Set-Content (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psm1") -Value $tempFunctions
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") `
-ModuleVersion $version `
-Description "$script:PublishModuleName module" `
-NestedModules "$script:PublishModuleName.psm1" `
-Author $Author `
-CompanyName $CompanyName `
-Copyright $CopyRight `
-Tags $Tags `
-FunctionsToExport $functionNames
Publish-Module -Path $script:PublishModuleBase `
-NuGetApiKey $script:ApiKey `
-Repository "PSGallery" `
-SkipAutomaticTags `
-WarningAction SilentlyContinue `
-WarningVariable wa
Assert (-not "$wa".Contains('4000 characters')) "Warning messages should not include 'Tag list exceeded 4000 characters and may not be accepted by some Nuget feeds.'"
} `
-Skip:$($PSVersionTable.PSVersion -lt '5.0.0')
# Purpose: Test Publish-Module cmdlet gets the PSData properties from the module manifest file and also with Uri objects specified to the cmdlet
#
# Action: Create a module manifest with PrivateData\PSData hashtable, try to publish it with Uri objects to ProjectUri, IconUri and LicenseUri parameters
#
# Expected Result: Publish operation should succeed and Find-Module should get the details as provided in PSData and *Uri parameters.
#
It PublishModuleWithUriObjectsAndPSDataInManifestFile {
$version = "1.0.0"
$Description = "$script:PublishModuleName module"
$ReleaseNotes = "$script:PublishModuleName release notes"
$Tags = "PSGet","DSC"
$ProjectUri = New-Object System.Uri "https://$script:PublishModuleName.com/Project"
$IconUri = New-Object System.Uri "https://$script:PublishModuleName.com/Icon"
$LicenseUri = New-Object System.Uri "https://$script:PublishModuleName.com/license"
$Author = "AuthorName"
$CompanyName = "CompanyName"
$CopyRight = "CopyRight"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") `
-ModuleVersion $version `
-Description "$script:PublishModuleName module" `
-NestedModules "$script:PublishModuleName.psm1" `
-Author $Author `
-CompanyName $CompanyName `
-Copyright $CopyRight `
-Tags $Tags `
-ReleaseNotes $ReleaseNotes
Publish-Module -Path $script:PublishModuleBase -NuGetApiKey $script:ApiKey `
-Repository "PSGallery" `
-ProjectUri $ProjectUri -LicenseUri $LicenseUri -IconUri $IconUri `
-WarningAction SilentlyContinue
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
AssertEquals $psgetItemInfo.Name $script:PublishModuleName "ModuleName should be same as the published one"
AssertEquals $psgetItemInfo.version $version "version should be same as the published one"
AssertEquals $psgetItemInfo.Description $Description "Description should be same as the published one"
AssertEquals $psgetItemInfo.ReleaseNotes $ReleaseNotes "ReleaseNotes should be same as the published one"
AssertEquals $psgetItemInfo.ProjectUri $ProjectUri "ProjectUri should be same as the published one"
AssertEquals $psgetItemInfo.Author $Author "Author should be same as the published one"
AssertEquals $psgetItemInfo.CompanyName $CompanyName "CompanyName should be same as the published one"
AssertEquals $psgetItemInfo.CopyRight $CopyRight "CopyRight should be same as the published one"
Assert ($psgetItemInfo.Tags -contains $Tags[0]) "Tags ($($psgetItemInfo.Tags)) should contain the published one ($($Tags[0]))"
Assert ($psgetItemInfo.Tags -contains $Tags[1]) "Tags ($($psgetItemInfo.Tags)) should contain the published one ($($Tags[1]))"
AssertEquals $psgetItemInfo.LicenseUri $LicenseUri "LicenseUri should be same as the published one"
Assert ($psgetItemInfo.PublishedDate -and ($psgetItemInfo.PublishedDate.GetType().Name -eq 'DateTime')) "PublishedDate is missing, $($psgetItemInfo.PublishedDate)"
} `
-Skip:$($PSVersionTable.PSVersion -lt '5.0.0')
# Purpose: Validate *-Module cmdlets without PowerShellGetFormatVersion and old package format
#
# Action:
# Create and Publish a module without PowerShellGetFormatVersion
# Run Find-Module, Install-Module and Update-Module for that module name
#
# Expected Result: *-Module cmdlets should support the older nuget package format
#
It ValidateWithoutPSGetFormatVersion {
$moduleName = "TestMod_$(Get-Random)"
CreateAndPublishTestModuleWithVersionFormat -ModuleName $moduleName `
-NuGetApiKey $script:ApiKey `
-Repository "PSGallery" `
-Versions @('1.0.0','2.0.0') `
-PSGetFormatVersion '0.0' `
-ModulesPath $script:TempModulesPath
try
{
$psgetItemInfo = Find-Module -Name $moduleName -RequiredVersion '1.0.0'
AssertNotNull $psgetItemInfo "Module without PowerShellGetFormatVersion is not found"
AssertNull $psgetItemInfo.PowerShellGetFormatVersion "PowerShellGetFormatVersion property is not null, $($psgetItemInfo.PowerShellGetFormatVersion)"
Install-Module -Name $moduleName -RequiredVersion '1.0.0'
$moduleInfo = Get-Module -ListAvailable -Name $moduleName
AssertEquals $moduleInfo.Name $moduleName "$moduleName Module without PowerShellGetFormatVersion is not installed"
AssertEquals $moduleInfo.Version "1.0.0" "Invalid PowerShellGetFormatVersion value on PSGetModuleInfo, $($moduleInfo.Version)"
Update-Module -Name $moduleName -RequiredVersion '2.0.0'
if(Test-ModuleSxSVersionSupport)
{
$moduleInfo = Get-Module -FullyQualifiedName @{ModuleName=$moduleName;RequiredVersion='2.0.0'} -ListAvailable
}
else
{
$moduleInfo = Get-Module $moduleName -ListAvailable
}
AssertEquals $moduleInfo.Name $moduleName "$moduleName Module without PowerShellGetFormatVersion is not updated properly"
AssertEquals $moduleInfo.Version "2.0.0" "Invalid PowerShellGetFormatVersion value on PSGetModuleInfo, $($moduleInfo.Version)"
}
finally
{
PSGetTestUtils\Uninstall-Module -Name $moduleName
#Get-InstalledModule -Name $moduleName -ErrorAction SilentlyContinue | Uninstall-Module -Force
}
}
# Purpose: Validate *-Module cmdlets with compatible PowerShellGetFormatVersion
#
# Action:
# Create and Publish a module with PowerShellGetFormatVersion as 1.7
# Run Find-Module, Install-Module and Update-Module for that module name
#
# Expected Result: *-Module cmdlets should work with campatible PowerShellGetFormatVersion
#
It ValidateCompatiblePSGetFormatVersion {
$PSGetFormatVersion = [Version]'1.7'
$moduleName = "TestMod_$(Get-Random)"
CreateAndPublishTestModuleWithVersionFormat -ModuleName $moduleName `
-NuGetApiKey $script:ApiKey `
-Repository "PSGallery" `
-Versions @('1.0.0','2.0.0') `
-PSGetFormatVersion $PSGetFormatVersion `
-ModulesPath $script:TempModulesPath
try
{
$psgetItemInfo = Find-Module -Name $moduleName -RequiredVersion '1.0.0'
AssertNotNull $psgetItemInfo "Module without PowerShellGetFormatVersion is not found"
AssertEquals $psgetItemInfo.PowerShellGetFormatVersion $PSGetFormatVersion "PowerShellGetFormatVersion property is not null, $($psgetItemInfo.PowerShellGetFormatVersion)"
Install-Module -Name $moduleName -RequiredVersion '1.0.0'
$moduleInfo = Get-Module -ListAvailable -Name $moduleName
AssertEquals $moduleInfo.Name $moduleName "$moduleName Module without PowerShellGetFormatVersion is not installed"
AssertEquals $moduleInfo.Version "1.0.0" "Invalid PowerShellGetFormatVersion value on PSGetModuleInfo, $($moduleInfo.Version)"
Update-Module -Name $moduleName -RequiredVersion '2.0.0'
if(Test-ModuleSxSVersionSupport)
{
$moduleInfo = Get-Module -FullyQualifiedName @{ModuleName=$moduleName;RequiredVersion='2.0.0'} -ListAvailable
}
else
{
$moduleInfo = Get-Module $moduleName -ListAvailable
}
AssertEquals $moduleInfo.Name $moduleName "$moduleName Module without PowerShellGetFormatVersion is not updated properly"
AssertEquals $moduleInfo.Version "2.0.0" "Invalid PowerShellGetFormatVersion value on PSGetModuleInfo, $($moduleInfo.Version)"
}
finally
{
PSGetTestUtils\Uninstall-Module -Name $moduleName
}
}
# Purpose: Validate *-Module cmdlets with incompatible format version
#
# Action:
# Create and Publish a module with PowerShellGetFormatVersion as 2.1
# Run Find-Module, Install-Module and Update-Module for that module name
#
# Expected Result: Find-Module should work and Install-Module should fail