Skip to content

Commit

Permalink
Requested code changes updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ragavareddychalapala committed Jan 31, 2025
1 parent 9514da7 commit f99fd0b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
19 changes: 6 additions & 13 deletions src/LCT.Common.UTests/CommonHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Property>() });
ComponentsForBom.Add(new Component() { Name = "Newton", Version = "3.1.3", Purl = "pkg:npm/Newton@3.1.3", Properties = new List<Property>() });
ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.4", Purl = "pkg:npm/Log4t@3.1.4", Properties = new List<Property>() });
ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.5",Purl= "pkg:npm/Log4t@3.1.5", Properties = new List<Property>() });
ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.5", Purl = "pkg:npm/Log4t@3.1.5", Properties = new List<Property>() });
ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.5",Purl= "pkg:npm/Log4t@3.1.5", Properties = new List<Property>() });

int noOfExcludedComponents = 0;

Expand All @@ -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");

}

Expand Down Expand Up @@ -273,9 +268,9 @@ public void RemoveExcludedComponents_WhenExcludedComponentMatches_ReturnsExclude
// Arrange
List<Component> componentList = new List<Component>
{
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<Property>() },
new Component { Name = "Component2", Version = "2.0", Properties = new List<Property>() },
new Component { Name = "Component3", Version = "3.0", Properties = new List<Property>() }
};
List<string> excludedComponents = new List<string> { "Component1:*", "Component2:2.0" };
int noOfExcludedComponents = 0;
Expand All @@ -284,9 +279,7 @@ public void RemoveExcludedComponents_WhenExcludedComponentMatches_ReturnsExclude
List<Component> 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);
}

Expand Down
16 changes: 8 additions & 8 deletions src/LCT.Common/CommonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ public static List<Component> RemoveExcludedComponents(List<Component> Component
List<string> ExcludedComponentsFromPurl = ExcludedComponents?.Where(ec => ec.StartsWith("pkg:")).ToList();
List<string> 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;
}

Expand Down Expand Up @@ -297,7 +296,7 @@ private static string Sw360URL(string sw360Env, string releaseId)
return sw360URL;
}

private static List<Component> ValidateExcludedComponentsFromPurl(List<Component> ComponentList, List<string> ExcludedComponentsFromPurl, ref int noOfExcludedComponents)
private static List<Component> AddExcludedComponentsPropertyFromPurl(List<Component> ComponentList, List<string> ExcludedComponentsFromPurl, ref int noOfExcludedComponents)
{


Expand All @@ -310,6 +309,7 @@ private static List<Component> ValidateExcludedComponentsFromPurl(List<Component
if (component.Purl != null && componentPurl.Equals(excludedComponent, StringComparison.OrdinalIgnoreCase))
{
component.Properties.Add(excludeProperty);
noOfExcludedComponents++;
}
}
}
Expand All @@ -323,10 +323,10 @@ private static string NormalizePurl(string purl)
}
return purl;
}
private static List<Component> RemoveOtherExcludedComponents(List<Component> ComponentList, List<string> otherExcludedComponents, ref int noOfExcludedComponents)
private static List<Component> AddExcludedComponentsPropertyFromNameAndVersion(List<Component> ComponentList, List<string> otherExcludedComponents, ref int noOfExcludedComponents)
{
List<Component> ExcludedList = new List<Component>();

Property excludeProperty = new() { Name = Dataconstant.Cdx_ExcludeComponent, Value = "true" };
foreach (string excludedComponent in otherExcludedComponents)
{
string[] excludedcomponent = excludedComponent.ToLower().Split(':');
Expand All @@ -341,12 +341,12 @@ private static List<Component> RemoveOtherExcludedComponents(List<Component> Com
(component.Version.ToLowerInvariant().Contains(excludedcomponent[1].ToLowerInvariant()) || excludedcomponent[1].ToLowerInvariant() == "*"))
{
noOfExcludedComponents++;
ExcludedList.Add(component);
component.Properties.Add(excludeProperty);
}
}
}

return ExcludedList;
return ComponentList;
}
#endregion
}
Expand Down
2 changes: 1 addition & 1 deletion src/LCT.PackageIdentifier.UTest/ConanParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit f99fd0b

Please sign in to comment.