From c741263fab433e9e586143872ed251ef5e75cb3b Mon Sep 17 00:00:00 2001 From: Nikolay Chashnikov Date: Tue, 26 May 2020 22:42:30 +0300 Subject: [PATCH] [junit] automatically set scope to 'test' when dependency on JUnit5 library is added JUnit5 depends on apiguardian-api JAR which may be used not only in tests, so let's suppose that the library is a JUnit library if more than half of its JARs are for testing. GitOrigin-RevId: 3888bbf5ecccbe1515c25e377772893a428e57bb --- .../junit/JUnitDependencyScopeSuggester.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/plugins/junit/src/com/intellij/execution/junit/JUnitDependencyScopeSuggester.java b/plugins/junit/src/com/intellij/execution/junit/JUnitDependencyScopeSuggester.java index 79fbad41a199..c8c295247283 100644 --- a/plugins/junit/src/com/intellij/execution/junit/JUnitDependencyScopeSuggester.java +++ b/plugins/junit/src/com/intellij/execution/junit/JUnitDependencyScopeSuggester.java @@ -24,24 +24,22 @@ import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; import java.util.Collections; public class JUnitDependencyScopeSuggester extends LibraryDependencyScopeSuggester { private static final String[] JUNIT_JAR_MARKERS = { - "org.junit.Test", "junit.framework.TestCase", "org.hamcrest.Matcher", "org.hamcrest.Matchers" + "org.junit.Test", "junit.framework.TestCase", "org.hamcrest.Matcher", "org.hamcrest.Matchers", + "org.junit.jupiter.api.Test", "org.junit.platform.commons.JUnitException", "org.opentest4j.AssertionFailedError" }; @Nullable @Override public DependencyScope getDefaultDependencyScope(@NotNull Library library) { VirtualFile[] files = library.getFiles(OrderRootType.CLASSES); - if (files.length == 0) return null; - for (VirtualFile file : files) { - if (!isTestJarRoot(file)) { - return null; - } - } - return DependencyScope.TEST; + long testJars = Arrays.stream(files).filter(JUnitDependencyScopeSuggester::isTestJarRoot).count(); + long regularJars = files.length - testJars; + return testJars > regularJars ? DependencyScope.TEST : null; } private static boolean isTestJarRoot(VirtualFile file) {