Skip to content

Commit

Permalink
chore:highlight test
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Apr 24, 2024
1 parent db9760e commit 362f842
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.lang.annotation.Annotator
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.openapi.components.service
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import org.jetbrains.yaml.psi.YAMLKeyValue

class HighlightAnnotator : Annotator {
class HighlightAnnotator : Annotator, DumbAware {
override fun annotate(element: PsiElement, holder: AnnotationHolder) {
if (!element.isValid || element !is YAMLKeyValue) {
return
Expand Down
150 changes: 76 additions & 74 deletions src/test/kotlin/com/dsoftware/ghmanager/TestTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,79 +23,81 @@ fun Job.withStep(
return this.copy(steps = this.steps.plus(jobStep))
}

fun createJob(
id: Long = 21454796844, runId: Long = 7863783013, owner: String = "owner", repo: String = "repo",
name: String = "Job created for test",
): Job {
return Job(
id = id,
runId = runId,
workflowName = "Push on master",
headBranch = "master",
runUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$runId",
runAttempt = 1,
nodeId = "CR_kwDOHLrRQs8AAAAE_s44LA",
headSha = "a0576c489ba7cad8cad4ba7e14a7fe30ef9959a1",
url = "https://api.github.com/repos/$owner/$repo/actions/jobs/$id",
htmlUrl = "https://github.com/$owner/$repo/actions/runs/$runId/job/$id",
status = "completed",
conclusion = "success",
createdAt = Clock.System.now(),
startedAt = Clock.System.now(),
completedAt = Clock.System.now(),
name = name,
steps = mutableListOf(),
checkRunUrl = "https://api.github.com/repos/$owner/$repo/check-runs/21454796844",
labels = arrayOf("ubuntu-latest"),
runnerId = 9,
runnerName = "GitHub Actions 9",
runnerGroupId = 2,
runnerGroupName = "GitHub Actions",
)
}
object TestTools {
fun createJob(
id: Long = 21454796844, runId: Long = 7863783013, owner: String = "owner", repo: String = "repo",
name: String = "Job created for test",
): Job {
return Job(
id = id,
runId = runId,
workflowName = "Push on master",
headBranch = "master",
runUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$runId",
runAttempt = 1,
nodeId = "CR_kwDOHLrRQs8AAAAE_s44LA",
headSha = "a0576c489ba7cad8cad4ba7e14a7fe30ef9959a1",
url = "https://api.github.com/repos/$owner/$repo/actions/jobs/$id",
htmlUrl = "https://github.com/$owner/$repo/actions/runs/$runId/job/$id",
status = "completed",
conclusion = "success",
createdAt = Clock.System.now(),
startedAt = Clock.System.now(),
completedAt = Clock.System.now(),
name = name,
steps = mutableListOf(),
checkRunUrl = "https://api.github.com/repos/$owner/$repo/check-runs/21454796844",
labels = arrayOf("ubuntu-latest"),
runnerId = 9,
runnerName = "GitHub Actions 9",
runnerGroupId = 2,
runnerGroupName = "GitHub Actions",
)
}

fun createWorkflowRun(
id: Long = 21454796844,
owner: String = "owner",
repo: String = "repo",
status: String = "completed",
conclusion: String = "success",
branch: String = "master",
workflowId: Long = 7863783013,
): WorkflowRun {
return WorkflowRun(
id = id,
path = "cunla/fakeredis-py",
nodeId = "CR_kwDOHLrRQs8AAAAE_s44LA",
headBranch = branch,
headSha = "a0576c489ba7cad8cad4ba7e14a7fe30ef9959a1",
runNumber = 1,
event = "push",
status = status,
conclusion = conclusion,
url = "https://api.github.com/repos/$owner/$repo/actions/runs/$id",
htmlUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id",
createdAt = Clock.System.now(),
updatedAt = Clock.System.now(),
jobsUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/jobs",
logsUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/logs",
checkSuiteUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/check-suites",
artifactsUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/artifacts",
cancelUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/cancel",
rerunUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/rerun",
workflowId = workflowId,
workflowUrl = "https://api.github.com/repos/$owner/$repo/actions/workflows/$workflowId",
name = "Push on master",
headCommit = GitHubHeadCommit(
id = "a0576c489ba7cad8cad4ba7e14a7fe30ef9959a1",
message = "Commit message",
author = GitHubAuthor(name = owner, email = "$owner@a.com")
),
repository = GitHubRepository(
id = 1,
pullsUrl = "https://api.github.com/repos/$owner/$repo/pulls",
htmlUrl = ""
),
pullRequests = emptyList()
)
fun createWorkflowRun(
id: Long = 21454796844,
owner: String = "owner",
repo: String = "repo",
status: String = "completed",
conclusion: String = "success",
branch: String = "master",
workflowId: Long = 7863783013,
): WorkflowRun {
return WorkflowRun(
id = id,
path = "cunla/fakeredis-py",
nodeId = "CR_kwDOHLrRQs8AAAAE_s44LA",
headBranch = branch,
headSha = "a0576c489ba7cad8cad4ba7e14a7fe30ef9959a1",
runNumber = 1,
event = "push",
status = status,
conclusion = conclusion,
url = "https://api.github.com/repos/$owner/$repo/actions/runs/$id",
htmlUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id",
createdAt = Clock.System.now(),
updatedAt = Clock.System.now(),
jobsUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/jobs",
logsUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/logs",
checkSuiteUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/check-suites",
artifactsUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/artifacts",
cancelUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/cancel",
rerunUrl = "https://api.github.com/repos/$owner/$repo/actions/runs/$id/rerun",
workflowId = workflowId,
workflowUrl = "https://api.github.com/repos/$owner/$repo/actions/workflows/$workflowId",
name = "Push on master",
headCommit = GitHubHeadCommit(
id = "a0576c489ba7cad8cad4ba7e14a7fe30ef9959a1",
message = "Commit message",
author = GitHubAuthor(name = owner, email = "$owner@a.com")
),
repository = GitHubRepository(
id = 1,
pullsUrl = "https://api.github.com/repos/$owner/$repo/pulls",
htmlUrl = ""
),
pullRequests = emptyList()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.dsoftware.ghmanager.psi

import com.intellij.openapi.components.service
import com.intellij.psi.PsiManager
import com.intellij.testFramework.EditorTestUtil
import com.intellij.testFramework.common.initTestApplication
import com.intellij.testFramework.junit5.RunInEdt
import com.intellij.testFramework.rules.ProjectModelExtension
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension

@RunInEdt(writeIntent = true)
class HighlightAnnotatorTest {
init {
initTestApplication()
}

@JvmField
@RegisterExtension
protected val projectRule: ProjectModelExtension = ProjectModelExtension()

@BeforeEach
fun setUp() {

}

@Test
fun testAnnotate() {
val workflowContent = HighlightAnnotatorTest::class.java.getResource("/testproject/workflow.yaml")!!.readText()
val workflowFile =
projectRule.baseProjectDir.newVirtualFile(".github/workflows/workflow.yaml", workflowContent.toByteArray())
val psiManager = projectRule.project.service<PsiManager>()
val answer = """"""
val psiFile = psiManager.findFile(workflowFile)
EditorTestUtil.testFileSyntaxHighlighting(psiFile!!, true, answer)
}
}

//
//class FakePsiFile(project: Project, private val filename: String) :
// MockPsiFile(LightVirtualFile(filename), MockPsiManager(project)) {
// override fun getName(): String = filename
//
// override fun getContainingFile(): PsiFile = this
//}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ abstract class GhActionsMgrBaseTest {
}

private val host: GithubServerPath = GithubServerPath.from("github.com")
private lateinit var testInfo: TestInfo

@JvmField
@RegisterExtension
Expand All @@ -53,8 +52,7 @@ abstract class GhActionsMgrBaseTest {
protected lateinit var toolWindowContent: GhActionsMgrToolWindowContent

@BeforeEach
open fun setUp(testInfo: TestInfo) {
this.testInfo = testInfo
open fun setUp() {
val toolWindowManager = ToolWindowHeadlessManagerImpl(projectRule.project)
toolWindow = toolWindowManager.doRegisterToolWindow("GitHub Actions")
Disposer.register(projectRule.project, toolWindow.disposable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import com.dsoftware.ghmanager.api.model.WorkflowRunJobs
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.createJob
import com.dsoftware.ghmanager.createWorkflowRun
import com.dsoftware.ghmanager.TestTools
import com.dsoftware.ghmanager.data.WorkflowDataContextService
import com.dsoftware.ghmanager.i18n.MessagesBundle.message
import com.dsoftware.ghmanager.ui.GhActionsMgrToolWindowContent
Expand Down Expand Up @@ -38,7 +37,6 @@ import org.jetbrains.plugins.github.util.GHCompatibilityUtil
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInfo
import org.junit.jupiter.api.extension.ExtendWith
import javax.swing.JPanel
import javax.swing.JTextPane
Expand All @@ -55,8 +53,8 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
}

@BeforeEach
override fun setUp(testInfo: TestInfo) {
super.setUp(testInfo)
override fun setUp() {
super.setUp()
mockGhActionsService(setOf("http://github.com/owner/repo"), setOf("account1"))
toolWindowContent = GhActionsMgrToolWindowContent(toolWindow)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)
Expand All @@ -65,9 +63,9 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
@Test
fun `test repo with different workflow-runs`() {
val workflowRunsList = listOf(
createWorkflowRun(id = 1, status = "in_progress"),
createWorkflowRun(id = 2, status = "completed"),
createWorkflowRun(id = 2, status = "queued"),
TestTools.createWorkflowRun(id = 1, status = "in_progress"),
TestTools.createWorkflowRun(id = 2, status = "completed"),
TestTools.createWorkflowRun(id = 2, status = "queued"),
)
mockGithubApiRequestExecutor(workflowRunsList)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)
Expand All @@ -88,8 +86,7 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
Assertions.assertEquals(workflowRunsList.size, workflowRunsListPanel.runListComponent.model.size)
// assert workflow run list selection
workflowRunsListPanel.runListComponent.setSelectedValue(
workflowRunsListPanel.runListComponent.model.getElementAt(0),
false
workflowRunsListPanel.runListComponent.model.getElementAt(0), false
)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)
Assertions.assertEquals(
Expand All @@ -101,12 +98,12 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
@Test
fun `test selecting workflow-run shows jobs`() {
val workflowRunsList = listOf(
createWorkflowRun(id = 1, status = "in_progress"),
createWorkflowRun(id = 2, status = "completed"),
createWorkflowRun(id = 2, status = "queued"),
TestTools.createWorkflowRun(id = 1, status = "in_progress"),
TestTools.createWorkflowRun(id = 2, status = "completed"),
TestTools.createWorkflowRun(id = 2, status = "queued"),
)
val jobsList = listOf(
createJob(id = 1, runId = 2, name = "job1"),
TestTools.createJob(id = 1, runId = 2, name = "job1"),
)
mockGithubApiRequestExecutor(workflowRunsList = workflowRunsList, jobs = jobsList)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)
Expand All @@ -124,17 +121,15 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {

// assert workflow run selected => jobs listed
workflowRunsListPanel.runListComponent.setSelectedValue(
workflowRunsListPanel.runListComponent.model.getElementAt(0),
false
workflowRunsListPanel.runListComponent.model.getElementAt(0), false
)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)
Assertions.assertEquals(
workflowRunsListPanel.runListComponent.selectedValue,
workflowRunsListPanel.runListComponent.model.getElementAt(0)
)
Assertions.assertNotEquals(
jobsPanelEmptyText,
(selectedRunPanel.firstComponent.components[0] as JPanel).components[0]
jobsPanelEmptyText, (selectedRunPanel.firstComponent.components[0] as JPanel).components[0]
)
val jobsPanel = (selectedRunPanel.firstComponent.components[0] as JobsListPanel)
Assertions.assertEquals(2, jobsPanel.componentCount)
Expand All @@ -154,12 +149,12 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
@Test
fun `test selecting job shows log for job`() {
val workflowRunsList = listOf(
createWorkflowRun(id = 1, status = "in_progress"),
createWorkflowRun(id = 2, status = "completed"),
createWorkflowRun(id = 2, status = "queued"),
TestTools.createWorkflowRun(id = 1, status = "in_progress"),
TestTools.createWorkflowRun(id = 2, status = "completed"),
TestTools.createWorkflowRun(id = 2, status = "queued"),
)
val jobsList = listOf(
createJob(id = 1, runId = 2, name = "job1"),
TestTools.createJob(id = 1, runId = 2, name = "job1"),
)
val log = "log for job1"
mockGithubApiRequestExecutor(workflowRunsList = workflowRunsList, jobs = jobsList, log = log)
Expand All @@ -178,8 +173,7 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {

// act workflow run selected => jobs listed
workflowRunsListPanel.runListComponent.setSelectedValue(
workflowRunsListPanel.runListComponent.model.getElementAt(0),
false
workflowRunsListPanel.runListComponent.model.getElementAt(0), false
)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)

Expand All @@ -189,8 +183,7 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
workflowRunsListPanel.runListComponent.model.getElementAt(0)
)
Assertions.assertNotEquals(
jobsPanelEmptyText,
(selectedRunPanel.firstComponent.components[0] as JPanel).components[0]
jobsPanelEmptyText, (selectedRunPanel.firstComponent.components[0] as JPanel).components[0]
)
val jobsPanel = (selectedRunPanel.firstComponent.components[0] as JobsListPanel)
Assertions.assertEquals(2, jobsPanel.componentCount)
Expand All @@ -211,8 +204,7 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)

Assertions.assertNotEquals(
logPanelEmptyText,
(selectedRunPanel.secondComponent.components[0] as JPanel).components[0]
logPanelEmptyText, (selectedRunPanel.secondComponent.components[0] as JPanel).components[0]
)
val logPanel =
((selectedRunPanel.secondComponent.components[0] as JBPanelWithEmptyText).components[0] as LogConsolePanel)
Expand Down Expand Up @@ -361,9 +353,8 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
return Pair(workflowRunsListPanel, splitterComponent.secondComponent as OnePixelSplitter)
}

private fun <T> MockKMatcherScope.matchApiRequestUrl(url: String) =
match<GithubApiRequest<T>> {
it.url.split('?')[0].endsWith(url)
}
private fun <T> MockKMatcherScope.matchApiRequestUrl(url: String) = match<GithubApiRequest<T>> {
it.url.split('?')[0].endsWith(url)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import javax.swing.JPanel
class ToolWindowFactoryTest : GhActionsMgrBaseTest() {

@BeforeEach
override fun setUp(testInfo: TestInfo) {
super.setUp(testInfo)
override fun setUp() {
super.setUp()
mockkObject(GhApiRequestExecutor)
every { GhApiRequestExecutor.create(token = any()) } throws Exception("No executor")
}
Expand Down
Loading

0 comments on commit 362f842

Please sign in to comment.