diff --git a/src/code/SetPSResourceRepository.cs b/src/code/SetPSResourceRepository.cs index d46c5ba48..91868af9e 100644 --- a/src/code/SetPSResourceRepository.cs +++ b/src/code/SetPSResourceRepository.cs @@ -162,6 +162,14 @@ protected override void ProcessRecord() PSRepositoryInfo.APIVersion? repoApiVersion = null; if (MyInvocation.BoundParameters.ContainsKey(nameof(ApiVersion))) { + if (ApiVersion == PSRepositoryInfo.APIVersion.Unknown) + { + ThrowTerminatingError(new ErrorRecord( + new ArgumentException("ApiVersion 'Unknown' is not a valid value for Set-PSResourceRepository. Valid values are: V2, V3, Local, NugetServer, ContainerRegistry"), + "InvalidApiVersion", + ErrorCategory.InvalidArgument, + this)); + } repoApiVersion = ApiVersion; } diff --git a/test/ResourceRepositoryTests/SetPSResourceRepository.Tests.ps1 b/test/ResourceRepositoryTests/SetPSResourceRepository.Tests.ps1 index fa9120dfe..c14df0cb1 100644 --- a/test/ResourceRepositoryTests/SetPSResourceRepository.Tests.ps1 +++ b/test/ResourceRepositoryTests/SetPSResourceRepository.Tests.ps1 @@ -345,15 +345,16 @@ Describe "Test Set-PSResourceRepository" -tags 'CI' { $repo.Priority | Should -Be 25 } - It "should not change ApiVersion of repository if -ApiVersion parameter was not used" { + It "should throw error when trying to set ApiVersion to unknown" { Register-PSResourceRepository -Name $TestRepoName1 -Uri $tmpDir1Path $repo = Get-PSResourceRepository $TestRepoName1 $repoApiVersion = $repo.ApiVersion $repoApiVersion | Should -Be "local" - Set-PSResourceRepository -Name $TestRepoName1 -ApiVersion "unknown" -ErrorVariable err -ErrorAction SilentlyContinue + {Set-PSResourceRepository -Name $TestRepoName1 -ApiVersion "unknown" -ErrorAction Stop} | Should -Throw -ErrorId "InvalidApiVersion,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository" + + # Verify the repository ApiVersion was not changed $repo = Get-PSResourceRepository $TestRepoName1 - $repo.ApiVersion | Should -Be "unknown" - $err.Count | Should -Be 0 + $repo.ApiVersion | Should -Be "local" } }