Skip to content

Commit 3952097

Browse files
authored
Check for ModulePrefix parameter presence instead of it being a dynamic parameter (#1868)
1 parent 87375d6 commit 3952097

File tree

3 files changed

+100
-29
lines changed

3 files changed

+100
-29
lines changed

src/code/PublishPSResource.cs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
1717
"PSResource",
1818
SupportsShouldProcess = true)]
1919
[Alias("pbres")]
20-
public sealed class PublishPSResource : PSCmdlet, IDynamicParameters
20+
public sealed class PublishPSResource : PSCmdlet
2121
{
2222
#region Parameters
2323

2424
private const string PathParameterSet = "PathParameterSet";
2525
private const string NupkgPathParameterSet = "NupkgPathParameterSet";
26-
private ContainerRegistryDynamicParameters _pkgPrefix;
2726

2827
/// <summary>
2928
/// Specifies the API key that you want to use to publish a module to the online gallery.
@@ -117,20 +116,12 @@ public PSCredential ProxyCredential {
117116
[ValidateNotNullOrEmpty]
118117
public string NupkgPath { get; set; }
119118

120-
#endregion
121-
122-
#region DynamicParameters
123-
public object GetDynamicParameters()
124-
{
125-
PSRepositoryInfo repository = RepositorySettings.Read(new[] { Repository }, out string[] _).FirstOrDefault();
126-
if (repository is not null && repository.ApiVersion == PSRepositoryInfo.APIVersion.ContainerRegistry)
127-
{
128-
_pkgPrefix = new ContainerRegistryDynamicParameters();
129-
return _pkgPrefix;
130-
}
131-
132-
return null;
133-
}
119+
/// <summary>
120+
/// Prefix for module name which only applies to repositories of type 'ContainerRegistry'
121+
/// </summary>
122+
[Parameter]
123+
[ValidateNotNullOrEmpty]
124+
public string ModulePrefix { get; set; }
134125

135126
#endregion
136127

@@ -151,16 +142,41 @@ protected override void BeginProcessing()
151142

152143
_networkCredential = Credential != null ? new NetworkCredential(Credential.UserName, Credential.Password) : null;
153144

145+
// Create the repository store file (PSResourceRepository.xml) if it does not already exist
146+
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
147+
RepositorySettings.CheckRepositoryStore();
148+
149+
if (MyInvocation.BoundParameters.ContainsKey(nameof(ModulePrefix)))
150+
{
151+
if (MyInvocation.BoundParameters.ContainsKey(nameof(Repository)))
152+
{
153+
// at this point it is ensured PSResourceRepository.xml file is created
154+
PSRepositoryInfo repository = RepositorySettings.Read(new[] { Repository }, out string[] _).FirstOrDefault();
155+
if (repository is null || repository.ApiVersion != PSRepositoryInfo.APIVersion.ContainerRegistry)
156+
{
157+
ThrowTerminatingError(new ErrorRecord(
158+
new PSInvalidOperationException("ModulePrefix parameter can only be provided for a registered repository of type 'ContainerRegistry'"),
159+
"ModulePrefixParameterIncorrectlyProvided",
160+
ErrorCategory.InvalidOperation,
161+
this));
162+
}
163+
}
164+
else
165+
{
166+
ThrowTerminatingError(new ErrorRecord(
167+
new PSInvalidOperationException("ModulePrefix parameter can only be provided with the Repository parameter."),
168+
"ModulePrefixParameterProvidedWithoutRepositoryParameter",
169+
ErrorCategory.InvalidOperation,
170+
this));
171+
}
172+
}
173+
154174
if (!string.IsNullOrEmpty(NupkgPath))
155175
{
156176
_isNupkgPathSpecified = true;
157177
Path = NupkgPath;
158178
}
159179

160-
// Create a respository story (the PSResourceRepository.xml file) if it does not already exist
161-
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
162-
RepositorySettings.CheckRepositoryStore();
163-
164180
_publishHelper = new PublishHelper(
165181
this,
166182
Credential,
@@ -186,18 +202,10 @@ protected override void EndProcessing()
186202
return;
187203
}
188204

189-
string modulePrefix = _pkgPrefix?.ModulePrefix;
190-
191-
_publishHelper.PushResource(Repository, modulePrefix, SkipDependenciesCheck, _networkCredential);
205+
_publishHelper.PushResource(Repository, ModulePrefix, SkipDependenciesCheck, _networkCredential);
192206
}
193207

194208
#endregion
195209

196210
}
197-
198-
public class ContainerRegistryDynamicParameters
199-
{
200-
[Parameter]
201-
public string ModulePrefix { get; set; }
202-
}
203211
}

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)