diff --git a/src/LCT.Common.UTests/CommonHelperTest.cs b/src/LCT.Common.UTests/CommonHelperTest.cs index 4be897ee..669e2beb 100644 --- a/src/LCT.Common.UTests/CommonHelperTest.cs +++ b/src/LCT.Common.UTests/CommonHelperTest.cs @@ -59,8 +59,7 @@ public void RemoveMultipleExcludedComponents_ReturnSuccess() ComponentsForBom.Add(new Component() { Name = "Debian", Version = "3.1.2", Purl = "pkg:npm/Debian@3.1.2", Properties = new List() }); ComponentsForBom.Add(new Component() { Name = "Newton", Version = "3.1.3", Purl = "pkg:npm/Newton@3.1.3", Properties = new List() }); ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.4", Purl = "pkg:npm/Log4t@3.1.4", Properties = new List() }); - ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.5",Purl= "pkg:npm/Log4t@3.1.5", Properties = new List() }); - ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.5", Purl = "pkg:npm/Log4t@3.1.5", Properties = new List() }); + ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.5",Purl= "pkg:npm/Log4t@3.1.5", Properties = new List() }); int noOfExcludedComponents = 0; @@ -73,11 +72,7 @@ public void RemoveMultipleExcludedComponents_ReturnSuccess() CommonHelper.RemoveExcludedComponents(ComponentsForBom, list, ref noOfExcludedComponents); //Assert - Assert.That(noOfExcludedComponents, Is.EqualTo(4), "Returns the count of excluded components"); - Assert.That(ComponentsForBom[0].Purl, Is.EqualTo("pkg:npm/Log4t@3.1.4"), "Checks the component PURL is correct"); - Assert.That(ComponentsForBom[0].Properties.Count, Is.EqualTo(0), "Checks the component has no property"); - Assert.That(ComponentsForBom[1].Purl, Is.EqualTo("pkg:npm/Log4t@3.1.5"), "Checks the component PURL is correct"); - Assert.That(ComponentsForBom[1].Properties.Count, Is.EqualTo(1), "Checks the component has one property"); + Assert.That(noOfExcludedComponents, Is.EqualTo(5), "Returns the count of excluded components"); } @@ -273,9 +268,9 @@ public void RemoveExcludedComponents_WhenExcludedComponentMatches_ReturnsExclude // Arrange List componentList = new List { - new Component { Name = "Component1", Version = "1.0" }, - new Component { Name = "Component2", Version = "2.0" }, - new Component { Name = "Component3", Version = "3.0" } + new Component { Name = "Component1", Version = "1.0", Properties = new List() }, + new Component { Name = "Component2", Version = "2.0", Properties = new List() }, + new Component { Name = "Component3", Version = "3.0", Properties = new List() } }; List excludedComponents = new List { "Component1:*", "Component2:2.0" }; int noOfExcludedComponents = 0; @@ -284,9 +279,7 @@ public void RemoveExcludedComponents_WhenExcludedComponentMatches_ReturnsExclude List result = CommonHelper.RemoveExcludedComponents(componentList, excludedComponents, ref noOfExcludedComponents); // Assert - Assert.AreEqual(1, result.Count); - Assert.IsFalse(result.Any(c => c.Name == "Component1" && c.Version == "1.0")); - Assert.IsTrue(result.Any(c => c.Name == "Component3" && c.Version == "3.0")); + Assert.AreEqual(3, result.Count); Assert.AreEqual(2, noOfExcludedComponents); } diff --git a/src/LCT.Common/CommonHelper.cs b/src/LCT.Common/CommonHelper.cs index 02caa301..0abdbbf6 100644 --- a/src/LCT.Common/CommonHelper.cs +++ b/src/LCT.Common/CommonHelper.cs @@ -44,9 +44,8 @@ public static List RemoveExcludedComponents(List Component List ExcludedComponentsFromPurl = ExcludedComponents?.Where(ec => ec.StartsWith("pkg:")).ToList(); List otherExcludedComponents = ExcludedComponents?.Where(ec => !ec.StartsWith("pkg:")).ToList(); - ValidateExcludedComponentsFromPurl(ComponentList, ExcludedComponentsFromPurl, ref noOfExcludedComponents); - ExcludedList.AddRange(RemoveOtherExcludedComponents(ComponentList, otherExcludedComponents, ref noOfExcludedComponents)); - ComponentList.RemoveAll(item => ExcludedList.Contains(item)); + AddExcludedComponentsPropertyFromPurl(ComponentList, ExcludedComponentsFromPurl, ref noOfExcludedComponents); + AddExcludedComponentsPropertyFromNameAndVersion(ComponentList, otherExcludedComponents, ref noOfExcludedComponents); return ComponentList; } @@ -297,7 +296,7 @@ private static string Sw360URL(string sw360Env, string releaseId) return sw360URL; } - private static List ValidateExcludedComponentsFromPurl(List ComponentList, List ExcludedComponentsFromPurl, ref int noOfExcludedComponents) + private static List AddExcludedComponentsPropertyFromPurl(List ComponentList, List ExcludedComponentsFromPurl, ref int noOfExcludedComponents) { @@ -310,6 +309,7 @@ private static List ValidateExcludedComponentsFromPurl(List RemoveOtherExcludedComponents(List ComponentList, List otherExcludedComponents, ref int noOfExcludedComponents) + private static List AddExcludedComponentsPropertyFromNameAndVersion(List ComponentList, List otherExcludedComponents, ref int noOfExcludedComponents) { - List ExcludedList = new List(); + Property excludeProperty = new() { Name = Dataconstant.Cdx_ExcludeComponent, Value = "true" }; foreach (string excludedComponent in otherExcludedComponents) { string[] excludedcomponent = excludedComponent.ToLower().Split(':'); @@ -341,12 +341,12 @@ private static List RemoveOtherExcludedComponents(List Com (component.Version.ToLowerInvariant().Contains(excludedcomponent[1].ToLowerInvariant()) || excludedcomponent[1].ToLowerInvariant() == "*")) { noOfExcludedComponents++; - ExcludedList.Add(component); + component.Properties.Add(excludeProperty); } } } - return ExcludedList; + return ComponentList; } #endregion } diff --git a/src/LCT.PackageIdentifier.UTest/ConanParserTests.cs b/src/LCT.PackageIdentifier.UTest/ConanParserTests.cs index aeee702d..177012b1 100644 --- a/src/LCT.PackageIdentifier.UTest/ConanParserTests.cs +++ b/src/LCT.PackageIdentifier.UTest/ConanParserTests.cs @@ -95,7 +95,7 @@ public void ParseLockFile_GivenAInputFilePath_ReturnDevDependentComp() public void ParseLockFile_GivenAInputFilePathExcludeComponent_ReturnComponentCount() { //Arrange - int totalComponentsAfterExclusion = 15; + int totalComponentsAfterExclusion = 17; string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location; string outFolder = Path.GetDirectoryName(exePath); string packagefilepath = outFolder + @"\PackageIdentifierUTTestFiles";