[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
This commit is contained in:
Nikolay Chashnikov
2020-05-26 22:48:33 +03:00
committed by intellij-monorepo-bot
parent 9858c931fc
commit c741263fab
@@ -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) {