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 6, 2024
1 parent 52beb97 commit 08a57f8
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ data class RepositoryCoordinates(
@Service(Service.Level.PROJECT)
class WorkflowDataContextService(project: Project) {
private val settingsService = GhActionsSettingsService.getInstance(project)
private val repositories =
val repositories =
mutableMapOf<GitRemoteUrlCoordinates, LazyCancellableBackgroundProcessValue<WorkflowRunSelectionContext>>()

@RequiresEdt
Expand Down Expand Up @@ -75,7 +75,7 @@ class WorkflowDataContextService(project: Project) {
@RequiresBackgroundThread
@Throws(IOException::class)
private fun getContext(
disposable: CheckedDisposable,
checkedDisposable: CheckedDisposable,
account: GithubAccount,
repositoryMapping: GHGitRepositoryMapping,
toolWindow: ToolWindow,
Expand All @@ -98,15 +98,15 @@ class WorkflowDataContextService(project: Project) {
singleRunDataLoader.invalidateAllData()
}
val runsLoader = WorkflowRunListLoader(
disposable,
checkedDisposable,
requestExecutor,
repositoryCoordinates,
settingsService,
WorkflowRunFilter(),
)

return WorkflowRunSelectionContext(
disposable,
checkedDisposable,
toolWindow.project,
account,
singleRunDataLoader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import com.intellij.collaboration.async.CompletableFutureUtil.submitIOTask
import com.intellij.collaboration.ui.SimpleEventListener
import com.intellij.openapi.Disposable
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.observable.util.whenDisposed
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.util.CheckedDisposable
import com.intellij.openapi.util.Disposer
import com.intellij.ui.CollectionListModel
import com.intellij.util.EventDispatcher
Expand All @@ -28,7 +30,7 @@ import java.util.concurrent.TimeUnit
import kotlin.properties.Delegates

class WorkflowRunListLoader(
parentDisposable: Disposable,
parentDisposable: CheckedDisposable,
private val requestExecutor: GithubApiRequestExecutor,
private val repositoryCoordinates: RepositoryCoordinates,
private val settingsService: GhActionsSettingsService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.intellij.ide.actions.RefreshAction
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.util.CheckedDisposable
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.Key
import com.intellij.openapi.wm.ToolWindow
Expand Down Expand Up @@ -42,7 +43,7 @@ class WorkflowToolWindowTabController(
val loadingModel: GHCompletableFutureLoadingModel<WorkflowRunSelectionContext>
private val settingsService = GhActionsSettingsService.getInstance(toolWindow.project)
private val actionManager = ActionManager.getInstance()
val disposable = Disposer.newCheckedDisposable("WorkflowToolWindowTabController")
val disposable: CheckedDisposable = Disposer.newCheckedDisposable("WorkflowToolWindowTabController")
val panel: JComponent
private var contentDisposable by Delegates.observable<Disposable?>(null) { _, oldValue, newValue ->
if (oldValue != null) Disposer.dispose(oldValue)
Expand All @@ -52,8 +53,8 @@ class WorkflowToolWindowTabController(
init {
Disposer.register(parentDisposable, disposable)
contentDisposable = Disposable {
Disposer.dispose(disposable)
dataContextRepository.clearContext(repositoryMapping)
Disposer.dispose(disposable)
}
loadingModel = GHCompletableFutureLoadingModel<WorkflowRunSelectionContext>(disposable).apply {
future = dataContextRepository.acquireContext(
Expand Down
73 changes: 64 additions & 9 deletions src/test/kotlin/com/dsoftware/ghmanager/TestWindowTabController.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,97 @@
package com.dsoftware.ghmanager

import com.dsoftware.ghmanager.api.model.WorkflowRuns
import com.dsoftware.ghmanager.api.model.WorkflowType
import com.dsoftware.ghmanager.api.model.WorkflowTypes
import com.dsoftware.ghmanager.data.WorkflowDataContextService
import com.dsoftware.ghmanager.data.WorkflowRunSelectionContext
import com.intellij.openapi.components.service
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.api.data.GithubBranch
import org.jetbrains.plugins.github.api.data.GithubResponsePage
import org.jetbrains.plugins.github.api.data.GithubUserWithPermissions
import org.jetbrains.plugins.github.util.GHCompatibilityUtil
import javax.swing.JPanel

class TestWindowTabController : GitHubActionsManagerBaseTest() {
private lateinit var workflowDataContextService: WorkflowDataContextService
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)
workflowDataContextService = project.service<WorkflowDataContextService>()
}

fun testGitHubAccountWithReposPanel() {
fun testNoWorkflowRunsInRepo() {

mockGithubApiRequestExecutor(WorkflowRuns(0, emptyList()))

executeSomeCoroutineTasksAndDispatchAllInvocationEvents(project)

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)
TestCase.assertEquals(1, workflowDataContextService.repositories.size)
val workflowRunSelectionContext: WorkflowRunSelectionContext =
workflowDataContextService.repositories.values.first().lastLoadedValue!!
TestCase.assertEquals(0, workflowRunSelectionContext.runsListModel.size)
}

private fun mockGithubApiRequestExecutor(
workflowRuns: WorkflowRuns,
collaborators: Collection<String> = emptyList(),
branches: Collection<String> = emptyList(),
workflowTypes: Collection<WorkflowType> = emptyList(),
) {
val collaboratorsResponse = GithubResponsePage(collaborators.map {
val user = mockk<GithubUserWithPermissions> {
every { login }.returns(it)
}
user
})
val branchesResponse = GithubResponsePage(branches.map {
val branch = mockk<GithubBranch> {
every { name }.returns(it)
}
branch
})
val workflowTypesResponse = WorkflowTypes(workflowTypes.size, workflowTypes.toList())

mockkObject(GithubApiRequestExecutor.Factory)
every { GithubApiRequestExecutor.Factory.getInstance() } returns mockk<GithubApiRequestExecutor.Factory> {
every { create(token = any()) } returns mockk<GithubApiRequestExecutor>(relaxed = true) {
every { execute(any(), any<GithubApiRequest<WorkflowRuns>>()) } returns workflowRuns
every {// collaborators
execute(
any(),
any<GithubApiRequest<GithubResponsePage<GithubUserWithPermissions>>>()
)
} returns collaboratorsResponse
every { // branches
execute(
any(),
any<GithubApiRequest<GithubResponsePage<GithubBranch>>>()
)
} returns branchesResponse
every { // branches
execute(
any(),
any<GithubApiRequest<WorkflowTypes>>()
)
} returns workflowTypesResponse
}
}
}
}
29 changes: 28 additions & 1 deletion src/test/kotlin/com/dsoftware/ghmanager/ToolWindowFactoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ package com.dsoftware.ghmanager

import com.intellij.ui.SimpleColoredComponent
import com.intellij.ui.components.JBPanelWithEmptyText
import io.mockk.Called
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.verify
import junit.framework.TestCase
import org.jetbrains.plugins.github.api.GithubApiRequestExecutor
import org.jetbrains.plugins.github.util.GHCompatibilityUtil
import javax.swing.JPanel


class ToolWindowFactoryTest : GitHubActionsManagerBaseTest() {

private lateinit var requestExecutorfactoryMock: GithubApiRequestExecutor.Factory
override fun setUp() {
super.setUp()
requestExecutorfactoryMock = mockk<GithubApiRequestExecutor.Factory> {
every { create(token = any()) } throws Exception("No executor")
}
mockkObject(GithubApiRequestExecutor.Factory)
every { GithubApiRequestExecutor.Factory.getInstance() } returns requestExecutorfactoryMock
}

fun testNoGitHubAccountPanel() {
mockGhActionsService(emptySet(), emptySet())
Expand All @@ -25,6 +41,9 @@ class ToolWindowFactoryTest : GitHubActionsManagerBaseTest() {
TestCase.assertEquals("GitHub account not configured and no API Token", subComponents[0].getCharSequence(true))
TestCase.assertEquals("Go to github Settings", subComponents[1].getCharSequence(true))
TestCase.assertEquals("Go to ghactions-manager Settings", subComponents[2].getCharSequence(true))
verify {
requestExecutorfactoryMock.create(token = any()) wasNot Called
}
}

fun testGitHubAccountNoReposPanel() {
Expand All @@ -38,9 +57,14 @@ class ToolWindowFactoryTest : GitHubActionsManagerBaseTest() {
TestCase.assertTrue(component is JBPanelWithEmptyText)
val panel = component as JBPanelWithEmptyText
TestCase.assertEquals("No git repositories in project", panel.emptyText.text)
verify {
requestExecutorfactoryMock.create(token = any()) wasNot Called
}
}

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

factory.init(toolWindow)
Expand All @@ -52,5 +76,8 @@ class ToolWindowFactoryTest : GitHubActionsManagerBaseTest() {
TestCase.assertTrue(content.component is JPanel)
val panel = content.component as JPanel
TestCase.assertEquals(1, panel.componentCount)
verify {
requestExecutorfactoryMock.create(token = any())
}
}
}

0 comments on commit 08a57f8

Please sign in to comment.