Skip to content

Commit

Permalink
Add a method to assert that a package version exists. (microsoft#344)
Browse files Browse the repository at this point in the history
* Added a PackageVersionExistsAsync method to the BaseProjectManager.
  • Loading branch information
jpinz authored Aug 5, 2022
1 parent 9f1c906 commit 071a442
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Shared/PackageManagers/BaseProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Microsoft.CST.OpenSource.PackageManagers
{
using Contracts;
using Helpers;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.CST.OpenSource.Model;
using System;
Expand All @@ -15,7 +15,6 @@ namespace Microsoft.CST.OpenSource.PackageManagers
using Utilities;
using Version = SemanticVersioning.Version;
using PackageUrl;
using System.Net;

public abstract class BaseProjectManager
{
Expand Down Expand Up @@ -320,6 +319,30 @@ public virtual async Task<bool> PackageExistsAsync(PackageURL purl, bool useCach
return (await EnumerateVersionsAsync(purl, useCache)).Any();
}

/// <summary>
/// Check if the package version exists in the repository.
/// </summary>
/// <param name="purl">The PackageURL to check, requires a version.</param>
/// <param name="useCache">If the cache should be checked for the existence of this package.</param>
/// <returns>True if the package version is confirmed to exist in the repository. False otherwise.</returns>
public virtual async Task<bool> PackageVersionExistsAsync(PackageURL purl, bool useCache = true)
{
Logger.Trace("PackageExists {0}", purl?.ToString());
if (purl is null)
{
Logger.Trace("Provided PackageURL was null.");
return false;
}

if(purl.Version.IsBlank())
{
Logger.Trace("Provided PackageURL version was null or blank.");
return false;
}

return (await EnumerateVersionsAsync(purl, useCache)).Contains(purl.Version);
}

/// <summary>
/// Static overload for getting the latest version.
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions src/oss-tests/ProjectManagerTests/NPMProjectManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ public async Task EnumerateVersionsSucceeds(string purlString, int count, string
Assert.AreEqual(count, versions.Count);
Assert.AreEqual(latestVersion, versions.First());
}

[DataTestMethod]
[DataRow("pkg:npm/[email protected]")]
[DataRow("pkg:npm/%40angular/[email protected]")]
[DataRow("pkg:npm/[email protected]")]
[DataRow("pkg:npm/[email protected]")]
[DataRow("pkg:npm/[email protected]")]
[DataRow("pkg:npm/[email protected]")]
public async Task PackageVersionExistsAsyncSucceeds(string purlString)
{
PackageURL purl = new(purlString);

Assert.IsTrue(await _projectManager.PackageVersionExistsAsync(purl, useCache: false));
}

[DataTestMethod]
[DataRow("pkg:npm/[email protected]")]
[DataRow("pkg:npm/%40angular/[email protected]")]
public async Task PackageVersionDoesntExistsAsyncSucceeds(string purlString)
{
PackageURL purl = new(purlString);

Assert.IsFalse(await _projectManager.PackageVersionExistsAsync(purl, useCache: false));
}

[DataTestMethod]
[DataRow("pkg:npm/[email protected]", "2019-07-19T02:28:46.584Z")]
Expand Down

0 comments on commit 071a442

Please sign in to comment.