Skip to content

Commit 4fe3725

Browse files
Copilotalerickson
andcommitted
Add validation to prevent setting ApiVersion to Unknown in Register-PSResourceRepository
Co-authored-by: alerickson <[email protected]>
1 parent 12c94f2 commit 4fe3725

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/code/RegisterPSResourceRepository.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ protected override void ProcessRecord()
145145
PSRepositoryInfo.APIVersion? repoApiVersion = null;
146146
if (MyInvocation.BoundParameters.ContainsKey(nameof(ApiVersion)))
147147
{
148+
if (ApiVersion == PSRepositoryInfo.APIVersion.Unknown)
149+
{
150+
ThrowTerminatingError(new ErrorRecord(
151+
new ArgumentException("ApiVersion 'Unknown' is not a valid value for Register-PSResourceRepository. Valid values are: V2, V3, Local, NugetServer, ContainerRegistry"),
152+
"InvalidApiVersion",
153+
ErrorCategory.InvalidArgument,
154+
this));
155+
}
148156
repoApiVersion = ApiVersion;
149157
}
150158

test/ResourceRepositoryTests/RegisterPSResourceRepository.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ Describe "Test Register-PSResourceRepository" -tags 'CI' {
404404
$res.ApiVersion | Should -Be 'v2'
405405
}
406406

407+
It "should throw error when trying to register repository with ApiVersion unknown" {
408+
{Register-PSResourceRepository -Name $TestRepoName1 -Uri $tmpDir1Path -ApiVersion "unknown" -ErrorAction Stop} | Should -Throw -ErrorId "InvalidApiVersion,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository"
409+
410+
# Verify the repository was not created
411+
$repo = Get-PSResourceRepository $TestRepoName1 -ErrorAction SilentlyContinue
412+
$repo | Should -BeNullOrEmpty
413+
}
414+
407415
It "should register container registry repository with correct ApiVersion" {
408416
$ContainerRegistryName = "ACRRepo"
409417
$ContainerRegistryUri = "https://psresourcegettest.azurecr.io/"

0 commit comments

Comments
 (0)