Skip to content

Commit

Permalink
Fix wildcard version NuGet downloads. (microsoft#332)
Browse files Browse the repository at this point in the history
* Fix wildcard version NuGet downloads.

* Added unit tests for wildcard version downloads for NPM and NuGet.

* Fixed another bug with the filePath in NuGetPackageActions.DownloadAsync.
  • Loading branch information
jpinz authored Apr 20, 2022
1 parent e7457e2 commit 06c4497
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Shared/PackageActions/NuGetPackageActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class NuGetPackageActions : IManagerPackageActions<NuGetPackageVersionMet
FindPackageByIdResource resource = await _sourceRepository.GetResourceAsync<FindPackageByIdResource>();

// Construct the path for the nupkg file.
string filePath = Path.ChangeExtension(Path.Join(topLevelDirectory, targetPath), Path.GetExtension(targetPath) + ".nupkg");
string filePath = Path.Join(topLevelDirectory, targetPath + ".nupkg");

// Create a new FileStream to populate with the contents of the .nupkg from CopyNupkgToStreamAsync.
int bufferSize = 4096;
Expand Down Expand Up @@ -67,7 +67,7 @@ public class NuGetPackageActions : IManagerPackageActions<NuGetPackageVersionMet
// If we want to extract the contents of the .nupkg, send it to ArchiveHelper.ExtractArchiveAsync.
if (doExtract)
{
return await ArchiveHelper.ExtractArchiveAsync(topLevelDirectory, Path.GetExtension(targetPath) + ".nupkg", packageStream, cached);
return await ArchiveHelper.ExtractArchiveAsync(topLevelDirectory, targetPath + ".nupkg", packageStream, cached);
}

return filePath;
Expand Down
9 changes: 7 additions & 2 deletions src/Shared/PackageManagers/TypedManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ public override async Task<IEnumerable<string>> DownloadVersionAsync(PackageURL

if (containingPath is string notNullPath)
{
return Directory.EnumerateFiles(notNullPath, "*",
new EnumerationOptions() {RecurseSubdirectories = true});
if (doExtract)
{
return Directory.EnumerateFiles(notNullPath, "*",
new EnumerationOptions() {RecurseSubdirectories = true});
}

return new[] { containingPath };
}

return Array.Empty<string>();
Expand Down
13 changes: 13 additions & 0 deletions src/oss-tests/DownloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Microsoft.CST.OpenSource.Tests
{
using PackageManagers;
using PackageUrl;
using System.Collections.Generic;

[TestClass]
public class DownloadTests
Expand Down Expand Up @@ -140,6 +141,18 @@ public async Task NuGet_Download_Version_Succeeds(string purl, string targetFile
{
await TestDownload(purl, targetFilename, expectedDirectoryCount);
}

[DataTestMethod]
[DataRow("pkg:npm/moment@*", "package.json")]
[DataRow("pkg:nuget/RandomType@*", "RandomType.nuspec")]
[DataRow("pkg:nuget/Newtonsoft.Json@*", "newtonsoft.json.nuspec")]
public async Task Wildcard_Download_Version_Succeeds(string packageUrl, string targetFilename)
{
PackageURL purl = new(packageUrl);
BaseProjectManager? manager = new ProjectManagerFactory().CreateProjectManager(purl);
IEnumerable<string> versions = await manager?.EnumerateVersionsAsync(purl) ?? throw new InvalidOperationException();
await TestDownload(packageUrl, targetFilename, versions.Count());
}

[DataTestMethod]
[DataRow(null, null, 1)]
Expand Down

0 comments on commit 06c4497

Please sign in to comment.