Skip to content

Commit d209bd2

Browse files
authored
Bugfix: Find and Install unlisted versions of a package given name and version (#1859)
1 parent 3399c73 commit d209bd2

File tree

9 files changed

+64
-14
lines changed

9 files changed

+64
-14
lines changed

src/code/ContainerRegistryResponseUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ContainerRegistryResponseUtil(PSRepositoryInfo repository) : base(reposit
2828

2929
#region Overriden Methods
3030

31-
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults)
31+
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults, bool isResourceRequestedWithWildcard = false)
3232
{
3333
Hashtable[] responses = responseResults.HashtableResponse;
3434
foreach (Hashtable response in responses)

src/code/FindHelper.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
689689
ErrorRecord errRecord = null;
690690
List<PSResourceInfo> parentPkgs = new List<PSResourceInfo>();
691691
string tagsAsString = String.Empty;
692+
bool isV2Resource = currentResponseUtil is V2ResponseUtil;
692693

693694
_cmdletPassedIn.WriteDebug("In FindHelper::SearchByNames()");
694695
foreach (string pkgName in _pkgsLeftToFind.ToArray())
@@ -699,6 +700,12 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
699700
{
700701
_cmdletPassedIn.WriteDebug("No version specified, package name is '*'");
701702
// Example: Find-PSResource -Name "*"
703+
704+
// Note: Just for resources from V2 servers, specifically PSGallery, if the resource is unlisted and was requested non-explicitly
705+
// (i.e requested name has wildcard) the resource should not be returned and ResponseUtil.ConvertToPSResourceResult() call needs to be informed of this.
706+
// In all other cases, return the resource regardless of whether it was requested explicitly or not.
707+
bool isResourceRequestedWithWildcard = isV2Resource;
708+
702709
FindResults responses = currentServer.FindAll(_prerelease, _type, out errRecord);
703710
if (errRecord != null)
704711
{
@@ -714,7 +721,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
714721
continue;
715722
}
716723

717-
foreach (PSResourceResult currentResult in currentResponseUtil.ConvertToPSResourceResult(responseResults: responses))
724+
foreach (PSResourceResult currentResult in currentResponseUtil.ConvertToPSResourceResult(responseResults: responses, isResourceRequestedWithWildcard))
718725
{
719726
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
720727
{
@@ -745,6 +752,10 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
745752
// Example: Find-PSResource -Name "Az*" -Tag "Storage"
746753
_cmdletPassedIn.WriteDebug("No version specified, package name contains a wildcard.");
747754

755+
// Note: Just for resources from V2 servers, specifically PSGallery, if the resource is unlisted and was requested non-explicitly
756+
// (i.e requested name has wildcard) the resource should not be returned and ResponseUtil.ConvertToPSResourceResult() call needs to be informed of this.
757+
// In all other cases, return the resource regardless of whether it was requested explicitly or not.
758+
bool isResourceRequestedWithWildcard = isV2Resource;
748759
FindResults responses = null;
749760
if (_tag.Length == 0)
750761
{
@@ -770,7 +781,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
770781
continue;
771782
}
772783

773-
foreach (PSResourceResult currentResult in currentResponseUtil.ConvertToPSResourceResult(responses))
784+
foreach (PSResourceResult currentResult in currentResponseUtil.ConvertToPSResourceResult(responses, isResourceRequestedWithWildcard))
774785
{
775786
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
776787
{

src/code/LocalResponseUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public LocalResponseUtil(PSRepositoryInfo repository) : base(repository)
2727
#endregion
2828

2929
#region Overriden Methods
30-
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults)
30+
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults, bool isResourceRequestedWithWildcard = false)
3131
{
3232
foreach (Hashtable response in responseResults.HashtableResponse)
3333
{

src/code/NuGetServerResponseUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public NuGetServerResponseUtil(PSRepositoryInfo repository) : base(repository)
2626
#endregion
2727

2828
#region Overriden Methods
29-
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults)
29+
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults, bool isResourceRequestedWithWildcard = false)
3030
{
3131
// in FindHelper:
3232
// serverApi.FindName() -> return responses, and out errRecord

src/code/ResponseUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ResponseUtil(PSRepositoryInfo repository)
2525

2626
#region Methods
2727

28-
public abstract IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults);
28+
public abstract IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults, bool isResourceRequestedWithWildcard = false);
2929

3030
#endregion
3131

src/code/V2ResponseUtil.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public V2ResponseUtil(PSRepositoryInfo repository) : base(repository)
2828
#endregion
2929

3030
#region Overriden Methods
31-
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults)
31+
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults, bool isResourceRequestedWithWildcard = false)
3232
{
3333
// in FindHelper:
3434
// serverApi.FindName() -> return responses, and out errRecord
@@ -58,8 +58,9 @@ public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResu
5858
yield return new PSResourceResult(returnedObject: null, exception: parseException, isTerminatingError: false);
5959
}
6060

61+
// For V2 resources, specifically PSGallery, return unlisted version resources only when not requested with wildcard name
6162
// Unlisted versions will have a published year as 1900 or earlier.
62-
if (!psGetInfo.PublishedDate.HasValue || psGetInfo.PublishedDate.Value.Year > 1900)
63+
if (!isResourceRequestedWithWildcard || !psGetInfo.PublishedDate.HasValue || psGetInfo.PublishedDate.Value.Year > 1900)
6364
{
6465
yield return new PSResourceResult(returnedObject: psGetInfo, exception: null, isTerminatingError: false);
6566
}

src/code/V3ResponseUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public V3ResponseUtil(PSRepositoryInfo repository) : base(repository)
2727

2828
#region Overriden Methods
2929

30-
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults)
30+
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults, bool isResourceRequestedWithWildcard = false)
3131
{
3232
// in FindHelper:
3333
// serverApi.FindName() -> return responses, and out errRecord

test/FindPSResourceTests/FindPSResourceV2Server.Tests.ps1

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'CI' {
1212
BeforeAll{
1313
$PSGalleryName = Get-PSGalleryName
1414
$testModuleName = "test_module"
15-
$testModuleNameWithUnlistedVersion = "test_module10"
1615
$testScriptName = "test_script"
1716
$commandName = "Get-TargetResource"
1817
$dscResourceName = "SystemLocale"
@@ -413,12 +412,40 @@ Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'CI' {
413412
$err.Count | Should -Be 0
414413
}
415414

416-
It "should not find and write error when finding package version that is unlisted" {
417-
$res = Find-PSResource -Name $testModuleNameWithUnlistedVersion -Version "1.0.0.0" -Repository $PSGalleryName -ErrorVariable err -ErrorAction SilentlyContinue
418-
$res | Should -HaveCount 0
419-
$err | Should -HaveCount 1
415+
It "find should not return a module that has all unlisted versions, given full name and no version (i.e non wildcard name)" {
416+
# FindName() scenario
417+
# 'test_completelyunlisted' only has version 0.0.1, which is unlisted
418+
$res = Find-PSResource -Name "test_completelyunlisted" -Repository $PSGalleryName -ErrorVariable err
419+
$res | Should -BeNullOrEmpty
420+
$err.Count | Should -BeGreaterThan 0
420421
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
421422
}
423+
424+
It "find should return a module version even if all versions are unlisted, given full name and version (i.e non wildcard name)" {
425+
# FindVersion() scenario
426+
# test_completelyunlisted has 1 version, 0.0.1, which is unlisted
427+
$res = Find-PSResource -Name "test_completelyunlisted" -Version "0.0.1" -Repository $PSGalleryName
428+
$res | Should -Not -BeNullOrEmpty
429+
$res.Version | Should -Be "0.0.1"
430+
}
431+
432+
It "find should return an unlisted module, where the module has a mix of listed and unlisted versions, given full name and version (i.e non wildcard name)" {
433+
# FindVersion scenario
434+
# 'test_unlisted' version 0.0.3 is unlisted
435+
$res = Find-PSResource -Name "test_unlisted" -Version "0.0.3" -Repository $PSGalleryName
436+
$res | Should -Not -BeNullOrEmpty
437+
$res.Version | Should -Be "0.0.3"
438+
}
439+
440+
It "find should not return an unlisted module with it was requested with wildcards in the name" {
441+
# FindNameGlobbing() scenario
442+
# 'test_completelyunlisted' has all unlisted versions -> should not be returned
443+
# whereas 'test_unlisted' has a listed verison and 'test_notunlisted' has all listed versions -> should be returned
444+
$res = Find-PSResource -Name "test_*unlisted" -Repository $PSGalleryName
445+
$res.Count | Should -Be 2
446+
$res.Name | Should -Contain 'test_unlisted'
447+
$res.Name | Should -Contain 'test_notunlisted'
448+
}
422449
}
423450

424451
Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'ManualValidationOnly' {

test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,17 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' {
573573
$res | Should -Not -BeNullOrEmpty
574574
$res.Version | Should -Be $version
575575
}
576+
577+
It "Install resource that is unlisted" {
578+
# InstallVersion scenario
579+
# 'test_unlisted' version 0.0.3 is unlisted
580+
$moduleName = 'test_unlisted'
581+
$version = '0.0.3'
582+
Install-PSResource -Name $moduleName -Version $version -Repository $PSGalleryName -TrustRepository
583+
$res = Get-InstalledPSResource $moduleName
584+
$res | Should -Not -BeNullOrEmpty
585+
$res.Version | Should -Be $version
586+
}
576587
}
577588

578589
Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'ManualValidationOnly' {

0 commit comments

Comments
 (0)