From af2186f67485d36952282304624eb306d2f96ba7 Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Mon, 23 Dec 2024 17:13:40 +0100 Subject: [PATCH] [java-test] IDEA-357036 Skip tests for files outside file roots GitOrigin-RevId: 5c7dbcde23fe3d239db6cc95a893634971a78a39 --- .../JavaCodeInsightSanityTest.java | 10 +++++- .../propertyBased/MadTestingUtil.java | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaCodeInsightSanityTest.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaCodeInsightSanityTest.java index 16e721d0430a..a34f7184b4e0 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaCodeInsightSanityTest.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaCodeInsightSanityTest.java @@ -8,6 +8,8 @@ import com.intellij.lang.java.JavaLanguage; import com.intellij.openapi.application.PathManager; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl; +import com.intellij.openapi.roots.ProjectFileIndex; +import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.RecursionManager; import com.intellij.openapi.util.text.StringUtil; @@ -121,7 +123,13 @@ public class JavaCodeInsightSanityTest extends LightJavaCodeInsightFixtureTestCa } private @NotNull Supplier actionsOnJavaFiles(Function> fileActions) { - return MadTestingUtil.actionsOnFileContents(myFixture, PathManager.getHomePath(), f -> f.getName().endsWith(".java"), fileActions); + return MadTestingUtil.actionsOnFileContents(myFixture, PathManager.getHomePath(), f -> f.getName().endsWith(".java"), + f -> { + ProjectFileIndex projectFileIndex = + ProjectRootManager.getInstance(myFixture.getProject()).getFileIndex(); + return projectFileIndex.isInSource(f.getVirtualFile()); + }, + fileActions); } public void _testGenerator() { diff --git a/platform/testFramework/src/com/intellij/testFramework/propertyBased/MadTestingUtil.java b/platform/testFramework/src/com/intellij/testFramework/propertyBased/MadTestingUtil.java index ba19d9ad88c0..65d9e92c93f0 100644 --- a/platform/testFramework/src/com/intellij/testFramework/propertyBased/MadTestingUtil.java +++ b/platform/testFramework/src/com/intellij/testFramework/propertyBased/MadTestingUtil.java @@ -271,6 +271,20 @@ public final class MadTestingUtil { env.executeCommands(Generator.from(data -> data.generate(actions.apply(fixture.getPsiManager().findFile(vFile)))))); } + /** + * Finds files under {@code rootPath} (e.g. test data root) satisfying {@code fileFilter condition} (e.g. correct extension) and uses {@code actions} to generate actions on those files (e.g. invoke completion/intentions or random editing). + * Almost: the files with same paths and contents are created inside the test project, then the actions are executed on them. + * Note that the test project contains only one file at each moment, so it's best to test actions that don't require much environment. + */ + @NotNull + public static Supplier actionsOnFileContents(CodeInsightTestFixture fixture, String rootPath, + FileFilter fileFilter, + @Nullable Predicate psiFileFilter, + Function> actions) { + return performOnFileContents(fixture, rootPath, fileFilter, psiFileFilter,(env, vFile) -> + env.executeCommands(Generator.from(data -> data.generate(actions.apply(fixture.getPsiManager().findFile(vFile)))))); + } + /** * Finds files under {@code rootPath} (e.g. test data root) satisfying {@code fileFilter condition} (e.g. correct extension) and invokes {@code action} on those files. * Almost: the files with same paths and contents are created inside the test project, then the actions are executed on them. @@ -281,6 +295,22 @@ public final class MadTestingUtil { String rootPath, FileFilter fileFilter, BiConsumer action) { + + return performOnFileContents(fixture, rootPath, fileFilter, null, action); + } + + /** + * Finds files under {@code rootPath} (e.g. test data root) satisfying {@code fileFilter condition} (e.g. correct extension) and invokes {@code action} on those files. + * Almost: the files with same paths and contents are created inside the test project, then the actions are executed on them. + * Note that the test project contains only one file at each moment, so it's best to test actions that don't require much environment. + * @param psiFileFilter can be used to filter based on psi or project structure + */ + @NotNull + public static Supplier performOnFileContents(CodeInsightTestFixture fixture, + String rootPath, + FileFilter fileFilter, + @Nullable Predicate psiFileFilter, + BiConsumer action) { Generator randomFiles = randomFiles(rootPath, fileFilter); return () -> assertNoErrorLoggedIn(env -> new RunAll( () -> { @@ -292,6 +322,9 @@ public final class MadTestingUtil { System.err.println("Can't check " + vFile + " due to incorrect file type: " + psiFile + " of " + psiFile.getClass()); return; } + if (psiFileFilter != null && !psiFileFilter.test(psiFile)) { + return; + } action.accept(env, vFile); }, () -> WriteAction.run(() -> {