mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
test discovery: prefix testng/junit tests to detect test frameworks which generated the tests
This commit is contained in:
+3
@@ -219,4 +219,7 @@ public abstract class TestDiscoveryConfiguration extends JavaTestConfigurationBa
|
||||
public String getChangeList() {
|
||||
return myChangeList;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public abstract String getFrameworkPrefix();
|
||||
}
|
||||
|
||||
+10
-2
@@ -22,6 +22,7 @@ import com.intellij.execution.actions.ConfigurationContext;
|
||||
import com.intellij.execution.configurations.ConfigurationType;
|
||||
import com.intellij.execution.junit.JavaRunConfigurationProducerBase;
|
||||
import com.intellij.execution.testframework.TestSearchScope;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.openapi.util.registry.Registry;
|
||||
@@ -31,6 +32,7 @@ import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.testIntegration.TestFramework;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
@@ -41,7 +43,7 @@ public abstract class TestDiscoveryConfigurationProducer extends JavaRunConfigur
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setupConfigurationFromContext(TestDiscoveryConfiguration configuration,
|
||||
protected boolean setupConfigurationFromContext(final TestDiscoveryConfiguration configuration,
|
||||
ConfigurationContext configurationContext,
|
||||
Ref<PsiElement> ref) {
|
||||
if (!Registry.is("testDiscovery.enabled")) {
|
||||
@@ -56,7 +58,13 @@ public abstract class TestDiscoveryConfigurationProducer extends JavaRunConfigur
|
||||
try {
|
||||
final Collection<String> testsByMethodName = TestDiscoveryIndex
|
||||
.getInstance(configuration.getProject()).getTestsByMethodName(position.first, position.second);
|
||||
if (testsByMethodName == null || testsByMethodName.isEmpty()) return false;
|
||||
if (testsByMethodName == null || ContainerUtil.filter(testsByMethodName, new Condition<String>() {
|
||||
@Override
|
||||
public boolean value(String s) {
|
||||
return s.startsWith(configuration.getFrameworkPrefix());
|
||||
}
|
||||
}).isEmpty()) return false;
|
||||
|
||||
}
|
||||
catch (IOException e) {
|
||||
return false;
|
||||
|
||||
+29
-17
@@ -18,6 +18,7 @@ package com.intellij.execution.testDiscovery;
|
||||
import com.intellij.codeInsight.actions.FormatChangedTextUtil;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.vcs.changes.Change;
|
||||
@@ -27,6 +28,7 @@ import com.intellij.openapi.vcs.changes.LocalChangeList;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.diff.FilesTooBigForDiffException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -35,17 +37,14 @@ import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
public class TestDiscoverySearchHelper {
|
||||
public static Set<String> search(final Project project, final Pair<String, String> position, final String changeList) {
|
||||
public static Set<String> search(final Project project,
|
||||
final Pair<String, String> position,
|
||||
final String changeList,
|
||||
final String frameworkPrefix) {
|
||||
final Set<String> patterns = new LinkedHashSet<String>();
|
||||
if (position != null) {
|
||||
try {
|
||||
final Collection<String> testsByMethodName = TestDiscoveryIndex
|
||||
.getInstance(project).getTestsByMethodName(position.first, position.second);
|
||||
if (testsByMethodName != null) {
|
||||
for (String pattern : testsByMethodName) {
|
||||
patterns.add(pattern.replace('-', ','));
|
||||
}
|
||||
}
|
||||
collectPatterns(project, patterns, position.first, position.second, frameworkPrefix);
|
||||
}
|
||||
catch (IOException ignore) {
|
||||
}
|
||||
@@ -73,7 +72,7 @@ public class TestDiscoverySearchHelper {
|
||||
methods.add(containingMethod);
|
||||
}
|
||||
for (PsiMethod changedMethod : methods) {
|
||||
final LinkedHashSet<String> detectedPatterns = collectPatterns(changedMethod);
|
||||
final LinkedHashSet<String> detectedPatterns = collectPatterns(changedMethod, frameworkPrefix);
|
||||
if (detectedPatterns != null) {
|
||||
patterns.addAll(detectedPatterns);
|
||||
}
|
||||
@@ -90,6 +89,25 @@ public class TestDiscoverySearchHelper {
|
||||
return patterns;
|
||||
}
|
||||
|
||||
private static void collectPatterns(final Project project,
|
||||
final Set<String> patterns,
|
||||
final String classFQName,
|
||||
final String methodName,
|
||||
final String frameworkId) throws IOException {
|
||||
final Collection<String> testsByMethodName = TestDiscoveryIndex
|
||||
.getInstance(project).getTestsByMethodName(classFQName, methodName);
|
||||
if (testsByMethodName != null) {
|
||||
for (String pattern : ContainerUtil.filter(testsByMethodName, new Condition<String>() {
|
||||
@Override
|
||||
public boolean value(String s) {
|
||||
return s.startsWith(frameworkId);
|
||||
}
|
||||
})) {
|
||||
patterns.add(pattern.substring(frameworkId.length()).replace('-', ','));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<VirtualFile> getAffectedFiles(String changeListName, Project project) {
|
||||
final ChangeListManager changeListManager = ChangeListManager.getInstance(project);
|
||||
@@ -115,20 +133,14 @@ public class TestDiscoverySearchHelper {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static LinkedHashSet<String> collectPatterns(PsiMethod psiMethod) {
|
||||
private static LinkedHashSet<String> collectPatterns(PsiMethod psiMethod, String frameworkId) {
|
||||
LinkedHashSet<String> patterns = new LinkedHashSet<String>();
|
||||
final PsiClass containingClass = psiMethod.getContainingClass();
|
||||
if (containingClass != null) {
|
||||
final String qualifiedName = containingClass.getQualifiedName();
|
||||
if (qualifiedName != null) {
|
||||
try {
|
||||
final Collection<String> testsByMethodName
|
||||
= TestDiscoveryIndex.getInstance(containingClass.getProject()).getTestsByMethodName(qualifiedName, psiMethod.getName());
|
||||
if (testsByMethodName != null) {
|
||||
for (String pattern : testsByMethodName) {
|
||||
patterns.add(pattern.replace('-', ','));
|
||||
}
|
||||
}
|
||||
collectPatterns(psiMethod.getProject(), patterns, qualifiedName, psiMethod.getName(), frameworkId);
|
||||
}
|
||||
catch (IOException e) {
|
||||
return null;
|
||||
|
||||
@@ -17,10 +17,11 @@ package com.intellij.execution;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class TestDiscoveryListener {
|
||||
public abstract class TestDiscoveryListener {
|
||||
public abstract String getFrameworkId();
|
||||
public void testStarted(String className, String methodName) {
|
||||
final Object data = getData();
|
||||
try {
|
||||
final Object data = getData();
|
||||
Method testStarted = data.getClass().getMethod("testStarted", new Class[] {String.class});
|
||||
testStarted.invoke(data, new Object[] {className + "-" + methodName});
|
||||
} catch (Throwable t) {
|
||||
@@ -29,24 +30,19 @@ public class TestDiscoveryListener {
|
||||
}
|
||||
|
||||
public void testFinished(String className, String methodName) {
|
||||
final Object data = getData();
|
||||
try {
|
||||
final Object data = getData();
|
||||
Method testEnded = data.getClass().getMethod("testEnded", new Class[] {String.class});
|
||||
testEnded.invoke(data, new Object[] {className + "-" + methodName});
|
||||
testEnded.invoke(data, new Object[] {getFrameworkId() + className + "-" + methodName});
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
protected Object getData() {
|
||||
try {
|
||||
return Class.forName("org.jetbrains.testme.instrumentation.ProjectData")
|
||||
protected Object getData() throws Exception {
|
||||
return Class.forName("org.jetbrains.testme.instrumentation.ProjectData")
|
||||
.getMethod("getProjectData", new Class[0])
|
||||
.invoke(null, new Object[0]);
|
||||
|
||||
} catch (Exception e) {
|
||||
return null; //should not happen
|
||||
}
|
||||
}
|
||||
|
||||
public void testRunStarted(String name) {}
|
||||
|
||||
+7
-1
@@ -61,6 +61,12 @@ public class JUnitTestDiscoveryConfiguration extends TestDiscoveryConfiguration
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFrameworkPrefix() {
|
||||
return "j";
|
||||
}
|
||||
|
||||
private class JUnitTestDiscoveryRunnableState extends TestObject {
|
||||
public JUnitTestDiscoveryRunnableState(ExecutionEnvironment environment) {
|
||||
super(((JUnitConfiguration)myDelegate), environment);
|
||||
@@ -71,7 +77,7 @@ public class JUnitTestDiscoveryConfiguration extends TestDiscoveryConfiguration
|
||||
return new SearchForTestsTask(getProject(), myServerSocket) {
|
||||
@Override
|
||||
protected void search() throws ExecutionException {
|
||||
final Set<String> patterns = TestDiscoverySearchHelper.search(getProject(), getPosition(), getChangeList());
|
||||
final Set<String> patterns = TestDiscoverySearchHelper.search(getProject(), getPosition(), getChangeList(), getFrameworkPrefix());
|
||||
addClassesListToJavaParameters(patterns, FunctionUtil.<String>id(), "", false, getJavaParameters());
|
||||
}
|
||||
|
||||
|
||||
@@ -18,4 +18,8 @@ package com.intellij.junit4;
|
||||
import com.intellij.execution.TestDiscoveryListener;
|
||||
import com.intellij.rt.execution.junit.IDEAJUnitListenerEx;
|
||||
|
||||
public class JUnitTestDiscoveryListener extends TestDiscoveryListener implements IDEAJUnitListenerEx {}
|
||||
public class JUnitTestDiscoveryListener extends TestDiscoveryListener implements IDEAJUnitListenerEx {
|
||||
public String getFrameworkId() {
|
||||
return "j";
|
||||
}
|
||||
}
|
||||
|
||||
+7
-1
@@ -65,6 +65,12 @@ public class TestNGTestDiscoveryConfiguration extends TestDiscoveryConfiguration
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFrameworkPrefix() {
|
||||
return "g";
|
||||
}
|
||||
|
||||
private class TestNGTestDiscoveryRunnableState extends TestNGRunnableState {
|
||||
public TestNGTestDiscoveryRunnableState(ExecutionEnvironment environment) {
|
||||
super(environment, ((TestNGConfiguration)myDelegate));
|
||||
@@ -76,7 +82,7 @@ public class TestNGTestDiscoveryConfiguration extends TestDiscoveryConfiguration
|
||||
@Override
|
||||
protected void search() throws CantRunException {
|
||||
myClasses.clear();
|
||||
final Set<String> patterns = TestDiscoverySearchHelper.search(getProject(), getPosition(), getChangeList());
|
||||
final Set<String> patterns = TestDiscoverySearchHelper.search(getProject(), getPosition(), getChangeList(), getFrameworkPrefix());
|
||||
final Module module = getConfigurationModule().getModule();
|
||||
final GlobalSearchScope searchScope =
|
||||
module != null ? GlobalSearchScope.moduleWithDependenciesScope(module) : GlobalSearchScope.projectScope(getProject());
|
||||
|
||||
@@ -53,4 +53,9 @@ public class TestNGTestDiscoveryListener extends TestDiscoveryListener implement
|
||||
public void onFinish(ISuite suite) {
|
||||
testRunFinished(suite.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFrameworkId() {
|
||||
return "g";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user