Skip to content

Commit a0189ad

Browse files
committed
Add tests
1 parent 3aa7901 commit a0189ad

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/code/PublishPSResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected override void BeginProcessing()
155155
if (repository is null || repository.ApiVersion != PSRepositoryInfo.APIVersion.ContainerRegistry)
156156
{
157157
ThrowTerminatingError(new ErrorRecord(
158-
new PSInvalidOperationException("ModulePrefix parameter can only be provided with the Repository parameter for a registered repository of type 'ContainerRegistry'"),
158+
new PSInvalidOperationException("ModulePrefix parameter can only be provided for a registered repository of type 'ContainerRegistry'"),
159159
"ModulePrefixParameterIncorrectlyProvided",
160160
ErrorCategory.InvalidOperation,
161161
this));

test/PublishPSResourceTests/PublishPSResource.Tests.ps1

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ Describe "Test Publish-PSResource" -tags 'CI' {
9999

100100
# Create test module with missing required module
101101
CreateTestModule -Path $TestDrive -ModuleName 'ModuleWithMissingRequiredModule'
102+
103+
$script:PSGalleryName = 'PSGallery'
102104
}
103105
AfterAll {
104106
Get-RevertPSResourceRepositoryFile
@@ -149,6 +151,44 @@ Describe "Test Publish-PSResource" -tags 'CI' {
149151
(Get-ChildItem $script:repositoryPath).FullName | Should -Be $expectedPath
150152
}
151153

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+
152192
#region Local Source Path
153193
It "Publish a module with -Path and -Repository" {
154194
$version = "1.0.0"

test/PublishPSResourceTests/PublishPSResourceContainerRegistryServer.Tests.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ Describe "Test Publish-PSResource" -tags 'CI' {
100100

101101
# Path to specifically to that invalid test nupkgs folder
102102
$script:testNupkgsFolderPath = Join-Path $script:testFilesFolderPath -ChildPath "testNupkgs"
103+
104+
$script:PSGalleryName = "PSGallery"
103105
}
104106
AfterEach {
105107
if(!(Test-Path $script:PublishModuleBase))
@@ -515,6 +517,27 @@ Describe "Test Publish-PSResource" -tags 'CI' {
515517
$results[0].Version | Should -Be $version
516518
}
517519

520+
It "not Publish a resource when ModulePrefix is given for a Repository that is not of type ContainerRegistry"
521+
{
522+
$version = "1.0.0"
523+
$modulePrefix = "unlisted"
524+
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
525+
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"
529+
}
530+
531+
It "not Publish a resource when ModulePrefix is provided without Repository parameter" {
532+
$version = "1.0.0"
533+
$modulePrefix = "unlisted"
534+
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
535+
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"
539+
}
540+
518541
It "Publish a package given NupkgPath to a package with .psd1" {
519542
$packageName = "temp-testmodule-nupkgpath"
520543
$version = "1.0.0.0"

0 commit comments

Comments
 (0)