Skip to content

Commit b502d34

Browse files
authored
Test clean up for Publish-PSResource (#1872)
1 parent 3952097 commit b502d34

File tree

2 files changed

+84
-50
lines changed

2 files changed

+84
-50
lines changed

test/PublishPSResourceTests/PublishPSResource.Tests.ps1

Lines changed: 81 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
55
Import-Module $modPath -Force -Verbose
66

7-
$testDir = (get-item $psscriptroot).parent.FullName
7+
$script:testDir = (get-item $psscriptroot).parent.FullName
88

9-
function CreateTestModule
9+
function script:CreateTestModule
1010
{
1111
param (
1212
[string] $Path = "$TestDrive",
@@ -56,7 +56,7 @@ Describe "Test Publish-PSResource" -tags 'CI' {
5656
$tmpRepoPath2 = Join-Path -Path $TestDrive -ChildPath "tmpRepoPath2"
5757
New-Item $tmpRepoPath2 -Itemtype directory -Force
5858
$testRepository2 = "testRepository2"
59-
Register-PSResourceRepository -Name $testRepository2 -Uri $tmpRepoPath2 -ErrorAction SilentlyContinue
59+
Register-PSResourceRepository -Name $testRepository2 -Uri $tmpRepoPath2
6060
$script:repositoryPath2 = [IO.Path]::GetFullPath((get-psresourcerepository "testRepository2").Uri.AbsolutePath)
6161

6262
# Create module
@@ -107,7 +107,7 @@ Describe "Test Publish-PSResource" -tags 'CI' {
107107
}
108108
AfterEach {
109109
# Delete all contents of the repository without deleting the repository directory itself
110-
$pkgsToDelete = Join-Path -Path "$script:repositoryPath" -ChildPath "*"
110+
$pkgsToDelete = Join-Path -Path "$script:repositoryPath" -ChildPath "*"
111111
Remove-Item $pkgsToDelete -Recurse
112112

113113
$pkgsToDelete = Join-Path -Path "$script:repositoryPath2" -ChildPath "*"
@@ -151,44 +151,6 @@ Describe "Test Publish-PSResource" -tags 'CI' {
151151
(Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath
152152
}
153153

154-
It "Publish should create PSResourceRepository.xml file if its not there" {
155-
# Remove the PSResourceRepository.xml file
156-
$powerShellGetPath = Join-Path -Path ([Environment]::GetFolderPath([System.Environment+SpecialFolder]::LocalApplicationData)) -ChildPath "PSResourceGet"
157-
$originalXmlFilePath = Join-Path -Path $powerShellGetPath -ChildPath "PSResourceRepository.xml" #this is the temporary PSResourceRepository.xml file created in the 'BeforeAll' section of this test file
158-
159-
$tempXmlFilePath = Join-Path -Path $powerShellGetPath -ChildPath "tempfortest.xml"
160-
161-
if (Test-Path -Path $originalXmlFilePath) {
162-
Copy-Item -Path $originalXmlFilePath -Destination $tempXmlFilePath
163-
Remove-Item -Path $originalXmlFilePath -Force -ErrorAction Ignore
164-
}
165-
166-
# Attempt to publish.
167-
# 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
168-
# But we can ensure the PSResourceRepository.xml file is created.
169-
$version = "1.0.0"
170-
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
171-
172-
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2 -ErrorVariable err -ErrorAction SilentlyContinue
173-
$err[0].FullyQualifiedErrorId | Should -Be "RepositoryNotRegistered,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
174-
175-
$registeredRepos = Get-PSResourceRepository
176-
$registeredRepos.Count | Should -Be 1
177-
$registeredRepos[0].Name | Should -Be $script:PSGalleryName
178-
179-
# Cleanup
180-
# 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
181-
if (Test-Path -Path $tempXmlFilePath) {
182-
Copy-Item -Path $tempXmlFilePath -Destination $originalXmlFilePath -Force -Verbose
183-
Remove-Item -Path $tempXmlFilePath -Force -ErrorAction Ignore
184-
}
185-
186-
# Verify old repositories are restored
187-
$originalRegisteredRepo = Get-PSResourceRepository -Name $testRepository2 -ErrorVariable err2 -ErrorAction SilentlyContinue
188-
$err2.Count | Should -Be 0
189-
$originalRegisteredRepo.Name | Should -Be $testRepository2
190-
}
191-
192154
#region Local Source Path
193155
It "Publish a module with -Path and -Repository" {
194156
$version = "1.0.0"
@@ -766,3 +728,80 @@ Describe "Test Publish-PSResource" -tags 'CI' {
766728
}
767729
#>
768730
}
731+
732+
733+
Describe "Test Publish-PSResource with Module Prefix" -tags 'CI' {
734+
735+
BeforeAll {
736+
Get-NewPSResourceRepositoryFile
737+
738+
$tmpRepoPath2 = Join-Path -Path $TestDrive -ChildPath "tmpRepoPath2"
739+
New-Item $tmpRepoPath2 -Itemtype directory -Force
740+
$testRepository2 = "testRepository2"
741+
Register-PSResourceRepository -Name $testRepository2 -Uri $tmpRepoPath2
742+
$script:repositoryPath2 = [IO.Path]::GetFullPath((get-psresourcerepository "testRepository2").Uri.AbsolutePath)
743+
744+
# Create module
745+
$script:tmpModulesPath = Join-Path -Path $TestDrive -ChildPath "tmpModulesPath"
746+
$script:PublishModuleName = "PSGetTestModule"
747+
$script:PublishModuleBase = Join-Path $script:tmpModulesPath -ChildPath $script:PublishModuleName
748+
if(!(Test-Path $script:PublishModuleBase))
749+
{
750+
New-Item -Path $script:PublishModuleBase -ItemType Directory -Force
751+
}
752+
753+
$script:PSGalleryName = 'PSGallery'
754+
}
755+
AfterAll {
756+
Get-RevertPSResourceRepositoryFile
757+
}
758+
AfterEach {
759+
# Delete all contents of the repository without deleting the repository directory itself
760+
$pkgsToDelete = Join-Path -Path "$script:repositoryPath2" -ChildPath "*"
761+
Remove-Item $pkgsToDelete -Recurse
762+
763+
$pkgsToDelete = Join-Path -Path $script:PublishModuleBase -ChildPath "*"
764+
Remove-Item $pkgsToDelete -Recurse -ErrorAction SilentlyContinue
765+
}
766+
767+
It "Publish should create PSResourceRepository.xml file if its not there" {
768+
# Remove the PSResourceRepository.xml file
769+
$powerShellGetPath = Join-Path -Path ([Environment]::GetFolderPath([System.Environment+SpecialFolder]::LocalApplicationData)) -ChildPath "PSResourceGet"
770+
$originalXmlFilePath = Join-Path -Path $powerShellGetPath -ChildPath "PSResourceRepository.xml" #this is the temporary PSResourceRepository.xml file created in the 'BeforeAll' section of this test file
771+
772+
$tempXmlFilePath = Join-Path -Path $powerShellGetPath -ChildPath "tempfortest.xml"
773+
774+
try {
775+
if (Test-Path -Path $originalXmlFilePath) {
776+
Copy-Item -Path $originalXmlFilePath -Destination $tempXmlFilePath
777+
Remove-Item -Path $originalXmlFilePath -Force -ErrorAction Ignore
778+
}
779+
780+
# Attempt to publish.
781+
# 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
782+
# But we can ensure the PSResourceRepository.xml file is created.
783+
$version = "1.0.0"
784+
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
785+
786+
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2 -ErrorVariable err -ErrorAction SilentlyContinue
787+
$err[0].FullyQualifiedErrorId | Should -Be "RepositoryNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
788+
789+
$registeredRepos = Get-PSResourceRepository
790+
$registeredRepos.Count | Should -Be 1
791+
$registeredRepos[0].Name | Should -Be $script:PSGalleryName
792+
}
793+
finally {
794+
# Cleanup
795+
# 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
796+
if (Test-Path -Path $tempXmlFilePath) {
797+
Copy-Item -Path $tempXmlFilePath -Destination $originalXmlFilePath -Force -Verbose
798+
Remove-Item -Path $tempXmlFilePath -Force -ErrorAction Ignore
799+
}
800+
}
801+
802+
# Verify old repositories are restored
803+
$originalRegisteredRepo = Get-PSResourceRepository -Name $testRepository2 -ErrorVariable err2 -ErrorAction SilentlyContinue
804+
$err2.Count | Should -Be 0
805+
$originalRegisteredRepo.Name | Should -Be $testRepository2
806+
}
807+
}

test/PublishPSResourceTests/PublishPSResourceContainerRegistryServer.Tests.ps1

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -517,25 +517,20 @@ Describe "Test Publish-PSResource" -tags 'CI' {
517517
$results[0].Version | Should -Be $version
518518
}
519519

520-
It "not Publish a resource when ModulePrefix is given for a Repository that is not of type ContainerRegistry"
521-
{
520+
It "not Publish a resource when ModulePrefix is given for a Repository that is not of type ContainerRegistry" {
522521
$version = "1.0.0"
523522
$modulePrefix = "unlisted"
524523
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
525524

526-
Publish-PSResource -Path $script:PublishModuleBase -Repository $script:PSGalleryName -ModulePrefix $modulePrefix -ErrorVariable err -ErrorAction SilentlyContinue
527-
$err.Count | Should -Not -Be 0
528-
$err[0].FullyQualifiedErrorId | Should -BeExactly "ModulePrefixParameterIncorrectlyProvided,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
525+
{ Publish-PSResource -Path $script:PublishModuleBase -Repository $script:PSGalleryName -ModulePrefix $modulePrefix -ErrorAction Stop } | Should -Throw "ModulePrefix parameter can only be provided for a registered repository of type 'ContainerRegistry'"
529526
}
530527

531528
It "not Publish a resource when ModulePrefix is provided without Repository parameter" {
532529
$version = "1.0.0"
533530
$modulePrefix = "unlisted"
534531
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
535532

536-
Publish-PSResource -Path $script:PublishModuleBase -ModulePrefix $modulePrefix -ErrorVariable err -ErrorAction SilentlyContinue
537-
$err.Count | Should -Not -Be 0
538-
$err[0].FullyQualifiedErrorId | Should -BeExactly "ModulePrefixParameterProvidedWithoutRepositoryParameter,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
533+
{ Publish-PSResource -Path $script:PublishModuleBase -ModulePrefix $modulePrefix -ErrorAction Stop } | Should -Throw "ModulePrefix parameter can only be provided with the Repository parameter."
539534
}
540535

541536
It "Publish a package given NupkgPath to a package with .psd1" {

0 commit comments

Comments
 (0)