Skip to content

Commit

Permalink
feat:implement tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Jan 29, 2024
1 parent aba723a commit a2e3c17
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import git4idea.remote.hosting.knownRepositories
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import org.jetbrains.plugins.github.authentication.GHAccountsUtil
import org.jetbrains.plugins.github.authentication.accounts.GHAccountManager
import org.jetbrains.plugins.github.authentication.accounts.GithubAccount
import org.jetbrains.plugins.github.util.GHGitRepositoryMapping
Expand All @@ -45,7 +44,7 @@ class GhActionsToolWindowFactory : ToolWindowFactory, DumbAware {
private lateinit var settingsService: GhActionsSettingsService
private val projectReposMap = mutableMapOf<Project, ProjectRepositories>()
private val scope = CoroutineScope(SupervisorJob())

private val gitHubAccounts: MutableSet<GithubAccount> = mutableSetOf()
override fun init(toolWindow: ToolWindow) {
val project = toolWindow.project
if (!projectReposMap.containsKey(toolWindow.project)) {
Expand All @@ -70,6 +69,8 @@ class GhActionsToolWindowFactory : ToolWindowFactory, DumbAware {
val accountManager = service<GHAccountManager>()
scope.launch {
accountManager.accountsState.collect {
gitHubAccounts.clear()
gitHubAccounts.addAll(it)
updateRepos(toolWindow, repositoriesManager.knownRepositories)
}
}
Expand Down Expand Up @@ -101,7 +102,7 @@ class GhActionsToolWindowFactory : ToolWindowFactory, DumbAware {
Disposer.register(toolWindow.disposable, disposable)
ApplicationManager.getApplication().invokeLater {
toolWindow.contentManager.removeAllContents(true)
if ((GHAccountsUtil.accounts.isEmpty() && settingsService.state.useGitHubSettings)
if ((gitHubAccounts.isEmpty() && settingsService.state.useGitHubSettings)
|| (!settingsService.state.useGitHubSettings && settingsService.state.apiToken == "")
) {
createNoAccountPanel(disposable, projectRepos)
Expand Down Expand Up @@ -199,8 +200,7 @@ class GhActionsToolWindowFactory : ToolWindowFactory, DumbAware {
}

private fun guessAccountForRepository(repo: GHGitRepositoryMapping): GithubAccount? {
val accounts = GHAccountsUtil.accounts
return accounts.firstOrNull { it.server.equals(repo.repository.serverPath, true) }
return gitHubAccounts.firstOrNull { it.server.equals(repo.repository.serverPath, true) }
}

private fun createRepoWorkflowsPanels(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import kotlinx.coroutines.yield
import org.jetbrains.plugins.github.api.GithubApiRequestExecutor
import org.jetbrains.plugins.github.api.GithubApiRequests
import org.jetbrains.plugins.github.api.GithubServerPath
import org.jetbrains.plugins.github.api.data.GithubRepo
import org.jetbrains.plugins.github.api.data.GithubResponsePage
import org.jetbrains.plugins.github.authentication.GHAccountsUtil
import org.jetbrains.plugins.github.authentication.accounts.GHAccountManager
import org.jetbrains.plugins.github.authentication.accounts.GithubAccount
Expand All @@ -42,44 +40,43 @@ import org.jetbrains.plugins.github.util.GHHostedRepositoriesManager
*
*/
abstract class GitHubActionsManagerBaseTest : BasePlatformTestCase() {
private lateinit var accountManager: GHAccountManager
private lateinit var organisation: String
protected lateinit var organisation: String
protected lateinit var executor: GithubApiRequestExecutor
protected lateinit var accountManager: GHAccountManager
protected lateinit var repositoriesManager: GHHostedRepositoriesManager
protected lateinit var mainAccount: AccountData

private val mainRepos = mutableSetOf<String>()
protected lateinit var myProject: Project
protected lateinit var factory: GhActionsToolWindowFactory
protected lateinit var toolWindow: ToolWindow

protected lateinit var host: GithubServerPath
override fun setUp() {
super.setUp()
myProject = project
factory = GhActionsToolWindowFactory()
toolWindow = MockToolWindow(myProject)
val host =
GithubServerPath.from(System.getenv("idea_test_github_host") ?: System.getenv("idea.test.github.host"))
host = GithubServerPath.from(System.getenv("idea_test_github_host") ?: System.getenv("idea.test.github.host"))

val token1 = System.getenv("idea_test_github_token1") ?: System.getenv("idea.test.github.token1")

assertNotNull(token1)
executor = service<GithubApiRequestExecutor.Factory>().create(token1)
accountManager = service()
repositoriesManager = project.service()

organisation = System.getenv("idea_test_github_org") ?: System.getenv("idea.test.github.org")
assertNotNull(organisation)
mainAccount = createAccountData(host, token1)
mainAccount = createAccountData(token1)
setCurrentAccount(mainAccount)
}

private fun createAccountData(host: GithubServerPath, token: String): AccountData {
protected fun createAccountData(token: String): AccountData {
val account = GHAccountManager.createAccount("token", host)
runBlocking { accountManager.updateAccount(account, token) }
val executor = service<GithubApiRequestExecutor.Factory>().create(token)
val username = executor.execute(GithubApiRequests.CurrentUser.get(account.server)).login
val repos =
executor.execute<GithubResponsePage<GithubRepo>>(GithubApiRequests.CurrentUser.Repos.get(account.server))
repos.items.forEach { mainRepos.add(it.name) }
val repos = executor.execute(GithubApiRequests.CurrentUser.Repos.get(account.server))

return AccountData(token, account, username, executor)
return AccountData(token, account, username, executor, repos.items.map { it.name }.toSet())
}


Expand All @@ -91,7 +88,8 @@ abstract class GitHubActionsManagerBaseTest : BasePlatformTestCase() {
val token: String,
val account: GithubAccount,
val username: String,
val executor: GithubApiRequestExecutor
val executor: GithubApiRequestExecutor,
val repos: Set<String>,
)


Expand Down
33 changes: 31 additions & 2 deletions src/test/kotlin/com/dsoftware/ghmanager/ToolWindowFactoryTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.dsoftware.ghmanager

import com.intellij.ui.components.JBPanelWithEmptyText
import git4idea.remote.hosting.findKnownRepositories
import junit.framework.TestCase
import kotlinx.coroutines.runBlocking


class ToolWindowFactoryTest : GitHubActionsManagerBaseTest() {
Expand All @@ -10,7 +12,34 @@ class ToolWindowFactoryTest : GitHubActionsManagerBaseTest() {
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(project)

TestCase.assertEquals(1, toolWindow.contentManager.contentCount)
val panel = toolWindow.contentManager.contents[0].component
TestCase.assertTrue(panel is JBPanelWithEmptyText)
val component = toolWindow.contentManager.contents[0].component
TestCase.assertTrue(component is JBPanelWithEmptyText)
val panel = component as JBPanelWithEmptyText
TestCase.assertEquals("GitHub account not configured and no API Token", panel.emptyText.text)
}

fun testGitHubAccountNoReposPanel() {
runBlocking { accountManager.updateAccount(mainAccount.account, mainAccount.token) }

factory.init(toolWindow)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(project)

TestCase.assertEquals(1, toolWindow.contentManager.contentCount)
val component = toolWindow.contentManager.contents[0].component
TestCase.assertTrue(component is JBPanelWithEmptyText)
val panel = component as JBPanelWithEmptyText
TestCase.assertEquals("No git repositories in project", panel.emptyText.text)
}

// fun testGitHubAccountWithReposPanel() {
// runBlocking { accountManager.updateAccount(mainAccount.account, mainAccount.token) }
// factory.init(toolWindow)
// executeSomeCoroutineTasksAndDispatchAllInvocationEvents(project)
//
// TestCase.assertEquals(1, toolWindow.contentManager.contentCount)
// val component = toolWindow.contentManager.contents[0].component
// TestCase.assertTrue(component is JBPanelWithEmptyText)
// val panel = component as JBPanelWithEmptyText
// TestCase.assertEquals("No git repositories in project", panel.emptyText.text)
// }
}

0 comments on commit a2e3c17

Please sign in to comment.