Skip to content

Commit 25d8f49

Browse files
authored
Fix Azure DevOps nuget v2 feed download (#1713)
1 parent e3d0303 commit 25d8f49

File tree

2 files changed

+275
-11
lines changed

2 files changed

+275
-11
lines changed

src/code/V2ServerAPICalls.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public V2ServerAPICalls (PSRepositoryInfo repository, PSCmdlet cmdletPassedIn, N
5656
HttpClientHandler handler = new HttpClientHandler();
5757
bool token = false;
5858

59-
if(networkCredential != null)
59+
if(networkCredential != null)
6060
{
6161
token = String.Equals("token", networkCredential.UserName) ? true : false;
6262
};
@@ -72,7 +72,7 @@ public V2ServerAPICalls (PSRepositoryInfo repository, PSCmdlet cmdletPassedIn, N
7272
} else {
7373

7474
handler.Credentials = networkCredential;
75-
75+
7676
_sessionClient = new HttpClient(handler);
7777
};
7878

@@ -359,7 +359,7 @@ public override FindResults FindName(string packageName, bool includePrerelease,
359359
if (type != ResourceType.None) {
360360
filterBuilder.AddCriterion(GetTypeFilterForRequest(type));
361361
}
362-
362+
363363
var requestUrlV2 = $"{Repository.Uri}/FindPackagesById()?{queryBuilder.BuildQueryString()}";
364364
string response = HttpRequestCall(requestUrlV2, out errRecord);
365365
if (errRecord != null)
@@ -424,7 +424,7 @@ public override FindResults FindNameWithTag(string packageName, string[] tags, b
424424
}
425425

426426
var requestUrlV2 = $"{Repository.Uri}/FindPackagesById()?{queryBuilder.BuildQueryString()}";
427-
427+
428428
string response = HttpRequestCall(requestUrlV2, out errRecord);
429429
if (errRecord != null)
430430
{
@@ -638,7 +638,7 @@ public override FindResults FindVersion(string packageName, string version, Reso
638638
if (!_isJFrogRepo) {
639639
filterBuilder.AddCriterion($"Id eq '{packageName}'");
640640
}
641-
641+
642642
filterBuilder.AddCriterion($"NormalizedVersion eq '{version}'");
643643
if (type != ResourceType.None) {
644644
filterBuilder.AddCriterion(GetTypeFilterForRequest(type));
@@ -694,7 +694,7 @@ public override FindResults FindVersionWithTag(string packageName, string versio
694694
if (!_isJFrogRepo) {
695695
filterBuilder.AddCriterion($"Id eq '{packageName}'");
696696
}
697-
697+
698698
filterBuilder.AddCriterion($"NormalizedVersion eq '{version}'");
699699
if (type != ResourceType.None) {
700700
filterBuilder.AddCriterion(GetTypeFilterForRequest(type));
@@ -943,7 +943,7 @@ private string FindTagFromEndpoint(string[] tags, bool includePrerelease, bool i
943943
}
944944

945945
filterBuilder.AddCriterion($"substringof('PS{(isSearchingModule ? "Module" : "Script")}', Tags) eq true");
946-
946+
947947
foreach (string tag in tags)
948948
{
949949
filterBuilder.AddCriterion($"substringof('{tag}', Tags) eq true");
@@ -987,7 +987,7 @@ private string FindCommandOrDscResource(string[] tags, bool includePrerelease, b
987987
" ",
988988
tags.Select(tag => $"tag:{tagPrefix}{tag}")
989989
) + "'";
990-
990+
991991

992992
var requestUrlV2 = $"{Repository.Uri}/Search()?{queryBuilder.BuildQueryString()}";
993993

@@ -1271,7 +1271,7 @@ private string FindVersionGlobbing(string packageName, VersionRange versionRange
12711271
if (!includePrerelease) {
12721272
filterBuilder.AddCriterion("IsPrerelease eq false");
12731273
}
1274-
1274+
12751275
// We need to explicitly add 'Id eq <packageName>' whenever $filter is used, otherwise arbitrary results are returned.
12761276

12771277
// If it's a JFrog repository do not include the Id filter portion since JFrog uses 'Title' instead of 'Id',
@@ -1283,7 +1283,7 @@ private string FindVersionGlobbing(string packageName, VersionRange versionRange
12831283
if (type == ResourceType.Script) {
12841284
filterBuilder.AddCriterion($"substringof('PS{type.ToString()}', Tags) eq true");
12851285
}
1286-
1286+
12871287

12881288
var requestUrlV2 = $"{Repository.Uri}/FindPackagesById()?{queryBuilder.BuildQueryString()}";
12891289

@@ -1306,7 +1306,7 @@ private Stream InstallVersion(string packageName, string version, out ErrorRecor
13061306
if (_isADORepo)
13071307
{
13081308
// eg: https://pkgs.dev.azure.com/<org>/<project>/_packaging/<feed>/nuget/v2?id=test_module&version=5.0.0
1309-
requestUrlV2 = $"{Repository.Uri}?id={packageName}&version={version}";
1309+
requestUrlV2 = $"{Repository.Uri}?id={packageName.ToLower()}&version={version}";
13101310
}
13111311
else if (_isJFrogRepo)
13121312
{
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
$ProgressPreference = "SilentlyContinue"
5+
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
6+
Import-Module $modPath -Force -Verbose
7+
8+
Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' {
9+
10+
BeforeAll {
11+
$testModuleName = "test_local_mod"
12+
$testModuleName2 = "test_local_mod2"
13+
$testScriptName = "test_ado_script"
14+
$ADORepoName = "PSGetTestingPublicFeed"
15+
$ADORepoUri = "https://pkgs.dev.azure.com/powershell/PowerShell/_packaging/psresourceget-public-test-ci/nuget/v2"
16+
Get-NewPSResourceRepositoryFile
17+
Register-PSResourceRepository -Name $ADORepoName -Uri $ADORepoUri
18+
}
19+
20+
AfterEach {
21+
Uninstall-PSResource $testModuleName, $testModuleName2, $testScriptName -Version "*" -SkipDependencyCheck -ErrorAction SilentlyContinue
22+
}
23+
24+
AfterAll {
25+
Get-RevertPSResourceRepositoryFile
26+
}
27+
28+
$testCases = @{Name="*"; ErrorId="NameContainsWildcard"},
29+
@{Name="Test_local_m*"; ErrorId="NameContainsWildcard"},
30+
@{Name="Test?local","Test[local"; ErrorId="ErrorFilteringNamesForUnsupportedWildcards"}
31+
32+
It "Should not install resource with wildcard in name" -TestCases $testCases {
33+
param($Name, $ErrorId)
34+
Install-PSResource -Name $Name -Repository $ADORepoName -ErrorVariable err -ErrorAction SilentlyContinue
35+
$err.Count | Should -BeGreaterThan 0
36+
$err[0].FullyQualifiedErrorId | Should -BeExactly "$ErrorId,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
37+
$res = Get-InstalledPSResource $testModuleName
38+
$res | Should -BeNullOrEmpty
39+
}
40+
41+
It "Install specific module resource by name" {
42+
Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository
43+
$pkg = Get-InstalledPSResource $testModuleName
44+
$pkg.Name | Should -Be $testModuleName
45+
$pkg.Version | Should -Be "5.0.0"
46+
}
47+
48+
It "Install specific script resource by name" {
49+
Install-PSResource -Name $testScriptName -Repository $ADORepoName -TrustRepository
50+
$pkg = Get-InstalledPSResource $testScriptName
51+
$pkg.Name | Should -Be $testScriptName
52+
$pkg.Version | Should -Be "1.0.0"
53+
}
54+
55+
It "Install multiple resources by name" {
56+
$pkgNames = @($testModuleName, $testModuleName2)
57+
Install-PSResource -Name $pkgNames -Repository $ADORepoName -TrustRepository
58+
$pkg = Get-InstalledPSResource $pkgNames
59+
$pkg.Name | Should -Be $pkgNames
60+
}
61+
62+
It "Should not install resource given nonexistant name" {
63+
Install-PSResource -Name "NonExistantModule" -Repository $ADORepoName -TrustRepository -ErrorVariable err -ErrorAction SilentlyContinue
64+
$pkg = Get-InstalledPSResource "NonExistantModule"
65+
$pkg | Should -BeNullOrEmpty
66+
$err.Count | Should -BeGreaterThan 0
67+
$err[0].FullyQualifiedErrorId | Should -BeExactly "InstallPackageFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
68+
}
69+
70+
# Do some version testing, but Find-PSResource should be doing thorough testing
71+
It "Should install resource given name and exact version" {
72+
Install-PSResource -Name $testModuleName -Version "1.0.0" -Repository $ADORepoName -TrustRepository
73+
$pkg = Get-InstalledPSResource $testModuleName
74+
$pkg.Name | Should -Be $testModuleName
75+
$pkg.Version | Should -Be "1.0.0"
76+
}
77+
78+
It "Should install resource given name and exact version with bracket syntax" {
79+
Install-PSResource -Name $testModuleName -Version "[1.0.0]" -Repository $ADORepoName -TrustRepository
80+
$pkg = Get-InstalledPSResource $testModuleName
81+
$pkg.Name | Should -Be $testModuleName
82+
$pkg.Version | Should -Be "1.0.0"
83+
}
84+
85+
It "Should install resource given name and exact range inclusive [1.0.0, 5.0.0]" {
86+
Install-PSResource -Name $testModuleName -Version "[1.0.0, 5.0.0]" -Repository $ADORepoName -TrustRepository
87+
$pkg = Get-InstalledPSResource $testModuleName
88+
$pkg.Name | Should -Be $testModuleName
89+
$pkg.Version | Should -Be "5.0.0"
90+
}
91+
92+
It "Should install resource given name and exact range exclusive (1.0.0, 5.0.0)" {
93+
Install-PSResource -Name $testModuleName -Version "(1.0.0, 5.0.0)" -Repository $ADORepoName -TrustRepository
94+
$pkg = Get-InstalledPSResource $testModuleName
95+
$pkg.Name | Should -Be $testModuleName
96+
$pkg.Version | Should -Be "3.0.0"
97+
}
98+
99+
# TODO: Update this test and others like it that use try/catch blocks instead of Should -Throw
100+
It "Should not install resource with incorrectly formatted version such as exclusive version (1.0.0.0)" {
101+
$Version = "(1.0.0.0)"
102+
try {
103+
Install-PSResource -Name $testModuleName -Version $Version -Repository $ADORepoName -TrustRepository -ErrorAction SilentlyContinue
104+
}
105+
catch
106+
{}
107+
$Error[0].FullyQualifiedErrorId | Should -be "IncorrectVersionFormat,Microsoft.PowerShell.PSResourceGet.Cmdlets.InstallPSResource"
108+
109+
$res = Get-InstalledPSResource $testModuleName
110+
$res | Should -BeNullOrEmpty
111+
}
112+
113+
It "Install resource when given Name, Version '*', should install the latest version" {
114+
Install-PSResource -Name $testModuleName -Version "*" -Repository $ADORepoName -TrustRepository
115+
$pkg = Get-InstalledPSResource $testModuleName
116+
$pkg.Name | Should -Be $testModuleName
117+
$pkg.Version | Should -Be "5.0.0"
118+
}
119+
120+
It "Install resource with latest (including prerelease) version given Prerelease parameter" {
121+
Install-PSResource -Name $testModuleName -Prerelease -Repository $ADORepoName -TrustRepository
122+
$pkg = Get-InstalledPSResource $testModuleName
123+
$pkg.Name | Should -Be $testModuleName
124+
$pkg.Version | Should -Be "5.2.5"
125+
$pkg.Prerelease | Should -Be "alpha001"
126+
}
127+
128+
It "Install resource via InputObject by piping from Find-PSresource" {
129+
Find-PSResource -Name $testModuleName -Repository $ADORepoName | Install-PSResource -TrustRepository
130+
$pkg = Get-InstalledPSResource $testModuleName
131+
$pkg.Name | Should -Be $testModuleName
132+
$pkg.Version | Should -Be "5.0.0"
133+
}
134+
135+
It "Install resource with companyname and repository source location and validate properties" {
136+
Install-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $ADORepoName -TrustRepository
137+
$pkg = Get-InstalledPSResource $testModuleName
138+
$pkg.Version | Should -Be "5.2.5"
139+
$pkg.Prerelease | Should -Be "alpha001"
140+
141+
$pkg.CompanyName | Should -Be "None"
142+
$pkg.RepositorySourceLocation | Should -Be $ADORepoUri
143+
}
144+
145+
# Windows only
146+
It "Install resource under CurrentUser scope - Windows only" -Skip:(!(Get-IsWindows)) {
147+
Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository -Scope CurrentUser
148+
$pkg = Get-InstalledPSResource $testModuleName
149+
$pkg.Name | Should -Be $testModuleName
150+
$pkg.InstalledLocation.ToString().Contains("Documents") | Should -Be $true
151+
}
152+
153+
# Windows only
154+
It "Install resource under AllUsers scope - Windows only" -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) {
155+
Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository -Scope AllUsers -Verbose
156+
$pkg = Get-InstalledPSResource $testModuleName -Scope AllUsers
157+
$pkg.Name | Should -Be $testModuleName
158+
$pkg.InstalledLocation.ToString().Contains("Program Files") | Should -Be $true
159+
}
160+
161+
# Windows only
162+
It "Install resource under no specified scope - Windows only" -Skip:(!(Get-IsWindows)) {
163+
Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository
164+
$pkg = Get-InstalledPSResource $testModuleName
165+
$pkg.Name | Should -Be $testModuleName
166+
$pkg.InstalledLocation.ToString().Contains("Documents") | Should -Be $true
167+
}
168+
169+
# Unix only
170+
# Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules'
171+
It "Install resource under CurrentUser scope - Unix only" -Skip:(Get-IsWindows) {
172+
Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository -Scope CurrentUser
173+
$pkg = Get-InstalledPSResource $testModuleName
174+
$pkg.Name | Should -Be $testModuleName
175+
$pkg.InstalledLocation.ToString().Contains("$env:HOME/.local") | Should -Be $true
176+
}
177+
178+
# Unix only
179+
# Expected path should be similar to: '/home/janelane/.local/share/powershell/Modules'
180+
It "Install resource under no specified scope - Unix only" -Skip:(Get-IsWindows) {
181+
Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository
182+
$pkg = Get-InstalledPSResource $testModuleName
183+
$pkg.Name | Should -Be $testModuleName
184+
$pkg.InstalledLocation.ToString().Contains("$env:HOME/.local") | Should -Be $true
185+
}
186+
187+
It "Should not install resource that is already installed" {
188+
Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository
189+
$pkg = Get-InstalledPSResource $testModuleName
190+
$pkg.Name | Should -Be $testModuleName
191+
Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository -WarningVariable WarningVar -warningaction SilentlyContinue
192+
$WarningVar | Should -Not -BeNullOrEmpty
193+
}
194+
195+
It "Reinstall resource that is already installed with -Reinstall parameter" {
196+
Install-PSResource -Name $testModuleName -Repository $ADORepoName -TrustRepository
197+
$pkg = Get-InstalledPSResource $testModuleName
198+
$pkg.Name | Should -Be $testModuleName
199+
$pkg.Version | Should -Be "5.0.0"
200+
Install-PSResource -Name $testModuleName -Repository $ADORepoName -Reinstall -TrustRepository
201+
$pkg = Get-InstalledPSResource $testModuleName
202+
$pkg.Name | Should -Be $testModuleName
203+
$pkg.Version | Should -Be "5.0.0"
204+
}
205+
206+
It "Install PSResourceInfo object piped in" {
207+
Find-PSResource -Name $testModuleName -Version "1.0.0.0" -Repository $ADORepoName | Install-PSResource -TrustRepository
208+
$res = Get-InstalledPSResource -Name $testModuleName
209+
$res.Name | Should -Be $testModuleName
210+
$res.Version | Should -Be "1.0.0"
211+
}
212+
213+
It "Install module using -PassThru" {
214+
$res = Install-PSResource -Name $testModuleName -Repository $ADORepoName -PassThru -TrustRepository
215+
$res.Name | Should -Contain $testModuleName
216+
}
217+
}
218+
219+
Describe 'Test Install-PSResource for V3Server scenarios' -tags 'ManualValidationOnly' {
220+
221+
BeforeAll {
222+
$testModuleName = "TestModule"
223+
$testModuleName2 = "testModuleWithlicense"
224+
Get-NewPSResourceRepositoryFile
225+
Register-LocalRepos
226+
}
227+
228+
AfterEach {
229+
Uninstall-PSResource $testModuleName, $testModuleName2 -SkipDependencyCheck -ErrorAction SilentlyContinue
230+
}
231+
232+
AfterAll {
233+
Get-RevertPSResourceRepositoryFile
234+
}
235+
236+
# Unix only manual test
237+
# Expected path should be similar to: '/usr/local/share/powershell/Modules'
238+
It "Install resource under AllUsers scope - Unix only" -Skip:(Get-IsWindows) {
239+
Install-PSResource -Name $testModuleName -Repository $TestGalleryName -Scope AllUsers
240+
$pkg = Get-Module $testModuleName -ListAvailable
241+
$pkg.Name | Should -Be $testModuleName
242+
$pkg.Path.Contains("/usr/") | Should -Be $true
243+
}
244+
245+
# This needs to be manually tested due to prompt
246+
It "Install resource that requires accept license without -AcceptLicense flag" {
247+
Install-PSResource -Name $testModuleName2 -Repository $TestGalleryName
248+
$pkg = Get-InstalledPSResource $testModuleName2
249+
$pkg.Name | Should -Be $testModuleName2
250+
$pkg.Version | Should -Be "0.0.1.0"
251+
}
252+
253+
# This needs to be manually tested due to prompt
254+
It "Install resource should prompt 'trust repository' if repository is not trusted" {
255+
Set-PSResourceRepository PoshTestGallery -Trusted:$false
256+
257+
Install-PSResource -Name $testModuleName -Repository $TestGalleryName -confirm:$false
258+
259+
$pkg = Get-Module $testModuleName -ListAvailable
260+
$pkg.Name | Should -Be $testModuleName
261+
262+
Set-PSResourceRepository PoshTestGallery -Trusted
263+
}
264+
}

0 commit comments

Comments
 (0)