Skip to content

Commit 12c94f2

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

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/code/SetPSResourceRepository.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ protected override void ProcessRecord()
162162
PSRepositoryInfo.APIVersion? repoApiVersion = null;
163163
if (MyInvocation.BoundParameters.ContainsKey(nameof(ApiVersion)))
164164
{
165+
if (ApiVersion == PSRepositoryInfo.APIVersion.Unknown)
166+
{
167+
ThrowTerminatingError(new ErrorRecord(
168+
new ArgumentException("ApiVersion 'Unknown' is not a valid value for Set-PSResourceRepository. Valid values are: V2, V3, Local, NugetServer, ContainerRegistry"),
169+
"InvalidApiVersion",
170+
ErrorCategory.InvalidArgument,
171+
this));
172+
}
165173
repoApiVersion = ApiVersion;
166174
}
167175

test/ResourceRepositoryTests/SetPSResourceRepository.Tests.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,16 @@ Describe "Test Set-PSResourceRepository" -tags 'CI' {
345345
$repo.Priority | Should -Be 25
346346
}
347347

348-
It "should not change ApiVersion of repository if -ApiVersion parameter was not used" {
348+
It "should throw error when trying to set ApiVersion to unknown" {
349349
Register-PSResourceRepository -Name $TestRepoName1 -Uri $tmpDir1Path
350350
$repo = Get-PSResourceRepository $TestRepoName1
351351
$repoApiVersion = $repo.ApiVersion
352352
$repoApiVersion | Should -Be "local"
353353

354-
Set-PSResourceRepository -Name $TestRepoName1 -ApiVersion "unknown" -ErrorVariable err -ErrorAction SilentlyContinue
354+
{Set-PSResourceRepository -Name $TestRepoName1 -ApiVersion "unknown" -ErrorAction Stop} | Should -Throw -ErrorId "InvalidApiVersion,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository"
355+
356+
# Verify the repository ApiVersion was not changed
355357
$repo = Get-PSResourceRepository $TestRepoName1
356-
$repo.ApiVersion | Should -Be "unknown"
357-
$err.Count | Should -Be 0
358+
$repo.ApiVersion | Should -Be "local"
358359
}
359360
}

0 commit comments

Comments
 (0)