From e1a03fe6a34af8309d72ba032126884a221c8be6 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 9 Jul 2019 12:24:07 +0200 Subject: [PATCH] =?UTF-8?q?use=20Path=20instead=20of=20VFS=20to=20open/imp?= =?UTF-8?q?ort/create=20project=20=E2=80=94=20=20ProjectManagerEx.newProje?= =?UTF-8?q?ct?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GitOrigin-RevId: 63b4d5441167cbf8e8d8994e45bd7d07458a9f11 --- .../SaveProjectAsTemplateTest.java | 2 +- .../ex/ProjectInspectionManagerTest.kt | 4 +-- .../impl/DaemonRespondToChangesTest.java | 1 - .../org/jetbrains/ide/BuiltInWebServerTest.kt | 8 ++--- .../testSrc/org/jetbrains/ide/TestManager.kt | 3 +- .../testSrc/ProjectStoreTest.kt | 16 ++++----- .../platform/ModuleAttachProcessor.kt | 2 +- .../intellij/ide/macro/MacroManagerTest.java | 19 ++--------- .../src/com/intellij/openapi/vfs/VfsUtil.java | 13 ++++--- .../ide/RecentProjectsManagerBase.java | 5 +-- .../com/intellij/ide/impl/ProjectUtil.java | 34 ++++++++++--------- .../openapi/project/ex/ProjectManagerEx.java | 11 ++---- .../project/impl/ProjectManagerImpl.java | 29 +++++++++------- .../intellij/formatting/FileSetTestCase.java | 6 ++-- .../OverwriteProjectConfigurationTest.java | 2 +- .../fileEditor/NonProjectFileAccessTest.java | 3 +- .../vfs/encoding/FileEncodingTest.java | 3 +- ...ficationTrackerTreeChangesUpdatesTest.java | 25 ++++---------- .../com/intellij/testFramework/FixtureRule.kt | 17 +++++----- .../testFramework/LightPlatformTestCase.java | 7 ++-- .../testFramework/PlatformTestCase.java | 19 +++++++---- .../testFramework/PlatformTestUtil.java | 27 ++++++++++++--- .../impl/HeavyIdeaTestFixtureImpl.java | 14 ++++---- .../vcs/LocalChangesUnderRootsTest.java | 25 ++++---------- .../changes/committed/VcsDirtyScopeTest.java | 5 +-- .../idea/devkit/PluginProjectWizardTest.java | 17 +--------- .../TestDataGuessByExistingFilesUtilTest.java | 24 ++----------- .../testAssistant/TestDataPathTestCase.java | 8 ++--- .../compiler/GradleCompilingTestCase.java | 3 +- .../service/task/GradleTaskManagerTest.kt | 10 +++--- .../dictionary/CustomDictionaryTest.java | 11 +++--- 31 files changed, 167 insertions(+), 206 deletions(-) diff --git a/java/idea-ui/testSrc/com/intellij/ide/projectWizard/SaveProjectAsTemplateTest.java b/java/idea-ui/testSrc/com/intellij/ide/projectWizard/SaveProjectAsTemplateTest.java index 38badbbb5f90..af40835fff56 100644 --- a/java/idea-ui/testSrc/com/intellij/ide/projectWizard/SaveProjectAsTemplateTest.java +++ b/java/idea-ui/testSrc/com/intellij/ide/projectWizard/SaveProjectAsTemplateTest.java @@ -161,7 +161,7 @@ public class SaveProjectAsTemplateTest extends NewProjectWizardTestCase { final Module module = super.createMainModule(); ApplicationManager.getApplication().runWriteAction(() -> { ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); - VirtualFile baseDir = module.getProject().getBaseDir(); + VirtualFile baseDir = PlatformTestUtil.getOrCreateProjectTestBaseDir(module.getProject()); ContentEntry entry = model.addContentEntry(baseDir); entry.addSourceFolder(baseDir, false); model.commit(); diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt b/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt index 2044cf469f50..2caddb0bbe01 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt +++ b/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt @@ -36,7 +36,7 @@ class ProjectInspectionManagerTest { private fun doTest(task: suspend (Project) -> Unit) { runBlocking { - loadAndUseProjectInLoadComponentStateMode(tempDirManager, { it.path }, task) + loadAndUseProjectInLoadComponentStateMode(tempDirManager, { Paths.get(it.path) }, task) } } @@ -224,7 +224,7 @@ class ProjectInspectionManagerTest { """.trimIndent() loadAndUseProjectInLoadComponentStateMode(tempDirManager, { - it.writeChild("test${ProjectFileType.DOT_DEFAULT_EXTENSION}", emptyProjectFile).path + Paths.get(it.writeChild("test${ProjectFileType.DOT_DEFAULT_EXTENSION}", emptyProjectFile).path) }) { project -> val projectInspectionProfileManager = ProjectInspectionProfileManager.getInstance(project) projectInspectionProfileManager.forceLoadSchemes() diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/impl/DaemonRespondToChangesTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/impl/DaemonRespondToChangesTest.java index 16bd841b1480..19d27d10739e 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/impl/DaemonRespondToChangesTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/impl/DaemonRespondToChangesTest.java @@ -1523,7 +1523,6 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase { String body = StringUtil.repeat("\"String field = null;\"\n", 1000); configureByText(StdFileTypes.JAVA, "class X{ void f() {" + body + "\n} }"); - File temp = createTempDirectory(); final Project alienProject = createProject(createTempDirectory().toPath().resolve("alien.ipr")); boolean succ2 = ProjectManagerEx.getInstanceEx().openProject(alienProject); assertTrue(succ2); diff --git a/platform/built-in-server/testSrc/org/jetbrains/ide/BuiltInWebServerTest.kt b/platform/built-in-server/testSrc/org/jetbrains/ide/BuiltInWebServerTest.kt index 7b68b05f2f23..68569699fc91 100644 --- a/platform/built-in-server/testSrc/org/jetbrains/ide/BuiltInWebServerTest.kt +++ b/platform/built-in-server/testSrc/org/jetbrains/ide/BuiltInWebServerTest.kt @@ -95,9 +95,9 @@ internal class HeavyBuiltInWebServerTest { @Test fun `path outside of project`() = runBlocking { val projectDir = tempDirManager.newPath().resolve("foo/bar") - val projectDirPath = projectDir.systemIndependentPath - createHeavyProject("$projectDirPath/test.ipr").use { project -> + createHeavyProject(projectDir.resolve("test.ipr")).use { project -> projectDir.createDirectories() + val projectDirPath = projectDir.systemIndependentPath LocalFileSystem.getInstance().refreshAndFindFileByPath(projectDirPath) createModule(projectDirPath, project) @@ -111,9 +111,9 @@ internal class HeavyBuiltInWebServerTest { @Test fun `file in hidden folder`() = runBlocking { val projectDir = tempDirManager.newPath().resolve("foo/bar") - val projectDirPath = projectDir.systemIndependentPath - createHeavyProject("$projectDirPath/test.ipr").use { project -> + createHeavyProject(projectDir.resolve("test.ipr")).use { project -> projectDir.createDirectories() + val projectDirPath = projectDir.systemIndependentPath LocalFileSystem.getInstance().refreshAndFindFileByPath(projectDirPath) createModule(projectDirPath, project) diff --git a/platform/built-in-server/testSrc/org/jetbrains/ide/TestManager.kt b/platform/built-in-server/testSrc/org/jetbrains/ide/TestManager.kt index 125aca0cd02e..4c62229f4c0e 100644 --- a/platform/built-in-server/testSrc/org/jetbrains/ide/TestManager.kt +++ b/platform/built-in-server/testSrc/org/jetbrains/ide/TestManager.kt @@ -7,6 +7,7 @@ import com.intellij.openapi.roots.ModuleRootModificationUtil import com.intellij.openapi.util.io.FileUtilRt import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.vfs.VirtualFile +import com.intellij.testFramework.PlatformTestUtil import com.intellij.testFramework.ProjectRule import com.intellij.testFramework.TemporaryDirectory import com.intellij.testFramework.runInEdtAndWait @@ -65,7 +66,7 @@ internal class TestManager(private val projectRule: ProjectRule, private val tem runInEdtAndWait { val normalizedFilePath = FileUtilRt.toSystemIndependentName(filePath!!) if (annotation!!.relativeToProject) { - val root = projectRule.project.baseDir + val root = PlatformTestUtil.getOrCreateProjectTestBaseDir(projectRule.project) runWriteAction { fileToDelete = root.findOrCreateChildData(this@TestManager, normalizedFilePath) } diff --git a/platform/configuration-store-impl/testSrc/ProjectStoreTest.kt b/platform/configuration-store-impl/testSrc/ProjectStoreTest.kt index dde413833263..a484a597b1a5 100644 --- a/platform/configuration-store-impl/testSrc/ProjectStoreTest.kt +++ b/platform/configuration-store-impl/testSrc/ProjectStoreTest.kt @@ -8,7 +8,6 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.project.ex.ProjectEx import com.intellij.openapi.project.ex.ProjectManagerEx import com.intellij.openapi.project.impl.ProjectImpl -import com.intellij.openapi.util.SystemInfo import com.intellij.openapi.util.io.FileUtilRt import com.intellij.openapi.vcs.readOnlyHandler.ReadonlyStatusHandlerImpl import com.intellij.openapi.vfs.ReadonlyStatusHandler @@ -25,15 +24,12 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withTimeout import org.intellij.lang.annotations.Language -import org.junit.Assume.assumeTrue import org.junit.ClassRule import org.junit.Rule import org.junit.Test import java.io.ByteArrayOutputStream import java.io.File -import java.nio.file.Files import java.nio.file.Paths -import java.nio.file.attribute.PosixFilePermission import java.util.concurrent.TimeUnit internal class ProjectStoreTest { @@ -73,7 +69,7 @@ internal class ProjectStoreTest { fun directoryBasedStorage() = runBlocking { loadAndUseProjectInLoadComponentStateMode(tempDirManager, { it.writeChild("${Project.DIRECTORY_STORE_FOLDER}/misc.xml", iprFileContent) - it.path + Paths.get(it.path) }) { project -> val testComponent = test(project as ProjectEx) @@ -100,7 +96,7 @@ internal class ProjectStoreTest { @Test fun fileBasedStorage() = runBlocking { loadAndUseProjectInLoadComponentStateMode(tempDirManager, { - it.writeChild("test${ProjectFileType.DOT_DEFAULT_EXTENSION}", iprFileContent).path + Paths.get(it.writeChild("test${ProjectFileType.DOT_DEFAULT_EXTENSION}", iprFileContent).path) }) { project -> test(project) @@ -118,7 +114,7 @@ internal class ProjectStoreTest { out.write(0xbf) out.write(iprFileContent.toByteArray()) it.writeChild("${Project.DIRECTORY_STORE_FOLDER}/misc.xml", out.toByteArray()) - it.path + Paths.get(it.path) }) { project -> val store = project.stateStore assertThat(store.nameFile).doesNotExist() @@ -153,7 +149,7 @@ internal class ProjectStoreTest { loadAndUseProjectInLoadComponentStateMode(tempDirManager, { it.writeChild("${Project.DIRECTORY_STORE_FOLDER}/misc.xml", iprFileContent) it.writeChild("${Project.DIRECTORY_STORE_FOLDER}/.name", name) - it.path + Paths.get(it.path) }) { project -> val store = project.stateStore assertThat(store.nameFile).hasContent(name) @@ -187,7 +183,7 @@ internal class ProjectStoreTest { """.trimIndent() it.writeChild("${Project.DIRECTORY_STORE_FOLDER}/foo.xml", expected) - it.path + Paths.get(it.path) }) { project -> val obsoleteStorageBean = ObsoleteStorageBean() val storageFileName = "foo.xml" @@ -227,7 +223,7 @@ internal class ProjectStoreTest { withTimeout(TimeUnit.SECONDS.toMillis(10)) { loadAndUseProjectInLoadComponentStateMode(tempDirManager, { it.writeChild("${Project.DIRECTORY_STORE_FOLDER}/misc.xml", iprFileContent) - it.path + Paths.get(it.path) }) { project -> val testComponent = test(project as ProjectEx) testComponent.state!!.AAvalue = "s" diff --git a/platform/lang-impl/src/com/intellij/platform/ModuleAttachProcessor.kt b/platform/lang-impl/src/com/intellij/platform/ModuleAttachProcessor.kt index c1c6d791233b..6383a1502c21 100644 --- a/platform/lang-impl/src/com/intellij/platform/ModuleAttachProcessor.kt +++ b/platform/lang-impl/src/com/intellij/platform/ModuleAttachProcessor.kt @@ -91,7 +91,7 @@ class ModuleAttachProcessor : ProjectAttachProcessor() { override fun attachToProject(project: Project, projectDir: Path, callback: ProjectOpenedCallback?): Boolean { val dotIdeaDir = projectDir.resolve(Project.DIRECTORY_STORE_FOLDER) if (!dotIdeaDir.exists()) { - val newProject = ProjectManagerEx.getInstanceEx().newProject(projectDir, true, false) ?: return false + val newProject = ProjectManagerEx.getInstanceEx().newProject(projectDir, true) ?: return false PlatformProjectOpenProcessor.runDirectoryProjectConfigurators(projectDir, newProject) StoreUtil.saveSettings(newProject) runWriteAction { Disposer.dispose(newProject) } diff --git a/platform/lang-impl/testSources/com/intellij/ide/macro/MacroManagerTest.java b/platform/lang-impl/testSources/com/intellij/ide/macro/MacroManagerTest.java index 4fa2d5d1e0a2..7879f1dda9e0 100644 --- a/platform/lang-impl/testSources/com/intellij/ide/macro/MacroManagerTest.java +++ b/platform/lang-impl/testSources/com/intellij/ide/macro/MacroManagerTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2013 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 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.ide.macro; import com.intellij.openapi.actionSystem.CommonDataKeys; @@ -25,6 +11,7 @@ import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; +import com.intellij.testFramework.PlatformTestUtil; import com.intellij.testFramework.fixtures.CodeInsightFixtureTestCase; import gnu.trove.THashMap; @@ -43,7 +30,7 @@ public class MacroManagerTest extends CodeInsightFixtureTestCase { Map dataId2data = new THashMap<>(); dataId2data.put(CommonDataKeys.PROJECT.getName(), project); dataId2data.put(CommonDataKeys.VIRTUAL_FILE.getName(), file); - dataId2data.put(PlatformDataKeys.PROJECT_FILE_DIRECTORY.getName(), project.getBaseDir()); + dataId2data.put(PlatformDataKeys.PROJECT_FILE_DIRECTORY.getName(), PlatformTestUtil.getOrCreateProjectTestBaseDir(project)); return SimpleDataContext.getSimpleContext(dataId2data, null); } diff --git a/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java b/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java index 6933bb971bb9..95b7ea15c5c4 100644 --- a/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java +++ b/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java @@ -201,16 +201,21 @@ public class VfsUtil extends VfsUtilCore { } @Nullable - public static VirtualFile findFile(@NotNull Path path, boolean refreshIfNeeded) { - return findFileByIoFile(path.toFile(), refreshIfNeeded); + public static VirtualFile findFile(@NotNull Path file, boolean refreshIfNeeded) { + return findFile(FileUtil.toSystemIndependentName(file.toAbsolutePath().toString()), refreshIfNeeded); } @Nullable public static VirtualFile findFileByIoFile(@NotNull File file, boolean refreshIfNeeded) { + return findFile(FileUtil.toSystemIndependentName(file.getAbsolutePath()), refreshIfNeeded); + } + + @Nullable + private static VirtualFile findFile(@NotNull String filePath, boolean refreshIfNeeded) { LocalFileSystem fileSystem = LocalFileSystem.getInstance(); - VirtualFile virtualFile = fileSystem.findFileByIoFile(file); + VirtualFile virtualFile = fileSystem.findFileByPath(filePath); if (refreshIfNeeded && (virtualFile == null || !virtualFile.isValid())) { - virtualFile = fileSystem.refreshAndFindFileByIoFile(file); + virtualFile = fileSystem.refreshAndFindFileByPath(filePath); } return virtualFile; } diff --git a/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java b/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java index 5f4eab3b7172..77fa2b7bbe1a 100644 --- a/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java +++ b/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java @@ -490,12 +490,13 @@ public class RecentProjectsManagerBase extends RecentProjectsManager implements @Nullable public Project doOpenProject(@NotNull @SystemIndependent String projectPath, @Nullable Project projectToClose, boolean forceOpenInNewFrame) { - Project existing = ProjectUtil.findAndFocusExistingProjectForPath(projectPath); + Path projectFile = Paths.get(projectPath); + + Project existing = ProjectUtil.findAndFocusExistingProjectForPath(projectFile); if (existing != null) { return existing; } - Path projectFile = Paths.get(projectPath); if (Files.isDirectory(projectFile.resolve(Project.DIRECTORY_STORE_FOLDER))) { EnumSet options = EnumSet.of(PlatformProjectOpenProcessor.Option.REOPEN); if (forceOpenInNewFrame) { diff --git a/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.java b/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.java index 1467b591daa5..ca602bacbb20 100644 --- a/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.java +++ b/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.java @@ -34,7 +34,10 @@ import com.intellij.ui.AppIcon; import com.intellij.util.PathUtil; import com.intellij.util.PlatformUtils; import com.intellij.util.SystemProperties; -import org.jetbrains.annotations.*; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.PropertyKey; import javax.swing.*; import java.awt.*; @@ -87,7 +90,7 @@ public class ProjectUtil { } public static Project openOrImport(@NotNull Path path, Project projectToClose, boolean forceOpenInNewFrame) { - return openOrImport(FileUtil.toSystemIndependentName(path.toString()), projectToClose, forceOpenInNewFrame); + return openOrImport(path, new OpenProjectTask(forceOpenInNewFrame, projectToClose)); } /** @@ -99,24 +102,19 @@ public class ProjectUtil { * null otherwise */ @Nullable - public static Project openOrImport(@NotNull @SystemIndependent String path, Project projectToClose, boolean forceOpenInNewFrame) { - return openOrImport(path, new OpenProjectTask(forceOpenInNewFrame, projectToClose)); + public static Project openOrImport(@NotNull String path, Project projectToClose, boolean forceOpenInNewFrame) { + return openOrImport(Paths.get(path), new OpenProjectTask(forceOpenInNewFrame, projectToClose)); } @Nullable - public static Project openOrImport(@NotNull Path path, @NotNull OpenProjectTask options) { - return openOrImport(FileUtil.toSystemIndependentName(path.toString()), options); - } - - @Nullable - public static Project openOrImport(@NotNull @SystemIndependent String path, @NotNull OpenProjectTask options) { - Project existing = findAndFocusExistingProjectForPath(path); + public static Project openOrImport(@NotNull Path file, @NotNull OpenProjectTask options) { + Project existing = findAndFocusExistingProjectForPath(file); if (existing != null) { return existing; } NullableLazyValue lazyVirtualFile = NullableLazyValue.createValue(() -> { - VirtualFile result = LocalFileSystem.getInstance().refreshAndFindFileByPath(path); + VirtualFile result = LocalFileSystem.getInstance().refreshAndFindFileByPath(FileUtil.toSystemIndependentName(file.toString())); if (result != null) { result.refresh(false, false); } @@ -136,7 +134,6 @@ public class ProjectUtil { } } - Path file = Paths.get(path); if (isValidProjectPath(file)) { return doOpenProject(file, options.getProjectToClose(), options.getForceOpenInNewFrame()); } @@ -196,7 +193,7 @@ public class ProjectUtil { } } - Project existing = findAndFocusExistingProjectForPath(path); + Project existing = findAndFocusExistingProjectForPath(file); if (existing != null) { return existing; } @@ -284,10 +281,15 @@ public class ProjectUtil { } @Nullable - public static Project findAndFocusExistingProjectForPath(@NotNull String path) { + public static Project findAndFocusExistingProjectForPath(@NotNull Path file) { Project[] openProjects = ProjectManager.getInstance().getOpenProjects(); + if (openProjects.length == 0) { + return null; + } + + String path= FileUtil.toSystemIndependentName(file.toString()); for (Project project : openProjects) { - if (!project.isDefault() && isSameProject(path, project)) { + if (isSameProject(path, project)) { focusProjectWindow(project, false); return project; } diff --git a/platform/platform-impl/src/com/intellij/openapi/project/ex/ProjectManagerEx.java b/platform/platform-impl/src/com/intellij/openapi/project/ex/ProjectManagerEx.java index f131ba936d65..46da00ede193 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/ex/ProjectManagerEx.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/ex/ProjectManagerEx.java @@ -5,7 +5,6 @@ import com.intellij.configurationStore.StoreReloadManager; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectManager; -import com.intellij.util.ObjectUtils; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -27,19 +26,13 @@ public abstract class ProjectManagerEx extends ProjectManager { public abstract Project newProject(@Nullable String projectName, @NotNull String filePath, boolean useDefaultProjectSettings, boolean isDummy); @Nullable - public abstract Project newProject(@NotNull Path filePath, boolean useDefaultProjectSettings, boolean isDummy); - - @TestOnly - @NotNull - public final Project newProject(@Nullable String projectName, @NotNull String filePath) { - return ObjectUtils.assertNotNull(newProject(projectName, filePath, false, false)); - } + public abstract Project newProject(@NotNull Path file, boolean useDefaultProjectSettings); @Nullable public abstract Project loadProject(@NotNull String filePath) throws IOException; @Nullable - public abstract Project loadProject(@NotNull String filePath, @Nullable String projectName) throws IOException; + public abstract Project loadProject(@NotNull Path file, @Nullable String projectName) throws IOException; public abstract boolean openProject(@NotNull Project project); diff --git a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java index c066682c894c..fb76442c10ce 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java @@ -169,13 +169,18 @@ public class ProjectManagerImpl extends ProjectManagerEx implements Disposable { private final Map myProjects = new WeakHashMap<>(); @Override - public Project newProject(@NotNull Path filePath, boolean useDefaultProjectSettings, boolean isDummy) { - return newProject(filePath.getFileName().toString(), FileUtil.toSystemIndependentName(filePath.toString()), useDefaultProjectSettings, isDummy); + public Project newProject(@NotNull Path file, boolean useDefaultProjectSettings) { + return newProject(file, null, useDefaultProjectSettings, true); } @Override @Nullable public Project newProject(@Nullable String projectName, @NotNull String filePath, boolean useDefaultProjectSettings, boolean isDummy) { + return newProject(Paths.get(toCanonicalName(filePath)), projectName, useDefaultProjectSettings, true); + } + + @Nullable + public Project newProject(@NotNull Path projectFile, @Nullable String projectName, boolean useDefaultProjectSettings, boolean isRefreshVfsNeeded) { if (ApplicationManager.getApplication().isUnitTestMode()) { //noinspection AssignmentToStaticFieldFromInstanceMethod TEST_PROJECTS_CREATED++; @@ -183,7 +188,6 @@ public class ProjectManagerImpl extends ProjectManagerEx implements Disposable { checkProjectLeaksInTests(); } - Path projectFile = Paths.get(toCanonicalName(filePath)); if (Files.isRegularFile(projectFile)) { try { FileUtil.delete(projectFile); @@ -203,7 +207,7 @@ public class ProjectManagerImpl extends ProjectManagerEx implements Disposable { ProjectImpl project = doCreateProject(projectName, projectFile); try { - initProject(projectFile, project, useDefaultProjectSettings ? getDefaultProject() : null, ProgressManager.getInstance().getProgressIndicator()); + initProject(projectFile, project, isRefreshVfsNeeded, useDefaultProjectSettings ? getDefaultProject() : null, ProgressManager.getInstance().getProgressIndicator()); if (LOG_PROJECT_LEAKAGE_IN_TESTS) { myProjects.put(project, null); } @@ -280,7 +284,7 @@ public class ProjectManagerImpl extends ProjectManagerEx implements Disposable { return (int)myProjects.keySet().stream().filter(project -> project.isDisposed() && !((ProjectImpl)project).isTemporarilyDisposed()).count(); } - private static void initProject(@NotNull Path file, @NotNull ProjectImpl project, @Nullable Project template, @Nullable ProgressIndicator indicator) { + private static void initProject(@NotNull Path file, @NotNull ProjectImpl project, boolean isRefreshVfsNeeded, @Nullable Project template, @Nullable ProgressIndicator indicator) { LOG.assertTrue(!project.isDefault()); if (indicator != null) { indicator.setIndeterminate(false); @@ -295,7 +299,7 @@ public class ProjectManagerImpl extends ProjectManagerEx implements Disposable { boolean succeed = false; try { project.registerComponents(); - project.getStateStore().setPath(file, true, template); + project.getStateStore().setPath(file, isRefreshVfsNeeded, template); project.init(indicator); succeed = true; } @@ -317,16 +321,15 @@ public class ProjectManagerImpl extends ProjectManagerEx implements Disposable { @Override @Nullable public Project loadProject(@NotNull String filePath) throws IOException { - return loadProject(filePath, null); + return loadProject(Paths.get(filePath).toAbsolutePath(), null); } @Override @Nullable - public Project loadProject(@NotNull String filePath, @Nullable String projectName) throws IOException { + public Project loadProject(@NotNull Path file, @Nullable String projectName) throws IOException { try { - Path file = Paths.get(filePath).toAbsolutePath(); ProjectImpl project = doCreateProject(projectName, file); - initProject(file, project, null, ProgressManager.getInstance().getProgressIndicator()); + initProject(file, project, /* isRefreshVfsNeeded = */ true, null, ProgressManager.getInstance().getProgressIndicator()); return project; } catch (Throwable t) { @@ -551,7 +554,7 @@ public class ProjectManagerImpl extends ProjectManagerEx implements Disposable { @Override public void run(@NotNull ProgressIndicator indicator) { try { - initProject(file, project, null, indicator); + initProject(file, project, /* isRefreshVfsNeeded = */ true, null, indicator); } catch (ProcessCanceledException e) { return; @@ -605,12 +608,12 @@ public class ProjectManagerImpl extends ProjectManagerEx implements Disposable { ProjectImpl project = doCreateProject(null, path); try { if (!ApplicationManager.getApplication().isDispatchThread() && ProgressManager.getInstance().getProgressIndicator() != null) { - initProject(path, project, null, ProgressManager.getInstance().getProgressIndicator()); + initProject(path, project, /* isRefreshVfsNeeded = */ true, null, ProgressManager.getInstance().getProgressIndicator()); } else { //noinspection CodeBlock2Expr ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> { - initProject(path, project, null, ProgressManager.getInstance().getProgressIndicator()); + initProject(path, project, /* isRefreshVfsNeeded = */ true, null, ProgressManager.getInstance().getProgressIndicator()); }, ProjectBundle.message("project.load.progress"), canCancelProjectLoading(), project); } } diff --git a/platform/platform-tests/testSrc/com/intellij/formatting/FileSetTestCase.java b/platform/platform-tests/testSrc/com/intellij/formatting/FileSetTestCase.java index d3a8f72adae3..2c6e755a901e 100644 --- a/platform/platform-tests/testSrc/com/intellij/formatting/FileSetTestCase.java +++ b/platform/platform-tests/testSrc/com/intellij/formatting/FileSetTestCase.java @@ -1,4 +1,4 @@ -// Copyright 2000-2018 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. +// Copyright 2000-2019 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.formatting; import com.intellij.openapi.application.WriteAction; @@ -6,15 +6,15 @@ import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.testFramework.PlatformTestCase; +import com.intellij.testFramework.PlatformTestUtil; import org.jetbrains.annotations.NotNull; import java.io.IOException; public abstract class FileSetTestCase extends PlatformTestCase { - protected VirtualFile createFile(@NotNull String path) { String[] dirNames = path.split("/"); - VirtualFile baseDir = getProject().getBaseDir(); + VirtualFile baseDir = PlatformTestUtil.getOrCreateProjectTestBaseDir(getProject()); for (int i = 0; i < dirNames.length - 1; i++) { VirtualFile existing = VfsUtilCore.findRelativeFile(dirNames[i], baseDir); if (existing == null) { diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/OverwriteProjectConfigurationTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/OverwriteProjectConfigurationTest.java index 8e86ac1f1a96..54b72f1b9ff0 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/OverwriteProjectConfigurationTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/OverwriteProjectConfigurationTest.java @@ -52,7 +52,7 @@ public class OverwriteProjectConfigurationTest extends PlatformTestCase { } private Project createProject() { - Project project = ProjectManagerEx.getInstanceEx().newProject("test", myProjectDir.getAbsolutePath()); + Project project = ProjectManagerEx.getInstanceEx().newProject(myProjectDir.toPath(), false); assertNotNull(project); disposeOnTearDown(project); return project; diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/NonProjectFileAccessTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/NonProjectFileAccessTest.java index cf10a77083a6..adc0a8ec3b86 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/NonProjectFileAccessTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/NonProjectFileAccessTest.java @@ -22,6 +22,7 @@ import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.WritingAccessProvider; import com.intellij.testFramework.EditorTestUtil; @@ -136,7 +137,7 @@ public class NonProjectFileAccessTest extends HeavyFileEditorManagerTestCase { String moduleName; ModifiableModuleModel moduleModel = ModuleManager.getInstance(getProject()).getModifiableModel(); try { - VirtualFile moduleDir = getProject().getBaseDir().createChildDirectory(this, "moduleWithoutContentRoot"); + VirtualFile moduleDir = VfsUtil.createDirectoryIfMissing(getProject().getBasePath() + "/moduleWithoutContentRoot"); moduleName = moduleModel.newModule(moduleDir.getPath() + "/moduleWithoutContentRoot.iml", EmptyModuleType.EMPTY_MODULE).getName(); moduleModel.commit(); } diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/encoding/FileEncodingTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/encoding/FileEncodingTest.java index 0152f3d7a7ae..a4b35b55b5ca 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/encoding/FileEncodingTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/encoding/FileEncodingTest.java @@ -60,6 +60,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.Set; @@ -778,7 +779,7 @@ public class FileEncodingTest extends PlatformTestCase implements TestDialog { File temp = createTempDirectory(); VirtualFile tempDir = ObjectUtils.assertNotNull(LocalFileSystem.getInstance().refreshAndFindFileByIoFile(temp)); - final Project newProject = ProjectManagerEx.getInstanceEx().newProject("new", tempDir.getPath()); + Project newProject = ProjectManagerEx.getInstanceEx().newProject(Paths.get(tempDir.getPath()), false); Disposer.register(getTestRootDisposable(), () -> ApplicationManager.getApplication().runWriteAction(() -> Disposer.dispose(newProject))); PlatformTestUtil.saveProject(newProject); diff --git a/platform/platform-tests/testSrc/com/intellij/psi/PsiModificationTrackerTreeChangesUpdatesTest.java b/platform/platform-tests/testSrc/com/intellij/psi/PsiModificationTrackerTreeChangesUpdatesTest.java index 6d9634c88ef3..8d681b7a53cf 100644 --- a/platform/platform-tests/testSrc/com/intellij/psi/PsiModificationTrackerTreeChangesUpdatesTest.java +++ b/platform/platform-tests/testSrc/com/intellij/psi/PsiModificationTrackerTreeChangesUpdatesTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2018 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 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.psi; import com.intellij.openapi.application.WriteAction; @@ -20,6 +6,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.impl.PsiManagerImpl; import com.intellij.psi.impl.PsiModificationTrackerImpl; import com.intellij.testFramework.PlatformTestCase; +import com.intellij.testFramework.PlatformTestUtil; import java.io.IOException; @@ -51,8 +38,8 @@ public class PsiModificationTrackerTreeChangesUpdatesTest extends PlatformTestCa public void testMoveFile() throws IOException { WriteAction.runAndWait(() -> { - final VirtualFile dir1 = getProject().getBaseDir().createChildDirectory(this, "dir1"); - final VirtualFile dir2 = getProject().getBaseDir().createChildDirectory(this, "dir2"); + final VirtualFile dir1 = PlatformTestUtil.getOrCreateProjectTestBaseDir(getProject()).createChildDirectory(this, "dir1"); + final VirtualFile dir2 = PlatformTestUtil.getOrCreateProjectTestBaseDir(getProject()).createChildDirectory(this, "dir2"); VirtualFile child = dir1.createChildData(this, "child"); long outOfCodeBlockCount = myTracker.getOutOfCodeBlockModificationCount(); @@ -67,8 +54,8 @@ public class PsiModificationTrackerTreeChangesUpdatesTest extends PlatformTestCa public void testMoveDir() throws IOException { WriteAction.runAndWait(() -> { - final VirtualFile dir1 = getProject().getBaseDir().createChildDirectory(this, "dir1"); - final VirtualFile dir2 = getProject().getBaseDir().createChildDirectory(this, "dir2"); + final VirtualFile dir1 = PlatformTestUtil.getOrCreateProjectTestBaseDir(getProject()).createChildDirectory(this, "dir1"); + final VirtualFile dir2 = PlatformTestUtil.getOrCreateProjectTestBaseDir(getProject()).createChildDirectory(this, "dir2"); VirtualFile child = dir1.createChildDirectory(this, "child"); long outOfCodeBlockCount = myTracker.getOutOfCodeBlockModificationCount(); diff --git a/platform/testFramework/src/com/intellij/testFramework/FixtureRule.kt b/platform/testFramework/src/com/intellij/testFramework/FixtureRule.kt index ba2609b6b611..3d4c039db136 100644 --- a/platform/testFramework/src/com/intellij/testFramework/FixtureRule.kt +++ b/platform/testFramework/src/com/intellij/testFramework/FixtureRule.kt @@ -36,6 +36,7 @@ import org.junit.runners.model.Statement import java.io.ByteArrayOutputStream import java.io.PrintStream import java.nio.file.Files +import java.nio.file.Path import java.util.concurrent.atomic.AtomicBoolean private var sharedModule: Module? = null @@ -231,8 +232,8 @@ inline fun Project.runInLoadComponentStateMode(task: () -> T): T { } } -fun createHeavyProject(path: String, useDefaultProjectSettings: Boolean = false): Project { - return ProjectManagerEx.getInstanceEx().newProject(null, path, useDefaultProjectSettings, false)!! +fun createHeavyProject(path: Path, useDefaultProjectSettings: Boolean = false): Project { + return ProjectManagerEx.getInstanceEx().newProject(path, useDefaultProjectSettings)!! } suspend fun Project.use(task: suspend (Project) -> Unit) { @@ -296,7 +297,7 @@ suspend fun createProjectAndUseInLoadComponentStateMode(tempDirManager: Temporar createOrLoadProject(tempDirManager, task = task, directoryBased = directoryBased, loadComponentState = true) } -suspend fun loadAndUseProjectInLoadComponentStateMode(tempDirManager: TemporaryDirectory, projectCreator: (suspend (VirtualFile) -> String)? = null, task: suspend (Project) -> Unit) { +suspend fun loadAndUseProjectInLoadComponentStateMode(tempDirManager: TemporaryDirectory, projectCreator: (suspend (VirtualFile) -> Path)? = null, task: suspend (Project) -> Unit) { createOrLoadProject(tempDirManager, projectCreator, task = task, directoryBased = false, loadComponentState = true) } @@ -315,14 +316,14 @@ suspend fun runNonUndoableWriteAction(file: VirtualFile, runnable: suspend ( } suspend fun createOrLoadProject(tempDirManager: TemporaryDirectory, - projectCreator: (suspend (VirtualFile) -> String)? = null, + projectCreator: (suspend (VirtualFile) -> Path)? = null, directoryBased: Boolean = true, loadComponentState: Boolean = false, useDefaultProjectSettings: Boolean = true, task: suspend (Project) -> Unit) { withContext(AppUIExecutor.onUiThread().coroutineDispatchingContext()) { - val filePath = if (projectCreator == null) { - tempDirManager.newPath("test${if (directoryBased) "" else ProjectFileType.DOT_DEFAULT_EXTENSION}", refreshVfs = true).systemIndependentPath + val file = if (projectCreator == null) { + tempDirManager.newPath("test${if (directoryBased) "" else ProjectFileType.DOT_DEFAULT_EXTENSION}", refreshVfs = true) } else { val dir = tempDirManager.newVirtualDirectory() @@ -332,8 +333,8 @@ suspend fun createOrLoadProject(tempDirManager: TemporaryDirectory, } val project = when (projectCreator) { - null -> createHeavyProject(filePath, useDefaultProjectSettings = useDefaultProjectSettings) - else -> ProjectManagerEx.getInstanceEx().loadProject(filePath)!! + null -> createHeavyProject(file, useDefaultProjectSettings = useDefaultProjectSettings) + else -> ProjectManagerEx.getInstanceEx().loadProject(file, null)!! } if (loadComponentState) { diff --git a/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java b/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java index 3500b8954950..86cc1558c87e 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java @@ -222,9 +222,10 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da } ApplicationManager.getApplication().runWriteAction(LightPlatformTestCase::cleanPersistedVFSContent); - Path tempFile = TemporaryDirectory.generateTemporaryPath(ProjectImpl.LIGHT_PROJECT_NAME + ProjectFileType.DOT_DEFAULT_EXTENSION); - ourProject = PlatformTestCase.createProject(tempFile); - ourPathToKeep = tempFile; + Path tempDirectory = TemporaryDirectory.generateTemporaryPath(ProjectImpl.LIGHT_PROJECT_NAME + ProjectFileType.DOT_DEFAULT_EXTENSION); + PlatformTestCase.synchronizeTempDirVfs(tempDirectory); + ourProject = PlatformTestCase.createProject(tempDirectory); + ourPathToKeep = tempDirectory; ourPsiManager = null; try { diff --git a/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java b/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java index 2f260ecf27ef..3e751744101b 100644 --- a/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java @@ -31,8 +31,10 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.module.ModuleType; import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectManager; import com.intellij.openapi.project.ex.ProjectManagerEx; import com.intellij.openapi.project.impl.ProjectImpl; +import com.intellij.openapi.project.impl.ProjectManagerImpl; import com.intellij.openapi.project.impl.TooManyProjectLeakedException; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.roots.ModuleRootModificationUtil; @@ -152,6 +154,14 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro tempDir.refresh(false, true); } + public static void synchronizeTempDirVfs(@NotNull Path tempDir) { + VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(FileUtil.toSystemIndependentName(tempDir.toString())); + // null is ok, because Path can be only generated, but not created + if (virtualFile != null) { + synchronizeTempDirVfs(Objects.requireNonNull(virtualFile)); + } + } + protected void initApplication() throws Exception { boolean firstTime = ourApplication == null; ourApplication = IdeaTestApplication.getInstance(null); @@ -277,15 +287,12 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro return createProject(projectFile); } - @NotNull - public static Project createProject(@NotNull File projectFile) { - return createProject(projectFile.toPath()); - } - @NotNull public static Project createProject(@NotNull Path file) { try { - return Objects.requireNonNull(ProjectManagerEx.getInstanceEx().newProject(file, /* useDefaultProjectSettings = */ false, false)); + ProjectManagerImpl projectManager = (ProjectManagerImpl)ProjectManager.getInstance(); + // in tests it is caller responsibility to refresh VFS (because often not only the project file must be refreshed, but the whole dir - so, no need to refresh several times) + return Objects.requireNonNull(projectManager.newProject(file, null, /* useDefaultProjectSettings = */ false, /* isRefreshVfsNeeded = */ false)); } catch (TooManyProjectLeakedException e) { if (ourReportedLeakedProjects) { diff --git a/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java b/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java index 0c9f98124460..8bd573f0c0da 100644 --- a/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java +++ b/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java @@ -41,10 +41,7 @@ import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.FileUtilRt; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.openapi.vfs.LocalFileSystem; -import com.intellij.openapi.vfs.VfsUtilCore; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.vfs.VirtualFileFilter; +import com.intellij.openapi.vfs.*; import com.intellij.openapi.vfs.ex.temp.TempFileSystem; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiRecursiveElementWalkingVisitor; @@ -979,4 +976,26 @@ public class PlatformTestUtil { " * Created by ${USER} on ${DATE}.\n" + " */\n", parentDisposable); } + + /** + * 1. Think twice before use - do you really need to use VFS. + * 2. Think again. + * 3. Be aware that this method doesn't refresh VFS as it should be done in tests (see {@link PlatformTestCase#synchronizeTempDirVfs}) (it is assumed that project is already created in a correct way). + */ + @NotNull + public static VirtualFile getOrCreateProjectTestBaseDir(@NotNull Project project) { + try { + String path = Objects.requireNonNull(project.getBasePath()); + VirtualFile result = LocalFileSystem.getInstance().refreshAndFindFileByPath(path); + if (result != null) { + return result; + } + + // createDirectories executes in write action + return Objects.requireNonNull(VfsUtil.createDirectories(Objects.requireNonNull(project.getBasePath()))); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } } \ No newline at end of file diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/HeavyIdeaTestFixtureImpl.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/HeavyIdeaTestFixtureImpl.java index 1df43e93763e..7929e134fed8 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/HeavyIdeaTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/HeavyIdeaTestFixtureImpl.java @@ -21,9 +21,7 @@ import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ex.ProjectManagerEx; import com.intellij.openapi.roots.ProjectRootManager; -import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.encoding.EncodingManager; @@ -45,6 +43,7 @@ import org.junit.Assert; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.util.*; import java.util.stream.Collectors; @@ -55,7 +54,7 @@ import java.util.stream.Stream; * @author mike */ @SuppressWarnings("TestOnlyProblems") -class HeavyIdeaTestFixtureImpl extends BaseFixture implements HeavyIdeaTestFixture { +final class HeavyIdeaTestFixtureImpl extends BaseFixture implements HeavyIdeaTestFixture { private Project myProject; private final Set myFilesToDelete = new HashSet<>(); private IdeaTestApplication myApplication; @@ -126,6 +125,9 @@ class HeavyIdeaTestFixtureImpl extends BaseFixture implements HeavyIdeaTestFixtu .filter(Objects::nonNull) .collect(Collectors.toList()); } + catch (NoSuchFileException ignore) { + errors = Collections.emptyList(); + } CompoundRuntimeException.throwIfNotEmpty(errors); }); } @@ -152,9 +154,9 @@ class HeavyIdeaTestFixtureImpl extends BaseFixture implements HeavyIdeaTestFixtu .run(); } - private void setUpProject() throws IOException { - Path tempDirectory = FileUtil.createTempDirectory(myName, "").toPath(); - PlatformTestCase.synchronizeTempDirVfs(Objects.requireNonNull(LocalFileSystem.getInstance().refreshAndFindFileByPath(FileUtil.toSystemIndependentName(tempDirectory.toString())))); + private void setUpProject() { + Path tempDirectory = TemporaryDirectory.generateTemporaryPath(myName); + PlatformTestCase.synchronizeTempDirVfs(tempDirectory); myFilesToDelete.add(tempDirectory); myProject = PlatformTestCase.createProject(generateProjectPath(tempDirectory)); diff --git a/platform/vcs-impl/testSrc/com/intellij/openapi/vcs/LocalChangesUnderRootsTest.java b/platform/vcs-impl/testSrc/com/intellij/openapi/vcs/LocalChangesUnderRootsTest.java index 755d35a5d4ba..0c063fbc4387 100644 --- a/platform/vcs-impl/testSrc/com/intellij/openapi/vcs/LocalChangesUnderRootsTest.java +++ b/platform/vcs-impl/testSrc/com/intellij/openapi/vcs/LocalChangesUnderRootsTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 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.vcs; import com.intellij.openapi.util.Pair; @@ -26,6 +12,7 @@ import com.intellij.openapi.vcs.impl.projectlevelman.AllVcses; import com.intellij.openapi.vcs.impl.projectlevelman.AllVcsesI; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.testFramework.PlatformTestCase; +import com.intellij.testFramework.PlatformTestUtil; import com.intellij.testFramework.VfsTestUtil; import com.intellij.testFramework.vcs.MockChangeListManager; import com.intellij.testFramework.vcs.MockContentRevision; @@ -44,7 +31,7 @@ public class LocalChangesUnderRootsTest extends PlatformTestCase { super.setUp(); myChangeListManager = new MockChangeListManager(); - myBaseDir = myProject.getBaseDir(); + myBaseDir = PlatformTestUtil.getOrCreateProjectTestBaseDir(myProject); myLocalChangesUnderRoots = new LocalChangesUnderRoots(myChangeListManager, ProjectLevelVcsManager.getInstance(myProject)); } @@ -61,7 +48,7 @@ public class LocalChangesUnderRootsTest extends PlatformTestCase { Change changeAfterCommunity = createChangeForPath("readme.txt"); Change changeInCommunity = createChangeForPath("community/com.txt"); myChangeListManager.addChanges(changeBeforeCommunity, changeAfterCommunity, changeInCommunity); - + Map> expected = new HashMap<>(); expected.put(roots.get(0), Arrays.asList(changeBeforeCommunity, changeAfterCommunity)); expected.put(roots.get(1), Collections.singletonList(changeInCommunity)); @@ -95,7 +82,7 @@ public class LocalChangesUnderRootsTest extends PlatformTestCase { for (Pair pathAndVc : pathAndVcs) { String path = pathAndVc.first; String vcs = pathAndVc.second; - + VirtualFile vf; if (path.equals(myBaseDir.getPath())) { vf = myBaseDir; @@ -117,5 +104,5 @@ public class LocalChangesUnderRootsTest extends PlatformTestCase { ContentRevision afterRevision = new MockContentRevision(filePath, new VcsRevisionNumber.Int(2)); return new Change(beforeRevision, afterRevision); } - + } diff --git a/platform/vcs-tests/testSrc/com/intellij/openapi/vcs/changes/committed/VcsDirtyScopeTest.java b/platform/vcs-tests/testSrc/com/intellij/openapi/vcs/changes/committed/VcsDirtyScopeTest.java index 70f58d36c5ad..a1be9b610bf3 100644 --- a/platform/vcs-tests/testSrc/com/intellij/openapi/vcs/changes/committed/VcsDirtyScopeTest.java +++ b/platform/vcs-tests/testSrc/com/intellij/openapi/vcs/changes/committed/VcsDirtyScopeTest.java @@ -1,4 +1,4 @@ -// Copyright 2000-2018 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. +// Copyright 2000-2019 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.vcs.changes.committed; import com.intellij.openapi.application.ApplicationManager; @@ -12,6 +12,7 @@ import com.intellij.openapi.vcs.changes.VcsModifiableDirtyScope; import com.intellij.openapi.vcs.impl.ProjectLevelVcsManagerImpl; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.testFramework.PlatformTestUtil; import com.intellij.testFramework.vcs.FileBasedTest; import com.intellij.util.Consumer; import com.intellij.util.ui.UIUtil; @@ -126,7 +127,7 @@ public class VcsDirtyScopeTest extends FileBasedTest { private Data createData() throws IOException { final Data data = new Data(); - data.baseDir = myProjectFixture.getProject().getBaseDir(); + data.baseDir = PlatformTestUtil.getOrCreateProjectTestBaseDir(myProjectFixture.getProject()); final IOException[] exc = new IOException[1]; final File ioFile = new File(data.baseDir.getPath()); final File[] files = ioFile.listFiles(); diff --git a/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/PluginProjectWizardTest.java b/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/PluginProjectWizardTest.java index 98e69e0071fe..ee67304ccbff 100644 --- a/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/PluginProjectWizardTest.java +++ b/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/PluginProjectWizardTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2012 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 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 org.jetbrains.idea.devkit; import com.intellij.execution.RunManager; @@ -29,7 +15,6 @@ import org.jetbrains.idea.devkit.projectRoots.IdeaJdk; * @author Dmitry Avdeev */ public class PluginProjectWizardTest extends NewProjectWizardTestCase { - public void testPluginProject() throws Exception { createSdk("devkit", IdeaJdk.getInstance()); Project project = createProjectFromTemplate(PluginModuleType.getInstance().getName(), null, null); diff --git a/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/testAssistant/TestDataGuessByExistingFilesUtilTest.java b/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/testAssistant/TestDataGuessByExistingFilesUtilTest.java index cac10fd7ab13..19c53d6a3eaf 100644 --- a/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/testAssistant/TestDataGuessByExistingFilesUtilTest.java +++ b/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/testAssistant/TestDataGuessByExistingFilesUtilTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 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 org.jetbrains.idea.devkit.testAssistant; import com.intellij.openapi.application.ApplicationManager; @@ -42,12 +28,8 @@ public class TestDataGuessByExistingFilesUtilTest extends TestDataPathTestCase { @Override public void setUp() throws Exception { super.setUp(); - VirtualFile resourcesDir = ApplicationManager.getApplication().runWriteAction(new ThrowableComputable() { - @Override - public VirtualFile compute() throws IOException { - return myContentRoot.createChildDirectory(TestDataGuessByExistingFilesUtilTest.this, RESOURCES_ROOT_NAME); - } - }); + + VirtualFile resourcesDir = ApplicationManager.getApplication().runWriteAction((ThrowableComputable)() -> myContentRoot.createChildDirectory(this, RESOURCES_ROOT_NAME)); PsiTestUtil.addSourceRoot(myFixture.getModule(), resourcesDir, JavaResourceRootType.RESOURCE); } diff --git a/plugins/devkit/devkit-tests-api/src/org/jetbrains/idea/devkit/testAssistant/TestDataPathTestCase.java b/plugins/devkit/devkit-tests-api/src/org/jetbrains/idea/devkit/testAssistant/TestDataPathTestCase.java index c3f2fd9bcec2..b1df726e12b4 100644 --- a/plugins/devkit/devkit-tests-api/src/org/jetbrains/idea/devkit/testAssistant/TestDataPathTestCase.java +++ b/plugins/devkit/devkit-tests-api/src/org/jetbrains/idea/devkit/testAssistant/TestDataPathTestCase.java @@ -1,8 +1,8 @@ -// Copyright 2000-2018 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. +// Copyright 2000-2019 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 org.jetbrains.idea.devkit.testAssistant; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.testFramework.builders.JavaModuleFixtureBuilder; import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase; @@ -17,12 +17,12 @@ abstract public class TestDataPathTestCase extends JavaCodeInsightFixtureTestCas @Override public void setUp() throws Exception { super.setUp(); - myContentRoot = LocalFileSystem.getInstance().refreshAndFindFileByPath(myFixture.getTempDirPath()); + myContentRoot = VfsUtil.createDirectoryIfMissing(myFixture.getTempDirPath()); ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { try { - myProjectSubdir = myFixture.getProject().getBaseDir().createChildDirectory(this, "projectSubdir"); + myProjectSubdir = VfsUtil.createDirectoryIfMissing(myFixture.getProject().getBasePath() + "/projectSubdir"); myContentRootSubdir = myContentRoot.createChildDirectory(this, "contentRootSubdir"); } catch (IOException e) { diff --git a/plugins/gradle/java/testSources/compiler/GradleCompilingTestCase.java b/plugins/gradle/java/testSources/compiler/GradleCompilingTestCase.java index f282ccee85da..1be9b4614d5c 100644 --- a/plugins/gradle/java/testSources/compiler/GradleCompilingTestCase.java +++ b/plugins/gradle/java/testSources/compiler/GradleCompilingTestCase.java @@ -50,8 +50,7 @@ public abstract class GradleCompilingTestCase extends GradleImportingTestCase { } private static String getArtifactBaseOutputPath(Project project) { - String outputUrl = project.getBaseDir().getUrl() + "/out/artifacts"; - return FileUtil.toSystemIndependentName(VfsUtilCore.urlToPath(outputUrl)); + return project.getBasePath() + "/out/artifacts"; } private static String getArtifactBaseOutputPath(Module module) { diff --git a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/service/task/GradleTaskManagerTest.kt b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/service/task/GradleTaskManagerTest.kt index a8e0c4064054..cbca2cd3553a 100644 --- a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/service/task/GradleTaskManagerTest.kt +++ b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/service/task/GradleTaskManagerTest.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2018 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. +// Copyright 2000-2019 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 org.jetbrains.plugins.gradle.service.task import com.intellij.openapi.application.runWriteAction @@ -7,6 +7,7 @@ import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotifica import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VfsUtil +import com.intellij.testFramework.PlatformTestUtil import com.intellij.testFramework.UsefulTestCase import com.intellij.testFramework.fixtures.IdeaProjectTestFixture import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory @@ -20,7 +21,6 @@ import org.junit.Before import org.junit.Test class GradleTaskManagerTest: UsefulTestCase() { - private lateinit var myTestFixture: IdeaProjectTestFixture private lateinit var myProject: Project private lateinit var tm: GradleTaskManager @@ -68,7 +68,8 @@ class GradleTaskManagerTest: UsefulTestCase() { @Test fun `test task manager uses wrapper task when wrapper already exists`() { runWriteAction { - val wrapperProps = myProject.baseDir + val baseDir = PlatformTestUtil.getOrCreateProjectTestBaseDir(myProject) + val wrapperProps = baseDir .createChildDirectory(this, "gradle") .createChildDirectory(this, "wrapper") .createChildData(this, "gradle-wrapper.properties") @@ -106,8 +107,7 @@ class GradleTaskManagerTest: UsefulTestCase() { private fun writeBuildScript(scriptText: String) { runWriteAction { - VfsUtil.saveText(myProject.baseDir.createChildData(this, "build.gradle"), - scriptText) + VfsUtil.saveText(PlatformTestUtil.getOrCreateProjectTestBaseDir(myProject).createChildData(this, "build.gradle"), scriptText) } } } diff --git a/spellchecker/testSrc/com/intellij/spellchecker/dictionary/CustomDictionaryTest.java b/spellchecker/testSrc/com/intellij/spellchecker/dictionary/CustomDictionaryTest.java index e36f70c23d7a..3256de8717b1 100644 --- a/spellchecker/testSrc/com/intellij/spellchecker/dictionary/CustomDictionaryTest.java +++ b/spellchecker/testSrc/com/intellij/spellchecker/dictionary/CustomDictionaryTest.java @@ -11,6 +11,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.spellchecker.SpellCheckerManager; import com.intellij.spellchecker.inspection.SpellcheckerInspectionTestCase; import com.intellij.spellchecker.settings.SpellCheckerSettings; +import com.intellij.testFramework.PlatformTestUtil; import com.intellij.util.PathUtil; import org.jetbrains.annotations.NotNull; @@ -38,7 +39,7 @@ public class CustomDictionaryTest extends SpellcheckerInspectionTestCase { List oldPaths = settings.getCustomDictionariesPaths(); WriteAction.runAndWait(() -> { - dictDir = getProject().getBaseDir().createChildDirectory(this, getDictDirName()); + dictDir = VfsUtil.createDirectoryIfMissing(getProject().getBasePath() + "/" + getDictDirName()); VfsUtil.copyDirectory(this, myFixture.copyDirectoryToProject(getTestName(true), getDictDirName()), dictDir, null); }); @@ -190,12 +191,12 @@ public class CustomDictionaryTest extends SpellcheckerInspectionTestCase { public void testMoveDict() throws IOException { try { doBeforeCheck(); - WriteAction.run(() -> getTestDictionaryFile().move(this, getProject().getBaseDir())); + WriteAction.run(() -> getTestDictionaryFile().move(this, PlatformTestUtil.getOrCreateProjectTestBaseDir(getProject()))); doAfterCheck(); } finally { WriteAction.run(() -> { - final VirtualFile child = getProject().getBaseDir().findChild(TEST_DIC); + final VirtualFile child = PlatformTestUtil.getOrCreateProjectTestBaseDir(getProject()).findChild(TEST_DIC); if (child.exists()) { child.delete(this); } @@ -237,12 +238,12 @@ public class CustomDictionaryTest extends SpellcheckerInspectionTestCase { public void testMoveDictDir() throws IOException { try { doBeforeCheck(); - WriteAction.run(() -> dictDir.move(this, getProject().getBaseDir().createChildDirectory(this, "new_dir"))); + WriteAction.run(() -> dictDir.move(this, PlatformTestUtil.getOrCreateProjectTestBaseDir(getProject()).createChildDirectory(this, "new_dir"))); doAfterCheck(); } finally { WriteAction.run(() -> { - final VirtualFile dir = getProject().getBaseDir().findChild("new_dir"); + final VirtualFile dir = PlatformTestUtil.getOrCreateProjectTestBaseDir(getProject()).findChild("new_dir"); if (dir.exists()) { dir.delete(this); }