Skip to content

Commit 50bdd3e

Browse files
committed
Add tests for Find (local), Install (V2,V3,local) (PowerShell#1011)
1 parent 07158f3 commit 50bdd3e

17 files changed

+2286
-137
lines changed

src/code/FindHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public IEnumerable<PSResourceInfo> FindByResourceName(
9595
}
9696

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

99100
// Error out if repository array of names to be searched contains wildcards.
100101
if (repository != null)
@@ -975,7 +976,7 @@ private IEnumerable<PSResourceInfo> FindFromPackageSourceSearchAPI(
975976
List<IPackageSearchMetadata> wildcardPkgs;
976977
try
977978
{
978-
string wildcardPkgName = pkgName.Equals("*") ? string.Empty : pkgName;
979+
string wildcardPkgName = string.Empty;
979980
// SearchAsync() API returns the latest version only for all packages that match the wild-card name
980981
wildcardPkgs = pkgSearchResource.SearchAsync(
981982
searchTerm: wildcardPkgName,

src/code/InstallHelper.cs

Lines changed: 198 additions & 107 deletions
Large diffs are not rendered by default.

src/code/PSGetException.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ public SpecifiedTagsNotFoundException(string message)
3737
{
3838
}
3939
}
40+
41+
public class InvalidOrEmptyResponse : Exception
42+
{
43+
public InvalidOrEmptyResponse(string message)
44+
: base (message)
45+
{
46+
}
47+
}
4048
}

src/code/Utils.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ public static bool TryParseVersionOrVersionRange(
239239
return true;
240240
}
241241

242-
// parse as Version range
243242
return VersionRange.TryParse(version, out versionRange);
244243
}
245244

src/code/V3ServerAPICalls.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ public override string FindName(string packageName, bool includePrerelease, Reso
234234
}
235235
}
236236

237+
if (String.IsNullOrEmpty(response))
238+
{
239+
edi = ExceptionDispatchInfo.Capture(new InvalidOrEmptyResponse($"FindName() with {packageName} returned empty response."));
240+
}
241+
237242
return response;
238243
}
239244

@@ -293,6 +298,11 @@ public override string FindNameWithTag(string packageName, string[] tags, bool i
293298
}
294299
}
295300

301+
if (String.IsNullOrEmpty(response))
302+
{
303+
edi = ExceptionDispatchInfo.Capture(new InvalidOrEmptyResponse($"FindNameWithTag() with {packageName} and tags {String.Join(",", tags)} returned empty response."));
304+
}
305+
296306
return response;
297307
}
298308

@@ -610,6 +620,11 @@ public override string FindVersion(string packageName, string version, ResourceT
610620
return String.Empty;
611621
}
612622

623+
if (String.IsNullOrEmpty(response))
624+
{
625+
edi = ExceptionDispatchInfo.Capture(new InvalidOrEmptyResponse($"FindVersion() with {packageName} and version {version} returned empty response."));
626+
}
627+
613628
return response;
614629
}
615630

@@ -642,6 +657,11 @@ public override string FindVersionWithTag(string packageName, string version, st
642657
return String.Empty;
643658
}
644659

660+
if (String.IsNullOrEmpty(response))
661+
{
662+
edi = ExceptionDispatchInfo.Capture(new InvalidOrEmptyResponse($"FindVersion() with {packageName}, tags {String.Join(", ", tags)} and version {version} returned empty response."));
663+
}
664+
645665
return response;
646666
}
647667

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
Import-Module "$((Get-Item $psscriptroot).parent)\PSGetTestUtils.psm1" -Force
5+
6+
Describe 'Test HTTP Find-PSResource for Module' {
7+
8+
BeforeAll{
9+
$localRepo = "psgettestlocal"
10+
$testModuleName = "test_local_mod"
11+
$testModuleName2 = "test_local_mod2"
12+
$commandName = "cmd1"
13+
$dscResourceName = "dsc1"
14+
$cmdName = "PSCommand_$commandName"
15+
$dscName = "PSDscResource_$dscResourceName"
16+
$prereleaseLabel = ""
17+
Get-NewPSResourceRepositoryFile
18+
Register-LocalRepos
19+
20+
$tags = @("Test", "Tag2", $cmdName, $dscName)
21+
Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName $localRepo "1.0.0"
22+
Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName $localRepo "3.0.0"
23+
Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName $localRepo "5.0.0" $prereleaseLabel $tags
24+
Get-ModuleResourcePublishedToLocalRepoTestDrive $testModuleName2 $localRepo "5.0.0" $prereleaseLabel $tags
25+
26+
$prereleaseLabel = "alpha001"
27+
$params = @{
28+
moduleName = $testModuleName
29+
repoName = $localRepo
30+
packageVersion = "5.2.5"
31+
prereleaseLabel = $prereleaseLabel
32+
tags = $tags
33+
}
34+
Get-ModuleResourcePublishedToLocalRepoTestDrive @params
35+
}
36+
37+
AfterAll {
38+
Get-RevertPSResourceRepositoryFile
39+
}
40+
41+
It "find resource given specific Name, Version null" {
42+
# FindName()
43+
$res = Find-PSResource -Name $testModuleName -Repository $localRepo
44+
$res.Name | Should -Be $testModuleName
45+
$res.Version | Should -Be "5.0.0.0"
46+
}
47+
48+
It "should not find resource given nonexistant Name" {
49+
$res = Find-PSResource -Name NonExistantModule -Repository $localRepo
50+
$res | Should -BeNullOrEmpty
51+
}
52+
53+
# It "find resource(s) given wildcard Name" {
54+
# # FindNameGlobbing
55+
# $res = Find-PSResource -Name "test_local_*" -Repository $localRepo
56+
# $res.Count | Should -BeGreaterThan 1
57+
# }
58+
59+
$testCases2 = @{Version="[5.0.0.0]"; ExpectedVersions=@("5.0.0.0"); Reason="validate version, exact match"},
60+
@{Version="5.0.0.0"; ExpectedVersions=@("5.0.0.0"); Reason="validate version, exact match without bracket syntax"},
61+
@{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"},
62+
@{Version="(1.0.0.0, 5.0.0.0)"; ExpectedVersions=@("3.0.0.0"); Reason="validate version, exact range exclusive"},
63+
@{Version="(1.0.0.0,)"; ExpectedVersions=@("3.0.0.0", "5.0.0.0"); Reason="validate version, minimum version exclusive"},
64+
@{Version="[1.0.0.0,)"; ExpectedVersions=@("1.0.0.0", "3.0.0.0", "5.0.0.0"); Reason="validate version, minimum version inclusive"},
65+
@{Version="(,3.0.0.0)"; ExpectedVersions=@("1.0.0.0"); Reason="validate version, maximum version exclusive"},
66+
@{Version="(,3.0.0.0]"; ExpectedVersions=@("1.0.0.0", "3.0.0.0"); Reason="validate version, maximum version inclusive"},
67+
@{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"}
68+
@{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"}
69+
70+
It "find resource when given Name to <Reason> <Version>" -TestCases $testCases2{
71+
# FindVersionGlobbing()
72+
param($Version, $ExpectedVersions)
73+
$res = Find-PSResource -Name $testModuleName -Version $Version -Repository $localRepo
74+
foreach ($item in $res) {
75+
$item.Name | Should -Be $testModuleName
76+
$ExpectedVersions | Should -Contain $item.Version
77+
}
78+
}
79+
80+
It "find all versions of resource when given specific Name, Version not null --> '*'" {
81+
# FindVersionGlobbing()
82+
$res = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo
83+
$res | ForEach-Object {
84+
$_.Name | Should -Be $testModuleName
85+
}
86+
87+
$res.Count | Should -BeGreaterOrEqual 1
88+
}
89+
90+
It "find resource with latest (including prerelease) version given Prerelease parameter" {
91+
# FindName()
92+
# test_module resource's latest version is a prerelease version, before that it has a non-prerelease version
93+
$res = Find-PSResource -Name $testModuleName -Repository $localRepo
94+
$res.Version | Should -Be "5.0.0.0"
95+
96+
$resPrerelease = Find-PSResource -Name $testModuleName -Prerelease -Repository $localRepo
97+
$resPrerelease.Version | Should -Be "5.2.5.0"
98+
$resPrerelease.Prerelease | Should -Be "alpha001"
99+
}
100+
101+
It "find resources, including Prerelease version resources, when given Prerelease parameter" {
102+
# FindVersionGlobbing()
103+
$resWithoutPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo
104+
$resWithPrerelease = Find-PSResource -Name $testModuleName -Version "*" -Repository $localRepo
105+
$resWithPrerelease.Count | Should -BeGreaterOrEqual $resWithoutPrerelease.Count
106+
}
107+
108+
It "find resource that satisfies given Name and Tag property (single tag)" {
109+
# FindNameWithTag()
110+
$requiredTag = "test"
111+
$res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $localRepo
112+
$res.Name | Should -Be $testModuleName
113+
$res.Tags | Should -Contain $requiredTag
114+
}
115+
116+
It "should not find resource if Name and Tag are not both satisfied (single tag)" {
117+
# FindNameWithTag
118+
$requiredTag = "Windows" # tag "windows" is not present for test_module package
119+
$res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $localRepo
120+
$res | Should -BeNullOrEmpty
121+
}
122+
123+
It "find resource that satisfies given Name and Tag property (multiple tags)" {
124+
# FindNameWithTag()
125+
$requiredTags = @("test", "Tag2")
126+
$res = Find-PSResource -Name $testModuleName -Tag $requiredTags -Repository $localRepo
127+
$res.Name | Should -Be $testModuleName
128+
$res.Tags | Should -Contain $requiredTags[0]
129+
$res.Tags | Should -Contain $requiredTags[1]
130+
}
131+
132+
It "find all resources that satisfy Name pattern and have specified Tag (single tag)" {
133+
# FindNameGlobbingWithTag()
134+
$requiredTag = "test"
135+
$nameWithWildcard = "test_local_mod*"
136+
$res = Find-PSResource -Name $nameWithWildcard -Tag $requiredTag -Repository $localRepo
137+
$res.Count | Should -BeGreaterThan 1
138+
foreach ($pkg in $res)
139+
{
140+
$pkg.Name | Should -BeLike $nameWithWildcard
141+
$pkg.Tags | Should -Contain $requiredTag
142+
}
143+
}
144+
145+
It "should not find resources if both Name pattern and Tags are not satisfied (single tag)" {
146+
# FindNameGlobbingWithTag()
147+
$requiredTag = "windows" # tag "windows" is not present for test_module package
148+
$res = Find-PSResource -Name "test_module*" -Tag $requiredTag -Repository $localRepo
149+
$res | Should -BeNullOrEmpty
150+
}
151+
152+
It "find all resources that satisfy Name pattern and have specified Tag (multiple tags)" {
153+
# FindNameGlobbingWithTag()
154+
$requiredTags = @("test", "Tag2")
155+
$nameWithWildcard = "test_local_mod*"
156+
$res = Find-PSResource -Name $nameWithWildcard -Tag $requiredTags -Repository $localRepo
157+
$res.Count | Should -BeGreaterThan 1
158+
foreach ($pkg in $res)
159+
{
160+
$pkg.Name | Should -BeLike $nameWithWildcard
161+
$pkg.Tags | Should -Contain $requiredTags[0]
162+
$pkg.Tags | Should -Contain $requiredTags[1]
163+
}
164+
}
165+
166+
It "find resource that satisfies given Name, Version and Tag property (single tag)" {
167+
# FindVersionWithTag()
168+
$requiredTag = "test"
169+
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTag -Repository $localRepo
170+
$res.Name | Should -Be $testModuleName
171+
$res.Version | Should -Be "5.0.0.0"
172+
$res.Tags | Should -Contain $requiredTag
173+
}
174+
175+
It "should not find resource if Name, Version and Tag property are not all satisfied (single tag)" {
176+
# FindVersionWithTag()
177+
$requiredTag = "windows" # tag "windows" is not present for test_module package
178+
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTag -Repository $localRepo
179+
$res | Should -BeNullOrEmpty
180+
}
181+
182+
It "find resource that satisfies given Name, Version and Tag property (multiple tags)" {
183+
# FindVersionWithTag()
184+
$requiredTags = @("test", "Tag2")
185+
$res = Find-PSResource -Name $testModuleName -Version "5.0.0.0" -Tag $requiredTags -Repository $localRepo
186+
$res.Name | Should -Be $testModuleName
187+
$res.Version | Should -Be "5.0.0.0"
188+
$res.Tags | Should -Contain $requiredTags[0]
189+
$res.Tags | Should -Contain $requiredTags[1]
190+
191+
}
192+
193+
It "find resource given CommandName" {
194+
$res = Find-PSResource -CommandName $commandName -Repository $localRepo
195+
foreach ($item in $res) {
196+
$item.Names | Should -Be $commandName
197+
$item.ParentResource.Includes.Command | Should -Contain $commandName
198+
}
199+
}
200+
201+
It "find resource given DscResourceName" {
202+
$res = Find-PSResource -DscResourceName $dscResourceName -Repository $localRepo
203+
foreach ($item in $res) {
204+
$item.Names | Should -Be $dscResourceName
205+
$item.ParentResource.Includes.DscResource | Should -Contain $dscResourceName
206+
}
207+
}
208+
}

test/FindPSResourceV2Server.Tests.ps1 renamed to test/FindPSResourceTests/FindPSResourceV2Server.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

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

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

test/FindPSResourceV3Server.Tests.ps1 renamed to test/FindPSResourceTests/FindPSResourceV3Server.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

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

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

0 commit comments

Comments
 (0)