From db79f7de2ef596a110dec3caf70acb49e078ba41 Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Wed, 28 Jun 2023 21:02:56 -0700 Subject: [PATCH] refactor: add `FluentAssertions.Analyzers` and fix some warnings --- Directory.Packages.props | 1 + .../DetectedComponentTests.cs | 7 +++---- ...osoft.ComponentDetection.Contracts.Tests.csproj | 1 + .../Experiments/ExperimentDiffTests.cs | 6 ++---- ...ft.ComponentDetection.Orchestrator.Tests.csproj | 1 + .../Services/BcdeScanExecutionServiceTests.cs | 5 ++--- .../Services/DetectorProcessingServiceTests.cs | 14 +++++++------- 7 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index fcf648a0b..75fb2d90c 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -10,6 +10,7 @@ + diff --git a/test/Microsoft.ComponentDetection.Contracts.Tests/DetectedComponentTests.cs b/test/Microsoft.ComponentDetection.Contracts.Tests/DetectedComponentTests.cs index 1d79d3f69..7cc7e33c5 100644 --- a/test/Microsoft.ComponentDetection.Contracts.Tests/DetectedComponentTests.cs +++ b/test/Microsoft.ComponentDetection.Contracts.Tests/DetectedComponentTests.cs @@ -1,5 +1,6 @@ namespace Microsoft.ComponentDetection.Contracts.Tests; +using FluentAssertions; using Microsoft.ComponentDetection.Contracts.TypedComponent; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -17,12 +18,10 @@ public void AddComponentFilePath_AddsPathsCorrectly() var component = new DetectedComponent(new NpmComponent(componentName, componentVersion)); - Assert.IsNotNull(component.FilePaths); - Assert.AreEqual(0, component.FilePaths.Count); + component.FilePaths.Should().NotBeNull().And.HaveCount(0); component.AddComponentFilePath(filePathToAdd); - Assert.AreEqual(1, component.FilePaths.Count); - Assert.IsTrue(component.FilePaths.Contains(filePathToAdd)); + component.FilePaths.Should().HaveCount(1).And.Contain(filePathToAdd); } } diff --git a/test/Microsoft.ComponentDetection.Contracts.Tests/Microsoft.ComponentDetection.Contracts.Tests.csproj b/test/Microsoft.ComponentDetection.Contracts.Tests/Microsoft.ComponentDetection.Contracts.Tests.csproj index ddcc3a6ad..ee6fce2b1 100644 --- a/test/Microsoft.ComponentDetection.Contracts.Tests/Microsoft.ComponentDetection.Contracts.Tests.csproj +++ b/test/Microsoft.ComponentDetection.Contracts.Tests/Microsoft.ComponentDetection.Contracts.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Experiments/ExperimentDiffTests.cs b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Experiments/ExperimentDiffTests.cs index 983b844ba..62626ed70 100644 --- a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Experiments/ExperimentDiffTests.cs +++ b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Experiments/ExperimentDiffTests.cs @@ -90,8 +90,7 @@ public void ExperimentDiff_DiffsAddedRootIds() diff.RemovedRootIds.Should().BeEmpty(); var addedRoot = diff.AddedRootIds[componentA.Component.Id]; - addedRoot.Should().HaveCount(1); - addedRoot.Should().BeEquivalentTo(rootComponent.Id); + addedRoot.Should().ContainSingle().And.BeEquivalentTo(rootComponent.Id); diff.AddedIds.Should().BeEmpty(); diff.RemovedIds.Should().BeEmpty(); @@ -118,8 +117,7 @@ public void ExperimentDiff_DiffsRemovedRootIds() diff.AddedRootIds.Should().BeEmpty(); var removedRoot = diff.RemovedRootIds[componentA.Component.Id]; - removedRoot.Should().HaveCount(1); - removedRoot.Should().BeEquivalentTo(rootComponent.Id); + removedRoot.Should().ContainSingle().And.BeEquivalentTo(rootComponent.Id); diff.AddedIds.Should().BeEmpty(); diff.RemovedIds.Should().BeEmpty(); diff --git a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Microsoft.ComponentDetection.Orchestrator.Tests.csproj b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Microsoft.ComponentDetection.Orchestrator.Tests.csproj index 2aa4b970e..21cb22247 100644 --- a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Microsoft.ComponentDetection.Orchestrator.Tests.csproj +++ b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Microsoft.ComponentDetection.Orchestrator.Tests.csproj @@ -9,6 +9,7 @@ + diff --git a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/BcdeScanExecutionServiceTests.cs b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/BcdeScanExecutionServiceTests.cs index 34576ad12..1d21ff6b1 100644 --- a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/BcdeScanExecutionServiceTests.cs +++ b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/BcdeScanExecutionServiceTests.cs @@ -257,8 +257,7 @@ public async Task DetectComponents_Graph_Happy_PathAsync() var matchingGraph = result.DependencyGraphs.First(); matchingGraph.Key.Should().Be(mockGraphLocation); var explicitlyReferencedComponents = matchingGraph.Value.ExplicitlyReferencedComponentIds; - explicitlyReferencedComponents.Count.Should().Be(1); - explicitlyReferencedComponents.Should().Contain(this.detectedComponents[0].Component.Id); + explicitlyReferencedComponents.Should().ContainSingle().And.Contain(this.detectedComponents[0].Component.Id); var actualGraph = matchingGraph.Value.Graph; actualGraph.Keys.Count.Should().Be(2); @@ -331,7 +330,7 @@ public async Task DetectComponents_Graph_AccumulatesGraphsOnSameLocationAsync() var matchingGraph = result.DependencyGraphs.First(); matchingGraph.Key.Should().Be(mockGraphLocation); var explicitlyReferencedComponents = matchingGraph.Value.ExplicitlyReferencedComponentIds; - explicitlyReferencedComponents.Count.Should().Be(2); + explicitlyReferencedComponents.Should().HaveCount(2); explicitlyReferencedComponents.Should().Contain(this.detectedComponents[0].Component.Id); explicitlyReferencedComponents.Should().Contain(this.detectedComponents[1].Component.Id); diff --git a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/DetectorProcessingServiceTests.cs b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/DetectorProcessingServiceTests.cs index 39a3b40d0..11e8ad74b 100644 --- a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/DetectorProcessingServiceTests.cs +++ b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/DetectorProcessingServiceTests.cs @@ -348,32 +348,32 @@ public void GenerateDirectoryExclusionPredicate_IgnoreCaseAndAllowWindowsPathsWo // Exclusion predicate is case sensitive and allow windows path, the exclusion list follow the windows path structure and has a case mismatch with the directory path, should not exclude args.DirectoryExclusionList = new[] { @"**\source\**" }; var exclusionPredicate = this.serviceUnderTest.GenerateDirectoryExclusionPredicate(@"C:\somefake\dir", args.DirectoryExclusionList, args.DirectoryExclusionListObsolete, allowWindowsPaths: true, ignoreCase: false); - Assert.IsFalse(exclusionPredicate(dn, dp)); + exclusionPredicate(dn, dp).Should().BeFalse(); // Exclusion predicate is case sensitive and allow windows path, the exclusion list follow the windows path structure and match directory path case, should exclude args.DirectoryExclusionList = new[] { @"**\Source\**" }; exclusionPredicate = this.serviceUnderTest.GenerateDirectoryExclusionPredicate(@"C:\somefake\dir", args.DirectoryExclusionList, args.DirectoryExclusionListObsolete, allowWindowsPaths: true, ignoreCase: false); - Assert.IsTrue(exclusionPredicate(dn, dp)); + exclusionPredicate(dn, dp).Should().BeTrue(); // Exclusion predicate is not case sensitive and allow windows path, the exclusion list follow the windows path, should exclude args.DirectoryExclusionList = new[] { @"**\sOuRce\**" }; exclusionPredicate = this.serviceUnderTest.GenerateDirectoryExclusionPredicate(@"C:\somefake\dir", args.DirectoryExclusionList, args.DirectoryExclusionListObsolete, allowWindowsPaths: true, ignoreCase: true); - Assert.IsTrue(exclusionPredicate(dn, dp)); + exclusionPredicate(dn, dp).Should().BeTrue(); // Exclusion predicate does not support windows path and the exclusion list define the path as a windows path, should not exclude args.DirectoryExclusionList = new[] { @"**\Source\**" }; exclusionPredicate = this.serviceUnderTest.GenerateDirectoryExclusionPredicate(@"C:\somefake\dir", args.DirectoryExclusionList, args.DirectoryExclusionListObsolete, allowWindowsPaths: false, ignoreCase: true); - Assert.IsFalse(exclusionPredicate(dn, dp)); + exclusionPredicate(dn, dp).Should().BeFalse(); // Exclusion predicate support windows path and the exclusion list define the path as a windows path, should exclude args.DirectoryExclusionList = new[] { @"**\Source\**" }; exclusionPredicate = this.serviceUnderTest.GenerateDirectoryExclusionPredicate(@"C:\somefake\dir", args.DirectoryExclusionList, args.DirectoryExclusionListObsolete, allowWindowsPaths: true, ignoreCase: true); - Assert.IsTrue(exclusionPredicate(dn, dp)); + exclusionPredicate(dn, dp).Should().BeTrue(); // Exclusion predicate support windows path and the exclusion list does not define a windows path, should exclude args.DirectoryExclusionList = new[] { @"**/Source/**", @"**/Source\**" }; exclusionPredicate = this.serviceUnderTest.GenerateDirectoryExclusionPredicate(@"C:\somefake\dir", args.DirectoryExclusionList, args.DirectoryExclusionListObsolete, allowWindowsPaths: true, ignoreCase: true); - Assert.IsTrue(exclusionPredicate(dn, dp)); + exclusionPredicate(dn, dp).Should().BeTrue(); } [TestMethod] @@ -602,7 +602,7 @@ private void ValidateExpectedComponents(DetectorProcessingResult result, IEnumer var check = isPresent.Select(i => i.GetType()); isPresent.All(discovered => shouldBePresent.Contains(discovered)); - shouldBePresent.Should().HaveCount(isPresent.Count()); + shouldBePresent.Should().HaveSameCount(isPresent); } private Mock SetupCommandDetectorMock(string id)