Skip to content

Commit

Permalink
fixed tests and used the func correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalk007 committed Sep 18, 2024
1 parent 03fd1f8 commit bacf6d4
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ private List<DependencyNode> getMatchDependencies(DescriptorFileTreeNode file, S
* @return true if the node matches the component name
*/
boolean isNodeMatch(DependencyNode node, String componentName) {
String artifactID = node.getComponentIdWithoutPrefix();
ImpactTree impactTree = node.getImpactTree();
String versionPrefix = ":";
return StringUtils.equals(extractArtifactIdWithoutVersion(artifactID), componentName) || impactTree.contains(componentName+versionPrefix);
return impactTree.contains(this.extractArtifactIdWithoutVersion(componentName)+versionPrefix);
}

abstract UpgradeVersion getUpgradeVersion(String componentName, String fixVersion, Collection<String> issues, String descriptorPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ String createComponentName(PsiElement element) {
VgoModuleSpec goElement = ((VgoModuleSpec) element);
if (goElement.getModuleVersion() != null) {
String version = goElement.getModuleVersion().getText();
// String leading "v" from version
version = StringUtils.strip(version, "v");
return String.join(":", goElement.getIdentifier().getText(), version);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ public GradleGroovyInspection() {
* @return the value of the literal
*/
public static String getLiteralValue(GrLiteral literal) {
String artifact = Objects.toString((literal).getValue(), "");
int versionIndex = artifact.lastIndexOf(':');
if (versionIndex == -1) {
return artifact;
}
return artifact.substring(0, versionIndex);
return Objects.toString((literal).getValue(), "");
}

public static boolean isNamedArgumentComponent(PsiElement element) {
Expand Down Expand Up @@ -122,7 +117,9 @@ String createComponentName(PsiElement element) {
// implementation group: 'j', name: 'k', version: 'l'
return String.join(":",
extractExpression(element, GRADLE_GROUP_KEY),
extractExpression(element, GRADLE_NAME_KEY));
extractExpression(element, GRADLE_NAME_KEY),
extractExpression(element, GRADLE_VERSION_KEY));

}
if (element instanceof GrLiteral) {
// implementation 'g:h:i'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ String createComponentName(PsiElement element) {
List<KtValueArgument> argumentList = ((KtValueArgumentList) element).getArguments();
if (argumentList.size() == 1) {
// "commons-collections:commons-collections:3.2.2"
String artifactId = extractArgument(argumentList.get(0));
return StringUtils.substringBeforeLast(artifactId, ":");
return extractArgument(argumentList.get(0));
}
if (argumentList.size() >= 3) {
// "commons-collections", "commons-collections"
return String.join(":",
extractArgument(argumentList.get(0)),
extractArgument(argumentList.get(1))
extractArgument(argumentList.get(1)),
extractArgument(argumentList.get(2))
);
}
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ String createComponentName(PsiElement element) {
return null;
}
DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement((XmlTag) element);

if (domElement instanceof MavenDomArtifactCoordinates) {
return String.join(":", groupId.getValue().getText(), artifactId.getValue().getText());
String version = ((MavenDomArtifactCoordinates) domElement).getVersion().getStringValue();
return String.join(":", groupId.getValue().getText(), artifactId.getValue().getText(), version);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void upgradeComponentVersion(@NotNull Project project, @NotNull ProblemDe
if (element instanceof GrLiteral) {
// 'com:guava:1.1.1' >> 'com:guava:2.2.2'
String componentString = getLiteralValue((GrLiteral) element);
String fixedComponentString = String.join(":", stripVersion(componentString), fixVersion);
String fixedComponentString = String.join(":", componentString, fixVersion);
element.replace(psiFactory.createExpressionFromText(StringUtils.wrap(fixedComponentString, "'")));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ public void addNavigation(DependencyNode treeNode, PsiElement navigationTargetEl
}
NavigationTarget navigationTarget = new NavigationTarget(navigationTargetElement, document.getLineNumber(navigationTargetElement.getTextOffset()), componentName);
Set<NavigationTarget> navigationTargets = navigationMap.get(treeNode);

if (navigationTargets == null) {
navigationTargets = new HashSet<>(Collections.singletonList(navigationTarget));

navigationMap.put(treeNode, navigationTargets);
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/jfrog/ide/idea/scan/MavenScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected List<FileTreeNode> groupDependenciesToDescriptorNodes(Collection<Depen
Map<String, Set<String>> visitedComponents = new HashMap<>();
for (DependencyNode dependencyNode : depScanResults) {
String vulnerableDepId = dependencyNode.getComponentIdWithoutPrefix();
Set<String> affectedModulesIds = getDependentModules(vulnerableDepId, depTree, parents, visitedComponents);
Set<String> affectedModulesIds = getDependentModules(vulnerableDepId, depTree, parents, visitedComponents);
for (String descriptorId : affectedModulesIds) {
String descriptorPath = depTree.nodes().get(descriptorId).getDescriptorFilePath();
descriptorMap.putIfAbsent(descriptorPath, new DescriptorFileTreeNode(descriptorPath));
Expand All @@ -187,6 +187,7 @@ protected List<FileTreeNode> groupDependenciesToDescriptorNodes(Collection<Depen
// The solution for this is to clone the dependency before adding it as a child of the POM.
DependencyNode clonedDep = (DependencyNode) dependencyNode.clone();
clonedDep.setIndirect(!parents.get(vulnerableDepId).contains(descriptorId));

descriptorMap.get(descriptorPath).addDependency(clonedDep);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Arrays;
import java.util.Collection;

import static com.jfrog.ide.idea.inspections.GradleInspection.stripVersion;
import static com.jfrog.ide.idea.inspections.GradleInspection.stripVersion; // Make sure this method exists
import static org.junit.Assert.assertEquals;

/**
Expand All @@ -22,8 +22,8 @@ public class GradleInspectionTest {
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"a:b:c", "a:b"},
{"a:b:c:d", "a:b:c"},
{"a", "a"},
{"a:b:c:d", "a:b"},
{"a:b", "a:b"},
{"xyz", "xyz"}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class GradleKotlinInspectionTest extends InspectionsTestBase {
// files as groovy-script.
private static final String PACKAGE_DESCRIPTOR = "build.gradle.kts";
private final InspectionTestDependency[] DEPENDENCIES = {
new InspectionTestDependency(119, "a", "b"),
new InspectionTestDependency(144, "d", "e"),
new InspectionTestDependency(119, "a", "b:c"),
new InspectionTestDependency(144, "d", "e:f"),
};

private final int[] NON_DEPENDENCIES_POSITIONS = {273, 338};
Expand Down

0 comments on commit bacf6d4

Please sign in to comment.