Skip to content

Add tests for Find (local), Install (V2,V3,local) #1011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 16, 2023
3 changes: 2 additions & 1 deletion src/code/FindHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public IEnumerable<PSResourceInfo> FindByResourceName(
}

_pkgsLeftToFind = new List<string>(name);
_tagsLeftToFind = tag == null ? new List<string>() : new List<string>(tag);

// Error out if repository array of names to be searched contains wildcards.
if (repository != null)
Expand Down Expand Up @@ -975,7 +976,7 @@ private IEnumerable<PSResourceInfo> FindFromPackageSourceSearchAPI(
List<IPackageSearchMetadata> wildcardPkgs;
try
{
string wildcardPkgName = pkgName.Equals("*") ? string.Empty : pkgName;
string wildcardPkgName = string.Empty;
// SearchAsync() API returns the latest version only for all packages that match the wild-card name
wildcardPkgs = pkgSearchResource.SearchAsync(
searchTerm: wildcardPkgName,
Expand Down
298 changes: 189 additions & 109 deletions src/code/InstallHelper.cs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/code/PSGetException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ public SpecifiedTagsNotFoundException(string message)
{
}
}

public class InvalidOrEmptyResponse : Exception
{
public InvalidOrEmptyResponse(string message)
: base (message)
{
}
}
}
1 change: 0 additions & 1 deletion src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ public static bool TryParseVersionOrVersionRange(
return true;
}

// parse as Version range
return VersionRange.TryParse(version, out versionRange);
}

Expand Down
20 changes: 20 additions & 0 deletions src/code/V3ServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ public override string FindName(string packageName, bool includePrerelease, Reso
}
}

if (String.IsNullOrEmpty(response))
{
edi = ExceptionDispatchInfo.Capture(new InvalidOrEmptyResponse($"FindName() with {packageName} returned empty response."));
}

return response;
}

Expand Down Expand Up @@ -293,6 +298,11 @@ public override string FindNameWithTag(string packageName, string[] tags, bool i
}
}

if (String.IsNullOrEmpty(response))
{
edi = ExceptionDispatchInfo.Capture(new InvalidOrEmptyResponse($"FindNameWithTag() with {packageName} and tags {String.Join(",", tags)} returned empty response."));
}

return response;
}

Expand Down Expand Up @@ -610,6 +620,11 @@ public override string FindVersion(string packageName, string version, ResourceT
return String.Empty;
}

if (String.IsNullOrEmpty(response))
{
edi = ExceptionDispatchInfo.Capture(new InvalidOrEmptyResponse($"FindVersion() with {packageName} and version {version} returned empty response."));
}

return response;
}

Expand Down Expand Up @@ -642,6 +657,11 @@ public override string FindVersionWithTag(string packageName, string version, st
return String.Empty;
}

if (String.IsNullOrEmpty(response))
{
edi = ExceptionDispatchInfo.Capture(new InvalidOrEmptyResponse($"FindVersion() with {packageName}, tags {String.Join(", ", tags)} and version {version} returned empty response."));
}

return response;
}

Expand Down
208 changes: 208 additions & 0 deletions test/FindPSResourceTests/FindPSResourceLocal.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Import-Module "$((Get-Item $psscriptroot).parent)\PSGetTestUtils.psm1" -Force

Describe 'Test HTTP Find-PSResource for Module' {

BeforeAll{
$localRepo = "psgettestlocal"
$testModuleName = "test_local_mod"
$testModuleName2 = "test_local_mod2"
$commandName = "cmd1"
$dscResourceName = "dsc1"
$cmdName = "PSCommand_$commandName"
$dscName = "PSDscResource_$dscResourceName"
$prereleaseLabel = ""
Get-NewPSResourceRepositoryFile
Register-LocalRepos

$tags = @("Test", "Tag2", $cmdName, $dscName)
Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName $localRepo "1.0.0"
Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName $localRepo "3.0.0"
Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName $localRepo "5.0.0" $prereleaseLabel $tags
Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName2 $localRepo "5.0.0" $prereleaseLabel $tags

$prereleaseLabel = "alpha001"
$params = @{
moduleName = $testModuleName
repoName = $localRepo
packageVersion = "5.2.5"
prereleaseLabel = $prereleaseLabel
tags = $tags
}
Get-ModuleResourcePublishedToLocalRepoTestDrive @params
}

AfterAll {
Get-RevertPSResourceRepositoryFile
}

It "find resource given specific Name, Version null" {
# FindName()
$res = Find-PSResource -Name $testModuleName -Repository $localRepo
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0.0"
}

It "should not find resource given nonexistant Name" {
$res = Find-PSResource -Name NonExistantModule -Repository $localRepo
$res | Should -BeNullOrEmpty
}

# It "find resource(s) given wildcard Name" {
# # FindNameGlobbing
# $res = Find-PSResource -Name "test_local_*" -Repository $localRepo
# $res.Count | Should -BeGreaterThan 1
# }

$testCases2 = @{Version="[5.0.0.0]"; ExpectedVersions=@("5.0.0.0"); Reason="validate version, exact match"},
@{Version="5.0.0.0"; ExpectedVersions=@("5.0.0.0"); Reason="validate version, exact match without bracket syntax"},
@{Version="[1.0.0.0, 5.0.0.0]"; ExpectedVersions=@("1.0.0.0", "3.0.0.0", "5.0.0.0"); Reason="validate version, exact range inclusive"},
@{Version="(1.0.0.0, 5.0.0.0)"; ExpectedVersions=@("3.0.0.0"); Reason="validate version, exact range exclusive"},
@{Version="(1.0.0.0,)"; ExpectedVersions=@("3.0.0.0", "5.0.0.0"); Reason="validate version, minimum version exclusive"},
@{Version="[1.0.0.0,)"; ExpectedVersions=@("1.0.0.0", "3.0.0.0", "5.0.0.0"); Reason="validate version, minimum version inclusive"},
@{Version="(,3.0.0.0)"; ExpectedVersions=@("1.0.0.0"); Reason="validate version, maximum version exclusive"},
@{Version="(,3.0.0.0]"; ExpectedVersions=@("1.0.0.0", "3.0.0.0"); Reason="validate version, maximum version inclusive"},
@{Version="[1.0.0.0, 5.0.0.0)"; ExpectedVersions=@("1.0.0.0", "3.0.0.0"); Reason="validate version, mixed inclusive minimum and exclusive maximum version"}
@{Version="(1.0.0.0, 5.0.0.0]"; ExpectedVersions=@("3.0.0.0", "5.0.0.0"); Reason="validate version, mixed exclusive minimum and inclusive maximum version"}

It "find resource when given Name to <Reason> <Version>" -TestCases $testCases2{
# FindVersionGlobbing()
param($Version, $ExpectedVersions)
$res = Find-PSResource -Name $testModuleName -Version $Version -Repository $localRepo
foreach ($item in $res) {
$item.Name | Should -Be $testModuleName
$ExpectedVersions | Should -Contain $item.Version
}
}

It "find all versions of resource when given specific Name, Version not null --> '*'" {
# FindVersionGlobbing()
$res = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo
$res | ForEach-Object {
$_.Name | Should -Be $testModuleName
}

$res.Count | Should -BeGreaterOrEqual 1
}

It "find resource with latest (including prerelease) version given Prerelease parameter" {
# FindName()
# test_module resource's latest version is a prerelease version, before that it has a non-prerelease version
$res = Find-PSResource -Name $testModuleName -Repository $localRepo
$res.Version | Should -Be "5.0.0.0"

$resPrerelease = Find-PSResource -Name $testModuleName -Prerelease -Repository $localRepo
$resPrerelease.Version | Should -Be "5.2.5.0"
$resPrerelease.Prerelease | Should -Be "alpha001"
}

It "find resources, including Prerelease version resources, when given Prerelease parameter" {
# FindVersionGlobbing()
$resWithoutPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo
$resWithPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo
$resWithPrerelease.Count | Should -BeGreaterOrEqual $resWithoutPrerelease.Count
}

It "find resource that satisfies given Name and Tag property (single tag)" {
# FindNameWithTag()
$requiredTag = "test"
$res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $localRepo
$res.Name | Should -Be $testModuleName
$res.Tags | Should -Contain $requiredTag
}

It "should not find resource if Name and Tag are not both satisfied (single tag)" {
# FindNameWithTag
$requiredTag = "Windows" # tag "windows" is not present for test_module package
$res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $localRepo
$res | Should -BeNullOrEmpty
}

It "find resource that satisfies given Name and Tag property (multiple tags)" {
# FindNameWithTag()
$requiredTags = @("test", "Tag2")
$res = Find-PSResource -Name $testModuleName -Tag $requiredTags -Repository $localRepo
$res.Name | Should -Be $testModuleName
$res.Tags | Should -Contain $requiredTags[0]
$res.Tags | Should -Contain $requiredTags[1]
}

It "find all resources that satisfy Name pattern and have specified Tag (single tag)" {
# FindNameGlobbingWithTag()
$requiredTag = "test"
$nameWithWildcard = "test_local_mod*"
$res = Find-PSResource -Name $nameWithWildcard -Tag $requiredTag -Repository $localRepo
$res.Count | Should -BeGreaterThan 1
foreach ($pkg in $res)
{
$pkg.Name | Should -BeLike $nameWithWildcard
$pkg.Tags | Should -Contain $requiredTag
}
}

It "should not find resources if both Name pattern and Tags are not satisfied (single tag)" {
# FindNameGlobbingWithTag()
$requiredTag = "windows" # tag "windows" is not present for test_module package
$res = Find-PSResource -Name "test_module*" -Tag $requiredTag -Repository $localRepo
$res | Should -BeNullOrEmpty
}

It "find all resources that satisfy Name pattern and have specified Tag (multiple tags)" {
# FindNameGlobbingWithTag()
$requiredTags = @("test", "Tag2")
$nameWithWildcard = "test_local_mod*"
$res = Find-PSResource -Name $nameWithWildcard -Tag $requiredTags -Repository $localRepo
$res.Count | Should -BeGreaterThan 1
foreach ($pkg in $res)
{
$pkg.Name | Should -BeLike $nameWithWildcard
$pkg.Tags | Should -Contain $requiredTags[0]
$pkg.Tags | Should -Contain $requiredTags[1]
}
}

It "find resource that satisfies given Name, Version and Tag property (single tag)" {
# FindVersionWithTag()
$requiredTag = "test"
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTag -Repository $localRepo
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0.0"
$res.Tags | Should -Contain $requiredTag
}

It "should not find resource if Name, Version and Tag property are not all satisfied (single tag)" {
# FindVersionWithTag()
$requiredTag = "windows" # tag "windows" is not present for test_module package
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTag -Repository $localRepo
$res | Should -BeNullOrEmpty
}

It "find resource that satisfies given Name, Version and Tag property (multiple tags)" {
# FindVersionWithTag()
$requiredTags = @("test", "Tag2")
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTags -Repository $localRepo
$res.Name | Should -Be $testModuleName
$res.Version | Should -Be "5.0.0.0"
$res.Tags | Should -Contain $requiredTags[0]
$res.Tags | Should -Contain $requiredTags[1]

}

It "find resource given CommandName" {
$res = Find-PSResource -CommandName $commandName -Repository $localRepo
foreach ($item in $res) {
$item.Names | Should -Be $commandName
$item.ParentResource.Includes.Command | Should -Contain $commandName
}
}

It "find resource given DscResourceName" {
$res = Find-PSResource -DscResourceName $dscResourceName -Repository $localRepo
foreach ($item in $res) {
$item.Names | Should -Be $dscResourceName
$item.ParentResource.Includes.DscResource | Should -Contain $dscResourceName
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force
Import-Module "$((Get-Item $psscriptroot).parent)\PSGetTestUtils.psm1" -Force

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force
Import-Module "$((Get-Item $psscriptroot).parent)\PSGetTestUtils.psm1" -Force

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

Expand Down
Loading