Skip to content

Commit

Permalink
Add FluentAssertions.Analyzers to Detectors.Tests, fix warnings (#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
KonH authored Oct 27, 2023
1 parent 0724ca1 commit 53b4059
Show file tree
Hide file tree
Showing 36 changed files with 711 additions and 708 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Microsoft.ComponentDetection.Detectors.Tests;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.ComponentDetection.Detectors.Poetry;
Expand Down Expand Up @@ -87,7 +88,7 @@ public async Task CondaComponentDetector_TestCondaLockFileAsync()

var detectedComponents = componentRecorder.GetDetectedComponents();

Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);

// packages from the conda section
this.AssertCondaLockComponentNameAndVersion(detectedComponents, "conda-lock", "2.1.0");
Expand All @@ -97,26 +98,24 @@ public async Task CondaComponentDetector_TestCondaLockFileAsync()
this.AssertPipComponentNameAndVersion(detectedComponents, "certifi", "2023.5.7");
this.AssertPipComponentNameAndVersion(detectedComponents, "requests", "2.31.0");

Assert.AreEqual(4, detectedComponents.Count());
detectedComponents.Should().HaveCount(4);
}

private void AssertCondaLockComponentNameAndVersion(IEnumerable<DetectedComponent> detectedComponents, string name, string version)
{
Assert.IsNotNull(
detectedComponents.SingleOrDefault(c =>
detectedComponents.SingleOrDefault(c =>
c.Component is CondaComponent component &&
component.Name.Equals(name) &&
component.Version.Equals(version)),
component.Version.Equals(version)).Should().NotBeNull(
$"Component with name {name} and version {version} was not found");
}

private void AssertPipComponentNameAndVersion(IEnumerable<DetectedComponent> detectedComponents, string name, string version)
{
Assert.IsNotNull(
detectedComponents.SingleOrDefault(c =>
detectedComponents.SingleOrDefault(c =>
c.Component is PipComponent component &&
component.Name.Equals(name) &&
component.Version.Equals(version)),
component.Version.Equals(version)).Should().NotBeNull(
$"Component with name {name} and version {version} was not found");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ public async Task TestGoModDetectorWithValidFile_ReturnsSuccessfullyAsync()
.WithFile("go.mod", goMod)
.ExecuteDetectorAsync();

Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);

var detectedComponents = componentRecorder.GetDetectedComponents();
Assert.AreEqual(4, detectedComponents.Count());
detectedComponents.Should().HaveCount(4);

var discoveredComponents = detectedComponents.ToArray();
discoveredComponents.Where(component => component.Component.Id == "github.com/Azure/azure-pipeline-go v0.2.1 - Go").Count().Should().Be(1);
discoveredComponents.Where(component => component.Component.Id == "github.com/dgrijalva/jwt-go v3.2.0+incompatible - Go").Count().Should().Be(1);
discoveredComponents.Where(component => component.Component.Id == "gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 - Go").Count().Should().Be(1);
discoveredComponents.Where(component => component.Component.Id == "github.com/kr/pretty v0.1.0 - Go").Count().Should().Be(1);
discoveredComponents.Where(component => component.Component.Id == "github.com/Azure/azure-pipeline-go v0.2.1 - Go").Should().ContainSingle();
discoveredComponents.Where(component => component.Component.Id == "github.com/dgrijalva/jwt-go v3.2.0+incompatible - Go").Should().ContainSingle();
discoveredComponents.Where(component => component.Component.Id == "gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 - Go").Should().ContainSingle();
discoveredComponents.Where(component => component.Component.Id == "github.com/kr/pretty v0.1.0 - Go").Should().ContainSingle();
}

[TestMethod]
Expand All @@ -79,23 +79,23 @@ public async Task TestGoSumDetectorWithValidFile_ReturnsSuccessfullyAsync()
.WithFile("go.sum", goSum)
.ExecuteDetectorAsync();

Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);

var detectedComponents = componentRecorder.GetDetectedComponents();
Assert.AreEqual(6, detectedComponents.Count());
detectedComponents.Should().HaveCount(6);
var typedComponents = detectedComponents.Select(d => d.Component).ToList();
Assert.IsTrue(typedComponents.Contains(
new GoComponent("github.com/golang/mock", "v1.1.1", "h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=")));
Assert.IsTrue(typedComponents.Contains(
new GoComponent("github.com/golang/mock", "v1.2.0", "h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=")));
Assert.IsTrue(typedComponents.Contains(
new GoComponent("github.com/golang/protobuf", "v0.0.0-20161109072736-4bd1920723d7", "h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=")));
Assert.IsTrue(typedComponents.Contains(
new GoComponent("github.com/golang/protobuf", "v1.2.0", "h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=")));
Assert.IsTrue(typedComponents.Contains(
new GoComponent("github.com/golang/protobuf", "v1.3.1", "h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=")));
Assert.IsTrue(typedComponents.Contains(
new GoComponent("github.com/golang/protobuf", "v1.3.2", "h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=")));
typedComponents.Should().Contain(
new GoComponent("github.com/golang/mock", "v1.1.1", "h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A="));
typedComponents.Should().Contain(
new GoComponent("github.com/golang/mock", "v1.2.0", "h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A="));
typedComponents.Should().Contain(
new GoComponent("github.com/golang/protobuf", "v0.0.0-20161109072736-4bd1920723d7", "h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U="));
typedComponents.Should().Contain(
new GoComponent("github.com/golang/protobuf", "v1.2.0", "h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U="));
typedComponents.Should().Contain(
new GoComponent("github.com/golang/protobuf", "v1.3.1", "h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg="));
typedComponents.Should().Contain(
new GoComponent("github.com/golang/protobuf", "v1.3.2", "h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs="));
}

[TestMethod]
Expand All @@ -115,16 +115,16 @@ public async Task TestGoModDetector_MultipleSpaces_ReturnsSuccessfullyAsync()
.WithFile("go.mod", goMod)
.ExecuteDetectorAsync();

Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);

var detectedComponents = componentRecorder.GetDetectedComponents();
Assert.AreEqual(4, detectedComponents.Count());
detectedComponents.Should().HaveCount(4);

var discoveredComponents = detectedComponents.ToArray();
discoveredComponents.Where(component => component.Component.Id == "github.com/Azure/azure-pipeline-go v0.2.1 - Go").Count().Should().Be(1);
discoveredComponents.Where(component => component.Component.Id == "github.com/dgrijalva/jwt-go v3.2.0+incompatible - Go").Count().Should().Be(1);
discoveredComponents.Where(component => component.Component.Id == "gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 - Go").Count().Should().Be(1);
discoveredComponents.Where(component => component.Component.Id == "github.com/kr/pretty v0.1.0 - Go").Count().Should().Be(1);
discoveredComponents.Where(component => component.Component.Id == "github.com/Azure/azure-pipeline-go v0.2.1 - Go").Should().ContainSingle();
discoveredComponents.Where(component => component.Component.Id == "github.com/dgrijalva/jwt-go v3.2.0+incompatible - Go").Should().ContainSingle();
discoveredComponents.Where(component => component.Component.Id == "gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 - Go").Should().ContainSingle();
discoveredComponents.Where(component => component.Component.Id == "github.com/kr/pretty v0.1.0 - Go").Should().ContainSingle();
}

[TestMethod]
Expand Down Expand Up @@ -154,11 +154,11 @@ public async Task TestGoModDetector_ComponentsWithMultipleLocations_ReturnsSucce
.WithFile("go.mod", goMod2, fileLocation: Path.Join(Path.GetTempPath(), "another-location", "go.mod"))
.ExecuteDetectorAsync();

Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
Assert.AreEqual(4, componentRecorder.GetDetectedComponents().Count());
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
componentRecorder.GetDetectedComponents().Count().Should().Be(4);

var dependencyGraphs = componentRecorder.GetDependencyGraphsByLocation();
Assert.IsTrue(dependencyGraphs.Keys.Count() == 2);
dependencyGraphs.Keys.Should().HaveCount(2);

var firstGraph = dependencyGraphs.Values.First();
var secondGraph = dependencyGraphs.Values.Skip(1).First();
Expand All @@ -179,8 +179,8 @@ four score and seven bugs ago
.WithFile("go.mod", invalidGoMod)
.ExecuteDetectorAsync();

Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
Assert.AreEqual(0, componentRecorder.GetDetectedComponents().Count());
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
componentRecorder.GetDetectedComponents().Count().Should().Be(0);
}

[TestMethod]
Expand Down Expand Up @@ -257,10 +257,10 @@ public async Task TestGoSumDetection_TwoEntriesForTheSameComponent_ReturnsSucces
.WithFile("go.sum", goSum)
.ExecuteDetectorAsync();

Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);

var detectedComponents = componentRecorder.GetDetectedComponents();
Assert.AreEqual(1, detectedComponents.Count());
detectedComponents.Should().ContainSingle();
}

[TestMethod]
Expand All @@ -282,14 +282,14 @@ public async Task TestGoModDetector_DetectorOnlyDetectInsideRequireSectionAsync(
.WithFile("go.mod", goMod)
.ExecuteDetectorAsync();

Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);

var detectedComponents = componentRecorder.GetDetectedComponents();
Assert.AreEqual(2, detectedComponents.Count());
detectedComponents.Should().HaveCount(2);

var discoveredComponents = detectedComponents.ToArray();
discoveredComponents.Where(component => component.Component.Id == "github.com/Azure/azure-pipeline-go v0.2.1 - Go").Count().Should().Be(1);
discoveredComponents.Where(component => component.Component.Id == "github.com/kr/pretty v0.1.0 - Go").Count().Should().Be(1);
discoveredComponents.Where(component => component.Component.Id == "github.com/Azure/azure-pipeline-go v0.2.1 - Go").Should().ContainSingle();
discoveredComponents.Where(component => component.Component.Id == "github.com/kr/pretty v0.1.0 - Go").Should().ContainSingle();
}

[TestMethod]
Expand Down Expand Up @@ -402,15 +402,15 @@ public async Task TestGoDetector_GoGraphHappyPathAsync()
.WithFile("go.mod", string.Empty)
.ExecuteDetectorAsync();

Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);

var detectedComponents = componentRecorder.GetDetectedComponents();
Assert.AreEqual(4, detectedComponents.Count());
detectedComponents.Where(component => component.Component.Id == "other v1.0.0 - Go").Should().HaveCount(0);
detectedComponents.Where(component => component.Component.Id == "other v1.2.0 - Go").Should().HaveCount(1);
detectedComponents.Where(component => component.Component.Id == "some-package v1.2.3 - Go").Should().HaveCount(1);
detectedComponents.Where(component => component.Component.Id == "test v2.0.0 - Go").Should().HaveCount(1);
detectedComponents.Where(component => component.Component.Id == "a v1.5.0 - Go").Should().HaveCount(1);
detectedComponents.Should().HaveCount(4);
detectedComponents.Should().NotContain(component => component.Component.Id == "other v1.0.0 - Go");
detectedComponents.Should().ContainSingle(component => component.Component.Id == "other v1.2.0 - Go");
detectedComponents.Should().ContainSingle(component => component.Component.Id == "some-package v1.2.3 - Go");
detectedComponents.Should().ContainSingle(component => component.Component.Id == "test v2.0.0 - Go");
detectedComponents.Should().ContainSingle(component => component.Component.Id == "a v1.5.0 - Go");
}

[TestMethod]
Expand Down Expand Up @@ -464,10 +464,10 @@ public async Task TestGoDetector_GoGraphCyclicDependenciesAsync()
.WithFile("go.mod", string.Empty)
.ExecuteDetectorAsync();

Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);

var detectedComponents = componentRecorder.GetDetectedComponents();
Assert.AreEqual(3, detectedComponents.Count());
detectedComponents.Should().HaveCount(3);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Microsoft.ComponentDetection.Detectors.Tests;

using System;
using FluentAssertions;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand All @@ -22,10 +23,10 @@ public void TestInitialize()
public void ConstructorTest_NameVersion()
{
var goComponent = new GoComponent(TestName, TestVersion);
Assert.AreEqual(TestName, goComponent.Name);
Assert.AreEqual(TestVersion, goComponent.Version);
Assert.AreEqual(string.Empty, goComponent.Hash);
Assert.AreEqual($"{TestName} {TestVersion} - Go", goComponent.Id);
goComponent.Name.Should().Be(TestName);
goComponent.Version.Should().Be(TestVersion);
goComponent.Hash.Should().Be(string.Empty);
goComponent.Id.Should().Be($"{TestName} {TestVersion} - Go");
}

[TestMethod]
Expand All @@ -46,10 +47,10 @@ public void ConstructorTest_NameVersion_NullName()
public void ConstructorTest_NameVersionHash()
{
var goComponent = new GoComponent(TestName, TestVersion, TestHash);
Assert.AreEqual(TestName, goComponent.Name);
Assert.AreEqual(TestVersion, goComponent.Version);
Assert.AreEqual(TestHash, goComponent.Hash);
Assert.AreEqual($"{TestName} {TestVersion} - Go", goComponent.Id);
goComponent.Name.Should().Be(TestName);
goComponent.Version.Should().Be(TestVersion);
goComponent.Hash.Should().Be(TestHash);
goComponent.Id.Should().Be($"{TestName} {TestVersion} - Go");
}

[TestMethod]
Expand Down Expand Up @@ -79,18 +80,18 @@ public void TestEquals()
var goComponent1 = new GoComponent(TestName, TestVersion, TestHash);
var goComponent2 = new GoComponent(TestName, TestVersion, TestHash);
var goComponent3 = new GoComponent(TestName, TestVersion, Guid.NewGuid().ToString());
Assert.IsTrue(goComponent1.Equals(goComponent2));
Assert.IsTrue(((object)goComponent1).Equals(goComponent2));
goComponent1.Equals(goComponent2).Should().BeTrue();
((object)goComponent1).Equals(goComponent2).Should().BeTrue();

Assert.IsFalse(goComponent1.Equals(goComponent3));
Assert.IsFalse(((object)goComponent1).Equals(goComponent3));
goComponent1.Equals(goComponent3).Should().BeFalse();
((object)goComponent1).Equals(goComponent3).Should().BeFalse();
}

[TestMethod]
public void TestGetHashCode()
{
var goComponent1 = new GoComponent(TestName, TestVersion, TestHash);
var goComponent2 = new GoComponent(TestName, TestVersion, TestHash);
Assert.IsTrue(goComponent1.GetHashCode() == goComponent2.GetHashCode());
goComponent1.GetHashCode().Should().Be(goComponent2.GetHashCode());
}
}
Loading

0 comments on commit 53b4059

Please sign in to comment.