Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed file path issue on Windows #348

Merged
merged 1 commit into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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 Down Expand Up @@ -62,10 +63,17 @@ 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"));
if (pathString != null && !pathString.isEmpty()) {
if (pathString.indexOf("~") == 0) {
pathString = System.getProperty("user.home") + (pathString.length() > 1? pathString.substring(1):"");
}
pathString = pathString.replace("/", File.separator);
pathString = pathString.replace("\\", File.separator);
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 java.io.File.separator;

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

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


@After
public void cleanup() {
String path = System.getProperty("user.dir") + "/" + targetTestFolder + "/" + testFolder;
String path = System.getProperty("user.dir") + separator + targetTestFolder + separator + 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 + separator + 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") + separator + testFolder + separator + "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 @@ -17,6 +17,7 @@
import java.nio.file.Paths;

import org.junit.Test;
import static java.io.File.separator;

/**
* FilesUtilsTest
Expand All @@ -27,18 +28,19 @@ public class FilesUtilsTest {
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(Paths.get(newBasePathString, newSubPathString).toString(), 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(Paths.get(System.getProperty("user.home"), "Test", "Folder").toString(), FilesUtils.normalizePath("~/Test/Folder"));
assertEquals(Paths.get(separator, "Test", "~", "Folder").toString(), FilesUtils.normalizePath("/Test/~/Folder"));
Copy link
Contributor Author

@NikolasKomonen NikolasKomonen Apr 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fbricon This test was failing for me on Windows, an extra \ was inserted at the beginning of the output of Paths.get(...) could you check if you have the same issue

assertEquals(Paths.get("~", "Test", "Folder").toString(), FilesUtils.normalizePath("./~/Test/Folder"));
assertEquals(Paths.get(separator, "Folder").toString(), FilesUtils.normalizePath("/Test/../Folder"));
assertEquals(Paths.get(separator, "Users", "Nikolas").toString(), FilesUtils.normalizePath("\\Users\\Nikolas\\"));
}
}
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.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -23,7 +24,7 @@ public class ProjectUtils {
* @return the current lsp4xml project directory
*/
public static Path getProjectDirectory() {
String currPath = ProjectUtils.class.getClassLoader().getResource("").getFile();
String currPath = new File(ProjectUtils.class.getClassLoader().getResource("").getPath()).toString();
Path dir = Paths.get(currPath);
while (!Files.exists(dir.resolve("pom.xml")) && dir.getParent() != null) {
dir = dir.getParent();
Expand Down