forked from microsoft/OSSGadget
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cargo package existence and package version existence logic update. (m…
…icrosoft#345) * Start the process of updating the logic for CargoProjectManager to use the github repo for the cargo index vs their API. Still todo: the contents of the file isn't valid json. * Update cargo to use the suggested solution from crates.io for crawling. The crates.io api always returns 200 even if a package/version doesn't exist, so it didn't work as expected currently anyways. * Switch JsonParsingOption.Unknown to None. And switched the nullable jsonParsingOption to use None instead of it being nullable.
- Loading branch information
Showing
7 changed files
with
258 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Microsoft Corporation. Licensed under the MIT License. | ||
|
||
namespace Microsoft.CST.OpenSource.Model.Enums; | ||
|
||
/// <summary> | ||
/// Special cases used for json parsing. | ||
/// </summary> | ||
public enum JsonParsingOption | ||
{ | ||
/// <summary> | ||
/// No specific option. | ||
/// </summary> | ||
None = 0, | ||
|
||
/// <summary> | ||
/// Used in Cargo only right now as their files aren't formatted correctly for json. | ||
/// <example>https://raw.githubusercontent.com/rust-lang/crates.io-index/master/ra/nd/rand</example> | ||
/// </summary> | ||
NotInArrayNotCsv, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/oss-tests/ProjectManagerTests/CargoProjectManagerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) Microsoft Corporation. Licensed under the MIT License. | ||
|
||
namespace Microsoft.CST.OpenSource.Tests.ProjectManagerTests | ||
{ | ||
using Moq; | ||
using oss; | ||
using PackageManagers; | ||
using PackageUrl; | ||
using RichardSzalay.MockHttp; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using VisualStudio.TestTools.UnitTesting; | ||
|
||
[TestClass] | ||
public class CargoProjectManagerTests | ||
{ | ||
private readonly IDictionary<string, string> _packages = new Dictionary<string, string>() | ||
{ | ||
{ "https://raw.githubusercontent.com/rust-lang/crates.io-index/master/ra/nd/rand", Resources.cargo_rand }, | ||
}.ToImmutableDictionary(); | ||
|
||
private readonly CargoProjectManager _projectManager; | ||
private readonly IHttpClientFactory _httpFactory; | ||
|
||
public CargoProjectManagerTests() | ||
{ | ||
Mock<IHttpClientFactory> mockFactory = new(); | ||
|
||
MockHttpMessageHandler mockHttp = new(); | ||
|
||
foreach ((string url, string json) in _packages) | ||
{ | ||
MockHttpFetchResponse(HttpStatusCode.OK, url, json, mockHttp); | ||
} | ||
|
||
mockFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(mockHttp.ToHttpClient()); | ||
_httpFactory = mockFactory.Object; | ||
|
||
_projectManager = new CargoProjectManager(_httpFactory, "."); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow("pkg:cargo/[email protected]", 68, "0.8.5")] | ||
public async Task EnumerateVersionsSucceeds(string purlString, int count, string latestVersion) | ||
{ | ||
PackageURL purl = new(purlString); | ||
List<string> versions = (await _projectManager.EnumerateVersionsAsync(purl, useCache: false)).ToList(); | ||
|
||
Assert.AreEqual(count, versions.Count); | ||
Assert.AreEqual(latestVersion, versions.First()); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow("pkg:cargo/[email protected]")] | ||
public async Task PackageVersionExistsAsyncSucceeds(string purlString) | ||
{ | ||
PackageURL purl = new(purlString); | ||
|
||
Assert.IsTrue(await _projectManager.PackageVersionExistsAsync(purl, useCache: false)); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow("pkg:cargo/[email protected]")] | ||
public async Task PackageVersionDoesntExistsAsyncSucceeds(string purlString) | ||
{ | ||
PackageURL purl = new(purlString); | ||
|
||
Assert.IsFalse(await _projectManager.PackageVersionExistsAsync(purl, useCache: false)); | ||
} | ||
|
||
private static void MockHttpFetchResponse( | ||
HttpStatusCode statusCode, | ||
string url, | ||
string content, | ||
MockHttpMessageHandler httpMock) | ||
{ | ||
httpMock | ||
.When(HttpMethod.Get, url) | ||
.Respond(statusCode, "application/json", content); | ||
|
||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.