Skip to content

Commit 312c8f8

Browse files
FantoomForNeVeR
authored andcommitted
(#291) Tests: fix CodeInsight tests and add EdtInterceptor
1 parent 401f2e2 commit 312c8f8

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

src/main/kotlin/com/intellij/plugin/powershell/lang/lsp/LSPInitMain.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ class LSPInitMain : PersistentStateComponent<LSPInitMain.PowerShellInfo>, Dispos
129129
}
130130

131131
override fun dispose() {
132-
psEditorLanguageServer.clear()
133-
psConsoleLanguageServer.clear()
132+
fun ConcurrentHashMap<Project, LanguageServerEndpoint>.shutdownAndClear() {
133+
//forEach { it.value.shutdown() }
134+
clear()
135+
}
136+
137+
psEditorLanguageServer.shutdownAndClear()
138+
psConsoleLanguageServer.shutdownAndClear()
134139
}
135140
}

src/test/kotlin/com/intellij/plugin/powershell/lang/PSLanguageHostUtilsTests.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ import com.intellij.execution.configurations.PathEnvironmentVariableUtil
44
import com.intellij.openapi.util.SystemInfo
55
import com.intellij.plugin.powershell.isOnCiServer
66
import com.intellij.plugin.powershell.lang.lsp.languagehost.PSLanguageHostUtils
7+
import com.intellij.plugin.powershell.lang.lsp.languagehost.PSVersionInfo
78
import com.intellij.plugin.powershell.lang.lsp.languagehost.PowerShellEdition
8-
import com.intellij.testFramework.fixtures.BasePlatformTestCase
9+
import com.intellij.testFramework.junit5.TestApplication
910
import kotlinx.coroutines.future.await
1011
import kotlinx.coroutines.runBlocking
12+
import org.junit.jupiter.api.Assertions
13+
import org.junit.jupiter.api.Test
1114
import java.nio.file.Path
1215
import kotlin.io.path.pathString
1316
import kotlin.test.assertTrue
17+
import kotlin.test.fail
1418

15-
class PSLanguageHostUtilsTests : BasePlatformTestCase() {
19+
@TestApplication
20+
class PSLanguageHostUtilsTests {
1621

22+
@Test
1723
fun testPowerShell5VersionDetector() {
1824
if (!SystemInfo.isWindows)
1925
return
@@ -33,6 +39,7 @@ class PSLanguageHostUtilsTests : BasePlatformTestCase() {
3339
doTest(executable, "5\\..+".toRegex(), PowerShellEdition.Desktop)
3440
}
3541

42+
@Test
3643
fun testPowerShellCoreVersionDetector() {
3744
val executable = findExecutable("pwsh")
3845
if (executable == null) {
@@ -53,11 +60,11 @@ class PSLanguageHostUtilsTests : BasePlatformTestCase() {
5360
}
5461

5562
private fun doTest(executable: Path, expectedVersionRegex: Regex, expectedEdition: PowerShellEdition) {
56-
val version = runBlocking { PSLanguageHostUtils.getPowerShellVersion(executable.pathString).await() }
63+
val version: PSVersionInfo = runBlocking { PSLanguageHostUtils.getPowerShellVersion(executable.pathString).await() }
5764
assertTrue(
5865
expectedVersionRegex.matches(version.versionString),
5966
"Version string ${version.versionString} is expected to satisfy a regular expression $expectedVersionRegex."
6067
)
61-
assertEquals(expectedEdition, version.edition)
68+
Assertions.assertEquals(expectedEdition, version.edition)
6269
}
6370
}

src/test/kotlin/com/intellij/plugin/powershell/lang/PowerShellCodeInsightTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package com.intellij.plugin.powershell.lang
33
import com.intellij.openapi.command.WriteCommandAction
44
import com.intellij.plugin.powershell.psi.PowerShellStringLiteralExpression
55
import com.intellij.plugin.powershell.testFramework.PowerShellCodeInsightTestBase
6+
import com.intellij.plugin.powershell.testFramework.RunInEdt
67
import com.intellij.psi.util.PsiTreeUtil
78
import com.intellij.testFramework.junit5.TestApplication
89
import org.junit.jupiter.api.Test
910
import java.io.File
1011

1112
@TestApplication
13+
@RunInEdt
1214
class PowerShellCodeInsightTest : PowerShellCodeInsightTestBase() {
1315

1416
private val TEST_DATA_PATH = "src" + File.separatorChar + "test" + File.separatorChar + "resources" + File.separatorChar +
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.intellij.plugin.powershell.testFramework
2+
3+
import org.junit.jupiter.api.extension.ExtensionContext
4+
import org.junit.jupiter.api.extension.InvocationInterceptor
5+
import org.junit.jupiter.api.extension.ReflectiveInvocationContext
6+
import java.lang.reflect.Method
7+
8+
class EdtInterceptor: InvocationInterceptor {
9+
override fun interceptTestMethod(
10+
invocation: InvocationInterceptor.Invocation<Void?>,
11+
invocationContext: ReflectiveInvocationContext<Method?>,
12+
extensionContext: ExtensionContext
13+
) {
14+
if(invocationContext.executable?.getAnnotation(RunInEdt::class.java) != null || invocationContext.targetClass?.getAnnotation(RunInEdt::class.java) != null)
15+
runInEdt { invocation.proceed() }
16+
else invocation.proceed()
17+
}
18+
}
19+
20+
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
21+
annotation class RunInEdt

src/test/kotlin/com/intellij/plugin/powershell/testFramework/PowerShellCodeInsightTestBase.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.intellij.plugin.powershell.testFramework
22

33
import com.intellij.plugin.powershell.lang.lsp.LSPInitMain
4+
import com.intellij.testFramework.LightPlatformTestCase
45
import com.intellij.testFramework.fixtures.CodeInsightTestFixture
6+
import com.intellij.testFramework.fixtures.IdeaProjectTestFixture
57
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory
68
import com.intellij.testFramework.fixtures.impl.TempDirTestFixtureImpl
79
import com.intellij.testFramework.junit5.fixture.tempPathFixture
@@ -10,23 +12,25 @@ import kotlinx.coroutines.withTimeout
1012
import org.junit.jupiter.api.AfterEach
1113
import org.junit.jupiter.api.BeforeEach
1214
import org.junit.jupiter.api.TestInfo
15+
import org.junit.jupiter.api.extension.ExtendWith
1316
import java.nio.file.Path
1417
import kotlin.time.Duration.Companion.seconds
1518

19+
@ExtendWith(EdtInterceptor::class)
1620
open class PowerShellCodeInsightTestBase {
1721
val tempPathFixture = tempPathFixture()
1822
lateinit var tempPath: Path
1923
lateinit var codeInsightTestFixture: CodeInsightTestFixture
20-
24+
lateinit var ideaProjectTestFixture: IdeaProjectTestFixture
2125
val project get() = codeInsightTestFixture.project
2226

2327
@BeforeEach
2428
fun setupFixture(testInfo: TestInfo){
2529
tempPath = tempPathFixture.get()
2630
val factory = IdeaTestFixtureFactory.getFixtureFactory()
2731
val fixtureBuilder = factory.createLightFixtureBuilder(null, testInfo.displayName)
28-
val fixture = fixtureBuilder.getFixture()
29-
codeInsightTestFixture = IdeaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(fixture,
32+
ideaProjectTestFixture = fixtureBuilder.getFixture()
33+
codeInsightTestFixture = IdeaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(ideaProjectTestFixture,
3034
TempDirTestFixtureImpl())
3135
codeInsightTestFixture.testDataPath = getTestDataPath()
3236
codeInsightTestFixture.setUp()
@@ -37,6 +41,7 @@ open class PowerShellCodeInsightTestBase {
3741
runInEdt {
3842
LSPInitMain.getInstance().dispose() // TODO: Remove this after LSPInitMain is refactored
3943
codeInsightTestFixture.tearDown()
44+
LightPlatformTestCase.closeAndDeleteProject()
4045
tearDownInEdt()
4146
}
4247
}

0 commit comments

Comments
 (0)