-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathPublishPSResource.Tests.ps1
More file actions
884 lines (713 loc) · 48.5 KB
/
PublishPSResource.Tests.ps1
File metadata and controls
884 lines (713 loc) · 48.5 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
Import-Module $modPath -Force -Verbose
$script:testDir = (get-item $psscriptroot).parent.FullName
function script:CreateTestModule
{
param (
[string] $Path = "$TestDrive",
[string] $ModuleName = 'TestModule'
)
$modulePath = Join-Path -Path $Path -ChildPath $ModuleName
$moduleMan = Join-Path $modulePath -ChildPath ($ModuleName + '.psd1')
$moduleSrc = Join-Path $modulePath -ChildPath ($ModuleName + '.psm1')
if ( Test-Path -Path $modulePath) {
Remove-Item -Path $modulePath -Recurse -Force
}
$null = New-Item -Path $modulePath -ItemType Directory -Force
@'
@{{
RootModule = "{0}.psm1"
ModuleVersion = '1.0.0'
Author = 'None'
Description = 'None'
GUID = '0c2829fc-b165-4d72-9038-ae3a71a755c1'
FunctionsToExport = @('Test1')
RequiredModules = @('NonExistentModule')
}}
'@ -f $ModuleName | Out-File -FilePath $moduleMan
@'
function Test1 {
Write-Output 'Hello from Test1'
}
'@ | Out-File -FilePath $moduleSrc
}
Describe "Test Publish-PSResource" -tags 'CI' {
BeforeAll {
Get-NewPSResourceRepositoryFile
# Register temporary repositories
$tmpRepoPath = Join-Path -Path $TestDrive -ChildPath "tmpRepoPath"
New-Item $tmpRepoPath -Itemtype directory -Force
$testRepository = "testRepository"
Register-PSResourceRepository -Name $testRepository -Uri $tmpRepoPath -Priority 1 -ErrorAction SilentlyContinue
$script:repositoryPath = [IO.Path]::GetFullPath((get-psresourcerepository "testRepository").Uri.AbsolutePath)
$tmpRepoPath2 = Join-Path -Path $TestDrive -ChildPath "tmpRepoPath2"
New-Item $tmpRepoPath2 -Itemtype directory -Force
$testRepository2 = "testRepository2"
Register-PSResourceRepository -Name $testRepository2 -Uri $tmpRepoPath2
$script:repositoryPath2 = [IO.Path]::GetFullPath((get-psresourcerepository "testRepository2").Uri.AbsolutePath)
# Create module
$script:tmpModulesPath = Join-Path -Path $TestDrive -ChildPath "tmpModulesPath"
$script:PublishModuleName = "PSGetTestModule"
$script:PublishModuleBase = Join-Path $script:tmpModulesPath -ChildPath $script:PublishModuleName
if(!(Test-Path $script:PublishModuleBase))
{
New-Item -Path $script:PublishModuleBase -ItemType Directory -Force
}
$script:PublishModuleBaseUNC = $script:PublishModuleBase -Replace '^(.):', '\\localhost\$1$'
#Create dependency module
$script:DependencyModuleName = "PackageManagement"
$script:DependencyModuleBase = Join-Path $script:tmpModulesPath -ChildPath $script:DependencyModuleName
if(!(Test-Path $script:DependencyModuleBase))
{
New-Item -Path $script:DependencyModuleBase -ItemType Directory -Force
}
# Create temp destination path
$script:destinationPath = [IO.Path]::GetFullPath((Join-Path -Path $TestDrive -ChildPath "tmpDestinationPath"))
New-Item $script:destinationPath -ItemType directory -Force
#Create folder where we shall place all script files to be published for these tests
$script:tmpScriptsFolderPath = Join-Path -Path $TestDrive -ChildPath "tmpScriptsPath"
if(!(Test-Path $script:tmpScriptsFolderPath))
{
New-Item -Path $script:tmpScriptsFolderPath -ItemType Directory -Force
}
# Path to folder, within our test folder, where we store invalid module and script files used for testing
$script:testFilesFolderPath = Join-Path $testDir -ChildPath "testFiles"
# Path to specifically to that invalid test modules folder
$script:testModulesFolderPath = Join-Path $script:testFilesFolderPath -ChildPath "testModules"
# Path to specifically to that invalid test scripts folder
$script:testScriptsFolderPath = Join-Path $script:testFilesFolderPath -ChildPath "testScripts"
# Create test module with missing required module
CreateTestModule -Path $TestDrive -ModuleName 'ModuleWithMissingRequiredModule'
$script:PSGalleryName = 'PSGallery'
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
AfterEach {
# Delete all contents of the repository without deleting the repository directory itself
$pkgsToDelete = Join-Path -Path "$script:repositoryPath" -ChildPath "*"
Remove-Item $pkgsToDelete -Recurse
$pkgsToDelete = Join-Path -Path "$script:repositoryPath2" -ChildPath "*"
Remove-Item $pkgsToDelete -Recurse
$pkgsToDelete = Join-Path -Path $script:PublishModuleBase -ChildPath "*"
Remove-Item $pkgsToDelete -Recurse -ErrorAction SilentlyContinue
}
It "Publish module with required module not installed on the local machine using -SkipModuleManifestValidate" {
# Skip the module manifest validation test, which fails from the missing manifest required module.
$testModulePath = Join-Path -Path $TestDrive -ChildPath ModuleWithMissingRequiredModule
Publish-PSResource -Path $testModulePath -Repository $testRepository2 -Confirm:$false -SkipDependenciesCheck -SkipModuleManifestValidate
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath 'ModuleWithMissingRequiredModule.1.0.0.nupkg'
$publishedModuleFound = Test-Path -Path $expectedPath
$publishedModuleFound | Should -BeTrue
if ($publishedModuleFound) {
Remove-Item $expectedPath -Force -ErrorAction SilentlyContinue
}
}
It "Publish a module with valid Author field without -SkipModuleManifestValidate" {
# This test verifies that the fix for runspace deserialization issue works correctly.
# Previously, PSModuleInfo.Author would return empty string when called via PowerShell.Create(),
# causing false positive "No author was provided" errors.
$version = "1.0.0"
$author = "TestAuthor"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -Author $author
# This should succeed without needing -SkipModuleManifestValidate
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "Publish a module with -Path to the highest priority repo" {
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $script:PublishModuleBase
$expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath
}
It "Publish a module using -Path positional parameter" {
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource $script:PublishModuleBase
$expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath
}
#region Local Source Path
It "Publish a module with -Path and -Repository" {
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "Publish a module with -Path pointing to a module directory (parent directory has same name)" {
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "Publish a module with -Path pointing to a module directory (parent directory has different name)" {
$version = "1.0.0"
$newModuleRoot = Join-Path -Path $script:PublishModuleBase -ChildPath "NewTestParentDirectory"
New-Item -Path $newModuleRoot -ItemType Directory
New-ModuleManifest -Path (Join-Path -Path $newModuleRoot -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $newModuleRoot -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "Publish a module with -Path pointing to a .psd1 (parent directory has same name)" {
$version = "1.0.0"
$manifestPath = Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1"
New-ModuleManifest -Path $manifestPath -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $manifestPath -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "Publish a module with -Path pointing to a .psd1 (parent directory has different name)" {
$version = "1.0.0"
$newModuleRoot = Join-Path -Path $script:PublishModuleBase -ChildPath "NewTestParentDirectory"
New-Item -Path $newModuleRoot -ItemType Directory
$manifestPath = Join-Path -Path $newModuleRoot -ChildPath "$script:PublishModuleName.psd1"
New-ModuleManifest -Path $manifestPath -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $manifestPath -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
#endregion Local Source Path
#region UNC Source Path
It "Publish a module with -Path and -Repository, with the path pointing to a network share" {
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBaseUNC -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $script:PublishModuleBaseUNC -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "Publish a module with -Path pointing to a module directory (parent directory has same name) on a network share" {
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBaseUNC -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $script:PublishModuleBaseUNC -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "Publish a module with -Path pointing to a module directory (parent directory has different name) on a network share" {
$version = "1.0.0"
$newModuleRoot = Join-Path -Path $script:PublishModuleBaseUNC -ChildPath "NewTestParentDirectory"
New-Item -Path $newModuleRoot -ItemType Directory
New-ModuleManifest -Path (Join-Path -Path $newModuleRoot -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $newModuleRoot -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "Publish a module with -Path pointing to a .psd1 (parent directory has same name) on a network share" {
$version = "1.0.0"
$manifestPath = Join-Path -Path $script:PublishModuleBaseUNC -ChildPath "$script:PublishModuleName.psd1"
New-ModuleManifest -Path $manifestPath -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $manifestPath -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "Publish a module with -Path pointing to a .psd1 (parent directory has different name) on a network share" {
$version = "1.0.0"
$newModuleRoot = Join-Path -Path $script:PublishModuleBaseUNC -ChildPath "NewTestParentDirectory"
New-Item -Path $newModuleRoot -ItemType Directory
$manifestPath = Join-Path -Path $newModuleRoot -ChildPath "$script:PublishModuleName.psd1"
New-ModuleManifest -Path $manifestPath -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $manifestPath -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
#endregion UNC Source Path
It "Publish a module with dependencies" {
# Create dependency module
$dependencyVersion = "2.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:DependencyModuleBase -ChildPath "$script:DependencyModuleName.psd1") -ModuleVersion $dependencyVersion -Description "$script:DependencyModuleName module"
Publish-PSResource -Path $script:DependencyModuleBase -Repository $testRepository2
$pkg1 = Find-PSResource $script:DependencyModuleName -Repository $testRepository2
$pkg1 | Should -Not -BeNullOrEmpty
$pkg1.Version | Should -Be $dependencyVersion
# Create module to test
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -RequiredModules @(@{ModuleName = 'PackageManagement'; ModuleVersion = '2.0.0' })
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2
$pkg2 = Find-PSResource $script:DependencyModuleName -Repository $testRepository2
$pkg2 | Should -Not -BeNullOrEmpty
$pkg2.Version | Should -Be $dependencyVersion
}
It "Publish a module with a dependency that is not published, should throw" {
$version = "1.0.0"
$dependencyVersion = "2.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -RequiredModules @(@{ModuleName = 'PackageManagement'; ModuleVersion = '1.4.4' })
{Publish-PSResource -Path $script:PublishModuleBase -ErrorAction Stop} | Should -Throw -ErrorId "DependencyNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
}
It "Publish a module with ExternalModuleDependencies that are not published" {
$version = "1.0.0"
$externalModuleName = "PackageManagement"
$manifestPath = Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1"
$moduleFilePath = Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psm1"
@'
function Test-PublishedFunction {
Write-Output "OK"
}
'@ | Out-File -FilePath $moduleFilePath
@"
@{
RootModule = '$script:PublishModuleName.psm1'
ModuleVersion = '$version'
Author = 'None'
Description = '$script:PublishModuleName module'
GUID = '6f703037-3e95-4dcf-b06c-916f2867cce7'
FunctionsToExport = @('Test-PublishedFunction')
RequiredModules = @('$externalModuleName')
PrivateData = @{
PSData = @{
ExternalModuleDependencies = @('$externalModuleName')
}
}
}
"@ | Out-File -FilePath $manifestPath
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
Add-Type -AssemblyName System.IO.Compression.FileSystem
$package = [System.IO.Compression.ZipFile]::OpenRead($expectedPath)
try {
$nuspecEntry = $package.Entries | Where-Object FullName -like '*.nuspec' | Select-Object -First 1
$nuspecEntry | Should -Not -BeNullOrEmpty
$reader = [System.IO.StreamReader]::new($nuspecEntry.Open())
try {
$nuspecXml = [xml]$reader.ReadToEnd()
}
finally {
$reader.Dispose()
}
}
finally {
$package.Dispose()
}
$namespaceManager = [System.Xml.XmlNamespaceManager]::new($nuspecXml.NameTable)
$namespaceManager.AddNamespace('ns', 'http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd')
$dependencyIds = @(
$nuspecXml.SelectNodes('//ns:package/ns:metadata/ns:dependencies/ns:dependency', $namespaceManager) |
ForEach-Object { $_.Attributes['id'].Value }
)
$dependencyIds | Should -Not -Contain $externalModuleName
}
It "Publish a module with -SkipDependenciesCheck" {
$version = "1.0.0"
$dependencyVersion = "2.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -RequiredModules @{ModuleName = "$script:DependencyModuleName"; ModuleVersion = "$dependencyVersion" }
Publish-PSResource -Path $script:PublishModuleBase -SkipDependenciesCheck
$expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath).FullName | select-object -Last 1 | Should -Be $expectedPath
}
It "Publish a module and preserve file structure" {
$version = "1.0.0"
$testFile = Join-Path -Path "TestSubDirectory" -ChildPath "TestSubDirFile.ps1"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
New-Item -Path (Join-Path -Path $script:PublishModuleBase -ChildPath $testFile) -Force
Publish-PSResource -Path $script:PublishModuleBase
# Must change .nupkg to .zip so that Expand-Archive can work on Windows PowerShell
$nupkgPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.nupkg"
$zipPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.zip"
Rename-Item -Path $nupkgPath -NewName $zipPath
$unzippedPath = Join-Path -Path $TestDrive -ChildPath "$script:PublishModuleName"
New-Item $unzippedPath -Itemtype directory -Force
Expand-Archive -Path $zipPath -DestinationPath $unzippedPath
Test-Path -Path (Join-Path -Path $unzippedPath -ChildPath $testFile) | Should -Be $True
}
It "Publish a module with -NupkgPath" {
$version = "1.0.0"
# Make a nupkg
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Compress-PSResource -Path $script:PublishModuleBase -DestinationPath $script:destinationPath
$expectedPath = Join-Path -Path $script:destinationPath -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:destinationPath).FullName | Should -Be $expectedPath
# Pass the nupkg via -NupkgPath
Publish-PSResource -NupkgPath $expectedPath -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
<# The following tests are related to passing in parameters to customize a nuspec.
# These parameters are not going in the current release, but is open for discussion to include in the future.
It "Publish a module with -Nuspec" {
$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"
# Create nuspec
$nuspec =
@'
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>PSGetTestModule</id>
<version>1.0.0</version>
<authors>americks</authors>
<owners>americks</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>test</description>
<releaseNotes></releaseNotes>
<copyright>(c) 2021 Contoso Corporation. All rights reserved.</copyright>
<tags>PSModule</tags>
</metadata>
</package>
'@
$nuspecPath = Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.nuspec"
New-Item $nuspecPath -ItemType File -Value $nuspec
Publish-PSResource -Path $script:PublishModuleBase -Nuspec $nuspecPath
$expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.nupkg"
Get-ChildItem $script:repositoryPath | Should -Be $expectedPath
}
It "Publish a module with -ReleaseNotes" {
$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"
$releaseNotes = "Test release notes."
Publish-PSResource -Path $script:PublishModuleBase -ReleaseNotes $releaseNotes
$expectedNupkgPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.nupkg"
Get-ChildItem $script:repositoryPath | Should -Be $expectedNupkgPath
$expectedExpandedPath = Join-Path -Path $script:repositoryPath -ChildPath "ExpandedPackage"
New-Item -Path $expectedExpandedPath -ItemType directory
Expand-Archive -Path $expectedNupkgPath -DestinationPath $expectedExpandedPath
$expectedNuspec = Join-path -Path $expectedExpandedPath -ChildPath "$script:PublishModuleName.nuspec"
$expectedNuspecContents = Get-Content -Path $expectedNuspec -Raw
$expectedNuspecContents.Contains($releaseNotes) | Should Be $true
}
It "Publish a module with -LicenseUrl" {
$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"
$licenseUrl = "https://www.fakelicenseurl.com"
Publish-PSResource -Path $script:PublishModuleBase -LicenseUrl $licenseUrl
$expectedNupkgPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.nupkg"
Get-ChildItem $script:repositoryPath | Should -Be $expectedNupkgPath
$expectedExpandedPath = Join-Path -Path $script:repositoryPath -ChildPath "ExpandedPackage"
New-Item -Path $expectedExpandedPath -ItemType directory
Expand-Archive -Path $expectedNupkgPath -DestinationPath $expectedExpandedPath
$expectedNuspec = Join-path -Path $expectedExpandedPath -ChildPath "$script:PublishModuleName.nuspec"
$expectedNuspecContents = Get-Content -Path $expectedNuspec -Raw
$expectedNuspecContents.Contains($licenseUrl) | Should Be $true
}
It "Publish a module with -IconUrl" {
$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"
$iconUrl = "https://www.fakeiconurl.com"
Publish-PSResource -Path $script:PublishModuleBase -IconUrl $iconUrl
$expectedNupkgPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.nupkg"
Get-ChildItem $script:repositoryPath | Should -Be $expectedNupkgPath
$expectedExpandedPath = Join-Path -Path $script:repositoryPath -ChildPath "ExpandedPackage"
New-Item -Path $expectedExpandedPath -ItemType directory
Expand-Archive -Path $expectedNupkgPath -DestinationPath $expectedExpandedPath
$expectedNuspec = Join-path -Path $expectedExpandedPath -ChildPath "$script:PublishModuleName.nuspec"
$expectedNuspecContents = Get-Content -Path $expectedNuspec -Raw
$expectedNuspecContents.Contains($iconUrl) | Should Be $true
}
It "Publish a module with -ProjectUrl" {
$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"
$projectUrl = "https://www.fakeprojectUrl.com"
Publish-PSResource -Path $script:PublishModuleBase -ProjectUrl $projectUrl
$expectedNupkgPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.nupkg"
Get-ChildItem $script:repositoryPath | Should -Be $expectedNupkgPath
$expectedExpandedPath = Join-Path -Path $script:repositoryPath -ChildPath "ExpandedPackage"
New-Item -Path $expectedExpandedPath -ItemType directory
Expand-Archive -Path $expectedNupkgPath -DestinationPath $expectedExpandedPath
$expectedNuspec = Join-path -Path $expectedExpandedPath -ChildPath "$script:PublishModuleName.nuspec"
$expectedNuspecContents = Get-Content -Path $expectedNuspec -Raw
$expectedNuspecContents.Contains($projectUrl) | Should Be $true
}
It "Publish a module with -Tags" {
$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"
$tags = "Tag1"
Publish-PSResource -Path $script:PublishModuleBase -Tags $tags
$expectedNupkgPath = Join-Path -Path $script:repositoryPath -ChildPath "$script:PublishModuleName.$version.nupkg"
Get-ChildItem $script:repositoryPath | Should -Be $expectedNupkgPath
$expectedExpandedPath = Join-Path -Path $script:repositoryPath -ChildPath "ExpandedPackage"
New-Item -Path $expectedExpandedPath -ItemType directory
Expand-Archive -Path $expectedNupkgPath -DestinationPath $expectedExpandedPath
$expectedNuspec = Join-path -Path $expectedExpandedPath -ChildPath "$script:PublishModuleName.nuspec"
$expectedNuspecContents = Get-Content -Path $expectedNuspec -Raw
$expectedNuspecContents.Contains($tags) | Should Be $true
}
#>
It "Publish a module to PSGallery without -APIKey, should throw" {
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $script:PublishModuleBase -Repository PSGallery -ErrorAction SilentlyContinue
$Error[0].FullyQualifiedErrorId | Should -be "401ApiKeyError,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
}
It "Publish a module to PSGallery using incorrect API key, should throw" {
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
$APIKey = New-Guid
Publish-PSResource -Path $script:PublishModuleBase -Repository PSGallery -APIKey $APIKey -ErrorAction SilentlyContinue
$Error[0].FullyQualifiedErrorId | Should -be "403Error,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
}
It "Publish a module with -Path -Repository and -DestinationPath" {
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2 -DestinationPath $script:destinationPath
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
$expectedPath = Join-Path -Path $script:destinationPath -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:destinationPath).FullName | Should -Be $expectedPath
}
It "Publish a module with -Path -Repository and -DestinationPath" {
$moduleName = "Pester"
$moduleVersion = "5.5.0"
Save-PSResource -Name $moduleName -Path $tmpRepoPath -Version $moduleVersion -Repository PSGallery -TrustRepository
$modulePath = Join-Path -Path $tmpRepoPath -ChildPath $moduleName
$moduleVersionPath = Join-Path -Path $modulePath -ChildPath $moduleVersion
$moduleManifestPath = Join-path -Path $moduleVersionPath -ChildPath "$moduleName.psd1"
Publish-PSResource -Path $moduleManifestPath -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$moduleName.$moduleVersion.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "Publish a module and clean up properly when file in module is readonly" {
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
# Create a readonly file that will throw access denied error if deletion is attempted
$file = Join-Path -Path $script:PublishModuleBase -ChildPath "inaccessiblefile.txt"
New-Item $file -Itemtype file -Force
Set-ItemProperty -Path $file -Name IsReadOnly -Value $true
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "Publish a module when the .psd1 version and the path version are different" {
$incorrectVersion = "15.2.4"
$correctVersion = "1.0.0"
$versionBase = (Join-Path -Path $script:PublishModuleBase -ChildPath $incorrectVersion)
New-Item -Path $versionBase -ItemType Directory
$modManifestPath = (Join-Path -Path $versionBase -ChildPath "$script:PublishModuleName.psd1")
New-ModuleManifest -Path $modManifestPath -ModuleVersion $correctVersion -Description "$script:PublishModuleName module"
Publish-PSResource -Path $modManifestPath -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$correctVersion.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
}
It "publish a script locally"{
$scriptName = "PSGetTestScript"
$scriptVersion = "1.0.0"
$params = @{
Version = $scriptVersion
GUID = [guid]::NewGuid()
Author = 'Jane'
CompanyName = 'Microsoft Corporation'
Copyright = '(c) 2020 Microsoft Corporation. All rights reserved.'
Description = "Description for the $scriptName script"
LicenseUri = "https://$scriptName.com/license"
IconUri = "https://$scriptName.com/icon"
ProjectUri = "https://$scriptName.com"
Tags = @('Tag1','Tag2', "Tag-$scriptName-$scriptVersion")
ReleaseNotes = "$scriptName release notes"
}
$scriptPath = (Join-Path -Path $script:tmpScriptsFolderPath -ChildPath "$scriptName.ps1")
New-PSScriptFileInfo @params -Path $scriptPath
Publish-PSResource -Path $scriptPath
$expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
(Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath
}
It "should publish a script without lines in between comment blocks locally" {
$scriptName = "ScriptWithoutEmptyLinesBetweenCommentBlocks"
$scriptVersion = "1.0.0"
$scriptPath = (Join-Path -Path $script:testScriptsFolderPath -ChildPath "$scriptName.ps1")
Publish-PSResource -Path $scriptPath
$expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
(Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath
}
It "should publish a script without lines in help block locally" {
$scriptName = "ScriptWithoutEmptyLinesInMetadata"
$scriptVersion = "1.0.0"
$scriptPath = (Join-Path -Path $script:testScriptsFolderPath -ChildPath "$scriptName.ps1")
Publish-PSResource -Path $scriptPath
$expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
(Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath
}
It "should publish a script with ExternalModuleDependencies that are not published" {
$scriptName = "test"
$scriptVersion = "1.0.0"
$scriptPath = Join-Path -Path $script:testScriptsFolderPath -ChildPath "$scriptName.ps1"
New-PSScriptFileInfo -Description 'test' -Version $scriptVersion -RequiredModules @{ModuleName='testModule'} -ExternalModuleDependencies 'testModule' -Path $scriptPath -Force
Publish-PSResource -Path $scriptPath
$expectedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
(Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath
}
It "should write error and not publish script when Author property is missing" {
$scriptName = "InvalidScriptMissingAuthor.ps1"
$scriptVersion = "1.0.0"
$scriptFilePath = Join-Path $script:testScriptsFolderPath -ChildPath $scriptName
Publish-PSResource -Path $scriptFilePath -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "psScriptMissingAuthor,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
$publishedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
Test-Path -Path $publishedPath | Should -Be $false
}
It "should write error and not publish script when Version property is missing" {
$scriptName = "InvalidScriptMissingVersion.ps1"
$scriptFilePath = Join-Path $script:testScriptsFolderPath -ChildPath $scriptName
Publish-PSResource -Path $scriptFilePath -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "psScriptMissingVersion,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
$publishedPkgs = Get-ChildItem -Path $script:repositoryPath -Filter *.nupkg
$publishedPkgs | Should -HaveCount 0
}
It "should write error and not publish script when Guid property is missing" {
$scriptName = "InvalidScriptMissingGuid.ps1"
$scriptVersion = "1.0.0"
$scriptFilePath = Join-Path $script:testScriptsFolderPath -ChildPath $scriptName
Publish-PSResource -Path $scriptFilePath -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "psScriptMissingGuid,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
$publishedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
Test-Path -Path $publishedPath | Should -Be $false
}
It "should write error and not publish script when Description property is missing" {
$scriptName = "InvalidScriptMissingDescription.ps1"
$scriptVersion = "1.0.0"
$scriptFilePath = Join-Path $script:testScriptsFolderPath -ChildPath $scriptName
Publish-PSResource -Path $scriptFilePath -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PSScriptInfoMissingDescription,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
$publishedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
Test-Path -Path $publishedPath | Should -Be $false
}
It "should write error and not publish script when Description block altogether is missing" {
# we expect .ps1 files to have a separate comment block for .DESCRIPTION property, not to be included in the PSScriptInfo comment block
$scriptName = "InvalidScriptMissingDescriptionCommentBlock.ps1"
$scriptVersion = "1.0.0"
$scriptFilePath = Join-Path $script:testScriptsFolderPath -ChildPath $scriptName
Publish-PSResource -Path $scriptFilePath -ErrorVariable err -ErrorAction SilentlyContinue
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "missingHelpInfoCommentError,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
$publishedPath = Join-Path -Path $script:repositoryPath -ChildPath "$scriptName.$scriptVersion.nupkg"
Test-Path -Path $publishedPath | Should -Be $false
}
It "Publish a module with that has an invalid version format, should throw" {
$moduleName = "incorrectmoduleversion"
$incorrectmoduleversion = Join-Path -Path $script:testModulesFolderPath -ChildPath $moduleName
{Publish-PSResource -Path $incorrectmoduleversion -ErrorAction Stop} | Should -Throw -ErrorId "InvalidModuleManifest,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
}
It "Publish a module with a dependency that has an invalid version format, should throw" {
$moduleName = "incorrectdepmoduleversion"
$incorrectdepmoduleversion = Join-Path -Path $script:testModulesFolderPath -ChildPath $moduleName
{Publish-PSResource -Path $incorrectdepmoduleversion -ErrorAction Stop} | Should -Throw -ErrorId "InvalidModuleManifest,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
}
It "Publish a module with using an invalid file path (path to .psm1), should throw" {
$fileName = "$script:PublishModuleName.psm1"
$psm1Path = Join-Path -Path $script:PublishModuleBase -ChildPath $fileName
$null = New-Item -Path $psm1Path -ItemType File -Force
{Publish-PSResource -Path $psm1Path -Repository $testRepository2 -ErrorAction Stop} | Should -Throw -ErrorId "InvalidPublishPath,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
}
It "Get definition for alias 'pbres'" {
(Get-Alias pbres).Definition | Should -BeExactly 'Publish-PSResource'
}
It "Publish a module with prerelease dependency" {
# look at functions in test utils for creating a module with prerelease
$DepModuleName = "PrereleaseModule"
$DepVersion = "1.0.0"
$DepPrereleaseLabel = "beta"
$DepModuleRoot = Join-Path -Path $script:PublishModuleBase -ChildPath $DepModuleName
New-TestModule -Path $DepModuleRoot -ModuleName $DepModuleName -RepoName $testRepository2 -PackageVersion $DepVersion -prereleaseLabel $DepPrereleaseLabel
Install-PSResource -Name $DepModuleName -Repository $testRepository2 -TrustRepository -Prerelease
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$DepModuleName.$DepVersion-$DepPrereleaseLabel.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
$ParentModuleName = "TestModuleWithPrereleaseDep"
$ParentVersion = "1.0.0"
$ParentModuleRoot = Join-Path -Path $script:PublishModuleBase -ChildPath $ParentModuleName
New-Item -Path $ParentModuleRoot -ItemType Directory -Force
$ParentManifestPath = Join-Path -Path $ParentModuleRoot -ChildPath "$ParentModuleName.psd1"
New-ModuleManifest -Path $ParentManifestPath -ModuleVersion $ParentVersion -Description "$ParentPkgName module" -RequiredModules $DepModuleName
Publish-PSResource -Path $ParentManifestPath -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$ParentModuleName.$ParentVersion.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Contain $expectedPath
}
<#
It "Publish a module with required modules (both in string format and hashtable format)" {
# look at functions in test utils for creating a module with prerelease
$ModuleName = "ParentModule"
$ModuleVersion = "1.0.0"
$ModuleRoot = Join-Path -Path $script:PublishModuleBase -ChildPath $ModuleName
New-Item $ModuleRoot -ItemType Directory
$ModuleManifestPath = Join-Path -Path $ModuleRoot -ChildPath "$ModuleName.psd1"
$ReqModule1Name = "ReqModule1"
$ReqModule1Version = "3.0.0"
$ReqModule1Root = Join-Path -Path $script:PublishModuleBase -ChildPath $ReqModule1Name
New-Item $ReqModule1Root -ItemType Directory
$ReqModule1ManifestPath = Join-Path -Path $ReqModule1Root -ChildPath "$ReqModule1Name.psd1"
$ReqModule2Name = "ReqModule2"
$ReqModule2Root = Join-Path -Path $script:PublishModuleBase -ChildPath $ReqModule2Name
New-Item $ReqModule2Root -ItemType Directory
$ReqModule2ManifestPath = Join-Path -Path $reqModule2Root -ChildPath "$ReqModule2Name.psd1"
New-ModuleManifest -Path $ModuleManifestPath -ModuleVersion $ModuleVersion -Description "$ModuleName module" -RequiredModules @( @{ "ModuleName" = $ReqModule1Name; "ModuleVersion" = $ReqModule1Version }, $ReqModule2Name )
New-ModuleManifest -Path $ReqModule1ManifestPath -ModuleVersion $ReqModule1Version -Description "$ReqModule1Name module"
New-ModuleManifest -Path $ReqModule2ManifestPath -Description "$ReqModule1Name module"
Publish-PSResource -Path $ReqModule1ManifestPath -Repository $testRepository2
Publish-PSResource -Path $ReqModule2ManifestPath -Repository $testRepository2
Install-PSResource $ReqModule1Name -Repository $testRepository2 -TrustRepository
Install-PSResource $ReqModule2Name -Repository $testRepository2 -TrustRepository
Publish-PSResource -Path $ModuleManifestPath -Repository $testRepository2
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$ModuleName.$ModuleVersion.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Contain $expectedPath
}
#>
}
Describe "Test Publish-PSResource with Module Prefix" -tags 'CI' {
BeforeAll {
Get-NewPSResourceRepositoryFile
$tmpRepoPath2 = Join-Path -Path $TestDrive -ChildPath "tmpRepoPath2"
New-Item $tmpRepoPath2 -Itemtype directory -Force
$testRepository2 = "testRepository2"
Register-PSResourceRepository -Name $testRepository2 -Uri $tmpRepoPath2
$script:repositoryPath2 = [IO.Path]::GetFullPath((get-psresourcerepository "testRepository2").Uri.AbsolutePath)
# Create module
$script:tmpModulesPath = Join-Path -Path $TestDrive -ChildPath "tmpModulesPath"
$script:PublishModuleName = "PSGetTestModule"
$script:PublishModuleBase = Join-Path $script:tmpModulesPath -ChildPath $script:PublishModuleName
if(!(Test-Path $script:PublishModuleBase))
{
New-Item -Path $script:PublishModuleBase -ItemType Directory -Force
}
$script:PSGalleryName = 'PSGallery'
}
AfterAll {
Get-RevertPSResourceRepositoryFile
}
AfterEach {
# Delete all contents of the repository without deleting the repository directory itself
$pkgsToDelete = Join-Path -Path "$script:repositoryPath2" -ChildPath "*"
Remove-Item $pkgsToDelete -Recurse
$pkgsToDelete = Join-Path -Path $script:PublishModuleBase -ChildPath "*"
Remove-Item $pkgsToDelete -Recurse -ErrorAction SilentlyContinue
}
It "Publish should create PSResourceRepository.xml file if its not there" {
# Remove the PSResourceRepository.xml file
$powerShellGetPath = Join-Path -Path ([Environment]::GetFolderPath([System.Environment+SpecialFolder]::LocalApplicationData)) -ChildPath "PSResourceGet"
$originalXmlFilePath = Join-Path -Path $powerShellGetPath -ChildPath "PSResourceRepository.xml" #this is the temporary PSResourceRepository.xml file created in the 'BeforeAll' section of this test file
$tempXmlFilePath = Join-Path -Path $powerShellGetPath -ChildPath "tempfortest.xml"
try {
if (Test-Path -Path $originalXmlFilePath) {
Copy-Item -Path $originalXmlFilePath -Destination $tempXmlFilePath
Remove-Item -Path $originalXmlFilePath -Force -ErrorAction Ignore
}
# Attempt to publish.
# Publish-PSResource should create PSResourceRepository.xml file if not present. It will register the 'PSGallery' repository as a default, so this test will still fail
# But we can ensure the PSResourceRepository.xml file is created.
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2 -ErrorVariable err -ErrorAction SilentlyContinue
$err[0].FullyQualifiedErrorId | Should -Be "RepositoryNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
$registeredRepos = Get-PSResourceRepository
$registeredRepos.Count | Should -Be 1
$registeredRepos[0].Name | Should -Be $script:PSGalleryName
}
finally {
# Cleanup
# Remove the new PSResourceRepository.xml file created by the Publish-PSResource command and put back the original created in the 'BeforeAll' section of this test file
if (Test-Path -Path $tempXmlFilePath) {
Copy-Item -Path $tempXmlFilePath -Destination $originalXmlFilePath -Force -Verbose
Remove-Item -Path $tempXmlFilePath -Force -ErrorAction Ignore
}
}
# Verify old repositories are restored
$originalRegisteredRepo = Get-PSResourceRepository -Name $testRepository2 -ErrorVariable err2 -ErrorAction SilentlyContinue
$err2.Count | Should -Be 0
$originalRegisteredRepo.Name | Should -Be $testRepository2
}
}