search for tests in directory restored for down-up approach
authorAnna.Kozlova <anna.kozlova@jetbrains.com>
Thu, 8 Feb 2018 12:43:44 +0000 (13:43 +0100)
committerAnna.Kozlova <anna.kozlova@jetbrains.com>
Thu, 8 Feb 2018 13:05:23 +0000 (14:05 +0100)
extended scope to ensure that the directory itself is included for new search

java/java-tests/testData/junit/configurations/module1/TestA.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/java/execution/ConfigurationsTest.java
plugins/junit/src/com/intellij/execution/junit/TestDirectory.java
plugins/junit/src/com/intellij/execution/junit/TestPackage.java

diff --git a/java/java-tests/testData/junit/configurations/module1/TestA.java b/java/java-tests/testData/junit/configurations/module1/TestA.java
new file mode 100644 (file)
index 0000000..3f935be
--- /dev/null
@@ -0,0 +1,6 @@
+package test1;
+
+import junit.framework.TestCase;
+
+public class TestA extends TestCase {
+}
\ No newline at end of file
index c040cbfad33ade3f38d8dd6cb62c712753271445..09a95dcb0c35bca8d1729955ad60c5e0c9d1543d 100644 (file)
@@ -185,6 +185,31 @@ public class ConfigurationsTest extends BaseConfigurationTestCase {
       lines);
   }
 
+
+  public void testRunningAllInDirectory() throws IOException, ExecutionException {
+    Module module1 = getModule1();
+    PsiClass psiClass = findTestA(module1);
+
+    JUnitConfiguration configuration =
+      new JUnitConfiguration("", myProject, JUnitConfigurationType.getInstance().getConfigurationFactories()[0]);
+    configuration.getPersistentData().TEST_OBJECT = JUnitConfiguration.TEST_DIRECTORY;
+    configuration.getPersistentData().setDirName(psiClass.getContainingFile().getContainingDirectory().getVirtualFile().getPath());
+    configuration.setModule(module1);
+    JavaParameters parameters = checkCanRun(configuration);
+    String filePath = ContainerUtil.find(parameters.getProgramParametersList().getArray(),
+                                         value -> StringUtil.startsWithChar(value, '@') && !StringUtil.startsWith(value, "@w@")).substring(1);
+    List<String> lines = readLinesFrom(new File(filePath));
+    lines.remove(0);
+    Assertion.compareUnordered(
+      //category, filters, classNames...
+      new Object[]{"", "", psiClass.getQualifiedName(),
+        "test1.DerivedTest", RT_INNER_TEST_NAME,
+        "test1.nested.TestA",
+        "test1.nested.TestWithJunit4",
+        "test1.ThirdPartyTest"},
+      lines);
+  }
+
   public void testRunAllInPackageWhenPackageIsEmptyInModule() throws ExecutionException {
     assignJdk(getModule2());
     JUnitConfiguration configuration =
index a013434875e0faacecf8f9f58d6528874002c8d8..1bcc35badefd006d6698614882e33944f6a22b21 100644 (file)
@@ -25,17 +25,20 @@ import com.intellij.execution.runners.ExecutionEnvironment;
 import com.intellij.execution.testframework.SourceScope;
 import com.intellij.execution.util.JavaParametersUtil;
 import com.intellij.execution.util.ProgramParametersUtil;
+import com.intellij.openapi.application.ReadAction;
 import com.intellij.openapi.module.Module;
 import com.intellij.openapi.module.ModuleUtilCore;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.util.io.FileUtil;
+import com.intellij.openapi.util.registry.Registry;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.*;
 import com.intellij.psi.search.GlobalSearchScope;
 import com.intellij.psi.search.GlobalSearchScopesCore;
+import com.intellij.util.ObjectUtils;
 import org.jetbrains.annotations.Nullable;
 
 import java.nio.file.Path;
@@ -51,8 +54,17 @@ class TestDirectory extends TestPackage {
   public SourceScope getSourceScope() {
     final String dirName = getConfiguration().getPersistentData().getDirName();
     final VirtualFile file = LocalFileSystem.getInstance().findFileByPath(FileUtil.toSystemIndependentName(dirName));
-    final GlobalSearchScope globalSearchScope = file == null ? GlobalSearchScope.EMPTY_SCOPE : GlobalSearchScopesCore.directoryScope(
-      getConfiguration().getProject(), file, true);
+    final Project project = getConfiguration().getProject();
+    final GlobalSearchScope globalSearchScope;
+    if (file == null) {
+      globalSearchScope = GlobalSearchScope.EMPTY_SCOPE;
+    }
+    else {
+      //package created by directory getDirectories(scope) should return the directory itself,
+      // currently the parent directory should be specified for scope
+      VirtualFile scopeDirectory = Registry.is("junit4.search.4.tests.all.in.scope", true) ? ObjectUtils.notNull(file.getParent(), file) : file;
+      globalSearchScope = GlobalSearchScopesCore.directoryScope(project, scopeDirectory, true);
+    }
     return new SourceScope() {
       @Override
       public GlobalSearchScope getGlobalSearchScope() {
@@ -61,14 +73,14 @@ class TestDirectory extends TestPackage {
 
       @Override
       public Project getProject() {
-        return getConfiguration().getProject();
+        return project;
       }
 
       @Override
       public GlobalSearchScope getLibrariesScope() {
         final Module module = getConfiguration().getConfigurationModule().getModule();
-        return module != null ? GlobalSearchScope.moduleWithLibrariesScope(module) : GlobalSearchScope.allScope(
-          getConfiguration().getProject());
+        return module != null ? GlobalSearchScope.moduleWithLibrariesScope(module)
+                              : GlobalSearchScope.allScope(project);
       }
 
       @Override
@@ -130,11 +142,11 @@ class TestDirectory extends TestPackage {
     if (file == null) {
       throw new CantRunException("Directory \'" + dirName + "\' is not found");
     }
-    final PsiDirectory directory = PsiManager.getInstance(getConfiguration().getProject()).findDirectory(file);
+    final PsiDirectory directory = ReadAction.compute(() -> PsiManager.getInstance(getConfiguration().getProject()).findDirectory(file));
     if (directory == null) {
       throw new CantRunException("Directory \'" + dirName + "\' is not found");
     }
-    return null;
+    return ReadAction.compute(() -> JavaDirectoryService.getInstance().getPackage(directory));
   }
 
   @Override
index b6bbdc5a42d95d5e9b64f8fabaa5c897c080971b..2b33f85427bba8d530cf205861b92360968a46cc 100644 (file)
@@ -84,8 +84,7 @@ public class TestPackage extends TestObject {
             LOG.assertTrue(classFilter.getBase() != null);
             long start = System.currentTimeMillis();
             if (Registry.is("junit4.search.4.tests.all.in.scope", true)) {
-              String packageName = getPackageName(data);
-              PsiPackage aPackage = JavaPsiFacade.getInstance(myProject).findPackage(packageName);
+              PsiPackage aPackage = getPackage(data);
               if (aPackage != null) {
                 collectClassesRecursively(aPackage, GlobalSearchScope.projectScope(myProject).intersectWith(classFilter.getScope()),
                                           aClass -> ReadAction.compute(() -> classFilter.isAccepted(aClass)));