Skip to content

Commit

Permalink
Bug fix for setting 'unknown' repository APIVersion (#1377)
Browse files Browse the repository at this point in the history
  • Loading branch information
alerickson authored Aug 24, 2023
1 parent bc0ccdd commit 0e23043
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
8 changes: 0 additions & 8 deletions src/code/RepositorySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,6 @@ public static PSRepositoryInfo UpdateRepositoryStore(string repoName, Uri repoUr
}
}

// determine if either 1 of 5 values are attempting to be set: Uri, Priority, Trusted, APIVerison, CredentialInfo.
// if none are (i.e only Name parameter was provided, write error)
if (repoUri == null && repoPriority == defaultPriority && _trustedNullable == null && repoCredentialInfo == null && (apiVersion == PSRepositoryInfo.APIVersion.unknown || apiVersion == null))
{
errorMsg = "Must set Uri, Priority, Trusted, ApiVersion, or CredentialInfo parameter";
return null;
}

if (!cmdletPassedIn.ShouldProcess(repoName, "Set repository's value(s) in repository store"))
{
return null;
Expand Down
16 changes: 16 additions & 0 deletions src/code/SetPSResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ protected override void BeginProcessing()

protected override void ProcessRecord()
{
// determine if either 1 of 5 values are attempting to be set: Uri, Priority, Trusted, APIVerison, CredentialInfo.
// if none are (i.e only Name parameter was provided, write error)
if (ParameterSetName.Equals(NameParameterSet) &&
!MyInvocation.BoundParameters.ContainsKey(nameof(Uri)) &&
!MyInvocation.BoundParameters.ContainsKey(nameof(Priority)) &&
!MyInvocation.BoundParameters.ContainsKey(nameof(Trusted)) &&
!MyInvocation.BoundParameters.ContainsKey(nameof(ApiVersion)) &&
!MyInvocation.BoundParameters.ContainsKey(nameof(CredentialInfo)))
{
ThrowTerminatingError(new ErrorRecord(
new ArgumentException("Must set Uri, Priority, Trusted, ApiVersion, or CredentialInfo parameter"),
"SetRepositoryParameterBindingFailure",
ErrorCategory.InvalidArgument,
this));
}

if (MyInvocation.BoundParameters.ContainsKey(nameof(Uri)))
{
if (!Utils.TryCreateValidUri(Uri, this, out _uri, out ErrorRecord errorRecord))
Expand Down
14 changes: 13 additions & 1 deletion test/ResourceRepositoryTests/SetPSResourceRepository.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Describe "Test Set-PSResourceRepository" -tags 'CI' {

It "not set repository and write error given just Name parameter" {
Register-PSResourceRepository -Name $TestRepoName1 -Uri $tmpDir1Path
{Set-PSResourceRepository -Name $TestRepoName1 -ErrorAction Stop} | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository"
{Set-PSResourceRepository -Name $TestRepoName1 -ErrorAction Stop} | Should -Throw -ErrorId "SetRepositoryParameterBindingFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository"
}

$testCases = @{Type = "contains *"; Name = "test*Repository"; ErrorId = "ErrorInNameParameterSet"},
Expand Down Expand Up @@ -344,4 +344,16 @@ Describe "Test Set-PSResourceRepository" -tags 'CI' {
$repo.ApiVersion | Should -Be "local"
$repo.Priority | Should -Be 25
}

It "should not change ApiVersion of repository if -ApiVersion parameter was not used" {
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
$repo = Get-PSResourceRepository $TestRepoName1
$repo.ApiVersion | Should -Be "unknown"
$err.Count | Should -Be 0
}
}

0 comments on commit 0e23043

Please sign in to comment.