Skip to content

Commit

Permalink
Fixes microsoft#93 (clean up download unit test). (microsoft#95)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Scovetta <[email protected]>
  • Loading branch information
scovetta and Michael Scovetta authored May 31, 2020
1 parent b710f11 commit 45c9443
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/tests/DownloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public async Task Ubuntu_Download_Version_Succeeds(string purl, string targetFil
}

[DataTestMethod]
[DataRow("pkg:vsm/ms-vscode/PowerShell", "extension.vsixmanifest", 8)]
[DataRow("pkg:vsm/ms-vscode/PowerShell", "extension.vsixmanifest", 1)]
public async Task VSM_Download_Version_Succeeds(string purl, string targetFilename, int expectedCount)
{
await TestDownload(purl, targetFilename, expectedCount);
Expand All @@ -127,7 +127,7 @@ await Assert.ThrowsExceptionAsync<FormatException>(async () =>
}

[DataTestMethod]
[DataRow("pkg:blah/blah", null, 1)]
[DataRow("pkg:invalid/invalid", "", 1)]
public async Task Invalid_Package_Test_Download(string purl, string targetFilename, int expectedCount)
{
await Assert.ThrowsExceptionAsync<ArgumentException>(async () =>
Expand All @@ -137,7 +137,7 @@ await Assert.ThrowsExceptionAsync<ArgumentException>(async () =>
}

[DataTestMethod]
[DataRow("ckg:blah/blah", null, 1)]
[DataRow("ckg:invalid@invalid@invalid", "", 0)]
public async Task Invalid_Purl_Test_Download(string purl, string targetFilename, int expectedCount)
{
await Assert.ThrowsExceptionAsync<FormatException>(async () =>
Expand All @@ -156,24 +156,34 @@ private async Task TestDownload(string purl, string targetFilename, int expected
}

var packageUrl = new PackageURL(purl);
Assert.IsNotNull(packageUrl);


Directory.CreateDirectory(tempDirectoryName);
var downloadTool = new DownloadTool(); // do common initialization
PackageDownloader packageDownloader1 = await DownloadPackage(packageUrl, tempDirectoryName);
string dir = Directory.GetCurrentDirectory();
bool fileMatchPresent = Directory.EnumerateFiles(tempDirectoryName, targetFilename, SearchOption.AllDirectories).Any();
Assert.IsTrue(fileMatchPresent);

Assert.AreEqual(expectedCount, Directory.GetDirectories(tempDirectoryName).Count());
var downloadTool = new DownloadTool();
var packageDownloader = await DownloadPackage(packageUrl, tempDirectoryName);
var wereFilesDownloaded = Directory.EnumerateFiles(tempDirectoryName, targetFilename, SearchOption.AllDirectories).Any();
if (!wereFilesDownloaded)
{
throw new InternalTestFailureException("No files were downloaded.");
}

if (expectedCount != Directory.GetDirectories(tempDirectoryName).Count())
{
throw new InternalTestFailureException(string.Format("Directory count {0} does not match expected {1}",
Directory.GetDirectories(tempDirectoryName).Count(), expectedCount));
}

// do that again with caching, this should not do anything since the cache already has the package
await DownloadPackage(packageUrl, tempDirectoryName, true);

Assert.AreEqual(expectedCount, Directory.GetDirectories(tempDirectoryName).Count());
if (expectedCount != Directory.GetDirectories(tempDirectoryName).Count())
{
throw new InternalTestFailureException(string.Format("Directory count {0} does not match expected {1}",
Directory.GetDirectories(tempDirectoryName).Count(), expectedCount));
}

// one delete is enough, since its only a single cached copy
deleteTempDirs(packageDownloader1, tempDirectoryName);
deleteTempDirs(packageDownloader, tempDirectoryName);
}

/// <summary>
Expand All @@ -195,6 +205,10 @@ private async Task<PackageDownloader> DownloadPackage(PackageURL packageUrl, str
packageDownloader.DownloadPackageLocalCopy(packageUrl, false, true).Wait();
break;
}
catch(ArgumentException)
{
throw;
}
catch (Exception)
{
Thread.Sleep(numSecondsWait * 1000);
Expand Down

0 comments on commit 45c9443

Please sign in to comment.