From bf032008e82a66167db15ec8ba4e49428715fa9a Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 7 Jan 2021 18:21:31 +0100 Subject: [PATCH] Refactor tests: use loadPluginWithText for all plugin loading tasks GitOrigin-RevId: d8fa2d45a961245c860b533148622b0225f08399 --- .../ide/plugins/DynamicPluginsTest.kt | 44 +++---------------- .../ide/plugins/DynamicPluginsTestUtil.kt | 22 +++++----- 2 files changed, 16 insertions(+), 50 deletions(-) diff --git a/platform/platform-tests/testSrc/com/intellij/ide/plugins/DynamicPluginsTest.kt b/platform/platform-tests/testSrc/com/intellij/ide/plugins/DynamicPluginsTest.kt index 6afde886dc2d..fd2cf452a510 100644 --- a/platform/platform-tests/testSrc/com/intellij/ide/plugins/DynamicPluginsTest.kt +++ b/platform/platform-tests/testSrc/com/intellij/ide/plugins/DynamicPluginsTest.kt @@ -542,21 +542,12 @@ class DynamicPluginsTest { try { val quuxDisposable = loadPluginWithText(quuxBuilder) try { - val directory = Files.createTempDirectory(inMemoryFs.fs.getPath("/"), null).resolve("plugin") - mainDescriptor.build(directory) - val descriptor = loadDescriptorInTest(directory) - setPluginClassLoaderForMainAndSubPlugins(descriptor, DynamicPluginsTest::class.java.classLoader) - assertThat(DynamicPlugins.checkCanUnloadWithoutRestart(descriptor)).isNull() - - DynamicPlugins.loadPlugin(descriptor) + val mainDisposable = loadPluginWithText(mainDescriptor) try { assertThat(ApplicationManager.getApplication().getService(MyPersistentComponent::class.java)).isNotNull() } finally { - val unloadDescriptor = loadDescriptorInTest(directory) - val canBeUnloaded = DynamicPlugins.allowLoadUnloadWithoutRestart(unloadDescriptor) - DynamicPlugins.unloadPlugin(unloadDescriptor) - assertThat(canBeUnloaded).isTrue() + Disposer.dispose(mainDisposable) assertThat(ApplicationManager.getApplication().getService(MyPersistentComponent::class.java)).isNull() } } @@ -571,8 +562,6 @@ class DynamicPluginsTest { @Test fun unloadNestedOptionalDependency() { - val directory = Files.createTempDirectory(inMemoryFs.fs.getPath("/"), null).resolve("plugin") - val barBuilder = PluginBuilder().randomId("bar") val quuxBuilder = PluginBuilder().randomId("quux") @@ -584,22 +573,14 @@ class DynamicPluginsTest { val barDisposable = loadPluginWithText(barBuilder) try { val quuxDisposable = loadPluginWithText(quuxBuilder) - mainDescriptor.build(directory) - val descriptor = loadDescriptorInTest(directory) - setPluginClassLoaderForMainAndSubPlugins(descriptor, DynamicPluginsTest::class.java.classLoader) - assertThat(DynamicPlugins.checkCanUnloadWithoutRestart(descriptor)).isNull() - - DynamicPlugins.loadPlugin(descriptor) + val mainDisposable = loadPluginWithText(mainDescriptor) try { assertThat(ApplicationManager.getApplication().getService(MyPersistentComponent::class.java)).isNotNull() Disposer.dispose(quuxDisposable) assertThat(ApplicationManager.getApplication().getService(MyPersistentComponent::class.java)).isNull() } finally { - val unloadDescriptor = loadDescriptorInTest(directory) - val canBeUnloaded = DynamicPlugins.allowLoadUnloadWithoutRestart(unloadDescriptor) - DynamicPlugins.unloadPlugin(unloadDescriptor) - assertThat(canBeUnloaded).isTrue() + Disposer.dispose(mainDisposable) } } finally { @@ -610,21 +591,8 @@ class DynamicPluginsTest { private fun loadPluginWithOptionalDependency(pluginDescriptor: PluginBuilder, optionalDependencyDescriptor: PluginBuilder, dependsOn: PluginBuilder): Disposable { - val directory = inMemoryFs.fs.getPath("/").resolve(Ksuid.generate()).resolve("plugin") - pluginDescriptor.depends(dependsOn.id, optionalDependencyDescriptor).build(directory) - - val descriptor = loadDescriptorInTest(directory) - setPluginClassLoaderForMainAndSubPlugins(descriptor, DynamicPluginsTest::class.java.classLoader) - assertThat(DynamicPlugins.checkCanUnloadWithoutRestart(descriptor)).isNull() - - DynamicPlugins.loadPlugin(descriptor) - - return Disposable { - val unloadDescriptor = loadDescriptorInTest(directory) - val canBeUnloaded = DynamicPlugins.allowLoadUnloadWithoutRestart(unloadDescriptor) - DynamicPlugins.unloadPlugin(unloadDescriptor) - assertThat(canBeUnloaded).isTrue() - } + pluginDescriptor.depends(dependsOn.id, optionalDependencyDescriptor) + return loadPluginWithText(pluginDescriptor) } } diff --git a/platform/platform-tests/testSrc/com/intellij/ide/plugins/DynamicPluginsTestUtil.kt b/platform/platform-tests/testSrc/com/intellij/ide/plugins/DynamicPluginsTestUtil.kt index 0ff475cf8469..449302501b66 100644 --- a/platform/platform-tests/testSrc/com/intellij/ide/plugins/DynamicPluginsTestUtil.kt +++ b/platform/platform-tests/testSrc/com/intellij/ide/plugins/DynamicPluginsTestUtil.kt @@ -11,7 +11,6 @@ import com.intellij.openapi.util.BuildNumber import com.intellij.openapi.util.io.FileUtil import com.intellij.testFramework.assertions.Assertions.assertThat import com.intellij.util.io.Ksuid -import com.intellij.util.io.write import java.nio.file.FileSystem import java.nio.file.FileSystems import java.nio.file.Files @@ -45,7 +44,14 @@ fun loadExtensionWithText( } internal fun loadPluginWithText(pluginBuilder: PluginBuilder, loader: ClassLoader, fs: FileSystem): Disposable { - val descriptor = preparePluginDescriptor(pluginBuilder.text(), fs).second + val directory = if (fs == FileSystems.getDefault()) + FileUtil.createTempDirectory("test", "test", true).toPath() + else + fs.getPath("/").resolve(Ksuid.generate()) + val pluginDirectory = directory.resolve("plugin") + + pluginBuilder.build(pluginDirectory) + val descriptor = loadDescriptorInTest(pluginDirectory) assertThat(DynamicPlugins.checkCanUnloadWithoutRestart(descriptor)).isNull() setPluginClassLoaderForMainAndSubPlugins(descriptor, loader) try { @@ -57,22 +63,14 @@ internal fun loadPluginWithText(pluginBuilder: PluginBuilder, loader: ClassLoade } return Disposable { - val canBeUnloaded = DynamicPlugins.allowLoadUnloadWithoutRestart(descriptor) + val unloadDescriptor = loadDescriptorInTest(pluginDirectory) + val canBeUnloaded = DynamicPlugins.allowLoadUnloadWithoutRestart(unloadDescriptor) unloadPlugin(descriptor) assertThat(canBeUnloaded).isTrue() } } -private fun preparePluginDescriptor(pluginXml: String, fs: FileSystem): Pair { - val directory = if (fs == FileSystems.getDefault()) FileUtil.createTempDirectory("test", "test", true).toPath() else fs.getPath("/").resolve( - Ksuid.generate()) - val plugin = directory.resolve("plugin/META-INF/plugin.xml") - plugin.write(pluginXml) - val descriptor = loadDescriptorInTest(plugin.parent.parent) - return Pair(plugin, descriptor) -} - internal fun setPluginClassLoaderForMainAndSubPlugins(rootDescriptor: IdeaPluginDescriptorImpl, classLoader: ClassLoader?) { rootDescriptor.classLoader = classLoader for (dependency in rootDescriptor.getPluginDependencies()) {