From 87bf970badf835f809c7915825d46000e50496f0 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 26 Jun 2019 14:41:51 +0200 Subject: [PATCH] fix SaveProjectAsTemplateTest - avoid reusing the same path for different macros (for simplicity) GitOrigin-RevId: a665b4620be233a2a5bd35599de22f1415f12881 --- java/idea-ui/intellij.java.ui.tests.iml | 1 + .../SaveProjectAsTemplateTest.java | 18 ++++++++++-------- .../src/ProjectStoreBase.kt | 5 ++++- .../src/StateStorageManagerImpl.kt | 8 +------- .../templates/SaveProjectAsTemplateAction.java | 13 ++----------- 5 files changed, 18 insertions(+), 27 deletions(-) diff --git a/java/idea-ui/intellij.java.ui.tests.iml b/java/idea-ui/intellij.java.ui.tests.iml index 5216dc6d2282..8739fa32388f 100644 --- a/java/idea-ui/intellij.java.ui.tests.iml +++ b/java/idea-ui/intellij.java.ui.tests.iml @@ -11,5 +11,6 @@ + \ No newline at end of file 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 a572c8ce7b15..521e796d22c4 100644 --- a/java/idea-ui/testSrc/com/intellij/ide/projectWizard/SaveProjectAsTemplateTest.java +++ b/java/idea-ui/testSrc/com/intellij/ide/projectWizard/SaveProjectAsTemplateTest.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.ide.projectWizard; import com.intellij.application.UtilKt; @@ -37,9 +37,12 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Date; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author Dmitry Avdeev */ @@ -67,7 +70,7 @@ public class SaveProjectAsTemplateTest extends NewProjectWizardTestCase { "public class Bar {\n" + "}"); } - + public void testSaveProjectUnescaped() throws Exception { doTest(false, false, "/** No comments */\n" + "\n" + @@ -88,7 +91,7 @@ public class SaveProjectAsTemplateTest extends NewProjectWizardTestCase { } private void doTest(boolean shouldEscape, boolean replaceParameters, String initialText, String expected) throws IOException { - assertEquals(StorageScheme.DIRECTORY_BASED, ProjectKt.getStateStore(getProject()).getStorageScheme()); + assertThat(ProjectKt.getStateStore(getProject()).getStorageScheme()).isEqualTo(StorageScheme.DIRECTORY_BASED); VirtualFile root = ProjectRootManager.getInstance(getProject()).getContentRoots()[0]; File rootFile = new File(VfsUtilCore.virtualToIoFile(root), FOO_BAR_JAVA); rootFile.getParentFile().mkdirs(); @@ -105,8 +108,8 @@ public class SaveProjectAsTemplateTest extends NewProjectWizardTestCase { SaveProjectAsTemplateAction.saveProject(getProject(), zipFile, null, "bar", replaceParameters, new MockProgressIndicator(), shouldEscape); return Unit.INSTANCE; }); - assertEquals("foo.zip", zipFile.getFileName().toString()); - assertTrue(Files.size(zipFile) > 0); + assertThat(zipFile.getFileName().toString()).isEqualTo("foo.zip"); + assertThat(Files.size(zipFile)).isGreaterThan(0); Project fromTemplate = createProjectFromTemplate(ProjectTemplatesFactory.CUSTOM_GROUP, "foo", null); VirtualFile descriptionFile = SaveProjectAsTemplateAction.getDescriptionFile(fromTemplate, LocalArchivedTemplate.DESCRIPTION_PATH); @@ -116,10 +119,9 @@ public class SaveProjectAsTemplateTest extends NewProjectWizardTestCase { VirtualFile[] roots = ProjectRootManager.getInstance(fromTemplate).getContentRoots(); VirtualFile child = roots[0].findFileByRelativePath(FOO_BAR_JAVA); assertNotNull(Arrays.asList(roots[0].getChildren()).toString(), child); - String s = VfsUtilCore.loadText(child); - assertEquals(expected, StringUtil.convertLineSeparators(s)); + assertEquals(expected, StringUtil.convertLineSeparators(VfsUtilCore.loadText(child))); - assertNotNull(fromTemplate.getBaseDir().findFileByRelativePath(".idea/workspace.xml")); + assertThat(Paths.get(fromTemplate.getBasePath(), ".idea/workspace.xml")).isRegularFile(); } @Override diff --git a/platform/configuration-store-impl/src/ProjectStoreBase.kt b/platform/configuration-store-impl/src/ProjectStoreBase.kt index 15588d382f75..75ddb3fdbc03 100644 --- a/platform/configuration-store-impl/src/ProjectStoreBase.kt +++ b/platform/configuration-store-impl/src/ProjectStoreBase.kt @@ -122,6 +122,8 @@ abstract class ProjectStoreBase(final override val project: Project) : Component if (isUnitTestMode) { // load state only if there are existing files isOptimiseTestLoadSpeed = !Paths.get(filePath).toFile().exists() + + storageManager.addMacro(StoragePathMacros.PRODUCT_WORKSPACE_FILE, workspacePath) } } else { @@ -135,6 +137,8 @@ abstract class ProjectStoreBase(final override val project: Project) : Component if (isUnitTestMode) { // load state only if there are existing files isOptimiseTestLoadSpeed = !Paths.get(filePath).exists() + + storageManager.addMacro(StoragePathMacros.PRODUCT_WORKSPACE_FILE, "$configDir/product-workspace.xml") } if (isRefreshVfsNeeded) { @@ -150,7 +154,6 @@ abstract class ProjectStoreBase(final override val project: Project) : Component storageManager.addMacro(StoragePathMacros.CACHE_FILE, appSystemDir.resolve("workspace").resolve(cacheFileName).systemIndependentPath) if (isUnitTestMode) { - storageManager.addMacro(StoragePathMacros.PRODUCT_WORKSPACE_FILE, storageManager.expandMacro(StoragePathMacros.WORKSPACE_FILE)) return } diff --git a/platform/configuration-store-impl/src/StateStorageManagerImpl.kt b/platform/configuration-store-impl/src/StateStorageManagerImpl.kt index 1bfe4ddeb930..edacab90a969 100644 --- a/platform/configuration-store-impl/src/StateStorageManagerImpl.kt +++ b/platform/configuration-store-impl/src/StateStorageManagerImpl.kt @@ -178,16 +178,10 @@ open class StateStorageManagerImpl(private val rootTagName: String, if (storageClass != StateStorage::class.java) { return storageClass.name!! } - if (normalizedCollapsedPath.isEmpty()) { throw Exception("Normalized path is empty, raw path '$collapsedPath'") } - - val key = storageCreator?.key ?: normalizedCollapsedPath - if (key == StoragePathMacros.PRODUCT_WORKSPACE_FILE && ApplicationManager.getApplication().isUnitTestMode) { - return StoragePathMacros.WORKSPACE_FILE - } - return key + return storageCreator?.key ?: normalizedCollapsedPath } fun getCachedFileStorages(): Set = storageLock.read { storages.values.toSet() } diff --git a/platform/lang-impl/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java b/platform/lang-impl/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java index 629e384380bb..0588a4d786af 100644 --- a/platform/lang-impl/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java +++ b/platform/lang-impl/src/com/intellij/platform/templates/SaveProjectAsTemplateAction.java @@ -31,7 +31,6 @@ import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.JDOMUtil; import com.intellij.openapi.util.io.FileUtilRt; -import com.intellij.openapi.util.io.StreamUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VfsUtilCore; @@ -145,10 +144,7 @@ public class SaveProjectAsTemplateAction extends AnAction implements DumbAware { indicator.setText("Saving project..."); StoreUtil.saveSettings(project, true); indicator.setText("Processing project files..."); - ZipOutputStream stream = null; - try { - stream = new ZipOutputStream(PathKt.outputStream(zipFile)); - + try (ZipOutputStream stream = new ZipOutputStream(PathKt.outputStream(zipFile))) { final VirtualFile dir = getDirectoryToSave(project, moduleToSave); List roots = collectStructure(project, moduleToSave); @@ -166,9 +162,7 @@ public class SaveProjectAsTemplateAction extends AnAction implements DumbAware { FileIndex index = moduleToSave == null ? ProjectRootManager.getInstance(project).getFileIndex() : ModuleRootManager.getInstance(moduleToSave).getFileIndex(); - final ZipOutputStream finalStream = stream; - - MyContentIterator iterator = new MyContentIterator(indicator, finalStream, project, parameters, shouldEscape); + MyContentIterator iterator = new MyContentIterator(indicator, stream, project, parameters, shouldEscape); for (LocalArchivedTemplate.RootDescription root : roots) { String prefix = LocalArchivedTemplate.ROOT_FILE_NAME + root.myIndex; VirtualFile rootFile = root.myFile; @@ -183,9 +177,6 @@ public class SaveProjectAsTemplateAction extends AnAction implements DumbAware { LOG.error(ex); UIUtil.invokeLaterIfNeeded(() -> Messages.showErrorDialog(project, "Can't save project as template", "Internal Error")); } - finally { - StreamUtil.closeStream(stream); - } } private static LocalArchivedTemplate.RootDescription findOrAddBaseRoot(List roots, VirtualFile dirToSave) {