From 90de2e682af92bb68930e0a436be73216d8add42 Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Tue, 1 Feb 2022 16:11:49 -0800 Subject: [PATCH] =?UTF-8?q?Use=20collection=E2=80=99s=20count=20property?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Count()` is a LINQ extension method, whereas `Count` is a property of the collection. See: https://stackoverflow.com/questions/4098186/lists-count-vs-count --- .../TabularStringFormat.cs | 4 ++-- .../linux/LinuxContainerDetector.cs | 2 +- .../ArgumentHelper.cs | 2 +- .../Services/DetectorProcessingService.cs | 2 +- .../Services/DetectorRegistryService.cs | 6 +++--- .../ComponentStreamEnumerableTests.cs | 2 +- ...odelProjectCentricComponentDetectorTests.cs | 8 ++++---- .../PythonCommandServiceTests.cs | 2 +- .../YarnLockDetectorTests.cs | 8 ++++---- .../GraphTranslationUtilityTests.cs | 2 +- .../Services/BcdeScanExecutionServiceTests.cs | 18 +++++++++--------- 11 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Microsoft.ComponentDetection.Common/TabularStringFormat.cs b/src/Microsoft.ComponentDetection.Common/TabularStringFormat.cs index e64270f17..a6e1c5260 100644 --- a/src/Microsoft.ComponentDetection.Common/TabularStringFormat.cs +++ b/src/Microsoft.ComponentDetection.Common/TabularStringFormat.cs @@ -48,7 +48,7 @@ public string GenerateString(IEnumerable> rows) foreach (var row in rows) { sb.Append(verticalLineChar); - if (row.Count() != columns.Count) + if (row.Count != columns.Count) { throw new InvalidOperationException("All rows must have length equal to the number of columns present."); } @@ -71,7 +71,7 @@ private void PrintTitleSection(StringBuilder sb) WriteFlatLine(sb, false); var tableWidth = columns.Sum(column => column.Width); sb.Append(verticalLineChar); - sb.Append(tableTitle.PadRight(tableWidth + columns.Count() - 1)); + sb.Append(tableTitle.PadRight(tableWidth + columns.Count - 1)); sb.Append(verticalLineChar); sb.AppendLine(); diff --git a/src/Microsoft.ComponentDetection.Detectors/linux/LinuxContainerDetector.cs b/src/Microsoft.ComponentDetection.Detectors/linux/LinuxContainerDetector.cs index 7db96dc81..e5466d9e6 100644 --- a/src/Microsoft.ComponentDetection.Detectors/linux/LinuxContainerDetector.cs +++ b/src/Microsoft.ComponentDetection.Detectors/linux/LinuxContainerDetector.cs @@ -243,7 +243,7 @@ await DockerService.TryPullImageAsync(refWithDigest, cancellationToken))) private bool ValidateBaseImageLayers(ContainerDetails scannedImageDetails, ContainerDetails baseImageDetails) { var scannedImageLayers = scannedImageDetails.Layers.ToArray(); - return !(baseImageDetails.Layers.Count() > scannedImageLayers.Count() || baseImageDetails.Layers.Where((layer, index) => scannedImageLayers[index].DiffId != layer.DiffId).Any()); + return !(baseImageDetails.Layers.Count() > scannedImageLayers.Length || baseImageDetails.Layers.Where((layer, index) => scannedImageLayers[index].DiffId != layer.DiffId).Any()); } } } diff --git a/src/Microsoft.ComponentDetection.Orchestrator/ArgumentHelper.cs b/src/Microsoft.ComponentDetection.Orchestrator/ArgumentHelper.cs index 47bc86fa9..05323b4d1 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/ArgumentHelper.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/ArgumentHelper.cs @@ -43,7 +43,7 @@ public static IDictionary GetDetectorArgs(IEnumerable de { var keyValue = arg.Split('='); - if (keyValue.Count() != 2) + if (keyValue.Length != 2) { continue; } diff --git a/src/Microsoft.ComponentDetection.Orchestrator/Services/DetectorProcessingService.cs b/src/Microsoft.ComponentDetection.Orchestrator/Services/DetectorProcessingService.cs index 04ae9d514..7b34809d9 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/Services/DetectorProcessingService.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/Services/DetectorProcessingService.cs @@ -233,7 +233,7 @@ private static IDictionary GetDetectorArgs(IEnumerable d { var keyValue = arg.Split('='); - if (keyValue.Count() != 2) + if (keyValue.Length != 2) { continue; } diff --git a/src/Microsoft.ComponentDetection.Orchestrator/Services/DetectorRegistryService.cs b/src/Microsoft.ComponentDetection.Orchestrator/Services/DetectorRegistryService.cs index 7ae676f4a..954a87518 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/Services/DetectorRegistryService.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/Services/DetectorRegistryService.cs @@ -51,7 +51,7 @@ public IEnumerable GetDetectors(Assembly assemblyToSearch, I var loadedDetectors = LoadComponentDetectorsFromAssemblies(new List { assemblyToSearch }, extraDetectorAssemblies); var pluralPhrase = loadedDetectors.Count == 1 ? "detector was" : "detectors were"; - Logger.LogInfo($"{loadedDetectors.Count()} {pluralPhrase} found in {assemblyToSearch.FullName}\n"); + Logger.LogInfo($"{loadedDetectors.Count} {pluralPhrase} found in {assemblyToSearch.FullName}\n"); return loadedDetectors; } @@ -69,7 +69,7 @@ private IList GetComponentDetectors(IEnumerable GetComponentDetectors(IEnumerable>(ommittedComponentInformationJson); - Assert.IsTrue(omittedComponentsWithCount.Keys.Count() > 5, "Ommitted framework assemblies are missing. There should be more than ten, but this is a gut check to make sure we have data."); + Assert.IsTrue(omittedComponentsWithCount.Keys.Count > 5, "Ommitted framework assemblies are missing. There should be more than ten, but this is a gut check to make sure we have data."); Assert.AreEqual(omittedComponentsWithCount["Microsoft.NETCore.App"], 4, "There should be four cases of the NETCore.App library being omitted in the test data."); } @@ -134,7 +134,7 @@ public async Task ScanDirectoryAsync_DependencyGraph_2_2_additional_Verification Assert.IsTrue(dependencies.Contains(expectedId)); } - Assert.AreEqual(dependencies.Count(), expectedDependencyIdsForCompositionTypedParts.Count()); + Assert.AreEqual(dependencies.Count(), expectedDependencyIdsForCompositionTypedParts.Length); Assert.AreEqual(graph.GetComponents().Count(), detectedComponents.Count()); @@ -225,7 +225,7 @@ public async Task ScanDirectoryAsync_ExcludedFrameworkComponent_3_1_Verification var omittedComponentsWithCount = JsonConvert.DeserializeObject>(ommittedComponentInformationJson); // With 3.X, we don't expect there to be a lot of these, but there are still netstandard libraries present which can bring things into the graph - Assert.AreEqual(omittedComponentsWithCount.Keys.Count(), 4, "Ommitted framework assemblies are missing. There should be more than ten, but this is a gut check to make sure we have data."); + Assert.AreEqual(omittedComponentsWithCount.Keys.Count, 4, "Ommitted framework assemblies are missing. There should be more than ten, but this is a gut check to make sure we have data."); Assert.AreEqual(omittedComponentsWithCount["System.Reflection"], 1, "There should be one case of the System.Reflection library being omitted in the test data."); } @@ -294,7 +294,7 @@ public async Task ScanDirectory_NoPackageSpec() var dependencyGraphs = componentRecorder.GetDependencyGraphsByLocation(); - dependencyGraphs.Count().Should().Be(0); + dependencyGraphs.Count.Should().Be(0); } private string Convert22SampleToOSAgnostic(string project_assets) diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/PythonCommandServiceTests.cs b/test/Microsoft.ComponentDetection.Detectors.Tests/PythonCommandServiceTests.cs index ec41cc726..bf8f5b644 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/PythonCommandServiceTests.cs +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/PythonCommandServiceTests.cs @@ -162,7 +162,7 @@ public async Task ParseFile_RequirementTxtHasComment_CommentAreIgnored() var result = await service.ParseFile(testPath); (string, GitComponent) expected = ("knack==0.4.1", null); - Assert.AreEqual(1, result.Count()); + Assert.AreEqual(1, result.Count); Assert.AreEqual(expected, result.First()); } finally diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/YarnLockDetectorTests.cs b/test/Microsoft.ComponentDetection.Detectors.Tests/YarnLockDetectorTests.cs index 1c79e64a7..bd50c08e9 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/YarnLockDetectorTests.cs +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/YarnLockDetectorTests.cs @@ -834,9 +834,9 @@ private string CreatePackageJsonFileContent(IList c builder.Append(@"""dependencies"": {"); var prodComponents = components.Where(c => !c.IsDevDependency).ToList(); - for (var i = 0; i < prodComponents.Count(); i++) + for (var i = 0; i < prodComponents.Count; i++) { - if (i == prodComponents.Count() - 1) + if (i == prodComponents.Count - 1) { builder.Append($@" ""{prodComponents[i].Name}"": ""{prodComponents[i].RequestedVersion}"""); } @@ -855,9 +855,9 @@ private string CreatePackageJsonFileContent(IList c var dependencyComponents = components.Where(c => c.IsDevDependency).ToList(); - for (var i = 0; i < dependencyComponents.Count(); i++) + for (var i = 0; i < dependencyComponents.Count; i++) { - if (i == dependencyComponents.Count() - 1) + if (i == dependencyComponents.Count - 1) { builder.Append($@" ""{dependencyComponents[i].Name}"": ""{dependencyComponents[i].RequestedVersion}"""); } diff --git a/test/Microsoft.ComponentDetection.Orchestrator.Tests/GraphTranslationUtilityTests.cs b/test/Microsoft.ComponentDetection.Orchestrator.Tests/GraphTranslationUtilityTests.cs index ca725d839..117acee18 100644 --- a/test/Microsoft.ComponentDetection.Orchestrator.Tests/GraphTranslationUtilityTests.cs +++ b/test/Microsoft.ComponentDetection.Orchestrator.Tests/GraphTranslationUtilityTests.cs @@ -29,7 +29,7 @@ public void Test_AccumulateAndConvertToContract() var convertedGraphContract = GraphTranslationUtility.AccumulateAndConvertToContract(dependencyGraphs); - convertedGraphContract.Count().Should().Be(2); + convertedGraphContract.Count.Should().Be(2); convertedGraphContract.Keys.Should().BeEquivalentTo(new List() { "file1.json", "file2.json" }); var graph1 = convertedGraphContract["file1.json"]; diff --git a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/BcdeScanExecutionServiceTests.cs b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/BcdeScanExecutionServiceTests.cs index e8455a320..3fba45dca 100644 --- a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/BcdeScanExecutionServiceTests.cs +++ b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Services/BcdeScanExecutionServiceTests.cs @@ -246,7 +246,7 @@ public void DetectComponents_Graph_Happy_Path() var result = DetectComponentsHappyPath(args, restrictions => { }, new List { componentRecorder }); result.Result.Should().Be(ProcessingResultCode.Success); - result.DependencyGraphs.Count().Should().Be(1); + result.DependencyGraphs.Count.Should().Be(1); var matchingGraph = result.DependencyGraphs.First(); matchingGraph.Key.Should().Be(mockGraphLocation); var explicitlyReferencedComponents = matchingGraph.Value.ExplicitlyReferencedComponentIds; @@ -255,7 +255,7 @@ public void DetectComponents_Graph_Happy_Path() var actualGraph = matchingGraph.Value.Graph; actualGraph.Keys.Count.Should().Be(2); - actualGraph[detectedComponents[0].Component.Id].Count().Should().Be(1); + actualGraph[detectedComponents[0].Component.Id].Count.Should().Be(1); actualGraph[detectedComponents[0].Component.Id].Should().Contain(detectedComponents[1].Component.Id); actualGraph[detectedComponents[1].Component.Id].Should().BeNull(); @@ -302,7 +302,7 @@ public void DetectComponents_Graph_AccumulatesGraphsOnSameLocation() var result = DetectComponentsHappyPath(args, restrictions => { }, new List { componentRecorder }); result.Result.Should().Be(ProcessingResultCode.Success); - result.DependencyGraphs.Count().Should().Be(1); + result.DependencyGraphs.Count.Should().Be(1); var matchingGraph = result.DependencyGraphs.First(); matchingGraph.Key.Should().Be(mockGraphLocation); var explicitlyReferencedComponents = matchingGraph.Value.ExplicitlyReferencedComponentIds; @@ -312,9 +312,9 @@ public void DetectComponents_Graph_AccumulatesGraphsOnSameLocation() var actualGraph = matchingGraph.Value.Graph; actualGraph.Keys.Count.Should().Be(2); - actualGraph[detectedComponents[0].Component.Id].Count().Should().Be(1); + actualGraph[detectedComponents[0].Component.Id].Count.Should().Be(1); actualGraph[detectedComponents[0].Component.Id].Should().Contain(detectedComponents[1].Component.Id); - actualGraph[detectedComponents[1].Component.Id].Count().Should().Be(1); + actualGraph[detectedComponents[1].Component.Id].Count.Should().Be(1); actualGraph[detectedComponents[1].Component.Id].Should().Contain(detectedComponents[0].Component.Id); } @@ -589,7 +589,7 @@ private TestOutput DetectComponentsHappyPath( detectorRestrictionServiceMock.Setup( x => x.ApplyRestrictions( It.IsAny(), - It.Is>(inputDetectors => registeredDetectors.Intersect(inputDetectors).Count() == registeredDetectors.Count()))) + It.Is>(inputDetectors => registeredDetectors.Intersect(inputDetectors).Count() == registeredDetectors.Length))) .Returns(restrictedDetectors) .Callback>( (restrictions, detectors) => restrictionAsserter?.Invoke(restrictions)); @@ -611,7 +611,7 @@ private TestOutput DetectComponentsHappyPath( detectorProcessingServiceMock.Setup(x => x.ProcessDetectorsAsync( args, - It.Is>(inputDetectors => restrictedDetectors.Intersect(inputDetectors).Count() == restrictedDetectors.Count()), + It.Is>(inputDetectors => restrictedDetectors.Intersect(inputDetectors).Count() == restrictedDetectors.Length), Match.Create(restriction => true))) .ReturnsAsync(processingResult); @@ -634,7 +634,7 @@ private ScanResult SetupRecorderBasedScanning( detectorRestrictionServiceMock.Setup( x => x.ApplyRestrictions( It.IsAny(), - It.Is>(inputDetectors => registeredDetectors.Intersect(inputDetectors).Count() == registeredDetectors.Count()))) + It.Is>(inputDetectors => registeredDetectors.Intersect(inputDetectors).Count() == registeredDetectors.Length))) .Returns(restrictedDetectors); // We initialize detected component's DetectedBy here because of a Moq constraint -- certain operations (Adding interfaces) have to happen before .Object @@ -654,7 +654,7 @@ private ScanResult SetupRecorderBasedScanning( detectorProcessingServiceMock.Setup(x => x.ProcessDetectorsAsync( args, - It.Is>(inputDetectors => restrictedDetectors.Intersect(inputDetectors).Count() == restrictedDetectors.Count()), + It.Is>(inputDetectors => restrictedDetectors.Intersect(inputDetectors).Count() == restrictedDetectors.Length), Match.Create(restriction => true))) .ReturnsAsync(processingResult);