generated from JetBrains/intellij-platform-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
176 additions
and
33 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/test/kotlin/com/dsoftware/ghmanager/GitHubActionsManagerBaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package com.dsoftware.ghmanager | ||
|
||
import com.dsoftware.ghmanager.ui.GhActionsToolWindowFactory | ||
import com.intellij.openapi.components.service | ||
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.RunAll | ||
import com.intellij.testFramework.fixtures.BasePlatformTestCase | ||
import com.intellij.toolWindow.ToolWindowHeadlessManagerImpl.MockToolWindow | ||
import com.intellij.util.ThrowableRunnable | ||
import com.intellij.util.concurrency.annotations.RequiresEdt | ||
import kotlinx.coroutines.runBlocking | ||
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 | ||
import org.jetbrains.plugins.github.util.GHHostedRepositoriesManager | ||
|
||
|
||
/** | ||
* | ||
* The base class for JUnit platform tests of the github plugin.<br></br> | ||
* Extend this test to write a test on GitHub which has the following features/limitations: | ||
* | ||
* * This is a "platform test case", which means that IDEA "almost" production platform is set up before the test starts. | ||
* * Project base directory is the root of everything. | ||
* | ||
* | ||
* All tests inherited from this class are required to have a token to access the Github server. | ||
* They are set up in Environment variables: <br></br> | ||
* `idea_test_github_host<br></br> | ||
* idea_test_github_token1<br></br> // token test user | ||
* idea_test_github_token2` // token user with configured test repositories | ||
* | ||
*/ | ||
abstract class GitHubActionsManagerBaseTest : BasePlatformTestCase() { | ||
private lateinit var accountManager: GHAccountManager | ||
private lateinit var organisation: String | ||
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 | ||
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")) | ||
val token1 = System.getenv("idea_test_github_token1") ?: System.getenv("idea.test.github.token1") | ||
|
||
assertNotNull(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) | ||
setCurrentAccount(mainAccount) | ||
} | ||
|
||
private fun createAccountData(host: GithubServerPath, 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) } | ||
|
||
return AccountData(token, account, username, executor) | ||
} | ||
|
||
|
||
protected open fun setCurrentAccount(accountData: AccountData?) { | ||
GHAccountsUtil.setDefaultAccount(myProject, accountData?.account) | ||
} | ||
|
||
protected data class AccountData( | ||
val token: String, | ||
val account: GithubAccount, | ||
val username: String, | ||
val executor: GithubApiRequestExecutor | ||
) | ||
|
||
|
||
@Throws(Exception::class) | ||
override fun tearDown() { | ||
RunAll( | ||
ThrowableRunnable { setCurrentAccount(null) }, | ||
ThrowableRunnable { if (::accountManager.isInitialized) runBlocking { accountManager.updateAccounts(emptyMap()) } }, | ||
ThrowableRunnable { super.tearDown() } | ||
).run() | ||
} | ||
|
||
// | ||
// private fun deleteRepos(account: AccountData, repos: Collection<String>) { | ||
// setCurrentAccount(account) | ||
// for (repo in repos) { | ||
// retry(LOG, true) { | ||
// account.executor.execute(GithubApiRequests.Repos.delete(repo)) | ||
// val info = account.executor.execute(GithubApiRequests.Repos.get(repo)) | ||
// check(info == null) { "Repository still exists" } | ||
// } | ||
// } | ||
// } | ||
|
||
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 | ||
fun executeSomeCoroutineTasksAndDispatchAllInvocationEvents(project: Project) { | ||
repeat(3) { | ||
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue() | ||
runWithModalProgressBlocking(project, "") { | ||
yield() | ||
} | ||
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue() | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/kotlin/com/dsoftware/ghmanager/ToolWindowFactoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.dsoftware.ghmanager | ||
|
||
import com.intellij.ui.components.JBPanelWithEmptyText | ||
import junit.framework.TestCase | ||
|
||
|
||
class ToolWindowFactoryTest : GitHubActionsManagerBaseTest() { | ||
fun testNoGitHubAccountPanel() { | ||
factory.init(toolWindow) | ||
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(project) | ||
|
||
TestCase.assertEquals(1, toolWindow.contentManager.contentCount) | ||
val panel = toolWindow.contentManager.contents[0].component | ||
TestCase.assertTrue(panel is JBPanelWithEmptyText) | ||
} | ||
} |