Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ dependencies {

testImplementation(libs.junit)
testImplementation(libs.junit.jupiter.api)
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

// Set the JVM language level used to build the project.
Expand Down Expand Up @@ -206,6 +207,7 @@ changelog {
}

tasks.withType<Test> {
useJUnitPlatform()
environment("GRADLE_RELEASE_REPOSITORY","https://services.gradle.org/distributions")
systemProperty("idea.log.leaked.projects.in.tests", "false")
systemProperty( "idea.maven.test.mirror", "https://repo1.maven.org/maven2")
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/redhat/devtools/lsp4ij/LSPIJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,7 @@ private static Language doGetFileLanguage(@NotNull VirtualFile file, @NotNull Pr
}

public static @NotNull URI toUri(@NotNull File file) {
// URI scheme specified by language server protocol and LSP
try {
return new URI("file", "", file.getAbsoluteFile().toURI().getPath(), null); //$NON-NLS-1$ //$NON-NLS-2$
} catch (URISyntaxException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
return file.getAbsoluteFile().toURI();
}
return file.toPath().toUri();
}

public static @Nullable URI toUri(@NotNull PsiFile psiFile) {
Expand Down Expand Up @@ -1059,6 +1053,10 @@ private static void applyRenameFile(RenameFile renameFile) {
return VfsUtil.findFileByIoFile(newFile, true);
}

public static @Nullable VirtualFile findResourceFor(@NotNull File file) {
return findResourceFor(toUri(file).toString());
}

public static @Nullable VirtualFile findResourceFor(@NotNull URI uri) {
return LocalFileSystem.getInstance().findFileByIoFile(Paths.get(uri).toFile());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected FormattingTask createFormattingTask(@NotNull AsyncFormattingRequest fo
if (ioFile == null) {
return null;
}
VirtualFile file = LSPIJUtils.findResourceFor(ioFile.toURI());
VirtualFile file = LSPIJUtils.findResourceFor(ioFile);
if (file == null) {
return null;
}
Expand Down
101 changes: 101 additions & 0 deletions src/test/java/com/redhat/devtools/lsp4ij/LSPIJUtils_toUriTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
* SAP SE - additional test cases
******************************************************************************/
package com.redhat.devtools.lsp4ij;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;

import java.io.File;
import java.net.URI;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Tests for {@link LSPIJUtils#toUri(File)} method.
*/
public class LSPIJUtils_toUriTest {

@Test
@EnabledOnOs(OS.WINDOWS)
void testToUriWithWslPath() {
// Given: Create a File object from a UNC path string
// Note: File object may normalize the path, so we test with the actual behavior
String wslPath = "\\\\wsl$\\Ubuntu\\home\\user\\project\\file.txt";
File wslFile = new File(wslPath);

// When
URI resultUri = LSPIJUtils.toUri(wslFile);

// Then
assertNotNull(resultUri);
assertEquals("file", resultUri.getScheme());

// The URI should either:
// 1. Have authority="wsl$" and path="/Ubuntu/home/user/project/file.txt" (correct UNC handling)
// 2. Or at minimum, not produce the old broken format with 4 slashes
String uriString = resultUri.toString();
assertNotNull(uriString);

// Verify it doesn't have the broken 4-slash format
if (uriString.contains("file:////")) {
fail("URI should not contain file://// (broken UNC format): " + uriString);
}
}

@Test
void testToUriWithLocalPath() {
// Given
File localFile = new File("/home/user/project/file.txt");
if (System.getProperty("os.name").toLowerCase().contains("win")) {
localFile = new File("C:\\Users\\user\\project\\file.txt");
}


// When
URI resultUri = LSPIJUtils.toUri(localFile);

// Then
assertNotNull(resultUri);
assertEquals("file", resultUri.getScheme());
if (System.getProperty("os.name").toLowerCase().contains("win")) {
assertEquals("/C:/Users/user/project/file.txt", resultUri.getPath());
} else {
assertEquals("/home/user/project/file.txt", resultUri.getPath());
}
}

@Test
void testToUriWithSpaces() {
// Given
File fileWithSpaces;
if (System.getProperty("os.name").toLowerCase().contains("win")) {
fileWithSpaces = new File("C:\\Users\\user name\\project name\\file.txt");
} else {
fileWithSpaces = new File("/home/user name/project name/file.txt");
}

// When
URI resultUri = LSPIJUtils.toUri(fileWithSpaces);

// Then
assertNotNull(resultUri);
assertEquals("file", resultUri.getScheme());
if (System.getProperty("os.name").toLowerCase().contains("win")) {
assertEquals("/C:/Users/user%20name/project%20name/file.txt", resultUri.getRawPath());
} else {
assertEquals("/home/user%20name/project%20name/file.txt", resultUri.getRawPath());
}
}
}
Loading