[platform tests] refactoring: remove DirectoryIndexTestCase

This is needed to get rid of the obsolete DirectoryIndex class (IDEA-276394).

GitOrigin-RevId: 96e32af644611015999cfe2ad22c4ccb755136f5
This commit is contained in:
Nikolay Chashnikov
2023-02-02 23:37:38 +00:00
committed by intellij-monorepo-bot
parent 5fe1004244
commit 0d36350bbd
6 changed files with 55 additions and 163 deletions
@@ -82,7 +82,7 @@ class ContentRootWithExcludedPatternsInProjectFileIndexTest {
assertNotExcluded(java2)
fileIndex.assertInModule(java3, module, moduleDir, IN_CONTENT or IN_SOURCE)
fileIndex.assertInModule(java4, module, moduleDir, IN_CONTENT or IN_SOURCE or IN_TEST_SOURCE)
DirectoryIndexTestCase.assertIteratedContent(module, listOf(java1, java2), listOf(txt1, txt2))
assertIteratedContent(module, listOf(java1, java2), listOf(txt1, txt2))
}
@Test
@@ -114,7 +114,7 @@ class ContentRootWithExcludedPatternsInProjectFileIndexTest {
assertExcluded(underExcUnderDir)
assertNotExcluded(dir)
assertNotExcluded(underDir)
DirectoryIndexTestCase.assertIteratedContent(module, listOf(underDir), Arrays.asList(underExc, underDirUnderExc, underExcUnderDir))
assertIteratedContent(module, listOf(underDir), Arrays.asList(underExc, underDirUnderExc, underExcUnderDir))
}
@Test
@@ -3,7 +3,6 @@ package com.intellij.openapi.roots.impl
import com.intellij.openapi.fileTypes.FileTypeRegistry
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.roots.impl.DirectoryIndexTestCase.assertIteratedContent
import com.intellij.openapi.util.io.IoTestUtil
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFile
@@ -1,145 +0,0 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.roots.impl;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.*;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.FileIndex;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.HeavyPlatformTestCase;
import com.intellij.testFramework.PsiTestUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.indexing.FileBasedIndex;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.java.JavaSourceRootType;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public abstract class DirectoryIndexTestCase extends HeavyPlatformTestCase {
protected DirectoryIndexImpl myIndex;
protected ProjectFileIndex myFileIndex;
@Override
protected void setUp() throws Exception {
super.setUp();
myIndex = (DirectoryIndexImpl)DirectoryIndex.getInstance(myProject);
myFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
}
@Override
protected void tearDown() throws Exception {
myFileIndex = null;
myIndex = null;
super.tearDown();
}
protected void assertNotInProject(VirtualFile file) {
DirectoryInfo info = myIndex.getInfoForFile(file);
assertFalse(info.toString(), myFileIndex.isInProject(file));
assertFalse(info.toString(), info.isExcluded(file));
assertNull(info.toString(), info.getUnloadedModuleName());
}
protected void assertExcluded(@NotNull VirtualFile file, Module module) {
DirectoryInfo info = myIndex.getInfoForFile(file);
assertTrue(file + " " + info, myFileIndex.isExcluded(file));
assertNull(file + " " + info, info.getUnloadedModuleName());
assertEquals(module, myFileIndex.getModuleForFile(file, false));
assertFalse(myFileIndex.isInSource(file));
assertFalse(myFileIndex.isInSourceContent(file));
assertFalse(myFileIndex.isInTestSourceContent(file));
assertFalse(myFileIndex.isUnderSourceRootOfType(file, ContainerUtil.set(JavaSourceRootType.SOURCE, JavaSourceRootType.TEST_SOURCE)));
}
protected void assertInLibrarySources(VirtualFile file, Module module) {
assertTrue(file.getPresentableUrl(), myFileIndex.isInLibrarySource(file));
assertEquals(file.getPresentableUrl(), module, myFileIndex.getModuleForFile(file));
}
protected void assertNotInLibrarySources(VirtualFile file, Module module) {
assertFalse(file.getPresentableUrl(), myFileIndex.isInLibrarySource(file));
assertEquals(file.getPresentableUrl(), module, myFileIndex.getModuleForFile(file));
}
protected DirectoryInfo assertInProject(VirtualFile file) {
assertTrue(file.toString(), myFileIndex.isInProject(file));
DirectoryInfo info = myIndex.getInfoForFile(file);
assertNull(info.toString(), info.getUnloadedModuleName());
myIndex.assertConsistency(info);
return info;
}
protected void assertNotExcluded(VirtualFile file) {
assertFalse(myFileIndex.isExcluded(file));
}
protected void assertExcludedFromProject(VirtualFile file) {
assertExcluded(file, null);
}
static void assertIteratedContent(@NotNull Module module, @Nullable List<VirtualFile> mustContain, @Nullable List<VirtualFile> mustNotContain) {
assertIteratedContent(ModuleRootManager.getInstance(module).getFileIndex(), mustContain, mustNotContain);
assertIteratedContent(ProjectFileIndex.getInstance(module.getProject()), mustContain, mustNotContain);
}
protected void assertIndexableContent(@Nullable List<VirtualFile> mustContain, @Nullable List<VirtualFile> mustNotContain) {
final Set<VirtualFile> collected = new HashSet<>();
FileBasedIndex.getInstance().iterateIndexableFiles(fileOrDir -> {
if (!collected.add(fileOrDir)) {
fail(fileOrDir + " visited twice");
}
return true;
}, getProject(), new EmptyProgressIndicator());
if (mustContain != null) assertContainsElements(collected, mustContain);
if (mustNotContain != null) assertDoesntContain(collected, mustNotContain);
}
protected static void assertIteratedContent(@NotNull FileIndex fileIndex,
@Nullable List<VirtualFile> mustContain,
@Nullable List<VirtualFile> mustNotContain) {
final Set<VirtualFile> collected = new HashSet<>();
fileIndex.iterateContent(fileOrDir -> {
if (!collected.add(fileOrDir)) {
fail(fileOrDir + " visited twice");
}
return true;
});
if (mustContain != null) assertContainsElements(collected, mustContain);
if (mustNotContain != null) assertDoesntContain(collected, mustNotContain);
}
protected static void assertIteratedContent(@NotNull FileIndex fileIndex,
@NotNull VirtualFile root,
@Nullable List<VirtualFile> mustContain,
@Nullable List<VirtualFile> mustNotContain) {
final Set<VirtualFile> collected = new HashSet<>();
fileIndex.iterateContentUnderDirectory(root, fileOrDir -> {
if (!collected.add(fileOrDir)) {
fail(fileOrDir + " visited twice");
}
return true;
});
if (mustContain != null) assertContainsElements(collected, mustContain);
if (mustNotContain != null) assertDoesntContain(collected, mustNotContain);
}
@NotNull
protected static Module createJavaModuleWithContent(@NotNull Project project, @NotNull String name, @NotNull VirtualFile contentRoot) {
ModuleType<?> type = ModuleTypeManager.getInstance().findByID(ModuleTypeId.JAVA_MODULE);
return WriteCommandAction.writeCommandAction(project).compute(() -> {
ModifiableModuleModel moduleModel = ModuleManager.getInstance(project).getModifiableModel();
Module module = moduleModel.newModule(contentRoot.toNioPath().resolve(name + ".iml"), type.getId());
moduleModel.commit();
assertNotNull(module);
PsiTestUtil.addContentRoot(module, contentRoot);
return module;
});
}
}
@@ -153,7 +153,7 @@ class ModuleRootsInProjectFileIndexTest {
PsiTestUtil.addSourceRoot(module, srcDir)
assertInContentSource(file)
DirectoryIndexTestCase.assertIteratedContent(module, listOf(srcDir, file), listOf(excludedDir))
assertIteratedContent(module, listOf(srcDir, file), listOf(excludedDir))
PsiTestUtil.removeSourceRoot(module, srcDir)
assertExcludedFromModule(file)
@@ -189,12 +189,12 @@ class ModuleRootsInProjectFileIndexTest {
PsiTestUtil.addSourceRoot(module, contentSourceDir)
fileIndex.assertInModule(file, module, contentSourceDir, IN_CONTENT or IN_SOURCE)
assertExcludedFromModule(excludedFile)
DirectoryIndexTestCase.assertIteratedContent(module, listOf(file, contentSourceDir), listOf(excludedDir, excludedFile))
assertIteratedContent(module, listOf(file, contentSourceDir), listOf(excludedDir, excludedFile))
PsiTestUtil.removeSourceRoot(module, contentSourceDir)
PsiTestUtil.removeContentEntry(module, contentSourceDir)
assertExcludedFromModule(file)
DirectoryIndexTestCase.assertIteratedContent(module, emptyList(), listOf(file, contentSourceDir, excludedDir, excludedFile))
assertIteratedContent(module, emptyList(), listOf(file, contentSourceDir, excludedDir, excludedFile))
}
@Test
@@ -208,15 +208,15 @@ class ModuleRootsInProjectFileIndexTest {
PsiTestUtil.addSourceRoot(module, excludedSourceDir)
assertInContentSource(file)
DirectoryIndexTestCase.assertIteratedContent(module, listOf(file, excludedSourceDir), listOf(excludedDir))
assertIteratedContent(module, listOf(file, excludedSourceDir), listOf(excludedDir))
PsiTestUtil.addExcludedRoot(module, excludedSourceDir)
assertExcludedFromModule(file)
DirectoryIndexTestCase.assertIteratedContent(module, emptyList(), listOf(file, excludedSourceDir, excludedDir))
assertIteratedContent(module, emptyList(), listOf(file, excludedSourceDir, excludedDir))
PsiTestUtil.removeExcludedRoot(module, excludedSourceDir)
assertInContentSource(file)
DirectoryIndexTestCase.assertIteratedContent(module, listOf(file, excludedSourceDir), listOf(excludedDir))
assertIteratedContent(module, listOf(file, excludedSourceDir), listOf(excludedDir))
}
@Test
@@ -252,7 +252,7 @@ class ModuleRootsInProjectFileIndexTest {
assertInModule(moduleDir)
assertExcludedFromModule(srcDir)
assertExcludedFromModule(file)
DirectoryIndexTestCase.assertIteratedContent(module, listOf(moduleDir), listOf(file, srcDir, excludedDir))
assertIteratedContent(module, listOf(moduleDir), listOf(file, srcDir, excludedDir))
}
@Test
@@ -298,11 +298,11 @@ class ModuleRootsInProjectFileIndexTest {
PsiTestUtil.addExcludedRoot(module, file)
assertExcludedFromModule(file)
DirectoryIndexTestCase.assertIteratedContent(module, listOf(moduleDir), listOf(file))
assertIteratedContent(module, listOf(moduleDir), listOf(file))
PsiTestUtil.removeExcludedRoot(module, file)
assertInModule(file)
DirectoryIndexTestCase.assertIteratedContent(module, listOf(moduleDir, file), emptyList())
assertIteratedContent(module, listOf(moduleDir, file), emptyList())
}
@Test
@@ -313,11 +313,11 @@ class ModuleRootsInProjectFileIndexTest {
PsiTestUtil.addExcludedRoot(module, file)
fileIndex.assertInModule(file, module, file, EXCLUDED)
DirectoryIndexTestCase.assertIteratedContent(module, emptyList(), listOf(file))
assertIteratedContent(module, emptyList(), listOf(file))
PsiTestUtil.removeExcludedRoot(module, file)
fileIndex.assertInModule(file, module, file)
DirectoryIndexTestCase.assertIteratedContent(module, listOf(file), emptyList())
assertIteratedContent(module, listOf(file), emptyList())
}
@Test
@@ -86,8 +86,8 @@ class NestedModuleRootsInProjectFileIndex {
PsiTestUtil.addContentRoot(outerModule, outerFile.parent)
PsiTestUtil.addContentRoot(innerModule, innerFile.parent)
val moduleFileIndex = ModuleRootManager.getInstance(outerModule).fileIndex
DirectoryIndexTestCase.assertIteratedContent(moduleFileIndex, outerFile.parent,
listOf(outerFile, outerFile.parent), listOf(innerFile, innerFile.parent))
assertIteratedContent(moduleFileIndex, outerFile.parent,
listOf(outerFile, outerFile.parent), listOf(innerFile, innerFile.parent))
}
/**
@@ -2,10 +2,16 @@
package com.intellij.openapi.roots.impl
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.FileIndex
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.testFramework.UsefulTestCase
import junit.framework.TestCase
import org.intellij.lang.annotations.MagicConstant
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.fail
object ProjectFileIndexScopes {
@@ -77,4 +83,36 @@ object ProjectFileIndexScopes {
fail("${file.presentableUrl} expected to be ${if (expected) "in" else "not in"} $description, but it is ${if (actual) "in" else "not in"} $description")
}
}
}
}
internal fun assertIteratedContent(module: Module, mustContain: List<VirtualFile>?, mustNotContain: List<VirtualFile>?) {
assertIteratedContent(ModuleRootManager.getInstance(module).fileIndex, mustContain, mustNotContain)
assertIteratedContent(ProjectFileIndex.getInstance(module.project), mustContain, mustNotContain)
}
private fun assertIteratedContent(fileIndex: FileIndex, mustContain: List<VirtualFile>?, mustNotContain: List<VirtualFile>?) {
val collected = HashSet<VirtualFile>()
fileIndex.iterateContent { fileOrDir: VirtualFile ->
if (!collected.add(fileOrDir)) {
fail("$fileOrDir visited twice")
}
true
}
if (mustContain != null) UsefulTestCase.assertContainsElements(collected, mustContain)
if (mustNotContain != null) UsefulTestCase.assertDoesntContain(collected, mustNotContain)
}
internal fun assertIteratedContent(fileIndex: FileIndex,
root: VirtualFile,
mustContain: List<VirtualFile>?,
mustNotContain: List<VirtualFile>?) {
val collected = HashSet<VirtualFile>()
fileIndex.iterateContentUnderDirectory(root) { fileOrDir: VirtualFile ->
if (!collected.add(fileOrDir)) {
fail("$fileOrDir visited twice")
}
true
}
if (mustContain != null) UsefulTestCase.assertContainsElements(collected, mustContain)
if (mustNotContain != null) UsefulTestCase.assertDoesntContain(collected, mustNotContain)
}