|
| 1 | +package com.github.pyvenvmanage |
| 2 | + |
| 3 | +import io.mockk.every |
| 4 | +import io.mockk.mockk |
| 5 | +import io.mockk.mockkObject |
| 6 | +import io.mockk.mockkStatic |
| 7 | +import io.mockk.slot |
| 8 | +import io.mockk.unmockkObject |
| 9 | +import io.mockk.unmockkStatic |
| 10 | +import io.mockk.verify |
| 11 | +import kotlinx.coroutines.runBlocking |
| 12 | +import org.junit.jupiter.api.AfterEach |
| 13 | +import org.junit.jupiter.api.BeforeEach |
| 14 | +import org.junit.jupiter.api.Test |
| 15 | + |
| 16 | +import com.intellij.ide.plugins.IdeaPluginDescriptor |
| 17 | +import com.intellij.ide.plugins.PluginManagerCore |
| 18 | +import com.intellij.notification.Notification |
| 19 | +import com.intellij.notification.NotificationAction |
| 20 | +import com.intellij.notification.NotificationGroup |
| 21 | +import com.intellij.notification.NotificationGroupManager |
| 22 | +import com.intellij.notification.NotificationType |
| 23 | +import com.intellij.openapi.extensions.PluginId |
| 24 | +import com.intellij.openapi.project.Project |
| 25 | + |
| 26 | +import com.github.pyvenvmanage.settings.PyVenvManageSettings |
| 27 | + |
| 28 | +class PythonRequiredStartupActivityTest { |
| 29 | + private lateinit var project: Project |
| 30 | + private lateinit var notificationGroupManager: NotificationGroupManager |
| 31 | + private lateinit var notificationGroup: NotificationGroup |
| 32 | + private lateinit var notification: Notification |
| 33 | + private lateinit var settings: PyVenvManageSettings |
| 34 | + |
| 35 | + @BeforeEach |
| 36 | + fun setUp() { |
| 37 | + project = mockk(relaxed = true) |
| 38 | + notificationGroupManager = mockk(relaxed = true) |
| 39 | + notificationGroup = mockk(relaxed = true) |
| 40 | + notification = mockk(relaxed = true) |
| 41 | + settings = mockk(relaxed = true) |
| 42 | + |
| 43 | + mockkStatic(NotificationGroupManager::class) |
| 44 | + mockkStatic(PluginManagerCore::class) |
| 45 | + mockkObject(PyVenvManageSettings) |
| 46 | + |
| 47 | + every { NotificationGroupManager.getInstance() } returns notificationGroupManager |
| 48 | + every { notificationGroupManager.getNotificationGroup("PyVenv Manage") } returns notificationGroup |
| 49 | + every { |
| 50 | + notificationGroup.createNotification(any<String>(), any<String>(), any<NotificationType>()) |
| 51 | + } returns notification |
| 52 | + every { notification.addAction(any<NotificationAction>()) } returns notification |
| 53 | + every { PyVenvManageSettings.getInstance() } returns settings |
| 54 | + every { settings.dismissedPythonWarning } returns false |
| 55 | + } |
| 56 | + |
| 57 | + @AfterEach |
| 58 | + fun tearDown() { |
| 59 | + unmockkStatic(NotificationGroupManager::class) |
| 60 | + unmockkStatic(PluginManagerCore::class) |
| 61 | + unmockkObject(PyVenvManageSettings) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `does not show notification when python module is available`(): Unit = |
| 66 | + runBlocking { |
| 67 | + val pythonPlugin: IdeaPluginDescriptor = mockk(relaxed = true) |
| 68 | + every { pythonPlugin.isEnabled } returns true |
| 69 | + every { |
| 70 | + PluginManagerCore.getPlugin(PluginId.getId("com.intellij.modules.python")) |
| 71 | + } returns pythonPlugin |
| 72 | + |
| 73 | + PythonRequiredStartupActivity().execute(project) |
| 74 | + |
| 75 | + verify(exactly = 0) { notification.notify(any()) } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun `does not show notification when PythonCore plugin is available`(): Unit = |
| 80 | + runBlocking { |
| 81 | + val pythonCorePlugin: IdeaPluginDescriptor = mockk(relaxed = true) |
| 82 | + every { pythonCorePlugin.isEnabled } returns true |
| 83 | + every { PluginManagerCore.getPlugin(PluginId.getId("com.intellij.modules.python")) } returns null |
| 84 | + every { PluginManagerCore.getPlugin(PluginId.getId("PythonCore")) } returns pythonCorePlugin |
| 85 | + |
| 86 | + PythonRequiredStartupActivity().execute(project) |
| 87 | + |
| 88 | + verify(exactly = 0) { notification.notify(any()) } |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun `shows warning notification when python is not available`(): Unit = |
| 93 | + runBlocking { |
| 94 | + every { PluginManagerCore.getPlugin(PluginId.getId("com.intellij.modules.python")) } returns null |
| 95 | + every { PluginManagerCore.getPlugin(PluginId.getId("PythonCore")) } returns null |
| 96 | + |
| 97 | + PythonRequiredStartupActivity().execute(project) |
| 98 | + |
| 99 | + verify { |
| 100 | + notificationGroup.createNotification( |
| 101 | + "PyVenv Manage requires Python support", |
| 102 | + "Please install the Python plugin or use PyCharm for full functionality.", |
| 103 | + NotificationType.WARNING, |
| 104 | + ) |
| 105 | + } |
| 106 | + verify { notification.notify(project) } |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun `shows warning when python plugin exists but is disabled`(): Unit = |
| 111 | + runBlocking { |
| 112 | + val disabledPlugin: IdeaPluginDescriptor = mockk(relaxed = true) |
| 113 | + every { disabledPlugin.isEnabled } returns false |
| 114 | + every { PluginManagerCore.getPlugin(PluginId.getId("com.intellij.modules.python")) } returns disabledPlugin |
| 115 | + every { PluginManagerCore.getPlugin(PluginId.getId("PythonCore")) } returns null |
| 116 | + |
| 117 | + PythonRequiredStartupActivity().execute(project) |
| 118 | + |
| 119 | + verify { notification.notify(project) } |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun `does not show notification when warning was dismissed`(): Unit = |
| 124 | + runBlocking { |
| 125 | + every { PluginManagerCore.getPlugin(PluginId.getId("com.intellij.modules.python")) } returns null |
| 126 | + every { PluginManagerCore.getPlugin(PluginId.getId("PythonCore")) } returns null |
| 127 | + every { settings.dismissedPythonWarning } returns true |
| 128 | + |
| 129 | + PythonRequiredStartupActivity().execute(project) |
| 130 | + |
| 131 | + verify(exactly = 0) { notification.notify(any()) } |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + fun `notification includes dont show again action`(): Unit = |
| 136 | + runBlocking { |
| 137 | + every { PluginManagerCore.getPlugin(PluginId.getId("com.intellij.modules.python")) } returns null |
| 138 | + every { PluginManagerCore.getPlugin(PluginId.getId("PythonCore")) } returns null |
| 139 | + |
| 140 | + PythonRequiredStartupActivity().execute(project) |
| 141 | + |
| 142 | + verify { notification.addAction(any<NotificationAction>()) } |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + fun `dont show again action sets dismissed flag and expires notification`(): Unit = |
| 147 | + runBlocking { |
| 148 | + every { PluginManagerCore.getPlugin(PluginId.getId("com.intellij.modules.python")) } returns null |
| 149 | + every { PluginManagerCore.getPlugin(PluginId.getId("PythonCore")) } returns null |
| 150 | + |
| 151 | + val actionSlot = slot<NotificationAction>() |
| 152 | + every { notification.addAction(capture(actionSlot)) } returns notification |
| 153 | + |
| 154 | + PythonRequiredStartupActivity().execute(project) |
| 155 | + |
| 156 | + val event: com.intellij.openapi.actionSystem.AnActionEvent = mockk(relaxed = true) |
| 157 | + actionSlot.captured.actionPerformed(event, notification) |
| 158 | + |
| 159 | + verify { settings.dismissedPythonWarning = true } |
| 160 | + verify { notification.expire() } |
| 161 | + } |
| 162 | +} |
0 commit comments