Skip to content

Commit

Permalink
Fix broken tests, add NPM scoped test. (microsoft#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
scovetta authored Nov 17, 2020
1 parent b4a8481 commit 6ea4e27
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/core/sdk:3.1
FROM mcr.microsoft.com/dotnet/sdk:5.0
COPY . /app/
WORKDIR /app/src

Expand Down
8 changes: 6 additions & 2 deletions src/Shared/PackageManagers/NPMProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;
using Version = SemVer.Version;
Expand Down Expand Up @@ -184,7 +185,7 @@ protected async override Task<Dictionary<PackageURL, double>> PackageMetadataSea
string metadata)
{
var mapping = new Dictionary<PackageURL, double>();
if (purl?.Name is string purlName && (purlName.StartsWith('_') || npm_internal_modules.Contains(purlName)))
if (purl?.Name is string purlName && (purlName.StartsWith('_') || NODEJS_INTERNAL_MODULES.Contains(purlName)))
{
// url = 'https://github.com/nodejs/node/tree/master/lib' + package.name,

Expand Down Expand Up @@ -233,7 +234,10 @@ protected async override Task<Dictionary<PackageURL, double>> PackageMetadataSea
return mapping;
}

private static readonly List<string> npm_internal_modules = new List<string>()
/// <summary>
/// Internal Node.js modules that should be ignored when searching metadata.
/// </summary>
private static readonly List<string> NODEJS_INTERNAL_MODULES = new List<string>()
{
"assert",
"async_hooks",
Expand Down
1 change: 1 addition & 0 deletions src/Shared/PackageManagers/VSMProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public override async Task<IEnumerable<string>> EnumerateVersions(PackageURL pur
SetCache(packageName, resultStreamReader.ReadToEnd());
resultStream.Seek(0, SeekOrigin.Begin);
}

var doc = await JsonDocument.ParseAsync(resultStream);
await resultStream.DisposeAsync();

Expand Down
17 changes: 11 additions & 6 deletions src/oss-tests/DownloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ public async Task NPM_Download_Version_Succeeds(string purl, string targetFilena
await TestDownload(purl, targetFilename, expectedDirectoryCount);
}

[DataTestMethod]
[DataRow("pkg:npm/%40angular%[email protected]", "package.json", 1)]
public async Task NPM_Download_ScopedVersion_Succeeds(string purl, string targetFilename, int expectedDirectoryCount)
{
await TestDownload(purl, targetFilename, expectedDirectoryCount);
}

[DataTestMethod]
[DataRow("pkg:nuget/[email protected]", "RandomType.nuspec", 1)]
[DataRow("pkg:nuget/d3.TypeScript.DefinitelyTyped", "d3.TypeScript.DefinitelyTyped.nuspec", 1)]
Expand Down Expand Up @@ -154,7 +161,7 @@ await Assert.ThrowsExceptionAsync<InternalTestFailureException>(async () =>
}

[DataTestMethod]
[DataRow("pkg:vsm/ms-vscode/Theme-1337", "extension.vsixmanifest", 1)]
[DataRow("pkg:vsm/ms-vscode/PowerShell", "extension.vsixmanifest", 1)]
public async Task VSM_Download_Version_Succeeds(string purl, string targetFilename, int expectedDirectoryCount)
{
await TestDownload(purl, targetFilename, expectedDirectoryCount);
Expand Down Expand Up @@ -192,7 +199,7 @@ private void deleteTempDirs(PackageDownloader? packageDownloader, string tempDir
/// <param name="packageUrl"> </param>
/// <param name="tempDirectoryName"> </param>
/// <returns> </returns>
private async Task<PackageDownloader?> DownloadPackage(PackageURL packageUrl, string tempDirectoryName, bool doCache = false)
private PackageDownloader? DownloadPackage(PackageURL packageUrl, string tempDirectoryName, bool doCache = false)
{
int numAttempts = 3;
int numSecondsWait = 10;
Expand Down Expand Up @@ -231,7 +238,7 @@ private async Task TestDownload(string purl, string targetFilename, int expected
try
{
var packageUrl = new PackageURL(purl);
var packageDownloader = DownloadPackage(packageUrl, tempDirectoryName).Result;
var packageDownloader = DownloadPackage(packageUrl, tempDirectoryName);

var targetFileWasDownloaded = Directory.EnumerateFiles(tempDirectoryName, targetFilename, SearchOption.AllDirectories).Any();
if (!targetFileWasDownloaded)
Expand All @@ -242,18 +249,16 @@ private async Task TestDownload(string purl, string targetFilename, int expected
var topLevelDirectoryCount = Directory.GetDirectories(tempDirectoryName).Length;
if (expectedDirectoryCount != topLevelDirectoryCount)
{
Console.WriteLine(string.Join(";", Directory.GetDirectories(tempDirectoryName)));
errorString = string.Format("Directory count {0} does not match expected {1}", topLevelDirectoryCount, expectedDirectoryCount);
}

// Download again (with caching) - TODO, move this to a separate test.
await DownloadPackage(packageUrl, tempDirectoryName, true);
packageDownloader = DownloadPackage(packageUrl, tempDirectoryName, true);

// Re-calculate the top level directories, since in might have changed (it shouldn't).
topLevelDirectoryCount = Directory.GetDirectories(tempDirectoryName).Length;
if (expectedDirectoryCount != topLevelDirectoryCount)
{
Console.WriteLine(string.Join(";", Directory.GetDirectories(tempDirectoryName)));
errorString = string.Format("Directory count {0} does not match expected {1}", topLevelDirectoryCount, expectedDirectoryCount);
}

Expand Down

0 comments on commit 6ea4e27

Please sign in to comment.