Test for not indexing todo in libraries

This commit is contained in:
Dmitry Trofimov
2017-06-14 19:38:56 +03:00
committed by Alexander Zolotov
parent 42a8c58fa5
commit caf1d3ab02
5 changed files with 82 additions and 0 deletions
@@ -40,6 +40,10 @@ public class ModuleRootModificationUtil {
updateModel(module, model -> model.addContentEntry(VfsUtilCore.pathToUrl(path)));
}
public static void addContentRoot(@NotNull Module module, @NotNull VirtualFile path) {
updateModel(module, model -> model.addContentEntry(path));
}
public static void addModuleLibrary(@NotNull Module module,
@Nullable String libName,
@NotNull List<String> classesRoots,
@@ -0,0 +1 @@
# TODO
@@ -15,12 +15,31 @@
*/
package com.jetbrains.python;
import com.google.common.collect.Lists;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkModificator;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.cache.impl.IndexPatternUtil;
import com.intellij.psi.impl.cache.impl.todo.TodoIndex;
import com.intellij.psi.impl.cache.impl.todo.TodoIndexEntry;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.IndexPattern;
import com.intellij.psi.stubs.StubUpdatingIndex;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.python.fixtures.PyTestCase;
import com.jetbrains.python.psi.stubs.PyModuleNameIndex;
import com.jetbrains.python.sdk.PythonSdkType;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.List;
/**
* @author vlan
@@ -30,6 +49,7 @@ public class PyIndexingTest extends PyTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
final String testName = getTestName(false);
myFixture.copyDirectoryToProject(TEST_DIRECTORY + testName, "");
@@ -44,6 +64,63 @@ public class PyIndexingTest extends PyTestCase {
assertDoesntContain(modules, "ModuleNameIndex_baz");
}
private static List<VirtualFile> getTodoFiles(@NotNull Project project) {
final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
List<VirtualFile> files = Lists.newArrayList();
for (IndexPattern indexPattern : IndexPatternUtil.getIndexPatterns()) {
files.addAll(fileBasedIndex.getContainingFiles(
TodoIndex.NAME,
new TodoIndexEntry(indexPattern.getPatternString(), indexPattern.isCaseSensitive()), GlobalSearchScope.allScope(project)));
}
return files;
}
public void testTodoIndexInLibs() {
Sdk sdk = PythonSdkType.findPythonSdk(myFixture.getModule());
PsiFile file = myFixture.addFileToProject("libs/smtpd.py", "# TODO: fix it");
VirtualFile root = sdk.getRootProvider().getFiles(OrderRootType.CLASSES)[0];
VirtualFile libsRoot = file.getVirtualFile().getParent();
ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(myFixture.getModule()).getModifiableModel();
modifiableModel.clear();
modifiableModel.addContentEntry(myFixture.findFileInTempDir("project"));
SdkModificator sdkModificator = sdk.getSdkModificator();
sdkModificator.addRoot(libsRoot, OrderRootType.CLASSES);
ApplicationManager.getApplication().runWriteAction(() -> {
modifiableModel.commit();
sdkModificator.commitChanges();
});
try {
List<VirtualFile> indexFiles = getTodoFiles(myFixture.getProject());
// project file in the TodoIndex
assertTrue(indexFiles.stream().anyMatch((x) -> "a.py".equals(x.getName())));
// no library files in the TodoIndex
assertFalse(indexFiles.stream().anyMatch((x) -> "smtpd.py".equals(x.getName())));
ModuleRootModificationUtil.addContentRoot(myFixture.getModule(), libsRoot);
indexFiles = getTodoFiles(myFixture.getProject());
// but if it is added as a content root - it should be in the TodoIndex
assertTrue(indexFiles.stream().anyMatch((x) -> "smtpd.py".equals(x.getName())));
} finally {
// revert changes to sdk roots
SdkModificator modificator = sdk.getSdkModificator();
modificator.removeRoot(libsRoot, OrderRootType.CLASSES);
modificator.addRoot(root, OrderRootType.CLASSES);
ApplicationManager.getApplication().runWriteAction(() -> {
modificator.commitChanges();
});
}
}
// PY-19047
public void testPy19047() {
FileBasedIndex.getInstance().scheduleRebuild(StubUpdatingIndex.INDEX_ID, new Throwable());