Skip to content

Commit

Permalink
fix: remove the user directory only at the start of paths (in messages)
Browse files Browse the repository at this point in the history
The previous implementation of the `removeWorkingDirectory` utility
method was quite brutal and replaced all occurences of the "user.dir"
system property in paths, even when found in the middle of the path.

The new implementation only replaces the user directory when it occurs
at the beginning of the path, and does nothing when the user directory
is set to the root directory ("/").

Tests included.

Fixes #1181
  • Loading branch information
rdeltour committed Feb 26, 2021
1 parent edcd253 commit 5ee72e7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class CheckerMetadata
@JsonProperty
private int nUsage = 0;

private final String workingDirectory = System.getProperty("user.dir");

public void setFileInfo(File epubFile)
{
this.path = PathUtil.removeWorkingDirectory(epubFile.getAbsolutePath());
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/adobe/epubcheck/util/PathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
// This class should probably be entirely refactored at some point
public class PathUtil
{
static final String workingDirectory = System.getProperty("user.dir");

private static final Pattern REGEX_URI_SCHEME = Pattern
.compile("^\\p{Alpha}(\\p{Alnum}|\\.|\\+|-)*:");
Expand Down Expand Up @@ -151,7 +150,11 @@ public static String removeWorkingDirectory(String path)
{
return path;
}
return path.replace(workingDirectory, ".");
String workingDirectory = System.getProperty("user.dir");
if ("/".equals(workingDirectory) || !path.startsWith(workingDirectory)) {
return path;
}
return ".".concat(path.substring(workingDirectory.length()));
}

public static String getFragment(String uri)
Expand Down
53 changes: 37 additions & 16 deletions src/test/java/com/adobe/epubcheck/util/PathUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import org.junit.Test;


public class PathUtilTest
{

Expand All @@ -15,20 +14,19 @@ public void testIsRemoteNull()
{
PathUtil.isRemote(null);
}

@Test
public void testIsRemoteTrue()
{
assertTrue(PathUtil.isRemote("https://example.org"));
}

@Test
public void testIsRemoteFalse()
{
assertFalse(PathUtil.isRemote("OCF/path"));
}



@Test(expected = NullPointerException.class)
public void testNormalizePathNull()
{
Expand Down Expand Up @@ -131,7 +129,7 @@ public void testNormalizePathLeadingSlash()
assertEquals("/foo", PathUtil.normalizePath("/./foo"));
assertEquals("/../foo", PathUtil.normalizePath("/../foo"));
}

@Test
public void testNormalizePathAbsoluteURI()
{
Expand Down Expand Up @@ -174,7 +172,7 @@ public void testRelativizeAbsoluteWithNullBaseIsReturnedAsIs()
{
assertEquals("http://foo", PathUtil.resolveRelativeReference(null, "http://foo"));
}

@Test
public void testRelativizeAbsoluteWithNonNullBaseIsReturnedAsIs()
{
Expand All @@ -188,31 +186,35 @@ public void testRelativizeAbsoluteSchemes()
assertEquals("https://foo?q#f", PathUtil.resolveRelativeReference(null, "https://foo?q#f"));
assertEquals("data:foo", PathUtil.resolveRelativeReference(null, "data:foo"));
}

@Test
public void testRelativizeWithAbsoluteBase()
{
assertEquals("http://example.org/foo", PathUtil.resolveRelativeReference("http://example.org/", "foo"));
assertEquals("http://example.org/foo",
PathUtil.resolveRelativeReference("http://example.org/", "foo"));
}

@Test
public void testRelativizeWithAbsoluteBaseAndFragment()
{
assertEquals("http://example.org/foo", PathUtil.resolveRelativeReference("http://example.org/#bar", "foo"));
assertEquals("http://example.org/foo",
PathUtil.resolveRelativeReference("http://example.org/#bar", "foo"));
}

@Test
public void testRelativizeWithAbsoluteBaseAndQuery()
{
assertEquals("http://example.org/foo", PathUtil.resolveRelativeReference("http://example.org/?test#bar", "foo"));
assertEquals("http://example.org/foo",
PathUtil.resolveRelativeReference("http://example.org/?test#bar", "foo"));
}

@Test
public void testRelativizeWithAbsoluteBaseIsNormalized()
{
assertEquals("http://example.org/foo", PathUtil.resolveRelativeReference("http://example.org/foo/../bar", "bar/../foo"));
assertEquals("http://example.org/foo",
PathUtil.resolveRelativeReference("http://example.org/foo/../bar", "bar/../foo"));
}

@Test
public void testRelativizeWithRelBase()
{
Expand All @@ -229,15 +231,15 @@ public void testRelativizeWithRelBaseIsNormalized()
assertEquals("foo/foo/", PathUtil.resolveRelativeReference("foo/", "foo/"));
assertEquals("bar/foo", PathUtil.resolveRelativeReference("foo/..", "bar/foo"));
}

@Test
public void testRelativizeFragment()
{
assertEquals("foo#bar", PathUtil.resolveRelativeReference("foo", "#bar"));
assertEquals("foo/#bar", PathUtil.resolveRelativeReference("foo/", "#bar"));
assertEquals("#bar", PathUtil.resolveRelativeReference(".", "#bar"));
}

@Test
public void testRelativizeDecodes()
{
Expand All @@ -255,4 +257,23 @@ public void testRemoveAnchor()
assertEquals(urlWithoutAnchor, PathUtil.removeFragment(urlWithoutAnchor));
}

@Test
public void testRemoveWorkingDirectory()
{
String OLD_USER_DIR = System.getProperty("user.dir");

assertEquals(null, PathUtil.removeWorkingDirectory(null));
assertEquals("", PathUtil.removeWorkingDirectory(""));

System.setProperty("user.dir", "/user");
assertEquals("./epub", PathUtil.removeWorkingDirectory("/user/epub"));

assertEquals("/prefix/user/epub", PathUtil.removeWorkingDirectory("/prefix/user/epub"));

System.setProperty("user.dir", "/");
assertEquals("/dir/epub", PathUtil.removeWorkingDirectory("/dir/epub"));

System.setProperty("user.dir", OLD_USER_DIR);
}

}

0 comments on commit 5ee72e7

Please sign in to comment.