ProjectUtil.isSameProject check simplified and fixed to properly support directory-based projects, test added (IDEA-CR-15060)

This commit is contained in:
nik
2016-10-31 10:55:43 +03:00
parent dfdf276775
commit 47f85cf593
2 changed files with 41 additions and 14 deletions
@@ -283,26 +283,26 @@ public class ProjectUtil {
public static boolean isSameProject(String projectFilePath, @NotNull Project project) {
IProjectStore projectStore = ProjectKt.getStateStore(project);
String toOpen = FileUtil.toSystemIndependentName(projectFilePath);
String existing = projectStore.getProjectFilePath();
String existingBaseDir = projectStore.getProjectBasePath();
if (existingBaseDir == null) {
String existingBaseDirPath = projectStore.getProjectBasePath();
if (existingBaseDirPath == null) {
// could be null if not yet initialized
return false;
}
final File openFile = new File(toOpen);
if (openFile.isDirectory()) {
return FileUtil.pathsEqual(toOpen, existingBaseDir);
}
if (StorageScheme.DIRECTORY_BASED == projectStore.getStorageScheme()) {
// todo: check if IPR is located not under the project base dir
return FileUtil.pathsEqual(FileUtil.toSystemIndependentName(openFile.getParentFile().getPath()), existingBaseDir);
final File projectFile = new File(projectFilePath);
if (projectFile.isDirectory()) {
return FileUtil.pathsEqual(projectFilePath, existingBaseDirPath);
}
return FileUtil.pathsEqual(toOpen, existing);
if (projectStore.getStorageScheme() == StorageScheme.DEFAULT) {
return FileUtil.pathsEqual(projectFilePath, projectStore.getProjectFilePath());
}
File parent = projectFile.getParentFile();
if (parent.getName().equals(Project.DIRECTORY_STORE_FOLDER)) {
parent = parent.getParentFile();
}
return parent != null && FileUtil.pathsEqual(parent.getPath(), existingBaseDirPath);
}
public static void focusProjectWindow(final Project p, boolean executeIfAppInactive) {
@@ -15,6 +15,7 @@
*/
package com.intellij.openapi.project.impl;
import com.intellij.ide.impl.ProjectUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
@@ -29,6 +30,7 @@ import com.intellij.testFramework.PlatformTestUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import static com.intellij.openapi.startup.StartupActivity.POST_STARTUP_ACTIVITY;
@@ -85,6 +87,31 @@ public class ProjectOpeningTest extends PlatformTestCase {
*/
public void testIsSameProjectForDirectoryBasedProject() throws IOException {
File projectDir = createTempDir("project");
Project dirBasedProject = ProjectManager.getInstance().createProject("project", projectDir.getAbsolutePath());
disposeOnTearDown(dirBasedProject);
assertTrue(ProjectUtil.isSameProject(projectDir.getAbsolutePath(), dirBasedProject));
assertFalse(ProjectUtil.isSameProject(createTempDir("project2").getAbsolutePath(), dirBasedProject));
File iprFilePath = new File(projectDir, "project.ipr");
assertTrue(ProjectUtil.isSameProject(iprFilePath.getAbsolutePath(), dirBasedProject));
File miscXmlFilePath = new File(projectDir, ".idea/misc.xml");
assertTrue(ProjectUtil.isSameProject(miscXmlFilePath.getAbsolutePath(), dirBasedProject));
}
public void testIsSameProjectForFileBasedProject() throws IOException {
File projectDir = createTempDir("project");
File iprFilePath = new File(projectDir, "project.ipr");
Project fileBasedProject = ProjectManager.getInstance().createProject(iprFilePath.getName(), iprFilePath.getAbsolutePath());
disposeOnTearDown(fileBasedProject);
assertTrue(ProjectUtil.isSameProject(projectDir.getAbsolutePath(), fileBasedProject));
assertFalse(ProjectUtil.isSameProject(createTempDir("project2").getAbsolutePath(), fileBasedProject));
File iprFilePath2 = new File(projectDir, "project2.ipr");
assertFalse(ProjectUtil.isSameProject(iprFilePath2.getAbsolutePath(), fileBasedProject));
}
private static void closeProject(final Project project) {
if (project != null && !project.isDisposed()) {
ProjectManager.getInstance().closeProject(project);