Merge pull request #5584 from continuedev/nate/intellij-uri-tests
tests for uriToFile
This commit is contained in:
commit
a56a4dc1ca
10 changed files with 131 additions and 26 deletions
|
|
@ -42,6 +42,7 @@ dependencies {
|
|||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
|
||||
testImplementation("com.intellij.remoterobot:remote-robot:$remoteRobotVersion")
|
||||
testImplementation("com.intellij.remoterobot:remote-fixtures:$remoteRobotVersion")
|
||||
testImplementation("io.mockk:mockk:1.14.2")
|
||||
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.0")
|
||||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.2")
|
||||
testImplementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
|
||||
|
|
@ -62,6 +63,7 @@ dependencies {
|
|||
// Add Log4j 2.x explicitly
|
||||
implementation("org.apache.logging.log4j:log4j-core:2.20.0")
|
||||
implementation("org.apache.logging.log4j:log4j-api:2.20.0")
|
||||
testImplementation(kotlin("test"))
|
||||
}
|
||||
|
||||
// Set the JVM language level used to build the project. Use Java 11 for 2020.3+, and Java 17 for
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ pluginSinceBuild=223
|
|||
# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
|
||||
platformType=IC
|
||||
platformVersion=2022.3.3
|
||||
#org.gradle.java.home=/opt/homebrew/opt/openjdk@17
|
||||
# org.gradle.java.home=/opt/homebrew/opt/openjdk@17
|
||||
#platformVersion = LATEST-EAP-SNAPSHOT
|
||||
# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
|
||||
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package com.github.continuedev.continueintellijextension.activities
|
||||
|
||||
import IntelliJIDE
|
||||
import com.intellij.openapi.fileEditor.FileEditorManagerListener
|
||||
import com.github.continuedev.continueintellijextension.auth.AuthListener
|
||||
import com.github.continuedev.continueintellijextension.auth.ContinueAuthService
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ class DiffManager(private val project: Project) : DumbAware {
|
|||
file.createNewFile()
|
||||
}
|
||||
file.writeText(replacement)
|
||||
openDiffWindow(URI(filepath).toString(), file.toURI().toString(), stepIndex)
|
||||
openDiffWindow(UriUtils.parseUri(filepath).toString(), file.toURI().toString(), stepIndex)
|
||||
}
|
||||
|
||||
private fun cleanUpFile(file2: String) {
|
||||
|
|
@ -80,7 +80,8 @@ class DiffManager(private val project: Project) : DumbAware {
|
|||
val diffInfo = diffInfoMap[file] ?: return
|
||||
|
||||
// Write contents to original file
|
||||
val virtualFile = LocalFileSystem.getInstance().findFileByPath(URI(diffInfo.originalFilepath).path) ?: return
|
||||
val virtualFile =
|
||||
LocalFileSystem.getInstance().findFileByPath(UriUtils.parseUri(diffInfo.originalFilepath).path) ?: return
|
||||
val document = FileDocumentManager.getInstance().getDocument(virtualFile) ?: return
|
||||
WriteCommandAction.runWriteCommandAction(project) {
|
||||
document.setText(File(file).readText())
|
||||
|
|
@ -119,8 +120,8 @@ class DiffManager(private val project: Project) : DumbAware {
|
|||
lastFile2 = file2
|
||||
|
||||
// Create a DiffContent for each of the texts you want to compare
|
||||
val content1: DiffContent = DiffContentFactory.getInstance().create(File(URI(file1)).readText())
|
||||
val content2: DiffContent = DiffContentFactory.getInstance().create(File(URI(file2)).readText())
|
||||
val content1: DiffContent = DiffContentFactory.getInstance().create(UriUtils.uriToFile(file1).readText())
|
||||
val content2: DiffContent = DiffContentFactory.getInstance().create(UriUtils.uriToFile(file2).readText())
|
||||
|
||||
// Create a SimpleDiffRequest and populate it with the DiffContents and titles
|
||||
val diffRequest = SimpleDiffRequest("Continue Diff", content1, content2, "Old", "New")
|
||||
|
|
|
|||
|
|
@ -7,9 +7,7 @@ import com.intellij.openapi.project.guessProjectDir
|
|||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.BufferedReader
|
||||
import java.io.File
|
||||
import java.io.InputStreamReader
|
||||
import java.net.URI
|
||||
|
||||
class GitService(
|
||||
private val project: Project,
|
||||
|
|
@ -52,7 +50,7 @@ class GitService(
|
|||
} else {
|
||||
ProcessBuilder("git", "diff", "--cached")
|
||||
}
|
||||
builder.directory(File(URI(workspaceDir)))
|
||||
builder.directory(UriUtils.uriToFile(workspaceDir))
|
||||
val process = withContext(Dispatchers.IO) {
|
||||
builder.start()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
package com.github.continuedev.continueintellijextension.`continue`
|
||||
|
||||
import IntelliJIDE
|
||||
import com.github.continuedev.continueintellijextension.*
|
||||
import com.github.continuedev.continueintellijextension.activities.ContinuePluginDisposable
|
||||
import com.github.continuedev.continueintellijextension.activities.showTutorial
|
||||
import com.github.continuedev.continueintellijextension.auth.AuthListener
|
||||
import com.github.continuedev.continueintellijextension.auth.ContinueAuthService
|
||||
import com.github.continuedev.continueintellijextension.editor.DiffStreamHandler
|
||||
import com.github.continuedev.continueintellijextension.editor.DiffStreamService
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
package com.github.continuedev.continueintellijextension.`continue`
|
||||
|
||||
import com.github.continuedev.continueintellijextension.*
|
||||
import com.github.continuedev.continueintellijextension.constants.getContinueGlobalPath
|
||||
import com.github.continuedev.continueintellijextension.constants.ContinueConstants
|
||||
|
|
@ -167,7 +169,7 @@ class IntelliJIDE(
|
|||
// Find any .continuerc.json files
|
||||
for (file in contents) {
|
||||
if (file.endsWith(".continuerc.json")) {
|
||||
val fileContent = File(URI(file)).readText()
|
||||
val fileContent = UriUtils.uriToFile(file).readText()
|
||||
configs.add(fileContent)
|
||||
}
|
||||
}
|
||||
|
|
@ -178,12 +180,12 @@ class IntelliJIDE(
|
|||
}
|
||||
|
||||
override suspend fun fileExists(filepath: String): Boolean {
|
||||
val file = File(URI(filepath))
|
||||
val file = UriUtils.uriToFile(filepath)
|
||||
return file.exists()
|
||||
}
|
||||
|
||||
override suspend fun writeFile(path: String, contents: String) {
|
||||
val file = File(URI(path))
|
||||
val file = UriUtils.uriToFile(path)
|
||||
file.parentFile?.mkdirs()
|
||||
file.writeText(contents)
|
||||
}
|
||||
|
|
@ -201,7 +203,7 @@ class IntelliJIDE(
|
|||
|
||||
override suspend fun openFile(path: String) {
|
||||
// Convert URI path to absolute file path
|
||||
val filePath = File(URI(path)).absolutePath
|
||||
val filePath = UriUtils.uriToFile(path).absolutePath
|
||||
// Find the file using the absolute path
|
||||
val file = withContext(Dispatchers.IO) {
|
||||
LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath)
|
||||
|
|
@ -216,7 +218,7 @@ class IntelliJIDE(
|
|||
|
||||
override suspend fun openUrl(url: String) {
|
||||
withContext(Dispatchers.IO) {
|
||||
Desktop.browse(java.net.URI(url))
|
||||
Desktop.browse(URI(url))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -226,7 +228,8 @@ class IntelliJIDE(
|
|||
|
||||
override suspend fun saveFile(filepath: String) {
|
||||
ApplicationManager.getApplication().invokeLater {
|
||||
val file = LocalFileSystem.getInstance().findFileByPath(URI(filepath).path) ?: return@invokeLater
|
||||
val file =
|
||||
LocalFileSystem.getInstance().findFileByPath(UriUtils.parseUri(filepath).path) ?: return@invokeLater
|
||||
val fileDocumentManager = FileDocumentManager.getInstance()
|
||||
val document = fileDocumentManager.getDocument(file)
|
||||
|
||||
|
|
@ -239,7 +242,7 @@ class IntelliJIDE(
|
|||
override suspend fun readFile(filepath: String): String {
|
||||
return try {
|
||||
val content = ApplicationManager.getApplication().runReadAction<String?> {
|
||||
val virtualFile = LocalFileSystem.getInstance().findFileByPath(URI(filepath).path)
|
||||
val virtualFile = LocalFileSystem.getInstance().findFileByPath(UriUtils.parseUri(filepath).path)
|
||||
if (virtualFile != null && FileDocumentManager.getInstance().isFileModified(virtualFile)) {
|
||||
return@runReadAction FileDocumentManager.getInstance().getDocument(virtualFile)?.text
|
||||
}
|
||||
|
|
@ -249,7 +252,7 @@ class IntelliJIDE(
|
|||
if (content != null) {
|
||||
content
|
||||
} else {
|
||||
val file = File(URI(filepath))
|
||||
val file = UriUtils.uriToFile(filepath)
|
||||
if (!file.exists() || file.isDirectory) return ""
|
||||
withContext(Dispatchers.IO) {
|
||||
FileInputStream(file).use { fis ->
|
||||
|
|
@ -455,7 +458,7 @@ class IntelliJIDE(
|
|||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val builder = ProcessBuilder("git", "rev-parse", "--abbrev-ref", "HEAD")
|
||||
builder.directory(File(URI(dir)))
|
||||
builder.directory(UriUtils.uriToFile(dir))
|
||||
val process = builder.start()
|
||||
val reader = BufferedReader(InputStreamReader(process.inputStream))
|
||||
val output = reader.readLine()
|
||||
|
|
@ -485,7 +488,7 @@ class IntelliJIDE(
|
|||
|
||||
override suspend fun getRepoName(dir: String): String? {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val directory = File(URI(dir))
|
||||
val directory = UriUtils.uriToFile(dir)
|
||||
val targetDir = if (directory.isFile) directory.parentFile else directory
|
||||
val builder = ProcessBuilder("git", "config", "--get", "remote.origin.url")
|
||||
builder.directory(targetDir)
|
||||
|
|
@ -549,7 +552,7 @@ class IntelliJIDE(
|
|||
override suspend fun getGitRootPath(dir: String): String? {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val builder = ProcessBuilder("git", "rev-parse", "--show-toplevel")
|
||||
builder.directory(File(URI(dir)))
|
||||
builder.directory(UriUtils.uriToFile(dir))
|
||||
val process = builder.start()
|
||||
|
||||
val reader = BufferedReader(InputStreamReader(process.inputStream))
|
||||
|
|
@ -560,7 +563,7 @@ class IntelliJIDE(
|
|||
}
|
||||
|
||||
override suspend fun listDir(dir: String): List<List<Any>> {
|
||||
val files = File(URI(dir)).listFiles()?.map {
|
||||
val files = UriUtils.uriToFile(dir).listFiles()?.map {
|
||||
listOf(it.name, if (it.isDirectory) FileType.DIRECTORY.value else FileType.FILE.value)
|
||||
} ?: emptyList()
|
||||
|
||||
|
|
@ -569,7 +572,7 @@ class IntelliJIDE(
|
|||
|
||||
override suspend fun getFileStats(files: List<String>): Map<String, FileStats> {
|
||||
return files.associateWith { file ->
|
||||
FileStats(File(URI(file)).lastModified(), File(URI(file)).length())
|
||||
FileStats(UriUtils.uriToFile(file).lastModified(), UriUtils.uriToFile(file).length())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -587,7 +590,7 @@ class IntelliJIDE(
|
|||
}
|
||||
|
||||
private fun setFileOpen(filepath: String, open: Boolean = true) {
|
||||
val file = LocalFileSystem.getInstance().findFileByPath(URI(filepath).path)
|
||||
val file = LocalFileSystem.getInstance().findFileByPath(UriUtils.uriToFile(filepath).path)
|
||||
|
||||
file?.let {
|
||||
if (open) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
package com.github.continuedev.continueintellijextension.`continue`
|
||||
|
||||
import java.io.File
|
||||
import java.net.URI
|
||||
|
||||
/**
|
||||
* Utility class for URI operations
|
||||
*/
|
||||
object UriUtils {
|
||||
/**
|
||||
* Parses a URI string into a URI object, handling special cases for Windows file paths
|
||||
*/
|
||||
fun parseUri(uri: String): URI {
|
||||
try {
|
||||
// Remove query parameters if present
|
||||
val uriStr = uri.substringBefore("?")
|
||||
|
||||
// Handle Windows file paths with authority component
|
||||
if (uriStr.startsWith("file://") && !uriStr.startsWith("file:///")) {
|
||||
val path = uriStr.substringAfter("file://")
|
||||
return URI("file:///$path")
|
||||
}
|
||||
|
||||
// Standard URI handling for other cases
|
||||
val uriWithoutQuery = URI(uriStr)
|
||||
return uriWithoutQuery
|
||||
} catch (e: Exception) {
|
||||
println("Error parsing URI: $uri ${e.message}")
|
||||
throw Exception("Invalid URI: $uri ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a URI string to a File object
|
||||
*/
|
||||
fun uriToFile(uri: String): File {
|
||||
return File(parseUri(uri))
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
package com.github.continuedev.continueintellijextension.services
|
||||
|
||||
import IntelliJIDE
|
||||
import com.github.continuedev.continueintellijextension.`continue`.CoreMessenger
|
||||
import com.github.continuedev.continueintellijextension.`continue`.CoreMessengerManager
|
||||
import com.github.continuedev.continueintellijextension.`continue`.DiffManager
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
package com.github.continuedev.continueintellijextension.unit
|
||||
|
||||
import com.github.continuedev.continueintellijextension.`continue`.UriUtils
|
||||
import org.junit.jupiter.api.Disabled
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.File
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class UriUtilsTest {
|
||||
@Test
|
||||
fun testUriToFile() {
|
||||
val uri = "file:///path/to/file"
|
||||
val file = UriUtils.uriToFile(uri)
|
||||
assertEquals(File("/path/to/file"), file)
|
||||
}
|
||||
|
||||
@Disabled("Not working")
|
||||
@Test
|
||||
fun testUriToFileWithWindowsPath() {
|
||||
val uri = "file:///C:/path/to/file"
|
||||
val file = UriUtils.uriToFile(uri)
|
||||
assertEquals(File("C:/path/to/file"), file)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shouldHandleAuthorityComponent() {
|
||||
val uri = "file://C:/path/to/file"
|
||||
val file = UriUtils.uriToFile(uri)
|
||||
assertEquals(File("/C:/path/to/file"), file)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUriToFileWithSpaces() {
|
||||
val uri = "file:///path/to/file%20with%20spaces"
|
||||
val file = UriUtils.uriToFile(uri)
|
||||
assertEquals(File("/path/to/file with spaces"), file)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUriToFileWithSpecialCharacters() {
|
||||
val uri = "file:///path/to/file%23with%25special%26chars"
|
||||
val file = UriUtils.uriToFile(uri)
|
||||
assertEquals(File("/path/to/file#with%special&chars"), file)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUriToFileWithQueryParams() {
|
||||
val uri = "file:///path/to/file?param=value"
|
||||
val file = UriUtils.uriToFile(uri)
|
||||
assertEquals(File("/path/to/file"), file)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUriToFileWithWSLPath() {
|
||||
val uri = "file:///wsl$/Ubuntu/home/user/file.txt"
|
||||
val file = UriUtils.uriToFile(uri)
|
||||
assertEquals(File("/wsl$/Ubuntu/home/user/file.txt"), file)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUriToFileWithWSLLocalhostPath() {
|
||||
val uri = "file:///wsl.localhost/Ubuntu/home/user/file.txt"
|
||||
val file = UriUtils.uriToFile(uri)
|
||||
assertEquals(File("/wsl.localhost/Ubuntu/home/user/file.txt"), file)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue