Skip to content

Commit

Permalink
fix:improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Feb 5, 2024
1 parent 4cd7d62 commit 52beb97
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package com.dsoftware.ghmanager

import com.dsoftware.ghmanager.data.GhActionsService
import com.dsoftware.ghmanager.ui.GhActionsToolWindowFactory
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.platform.ide.progress.runWithModalProgressBlocking
import com.intellij.testFramework.PlatformTestUtil
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import com.intellij.testFramework.registerServiceInstance
import com.intellij.toolWindow.ToolWindowHeadlessManagerImpl.MockToolWindow
import com.intellij.toolWindow.ToolWindowHeadlessManagerImpl
import com.intellij.util.concurrency.annotations.RequiresEdt
import io.mockk.every
import io.mockk.mockk
Expand All @@ -23,14 +22,14 @@ import org.jetbrains.plugins.github.util.GHGitRepositoryMapping


abstract class GitHubActionsManagerBaseTest : BasePlatformTestCase() {
private val host: GithubServerPath = GithubServerPath.from("github.com")
protected lateinit var factory: GhActionsToolWindowFactory
protected lateinit var toolWindow: ToolWindow

protected val host: GithubServerPath = GithubServerPath.from("github.com")
override fun setUp() {
super.setUp()
factory = GhActionsToolWindowFactory()
toolWindow = MockToolWindow(project)
toolWindow = ToolWindowHeadlessManagerImpl.MockToolWindow(project)
}

fun mockGhActionsService(repoUrls: Set<String>, accountNames: Collection<String>) {
Expand All @@ -52,29 +51,6 @@ abstract class GitHubActionsManagerBaseTest : BasePlatformTestCase() {
get() = MutableStateFlow(accounts)
})
}


companion object {
private const val RETRIES = 3

internal fun retry(LOG: Logger, exception: Boolean = true, action: () -> Unit) {
for (i in 1..RETRIES) {
try {
LOG.debug("Attempt #$i")
return action()
} catch (e: Throwable) {
if (i == RETRIES) {
if (exception) throw e
else {
LOG.error(e)
return
}
}
Thread.sleep(1000L)
}
}
}
}
}

@RequiresEdt
Expand Down
42 changes: 42 additions & 0 deletions src/test/kotlin/com/dsoftware/ghmanager/TestWindowTabController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.dsoftware.ghmanager

import com.dsoftware.ghmanager.api.model.WorkflowRuns
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.mockkStatic
import junit.framework.TestCase
import org.jetbrains.plugins.github.api.GithubApiRequest
import org.jetbrains.plugins.github.api.GithubApiRequestExecutor
import org.jetbrains.plugins.github.util.GHCompatibilityUtil
import javax.swing.JPanel

class TestWindowTabController : GitHubActionsManagerBaseTest() {
override fun setUp() {
super.setUp()
mockGhActionsService(setOf("http://github.com/owner/repo"), setOf("account1"))
mockkStatic(GHCompatibilityUtil::class)
every { GHCompatibilityUtil.getOrRequestToken(any(), any()) } returns "token"

mockkObject(GithubApiRequestExecutor.Factory)
every { GithubApiRequestExecutor.Factory.getInstance() } returns mockk<GithubApiRequestExecutor.Factory> {
every { create(any(), useProxy = false) } returns mockk<GithubApiRequestExecutor>(relaxed = true) {
every { execute(any(), any<GithubApiRequest<WorkflowRuns>>()) } returns WorkflowRuns(0, emptyList())
}
}

factory.init(toolWindow)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(project)
}

fun testGitHubAccountWithReposPanel() {


TestCase.assertEquals(1, toolWindow.contentManager.contentCount)
val content = toolWindow.contentManager.contents[0]
TestCase.assertEquals("owner/repo", content.displayName)
TestCase.assertTrue(content.component is JPanel)
val panel = content.component as JPanel
TestCase.assertEquals(1, panel.componentCount)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import javax.swing.JPanel


class ToolWindowFactoryTest : GitHubActionsManagerBaseTest() {


fun testNoGitHubAccountPanel() {
mockGhActionsService(emptySet(), emptySet())

Expand Down

0 comments on commit 52beb97

Please sign in to comment.