Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/code/ContainerRegistryResponseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ContainerRegistryResponseUtil(PSRepositoryInfo repository) : base(reposit

#region Overriden Methods

public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults)
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults, bool isResourceRequestedWithWildcard = false)
{
Hashtable[] responses = responseResults.HashtableResponse;
foreach (Hashtable response in responses)
Expand Down
15 changes: 13 additions & 2 deletions src/code/FindHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
ErrorRecord errRecord = null;
List<PSResourceInfo> parentPkgs = new List<PSResourceInfo>();
string tagsAsString = String.Empty;
bool isV2Resource = currentResponseUtil is V2ResponseUtil;

_cmdletPassedIn.WriteDebug("In FindHelper::SearchByNames()");
foreach (string pkgName in _pkgsLeftToFind.ToArray())
Expand All @@ -699,6 +700,12 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
{
_cmdletPassedIn.WriteDebug("No version specified, package name is '*'");
// Example: Find-PSResource -Name "*"

// Note: Just for resources from V2 servers, specifically PSGallery, if the resource is unlisted and was requested non-explicitly
// (i.e requested name has wildcard) the resource should not be returned and ResponseUtil.ConvertToPSResourceResult() call needs to be informed of this.
// In all other cases, return the resource regardless of whether it was requested explicitly or not.
bool isResourceRequestedWithWildcard = isV2Resource;

FindResults responses = currentServer.FindAll(_prerelease, _type, out errRecord);
if (errRecord != null)
{
Expand All @@ -714,7 +721,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
continue;
}

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

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

foreach (PSResourceResult currentResult in currentResponseUtil.ConvertToPSResourceResult(responses))
foreach (PSResourceResult currentResult in currentResponseUtil.ConvertToPSResourceResult(responses, isResourceRequestedWithWildcard))
{
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
Expand Down
2 changes: 1 addition & 1 deletion src/code/LocalResponseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public LocalResponseUtil(PSRepositoryInfo repository) : base(repository)
#endregion

#region Overriden Methods
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults)
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults, bool isResourceRequestedWithWildcard = false)
{
foreach (Hashtable response in responseResults.HashtableResponse)
{
Expand Down
2 changes: 1 addition & 1 deletion src/code/NuGetServerResponseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public NuGetServerResponseUtil(PSRepositoryInfo repository) : base(repository)
#endregion

#region Overriden Methods
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults)
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults, bool isResourceRequestedWithWildcard = false)
{
// in FindHelper:
// serverApi.FindName() -> return responses, and out errRecord
Expand Down
2 changes: 1 addition & 1 deletion src/code/ResponseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ResponseUtil(PSRepositoryInfo repository)

#region Methods

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

#endregion

Expand Down
5 changes: 3 additions & 2 deletions src/code/V2ResponseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public V2ResponseUtil(PSRepositoryInfo repository) : base(repository)
#endregion

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

// For V2 resources, specifically PSGallery, return unlisted version resources only when not requested with wildcard name
// Unlisted versions will have a published year as 1900 or earlier.
if (!psGetInfo.PublishedDate.HasValue || psGetInfo.PublishedDate.Value.Year > 1900)
if (!isResourceRequestedWithWildcard || !psGetInfo.PublishedDate.HasValue || psGetInfo.PublishedDate.Value.Year > 1900)
{
yield return new PSResourceResult(returnedObject: psGetInfo, exception: null, isTerminatingError: false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/code/V3ResponseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public V3ResponseUtil(PSRepositoryInfo repository) : base(repository)

#region Overriden Methods

public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults)
public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResults responseResults, bool isResourceRequestedWithWildcard = false)
{
// in FindHelper:
// serverApi.FindName() -> return responses, and out errRecord
Expand Down
37 changes: 32 additions & 5 deletions test/FindPSResourceTests/FindPSResourceV2Server.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'CI' {
BeforeAll{
$PSGalleryName = Get-PSGalleryName
$testModuleName = "test_module"
$testModuleNameWithUnlistedVersion = "test_module10"
$testScriptName = "test_script"
$commandName = "Get-TargetResource"
$dscResourceName = "SystemLocale"
Expand Down Expand Up @@ -413,12 +412,40 @@ Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'CI' {
$err.Count | Should -Be 0
}

It "should not find and write error when finding package version that is unlisted" {
$res = Find-PSResource -Name $testModuleNameWithUnlistedVersion -Version "1.0.0.0" -Repository $PSGalleryName -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -HaveCount 0
$err | Should -HaveCount 1
It "find should not return a module that has all unlisted versions, given full name and no version (i.e non wildcard name)" {
# FindName() scenario
# 'test_completelyunlisted' only has version 0.0.1, which is unlisted
$res = Find-PSResource -Name "test_completelyunlisted" -Repository $PSGalleryName -ErrorVariable err
$res | Should -BeNullOrEmpty
$err.Count | Should -BeGreaterThan 0
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
}

It "find should return a module version even if all versions are unlisted, given full name and version (i.e non wildcard name)" {
# FindVersion() scenario
# test_completelyunlisted has 1 version, 0.0.1, which is unlisted
$res = Find-PSResource -Name "test_completelyunlisted" -Version "0.0.1" -Repository $PSGalleryName
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "0.0.1"
}

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)" {
# FindVersion scenario
# 'test_unlisted' version 0.0.3 is unlisted
$res = Find-PSResource -Name "test_unlisted" -Version "0.0.3" -Repository $PSGalleryName
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be "0.0.3"
}

It "find should not return an unlisted module with it was requested with wildcards in the name" {
# FindNameGlobbing() scenario
# 'test_completelyunlisted' has all unlisted versions -> should not be returned
# whereas 'test_unlisted' has a listed verison and 'test_notunlisted' has all listed versions -> should be returned
$res = Find-PSResource -Name "test_*unlisted" -Repository $PSGalleryName
$res.Count | Should -Be 2
$res.Name | Should -Contain 'test_unlisted'
$res.Name | Should -Contain 'test_notunlisted'
}
}

Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'ManualValidationOnly' {
Expand Down
11 changes: 11 additions & 0 deletions test/InstallPSResourceTests/InstallPSResourceV2Server.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,17 @@ Describe 'Test Install-PSResource for V2 Server scenarios' -tags 'CI' {
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be $version
}

It "Install resource that is unlisted" {
# InstallVersion scenario
# 'test_unlisted' version 0.0.3 is unlisted
$moduleName = 'test_unlisted'
$version = '0.0.3'
Install-PSResource -Name $moduleName -Version $version -Repository $PSGalleryName -TrustRepository
$res = Get-InstalledPSResource $moduleName
$res | Should -Not -BeNullOrEmpty
$res.Version | Should -Be $version
}
}

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