Skip to content

Commit 0e23043

Browse files
authored
Bug fix for setting 'unknown' repository APIVersion (#1377)
1 parent bc0ccdd commit 0e23043

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/code/RepositorySettings.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,6 @@ public static PSRepositoryInfo UpdateRepositoryStore(string repoName, Uri repoUr
204204
}
205205
}
206206

207-
// determine if either 1 of 5 values are attempting to be set: Uri, Priority, Trusted, APIVerison, CredentialInfo.
208-
// if none are (i.e only Name parameter was provided, write error)
209-
if (repoUri == null && repoPriority == defaultPriority && _trustedNullable == null && repoCredentialInfo == null && (apiVersion == PSRepositoryInfo.APIVersion.unknown || apiVersion == null))
210-
{
211-
errorMsg = "Must set Uri, Priority, Trusted, ApiVersion, or CredentialInfo parameter";
212-
return null;
213-
}
214-
215207
if (!cmdletPassedIn.ShouldProcess(repoName, "Set repository's value(s) in repository store"))
216208
{
217209
return null;

src/code/SetPSResourceRepository.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ protected override void BeginProcessing()
111111

112112
protected override void ProcessRecord()
113113
{
114+
// determine if either 1 of 5 values are attempting to be set: Uri, Priority, Trusted, APIVerison, CredentialInfo.
115+
// if none are (i.e only Name parameter was provided, write error)
116+
if (ParameterSetName.Equals(NameParameterSet) &&
117+
!MyInvocation.BoundParameters.ContainsKey(nameof(Uri)) &&
118+
!MyInvocation.BoundParameters.ContainsKey(nameof(Priority)) &&
119+
!MyInvocation.BoundParameters.ContainsKey(nameof(Trusted)) &&
120+
!MyInvocation.BoundParameters.ContainsKey(nameof(ApiVersion)) &&
121+
!MyInvocation.BoundParameters.ContainsKey(nameof(CredentialInfo)))
122+
{
123+
ThrowTerminatingError(new ErrorRecord(
124+
new ArgumentException("Must set Uri, Priority, Trusted, ApiVersion, or CredentialInfo parameter"),
125+
"SetRepositoryParameterBindingFailure",
126+
ErrorCategory.InvalidArgument,
127+
this));
128+
}
129+
114130
if (MyInvocation.BoundParameters.ContainsKey(nameof(Uri)))
115131
{
116132
if (!Utils.TryCreateValidUri(Uri, this, out _uri, out ErrorRecord errorRecord))

test/ResourceRepositoryTests/SetPSResourceRepository.Tests.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Describe "Test Set-PSResourceRepository" -tags 'CI' {
100100

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

106106
$testCases = @{Type = "contains *"; Name = "test*Repository"; ErrorId = "ErrorInNameParameterSet"},
@@ -344,4 +344,16 @@ Describe "Test Set-PSResourceRepository" -tags 'CI' {
344344
$repo.ApiVersion | Should -Be "local"
345345
$repo.Priority | Should -Be 25
346346
}
347+
348+
It "should not change ApiVersion of repository if -ApiVersion parameter was not used" {
349+
Register-PSResourceRepository -Name $TestRepoName1 -Uri $tmpDir1Path
350+
$repo = Get-PSResourceRepository $TestRepoName1
351+
$repoApiVersion = $repo.ApiVersion
352+
$repoApiVersion | Should -Be "local"
353+
354+
Set-PSResourceRepository -Name $TestRepoName1 -ApiVersion "unknown" -ErrorVariable err -ErrorAction SilentlyContinue
355+
$repo = Get-PSResourceRepository $TestRepoName1
356+
$repo.ApiVersion | Should -Be "unknown"
357+
$err.Count | Should -Be 0
358+
}
347359
}

0 commit comments

Comments
 (0)