-
Notifications
You must be signed in to change notification settings - Fork 126
Introduce C++ SDK component type #1599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -64,4 +64,7 @@ public enum ComponentType : byte | |
|
|
||
| [EnumMember] | ||
| DotNet = 19, | ||
|
|
||
| [EnumMember] | ||
| CppSdk = 20, | ||
| } | ||
55 changes: 55 additions & 0 deletions
55
src/Microsoft.ComponentDetection.Contracts/TypedComponent/CppSdkComponent.cs
This file contains hidden or 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,55 @@ | ||
| #nullable disable | ||
| namespace Microsoft.ComponentDetection.Contracts.TypedComponent; | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Text.Json.Serialization; | ||
| using PackageUrl; | ||
|
|
||
| /// <summary> | ||
| /// Represents a C++ SDK component. | ||
| /// </summary> | ||
| public class CppSdkComponent : TypedComponent | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="CppSdkComponent"/> class. | ||
| /// </summary> | ||
| public CppSdkComponent() | ||
| { | ||
| /* Reserved for deserialization */ | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="CppSdkComponent"/> class. | ||
| /// </summary> | ||
| /// <param name="name">The name of the component.</param> | ||
| /// <param name="version">The version of the component.</param> | ||
| public CppSdkComponent(string name, string version) | ||
| { | ||
| this.Name = this.ValidateRequiredInput(name, nameof(this.Name), nameof(ComponentType.CppSdk)); | ||
| this.Version = this.ValidateRequiredInput(version, nameof(this.Version), nameof(ComponentType.CppSdk)); | ||
| } | ||
|
|
||
| [JsonPropertyName("name")] | ||
| public string Name { get; set; } | ||
|
|
||
| [JsonPropertyName("version")] | ||
| public string Version { get; set; } | ||
|
|
||
| [JsonIgnore] | ||
| public override ComponentType Type => ComponentType.CppSdk; | ||
|
|
||
| [JsonPropertyName("packageUrl")] | ||
| public override PackageURL PackageUrl | ||
| { | ||
| get | ||
| { | ||
| var qualifiers = new SortedDictionary<string, string> | ||
| { | ||
| { "type", "cppsdk" }, | ||
| }; | ||
| return new PackageURL("generic", null, this.Name, this.Version, qualifiers, null); | ||
| } | ||
| } | ||
|
|
||
| protected override string ComputeId() => $"{this.Name} {this.Version} - {this.Type}"; | ||
| } | ||
This file contains hidden or 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
88 changes: 88 additions & 0 deletions
88
test/Microsoft.ComponentDetection.Contracts.Tests/CppSdkComponentTests.cs
This file contains hidden or 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,88 @@ | ||
| #nullable disable | ||
| namespace Microsoft.ComponentDetection.Contracts.Tests; | ||
|
|
||
| using System; | ||
| using AwesomeAssertions; | ||
| using Microsoft.ComponentDetection.Contracts.TypedComponent; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [TestClass] | ||
| [TestCategory("Governance/All")] | ||
| [TestCategory("Governance/ComponentDetection")] | ||
| public class CppSdkComponentTests | ||
| { | ||
| [TestMethod] | ||
| public void Constructor_ShouldInitializeProperties() | ||
| { | ||
| var name = "AwesomeCppLibrary"; | ||
| var version = "1.2.3"; | ||
|
|
||
| var component = new CppSdkComponent(name, version); | ||
|
|
||
| component.Name.Should().Be(name); | ||
| component.Version.Should().Be(version); | ||
| component.Type.Should().Be(ComponentType.CppSdk); | ||
| component.Id.Should().Be($"{name} {version} - {component.Type}"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Constructor_ShouldThrowException_WhenNameIsNull() | ||
| { | ||
| Action action = () => new CppSdkComponent(null, "1.2.3"); | ||
| action.Should().Throw<ArgumentException>().WithMessage("*Name*"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Constructor_ShouldThrowException_WhenNameIsWhitespace() | ||
| { | ||
| Action action = () => new CppSdkComponent(" ", "1.2.3"); | ||
| action.Should().Throw<ArgumentException>().WithMessage("*Name*"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Constructor_ShouldThrowException_WhenNameIsEmpty() | ||
| { | ||
| Action action = () => new CppSdkComponent(string.Empty, "1.2.3"); | ||
| action.Should().Throw<ArgumentException>().WithMessage("*Name*"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Constructor_ShouldThrowException_WhenVersionIsNull() | ||
| { | ||
| Action action = () => new CppSdkComponent("AwesomeCppLibrary", null); | ||
| action.Should().Throw<ArgumentException>().WithMessage("*Version*"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Constructor_ShouldThrowException_WhenVersionIsWhitespace() | ||
| { | ||
| Action action = () => new CppSdkComponent("AwesomeCppLibrary", " "); | ||
| action.Should().Throw<ArgumentException>().WithMessage("*Version*"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Constructor_ShouldThrowException_WhenVersionIsEmpty() | ||
| { | ||
| Action action = () => new CppSdkComponent("AwesomeCppLibrary", string.Empty); | ||
| action.Should().Throw<ArgumentException>().WithMessage("*Version*"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void PackageUrl_ShouldReturnCorrectFormat() | ||
| { | ||
| var name = "AwesomeCppLibrary"; | ||
| var version = "1.2.3"; | ||
| var component = new CppSdkComponent(name, version); | ||
|
|
||
| var packageUrl = component.PackageUrl; | ||
|
|
||
| packageUrl.Type.Should().Be("generic"); | ||
| #pragma warning disable CA1308 // PackageURL normalizes to lowercase | ||
| packageUrl.Name.Should().Be(name.ToLowerInvariant()); | ||
| #pragma warning restore CA1308 | ||
| packageUrl.Version.Should().Be(version); | ||
| packageUrl.Namespace.Should().BeNull(); | ||
| packageUrl.Qualifiers.Should().ContainKey("type"); | ||
| packageUrl.Qualifiers["type"].Should().Be("cppsdk"); | ||
| } | ||
|
grvillic marked this conversation as resolved.
|
||
| } | ||
|
grvillic marked this conversation as resolved.
|
||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.