[Java. Terminal] add tests for classpath generator separator

IDEA-359243

GitOrigin-RevId: 7546b68ad0d5a8d468e3516b5036b77f0c5be9ed
This commit is contained in:
Georgii Ustinov
2024-10-03 15:41:59 +03:00
committed by intellij-monorepo-bot
parent 4ad4c5e655
commit 30d39bef05
2 changed files with 39 additions and 5 deletions

View File

@@ -8,13 +8,13 @@ import com.intellij.terminal.completion.spec.ShellRuntimeDataGenerator
import org.jetbrains.plugins.terminal.block.completion.spec.ShellDataGenerators
import org.jetbrains.plugins.terminal.block.completion.spec.ShellRuntimeDataGenerator
internal object JavaShellCommandUtils {
internal fun getClassPathSeparator() = when {
object JavaShellCommandUtils {
fun getClassPathSeparator() = when {
SystemInfo.isWindows -> ";"
else -> ":"
}
internal fun classpathSuggestionsGenerator(): ShellRuntimeDataGenerator<List<ShellCompletionSuggestion>> {
fun classpathSuggestionsGenerator(): ShellRuntimeDataGenerator<List<ShellCompletionSuggestion>> {
val key = "classpath generator"
return ShellRuntimeDataGenerator(
debugName = key,

View File

@@ -3,6 +3,7 @@ package com.intellij.java.terminal.completion
import com.intellij.execution.vmOptions.*
import com.intellij.java.terminal.JavaShellCommandContext
import com.intellij.java.terminal.JavaShellCommandUtils
import com.intellij.openapi.application.ApplicationManager
import com.intellij.terminal.completion.spec.ShellCommandResult
import com.intellij.terminal.completion.spec.ShellCompletionSuggestion
@@ -55,19 +56,52 @@ class JavaShellCommandSpecsProviderTest : BasePlatformTestCase() {
UsefulTestCase.assertDoesntContain(fixture.getCompletionNames(), listOf("--add-experimental-exports", "--add-diagnostic-exports", "-XXadvanced"))
}
@Test
fun `classpath suggestion generator simple`() = runBlocking {
val fixture = createFixture()
val completion = fixture.getCompletions("java -cp ")
assertSameElements(completion.map { it.name }, listOf("file1.jar", "file2.jar", "dir1/"))
assertTrue(completion.all { it.prefixReplacementIndex == 0})
}
@Test
fun `classpath suggestion generator after colon`() = runBlocking {
val separator = JavaShellCommandUtils.getClassPathSeparator()
val fixture = createFixture()
val argument = "file1.jar$separator"
val completion = fixture.getCompletions("java -cp $argument")
assertSameElements(completion.map { it.name }, listOf("file1.jar", "file2.jar", "dir1/"))
assertTrue(completion.all { it.prefixReplacementIndex == argument.length})
}
@Test
fun `classpath suggestion generator with double colon`() = runBlocking {
val separator = JavaShellCommandUtils.getClassPathSeparator()
val fixture = createFixture()
val argument = "file1.jar$separator$separator"
val completion = fixture.getCompletions("java -cp $argument")
assertSameElements(completion.map { it.name }, listOf("file1.jar", "file2.jar", "dir1/"))
assertTrue(completion.all { it.prefixReplacementIndex == argument.length})
}
private fun createFixture(javaVersion: Int = 11): ShellCompletionTestFixture {
ApplicationManager.getApplication().replaceService(VMOptionsService::class.java, MockVMOptionsService(), testRootDisposable)
val fixture = ShellCompletionTestFixture.builder(project).mockShellCommandResults { command ->
if (command == JavaShellCommandContext.JAVA_SHOW_SETTINGS_PROPERTIES_VERSION_COMMAND) {
return@mockShellCommandResults ShellCommandResult.create("java.home = /jre/home\njava.version = ${javaVersion}", exitCode = 0)
}
if (command.startsWith("__jetbrains_intellij_get_directory_files")) {
return@mockShellCommandResults ShellCommandResult.create("file1.jar\nfile2.jar\ndir1/", exitCode = 0)
}
return@mockShellCommandResults ShellCommandResult.create("", exitCode = 1)
}.build()
return fixture
}
private suspend fun ShellCompletionTestFixture.getCompletionNames(): List<String> {
val actual: List<ShellCompletionSuggestion> = getCompletions("java ")
private suspend fun ShellCompletionTestFixture.getCompletionNames(command: String = "java "): List<String> {
val actual: List<ShellCompletionSuggestion> = getCompletions(command)
return actual.map { it.name }
}