Skip to content
Permalink
Browse files

[JENKINS-44088] Support edge case where the workspace filePath on Win…

…dows uses “/“ instead of “\”
  • Loading branch information...
cyrille-leclerc authored and Justin Barry committed May 11, 2017
1 parent 1ee0a61 commit ec2196124d464d3abac3b4d22128ae1aec4338fc
@@ -32,7 +32,9 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
@@ -179,20 +181,58 @@ public static String toString(@Nullable Node node) {
/**
* Relativize path
*
* @return same path if not matching workspace
* @return relativized path
* @throws IllegalArgumentException if {@code other} is not a {@code Path} that can be relativized
* against this path
* @see java.nio.file.Path#relativize(Path)
*/
@Nonnull
public static String getPathInWorkspace(@Nonnull String absoluteFilePath, @Nonnull FilePath workspace) {
String fileSeparator = getFileSeparatorOnRemote(workspace);
public static String getPathInWorkspace(@Nonnull final String absoluteFilePath, @Nonnull FilePath workspace) {
boolean windows = isWindows(workspace);

String workspaceRemote = workspace.getRemote();
if (!workspaceRemote.endsWith(fileSeparator)) {
workspaceRemote = workspaceRemote + fileSeparator;
final String workspaceRemote = workspace.getRemote();

String sanitizedAbsoluteFilePath = absoluteFilePath;
String sanitizedWorkspaceRemote = workspaceRemote;
if (windows) {
// sanitize see JENKINS-44088
sanitizedWorkspaceRemote = workspaceRemote.replace('/', '\\');
sanitizedAbsoluteFilePath = absoluteFilePath.replace('/', '\\');
}

if (!sanitizedAbsoluteFilePath.startsWith(sanitizedWorkspaceRemote)) {
throw new IllegalArgumentException("Cannot relativize '" + absoluteFilePath + "' relatively to '" + workspace.getRemote() + "'");
}

String relativePath = StringUtils.substringAfter(sanitizedAbsoluteFilePath, sanitizedWorkspaceRemote);
String fileSeparator = windows ? "\\" : "/";

if (relativePath.startsWith(fileSeparator)) {
relativePath = relativePath.substring(fileSeparator.length());
}
if (absoluteFilePath.startsWith(workspaceRemote)) {
return StringUtils.substringAfter(absoluteFilePath, workspaceRemote);
LOGGER.log(Level.FINEST, "getPathInWorkspace({0}, {1}: {2}", new Object[]{absoluteFilePath, workspaceRemote, relativePath});
return relativePath;
}

public static boolean isWindows(@Nonnull FilePath path) {
String remote = path.getRemote();
if (remote.length() > 3 && remote.charAt(1) == ':' && remote.charAt(2) == '\\') {
// windows path such as "C:\path\to\..."
return true;
} else if (remote.length() > 3 && remote.charAt(1) == ':' && remote.charAt(2) == '/') {
// nasty windows path such as "C:/path/to/...". See JENKINS-44088
return true;
}
int indexOfSlash = path.getRemote().indexOf('/');
int indexOfBackSlash = path.getRemote().indexOf('\\');
if (indexOfSlash == -1) {
return true;
} else if (indexOfBackSlash == -1) {
return false;
} else if (indexOfSlash < indexOfBackSlash) {
return false;
} else {
return absoluteFilePath;
return true;
}
}

@@ -133,20 +133,73 @@ public void concatenate_two_strings(){
}

@Test
public void test_getPathInWorkspace_linux(){
public void test_getPathInWorkspace_linux_ok(){
String workspace = "/path/to/spring-petclinic";
String absolutePath = "/path/to/spring-petclinic/pom.xml";
String actual = XmlUtils.getPathInWorkspace(absolutePath, new FilePath(new File(workspace)));
String expected = "pom.xml";
Assert.assertThat(actual, CoreMatchers.is(expected));
}

@Test(expected = IllegalArgumentException.class)
public void test_getPathInWorkspace_linux_ko(){
String workspace = "/path/to/spring-petclinic";
String absolutePath = "/different/path/to/spring-petclinic/pom.xml";
String actual = XmlUtils.getPathInWorkspace(absolutePath, new FilePath(new File(workspace)));
String expected = "pom.xml";
Assert.assertThat(actual, CoreMatchers.is(expected));
}

@Test
public void test_getPathInWorkspace_linux_with_trailing_file_separator_ok(){
String workspace = "/path/to/spring-petclinic/";
String absolutePath = "/path/to/spring-petclinic/pom.xml";
String actual = XmlUtils.getPathInWorkspace(absolutePath, new FilePath(new File(workspace)));
String expected = "pom.xml";
Assert.assertThat(actual, CoreMatchers.is(expected));
}

@Test
public void test_getPathInWorkspace_windows_ok(){
String workspace = "C:\\path\\to\\spring-petclinic";
String absolutePath = "C:\\path\\to\\spring-petclinic\\pom.xml";
String actual = XmlUtils.getPathInWorkspace(absolutePath, new FilePath(new File(workspace)));
String expected = "pom.xml";
Assert.assertThat(actual, CoreMatchers.is(expected));
}

@Test
public void test_getPathInWorkspace_windows(){
public void test_getPathInWorkspace_windows_with_mixed_separators_ok(){
String workspace = "C:\\path\\to\\spring-petclinic";
String absolutePath = "C:\\path\\to\\spring-petclinic\\target/abc.xml";
String actual = XmlUtils.getPathInWorkspace(absolutePath, new FilePath(new File(workspace)));
String expected = "target\\abc.xml";
Assert.assertThat(actual, CoreMatchers.is(expected));
}

@Test(expected = IllegalArgumentException.class)
public void test_getPathInWorkspace_windows_ko(){
String workspace = "C:\\path\\to\\spring-petclinic";
String absolutePath = "C:\\different\\path\\to\\spring-petclinic\\pom.xml";
String actual = XmlUtils.getPathInWorkspace(absolutePath, new FilePath(new File(workspace)));
Assert.fail("should have thrown an IllegalArgumentException, not return " + actual);
}

@Test
public void test_getPathInWorkspace_windows_with_trailing_file_separator_ok(){
String workspace = "C:\\path\\to\\spring-petclinic\\";
String absolutePath = "C:\\path\\to\\spring-petclinic\\pom.xml";
String actual = XmlUtils.getPathInWorkspace(absolutePath, new FilePath(new File(workspace)));
String expected = "pom.xml";
Assert.assertThat(actual, CoreMatchers.is(expected));
}

@Test
public void test_getPathInWorkspace_windows_JENKINS_44088(){
String workspace = "C:/Jenkins/workspace/maven-pipeline-plugin-test";
String absolutePath = "C:\\Jenkins\\workspace\\maven-pipeline-plugin-test\\pom.xml";
String actual = XmlUtils.getPathInWorkspace(absolutePath, new FilePath(new File(workspace)));
String expected = "pom.xml";
Assert.assertThat(actual, CoreMatchers.is(expected));
}
}

0 comments on commit ec21961

Please sign in to comment.
You can’t perform that action at this time.