Skip to content

Commit

Permalink
Fixed file path issue on Windows
Browse files Browse the repository at this point in the history
Fixes #redhat-developer/vscode-xml#125

Signed-off-by: Nikolas K <[email protected]>
  • Loading branch information
NikolasKomonen committed Apr 8, 2019
1 parent 9a71206 commit def18ec
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.eclipse.lsp4xml.settings.InitializationOptionsSettings;
import org.eclipse.lsp4xml.settings.LogsSettings;
import org.eclipse.lsp4xml.settings.ServerSettings;
import org.eclipse.lsp4xml.settings.SharedSettings;
import org.eclipse.lsp4xml.settings.XMLGeneralClientSettings;
import org.eclipse.lsp4xml.settings.XMLExperimentalCapabilities;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
Expand Down Expand Up @@ -208,6 +209,10 @@ public XMLLanguageService getXMLLanguageService() {
return xmlLanguageService;
}

public SharedSettings getSettings() {
return xmlTextDocumentService.getSharedSettings();
}

public ScheduledFuture<?> schedule(Runnable command, int delay, TimeUnit unit) {
return delayer.schedule(command, delay, unit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
*/
public class ServerSettings {

public static final String DEFAULT_WORK_DIR = "~/.lsp4xml";

private String workDir;


/**
* @return the workDir
*/
Expand All @@ -34,7 +35,16 @@ public void setWorkDir(String workDir) {
this.workDir = workDir;
}

/**
* Returns a normalized workDir that was defined in the client preferences.
*
* If null or empty, returns a default path.
*
*/
public String getNormalizedWorkDir() {
if(workDir == null || workDir.isEmpty()) {
workDir = DEFAULT_WORK_DIR;
}
return FilesUtils.normalizePath(workDir);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package org.eclipse.lsp4xml.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
Expand All @@ -18,6 +19,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.regex.Matcher;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
Expand All @@ -30,6 +32,8 @@ public class FilesUtils {

public static final String LSP4XML_WORKDIR_KEY = "lsp4xml.workdir";
private static String cachePathSetting = null;
public static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
public static final String SLASH = File.separator;

public static String getCachePathSetting() {
return cachePathSetting;
Expand Down Expand Up @@ -62,10 +66,30 @@ public static void resetDeployPath() {
/**
* Given a file path as a string, will normalize it
* and return the normalized string if valid, or null if not.
*
* The '~' home symbol will be converted into the actual home path.
* Slashes will be corrected depending on the OS.
*/
public static String normalizePath(String pathString) {

if(pathString != null && !pathString.isEmpty()) {
pathString = pathString.replaceFirst("^~", System.getProperty("user.home"));
String userHome = System.getProperty("user.home");
if(isWindows) {
/**
* We do this because on pathString.replaceFirst("^~", ...
* the replacement string (userHome) needs to be re-escaped
* or else it considers the escaped backslash from userHome
* as the beginning of an escape character which is then unintentionally
* combined with the character after it.
*/

userHome = Matcher.quoteReplacement(userHome); // "C:\\User\\Nikolas"
pathString = pathString.replaceAll("/", "\\\\"); // "\Cache\Folder";
pathString = pathString.replaceFirst("^(\\\\)+", ""); // Since Windows, cannot start with '\' characters
}

pathString = pathString.replaceFirst("^~", userHome); // "C:\User\Nikolas"

Path p = Paths.get(pathString);
pathString = p.normalize().toString();
return pathString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public void testCommentFormatSameLine() throws BadLocationException {
String expected =
"<a>" + lineSeparator() +
" Content" + lineSeparator() +
"</a> <!-- My Comment -->\n";
"</a> <!-- My Comment -->" + lineSeparator();

XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();
formattingOptions.setJoinCommentLines(true);
Expand Down Expand Up @@ -1544,8 +1544,8 @@ public void testUseSingleQuotesNoQuotesSplit() throws BadLocationException {
String content =
"<a name = test> </a>";
String expected =
"<a\n" +
" name=\n" +
"<a" + lineSeparator() +
" name=" + lineSeparator() +
" test></a>";
format(content, expected, formattingOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.File;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import static org.eclipse.lsp4xml.utils.FilesUtils.SLASH;

/**
* Tests for settings.
*/
public class SettingsTest {

private static String testFolder = "TestXMLCacheFolder";
private static String targetTestFolder = "target/generated-test-sources";
private static String targetTestFolder = "target" + SLASH +"generated-test-sources";


@After
public void cleanup() {
String path = System.getProperty("user.dir") + "/" + targetTestFolder + "/" + testFolder;
String path = System.getProperty("user.dir") + SLASH + targetTestFolder + SLASH + testFolder;

File f = new File(path);
if (f.exists()) {
Expand All @@ -49,7 +49,8 @@ public void cleanup() {
private final String json = "{\r\n" + //
" \"settings\": {\r\n" + //
// Content model settings
" \"xml\": {\r\n" + " \"fileAssociations\": [\r\n" + //
" \"xml\": {\r\n" +
" \"fileAssociations\": [\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\spring-beans-3.0.xsd\",\r\n" + //
" \"pattern\": \"**/test*.xml\"\r\n" + //
Expand All @@ -75,9 +76,13 @@ public void cleanup() {
" \"formatComments\": true,\r\n" + //
" \"joinCommentLines\": true,\r\n" + //
" \"quotations\": " + XMLFormattingOptions.DOUBLE_QUOTES_VALUE + "\r\n" + //
" },\r\n" + " \"server\": {\r\n" + //
" },\r\n" +
" \"server\": {\r\n" + //
" \"workDir\": \"~/" + testFolder + "/Nested\"\r\n" + //
" }\r\n" + " }\r\n" + " }\r\n" + "}";
" }\r\n" +
" }\r\n" +
" }\r\n" +
"}";

@Test
public void initializationOptionsSettings() {
Expand Down Expand Up @@ -174,21 +179,18 @@ public void cachePathSettings() {
String originalUserHome = System.getProperty("user.home");
String userDir = System.getProperty("user.dir");
try {
System.setProperty("user.home", userDir + "/" + targetTestFolder); // .../org.eclipse.lsp4xml/target/generated-test-sources/
System.setProperty("user.home", userDir + SLASH + targetTestFolder); // .../org.eclipse.lsp4xml/target/generated-test-sources/

languageServer.updateSettings(initializationOptionsSettings);

//Ensure the expanded absolute path is being used.
Assert.assertEquals(System.getProperty("user.home") + "/" + testFolder + "/Nested", FilesUtils.getCachePathSetting());
Assert.assertEquals(System.getProperty("user.home") + SLASH + testFolder + SLASH + "Nested", FilesUtils.getCachePathSetting());
} catch (Exception e) {
fail();
} finally {
//Reset static cache path
FilesUtils.setCachePathSetting(null);
System.setProperty("user.home", originalUserHome);
}



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,38 @@

import org.junit.Test;

import static org.eclipse.lsp4xml.utils.FilesUtils.*;

/**
* FilesUtilsTest
*/
public class FilesUtilsTest {



@Test
public void testFilesCachePathPreference() throws Exception {
System.clearProperty(FilesUtils.LSP4XML_WORKDIR_KEY);
String newBasePathString = System.getProperty("user.home");
String newSubPathString = "New/Sub/Path";
String newSubPathString = Paths.get("New", "Sub", "Path").toString();
Path newSubPath = Paths.get(newSubPathString);
FilesUtils.setCachePathSetting(newBasePathString);
Path finalPath = FilesUtils.getDeployedPath(newSubPath);
assertEquals(newBasePathString + "/" + newSubPathString, finalPath.toString());
assertEquals(newBasePathString + SLASH + newSubPathString, finalPath.toString());
}

@Test
public void normalizePathTest() {
assertEquals(System.getProperty("user.home") + "/Test/Folder", FilesUtils.normalizePath("~/Test/Folder"));
assertEquals("/Test/~/Folder", FilesUtils.normalizePath("/Test/~/Folder"));
assertEquals("~/Test/Folder", FilesUtils.normalizePath("./~/Test/Folder"));
assertEquals("/Folder", FilesUtils.normalizePath("/Test/../Folder"));
assertEquals(System.getProperty("user.home") + SLASH + "Test" + SLASH +"Folder", FilesUtils.normalizePath("~/Test/Folder"));
assertEquals((isWindows ? "" : SLASH) + "Test" + SLASH + "~" + SLASH + "Folder", FilesUtils.normalizePath("/Test/~/Folder"));
assertEquals("~" + SLASH + "Test" + SLASH + "Folder", FilesUtils.normalizePath("./~/Test/Folder"));
assertEquals((isWindows ? "" : SLASH) + "Folder", FilesUtils.normalizePath("/Test/../Folder"));

}

@Test
public void testLeadingSlashes() {
String slashes = "\\\\\\";
assertEquals((isWindows ? "" : slashes) + "D:\\Users\\Nikolas\\Desktop", FilesUtils.normalizePath(slashes + "D:\\Users\\Nikolas\\Desktop"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ProjectUtils {
*/
public static Path getProjectDirectory() {
String currPath = ProjectUtils.class.getClassLoader().getResource("").getFile();
currPath = FilesUtils.normalizePath(currPath);
Path dir = Paths.get(currPath);
while (!Files.exists(dir.resolve("pom.xml")) && dir.getParent() != null) {
dir = dir.getParent();
Expand Down

0 comments on commit def18ec

Please sign in to comment.