[java-test] IDEA-357036 Skip tests for files outside file roots

GitOrigin-RevId: 5c7dbcde23fe3d239db6cc95a893634971a78a39
This commit is contained in:
Mikhail Pyltsin
2024-12-23 18:12:33 +00:00
committed by intellij-monorepo-bot
parent 0a7c7800b7
commit af2186f674
2 changed files with 42 additions and 1 deletions
@@ -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<MadTestingAction> actionsOnJavaFiles(Function<PsiFile, Generator<? extends MadTestingAction>> 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() {
@@ -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<MadTestingAction> actionsOnFileContents(CodeInsightTestFixture fixture, String rootPath,
FileFilter fileFilter,
@Nullable Predicate<PsiFile> psiFileFilter,
Function<? super PsiFile, ? extends Generator<? extends MadTestingAction>> 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<? super ImperativeCommand.Environment, ? super VirtualFile> 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<MadTestingAction> performOnFileContents(CodeInsightTestFixture fixture,
String rootPath,
FileFilter fileFilter,
@Nullable Predicate<PsiFile> psiFileFilter,
BiConsumer<? super ImperativeCommand.Environment, ? super VirtualFile> action) {
Generator<File> 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(() -> {