Skip to content

Commit 5c4116f

Browse files
authored
Fix tests (#1727)
1 parent 3cae2ae commit 5c4116f

File tree

8 files changed

+138
-13
lines changed

8 files changed

+138
-13
lines changed

src/code/LocalServerApiCalls.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,10 @@ private Hashtable GetMetadataFromNupkg(string packageName, string packagePath, s
649649
_cmdletPassedIn.WriteDebug($"Extracting '{zipFilePath}' to '{tempDiscoveryPath}'");
650650
System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, tempDiscoveryPath);
651651

652-
string psd1FilePath = Path.Combine(tempDiscoveryPath, $"{packageName}.psd1");
653-
string ps1FilePath = Path.Combine(tempDiscoveryPath, $"{packageName}.ps1");
654-
string nuspecFilePath = Path.Combine(tempDiscoveryPath, $"{packageName}.nuspec");
652+
string psd1FilePath = String.Empty;
653+
string ps1FilePath = String.Empty;
654+
string nuspecFilePath = String.Empty;
655+
Utils.GetMetadataFilesFromPath(tempDiscoveryPath, packageName, out psd1FilePath, out ps1FilePath, out nuspecFilePath);
655656

656657
List<string> pkgTags = new List<string>();
657658

src/code/Utils.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Net;
21
// Copyright (c) Microsoft Corporation. All rights reserved.
32
// Licensed under the MIT License.
43

@@ -15,13 +14,15 @@
1514
using System.Runtime.InteropServices;
1615
using Microsoft.PowerShell.Commands;
1716
using Microsoft.PowerShell.PSResourceGet.Cmdlets;
17+
using System.Net;
1818
using System.Net.Http;
1919
using System.Globalization;
2020
using System.Security;
2121
using Azure.Core;
2222
using Azure.Identity;
23-
using System.Threading.Tasks;
23+
using System.Text.RegularExpressions;
2424
using System.Threading;
25+
using System.Threading.Tasks;
2526

2627
namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
2728
{
@@ -1171,6 +1172,34 @@ internal static HashSet<string> GetInstalledPackages(List<string> pathsToSearch,
11711172
return pkgsInstalledOnMachine;
11721173
}
11731174

1175+
internal static void GetMetadataFilesFromPath(string dirPath, string packageName, out string psd1FilePath, out string ps1FilePath, out string nuspecFilePath)
1176+
{
1177+
psd1FilePath = String.Empty;
1178+
ps1FilePath = String.Empty;
1179+
nuspecFilePath = String.Empty;
1180+
1181+
var discoveredFiles = Directory.GetFiles(dirPath, "*.*", SearchOption.AllDirectories);
1182+
string pkgNamePattern = $"{packageName}*";
1183+
Regex rgx = new(pkgNamePattern, RegexOptions.IgnoreCase);
1184+
foreach (var file in discoveredFiles)
1185+
{
1186+
if (rgx.IsMatch(file))
1187+
{
1188+
if (file.EndsWith("psd1"))
1189+
{
1190+
psd1FilePath = file;
1191+
}
1192+
else if (file.EndsWith("nuspec"))
1193+
{
1194+
nuspecFilePath = file;
1195+
}
1196+
else if (file.EndsWith("ps1"))
1197+
{
1198+
ps1FilePath = file;
1199+
}
1200+
}
1201+
}
1202+
}
11741203
#endregion
11751204

11761205
#region PSDataFile parsing

src/code/V2ResponseUtil.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Licensed under the MIT License.
33

44
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
5+
using NuGet.Versioning;
56
using System;
67
using System.Collections.Generic;
8+
using System.Linq;
79
using System.Xml;
810

911
namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
@@ -70,6 +72,10 @@ public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResu
7072
#region V2 Specific Methods
7173

7274
public XmlNode[] ConvertResponseToXML(string httpResponse) {
75+
NuGetVersion emptyVersion = new NuGetVersion("0.0.0.0");
76+
NuGetVersion firstVersion = emptyVersion;
77+
NuGetVersion lastVersion = emptyVersion;
78+
bool shouldFixOrder = true;
7379

7480
//Create the XmlDocument.
7581
XmlDocument doc = new XmlDocument();
@@ -80,7 +86,50 @@ public XmlNode[] ConvertResponseToXML(string httpResponse) {
8086
XmlNode[] nodes = new XmlNode[entryNode.Count];
8187
for (int i = 0; i < entryNode.Count; i++)
8288
{
83-
nodes[i] = entryNode[i];
89+
XmlNode node = entryNode[i];
90+
nodes[i] = node;
91+
var entryChildNodes = node.ChildNodes;
92+
foreach (XmlElement childNode in entryChildNodes)
93+
{
94+
var entryKey = childNode.LocalName;
95+
if (entryKey.Equals("properties"))
96+
{
97+
var propertyChildNodes = childNode.ChildNodes;
98+
foreach (XmlElement propertyChild in propertyChildNodes)
99+
{
100+
var propertyKey = propertyChild.LocalName;
101+
var propertyValue = propertyChild.InnerText;
102+
if (propertyKey.Equals("NormalizedVersion"))
103+
{
104+
if (!NuGetVersion.TryParse(propertyValue, out NuGetVersion parsedNormalizedVersion))
105+
{
106+
// if a version couldn't be parsed, keep ordering as is.
107+
shouldFixOrder = false;
108+
}
109+
110+
if (i == 0)
111+
{
112+
firstVersion = parsedNormalizedVersion;
113+
}
114+
else
115+
{
116+
// later version element
117+
lastVersion = parsedNormalizedVersion;
118+
}
119+
120+
break; // don't care about rest of the childNode's properties
121+
}
122+
}
123+
124+
break; // don't care about rest of the childNode's keys
125+
}
126+
}
127+
}
128+
129+
// order the array in descending order if not already.
130+
if (shouldFixOrder && firstVersion.CompareTo(lastVersion) < 0)
131+
{
132+
nodes = nodes.Reverse().ToArray();
84133
}
85134

86135
return nodes;

src/code/V2ServerAPICalls.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,17 @@ public override FindResults FindName(string packageName, bool includePrerelease,
364364
string response = HttpRequestCall(requestUrlV2, out errRecord);
365365
if (errRecord != null)
366366
{
367+
// usually this is for errors in calling the V2 server, but for ADO V2 this error will include package not found errors which we want to deliver in a standard message
368+
if (_isADORepo && errRecord.Exception is ResourceNotFoundException)
369+
{
370+
errRecord = new ErrorRecord(
371+
new ResourceNotFoundException($"Package with name '{packageName}' could not be found in repository '{Repository.Name}'. For ADO feed, if the package is in an upstream feed make sure you are authenticated to the upstream feed.", errRecord.Exception),
372+
"PackageNotFound",
373+
ErrorCategory.ObjectNotFound,
374+
this);
375+
response = string.Empty;
376+
}
377+
367378
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
368379
}
369380

@@ -648,6 +659,17 @@ public override FindResults FindVersion(string packageName, string version, Reso
648659
string response = HttpRequestCall(requestUrlV2, out errRecord);
649660
if (errRecord != null)
650661
{
662+
// usually this is for errors in calling the V2 server, but for ADO V2 this error will include package not found errors which we want to deliver with a standard message
663+
if (_isADORepo && errRecord.Exception is ResourceNotFoundException)
664+
{
665+
errRecord = new ErrorRecord(
666+
new ResourceNotFoundException($"Package with name '{packageName}' and version '{version}' could not be found in repository '{Repository.Name}'. For ADO feed, if the package is in an upstream feed make sure you are authenticated to the upstream feed.", errRecord.Exception),
667+
"PackageNotFound",
668+
ErrorCategory.ObjectNotFound,
669+
this);
670+
response = string.Empty;
671+
}
672+
651673
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
652674
}
653675

test/FindPSResourceTests/FindPSResourceADOV2Server.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Describe 'Test HTTP Find-PSResource for ADO V2 Server Protocol' -tags 'CI' {
3131
$res = Find-PSResource -Name NonExistantModule -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
3232
$res | Should -BeNullOrEmpty
3333
$err.Count | Should -BeGreaterThan 0
34-
$err[0].FullyQualifiedErrorId | Should -BeExactly "ResourceNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
34+
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
3535
$res | Should -BeNullOrEmpty
3636
}
3737

test/InstallPSResourceTests/InstallPSResourceADOV2Server.Tests.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,14 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'CI' {
132132
$pkg.Version | Should -Be "5.0.0"
133133
}
134134

135-
It "Install resource with companyname and repository source location and validate properties" {
135+
It "Install resource with author and repository source location and validate properties" {
136+
# CompanyName is not present in ADO V2 feed response properties.
136137
Install-PSResource -Name $testModuleName -Version "5.2.5-alpha001" -Repository $ADORepoName -TrustRepository
137138
$pkg = Get-InstalledPSResource $testModuleName
138139
$pkg.Version | Should -Be "5.2.5"
139140
$pkg.Prerelease | Should -Be "alpha001"
140141

141-
$pkg.CompanyName | Should -Be "None"
142+
$pkg.Author | Should -Be "None"
142143
$pkg.RepositorySourceLocation | Should -Be $ADORepoUri
143144
}
144145

test/InstallPSResourceTests/InstallPSResourceLocal.Tests.ps1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
1616
BeforeAll {
1717
$localRepo = "psgettestlocal"
1818
$localUNCRepo = "psgettestlocal3"
19-
$localNupkgRepo = "LocalNupkgRepo"
20-
$localNupkgRepoUri = "test\testFiles\testNupkgs"
19+
$localNupkgRepo = "localNupkgRepo"
2120
$testModuleName = "test_local_mod"
2221
$testModuleName2 = "test_local_mod2"
2322
$testModuleClobber = "testModuleClobber"
2423
$testModuleClobber2 = "testModuleClobber2"
2524
Get-NewPSResourceRepositoryFile
2625
Register-LocalRepos
27-
Register-PSResourceRepository -Name $localNupkgRepo -SourceLocation $localNupkgRepoUri
26+
Register-LocalTestNupkgsRepo
2827

2928
$prereleaseLabel = "alpha001"
3029
$tags = @()
@@ -290,7 +289,11 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
290289
It "Install .nupkg that contains directories (specific package throws errors when accessed by ZipFile.OpenRead)" {
291290
$nupkgName = "Microsoft.Web.Webview2"
292291
$nupkgVersion = "1.0.2792.45"
293-
Install-PSResource -Name $nupkgName -Version $nupkgVersion -Repository $localNupkgRepo -TrustRepository
292+
$repoPath = Get-PSResourceRepository $localNupkgRepo
293+
Write-Verbose -Verbose "repoPath $($repoPath.Uri)"
294+
$searchPkg = Find-PSResource -Name $nupkgName -Version $nupkgVersion -Repository $localNupkgRepo
295+
Write-Verbose -Verbose "search name: $($searchPkg.Name)"
296+
Install-PSResource -Name $nupkgName -Version $nupkgVersion -Repository $localNupkgRepo -TrustRepository -Verbose
294297
$pkg = Get-InstalledPSResource $nupkgName
295298
$pkg.Name | Should -Be $nupkgName
296299
$pkg.Version | Should -Be $nupkgVersion

test/PSGetTestUtils.psm1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,26 @@ function Register-LocalRepos {
273273
Write-Verbose "registered psgettestlocal, psgettestlocal2, psgettestlocal3, psgettestlocal4"
274274
}
275275

276+
function Register-LocalTestNupkgsRepo {
277+
# Path to folder, within our test folder, where we store special case modules, scripts and nupkgs used for testing
278+
$testDir = (get-item $psscriptroot).FullName
279+
$testFilesFolderPath = Join-Path $testDir -ChildPath "testFiles"
280+
281+
# Path to specifically to that invalid test nupkgs folder
282+
$testNupkgsFolderPath = Join-Path $testFilesFolderPath -ChildPath "testNupkgs"
283+
Write-Verbose -Verbose "testNupkgsFolderPath: $testNupkgsFolderPath"
284+
285+
$repoUriAddress = $testNupkgsFolderPath
286+
$localRepoParams = @{
287+
Name = "localNupkgRepo"
288+
Uri = $repoUriAddress
289+
Priority = 70
290+
Trusted = $false
291+
}
292+
293+
Register-PSResourceRepository @localRepoParams
294+
}
295+
276296
function Register-PSGallery {
277297
$PSGalleryRepoParams = @{
278298
Name = $script:PSGalleryName

0 commit comments

Comments
 (0)