Skip to content

Commit 5ee72e7

Browse files
committed
fix: remove the user directory only at the start of paths (in messages)
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
1 parent edcd253 commit 5ee72e7

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

src/main/java/com/adobe/epubcheck/reporting/CheckerMetadata.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class CheckerMetadata
3434
@JsonProperty
3535
private int nUsage = 0;
3636

37-
private final String workingDirectory = System.getProperty("user.dir");
38-
3937
public void setFileInfo(File epubFile)
4038
{
4139
this.path = PathUtil.removeWorkingDirectory(epubFile.getAbsolutePath());

src/main/java/com/adobe/epubcheck/util/PathUtil.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
// This class should probably be entirely refactored at some point
4040
public class PathUtil
4141
{
42-
static final String workingDirectory = System.getProperty("user.dir");
4342

4443
private static final Pattern REGEX_URI_SCHEME = Pattern
4544
.compile("^\\p{Alpha}(\\p{Alnum}|\\.|\\+|-)*:");
@@ -151,7 +150,11 @@ public static String removeWorkingDirectory(String path)
151150
{
152151
return path;
153152
}
154-
return path.replace(workingDirectory, ".");
153+
String workingDirectory = System.getProperty("user.dir");
154+
if ("/".equals(workingDirectory) || !path.startsWith(workingDirectory)) {
155+
return path;
156+
}
157+
return ".".concat(path.substring(workingDirectory.length()));
155158
}
156159

157160
public static String getFragment(String uri)

src/test/java/com/adobe/epubcheck/util/PathUtilTest.java

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import org.junit.Test;
88

9-
109
public class PathUtilTest
1110
{
1211

@@ -15,20 +14,19 @@ public void testIsRemoteNull()
1514
{
1615
PathUtil.isRemote(null);
1716
}
18-
17+
1918
@Test
2019
public void testIsRemoteTrue()
2120
{
2221
assertTrue(PathUtil.isRemote("https://example.org"));
2322
}
24-
23+
2524
@Test
2625
public void testIsRemoteFalse()
2726
{
2827
assertFalse(PathUtil.isRemote("OCF/path"));
2928
}
30-
31-
29+
3230
@Test(expected = NullPointerException.class)
3331
public void testNormalizePathNull()
3432
{
@@ -131,7 +129,7 @@ public void testNormalizePathLeadingSlash()
131129
assertEquals("/foo", PathUtil.normalizePath("/./foo"));
132130
assertEquals("/../foo", PathUtil.normalizePath("/../foo"));
133131
}
134-
132+
135133
@Test
136134
public void testNormalizePathAbsoluteURI()
137135
{
@@ -174,7 +172,7 @@ public void testRelativizeAbsoluteWithNullBaseIsReturnedAsIs()
174172
{
175173
assertEquals("http://foo", PathUtil.resolveRelativeReference(null, "http://foo"));
176174
}
177-
175+
178176
@Test
179177
public void testRelativizeAbsoluteWithNonNullBaseIsReturnedAsIs()
180178
{
@@ -188,31 +186,35 @@ public void testRelativizeAbsoluteSchemes()
188186
assertEquals("https://foo?q#f", PathUtil.resolveRelativeReference(null, "https://foo?q#f"));
189187
assertEquals("data:foo", PathUtil.resolveRelativeReference(null, "data:foo"));
190188
}
191-
189+
192190
@Test
193191
public void testRelativizeWithAbsoluteBase()
194192
{
195-
assertEquals("http://example.org/foo", PathUtil.resolveRelativeReference("http://example.org/", "foo"));
193+
assertEquals("http://example.org/foo",
194+
PathUtil.resolveRelativeReference("http://example.org/", "foo"));
196195
}
197-
196+
198197
@Test
199198
public void testRelativizeWithAbsoluteBaseAndFragment()
200199
{
201-
assertEquals("http://example.org/foo", PathUtil.resolveRelativeReference("http://example.org/#bar", "foo"));
200+
assertEquals("http://example.org/foo",
201+
PathUtil.resolveRelativeReference("http://example.org/#bar", "foo"));
202202
}
203203

204204
@Test
205205
public void testRelativizeWithAbsoluteBaseAndQuery()
206206
{
207-
assertEquals("http://example.org/foo", PathUtil.resolveRelativeReference("http://example.org/?test#bar", "foo"));
207+
assertEquals("http://example.org/foo",
208+
PathUtil.resolveRelativeReference("http://example.org/?test#bar", "foo"));
208209
}
209210

210211
@Test
211212
public void testRelativizeWithAbsoluteBaseIsNormalized()
212213
{
213-
assertEquals("http://example.org/foo", PathUtil.resolveRelativeReference("http://example.org/foo/../bar", "bar/../foo"));
214+
assertEquals("http://example.org/foo",
215+
PathUtil.resolveRelativeReference("http://example.org/foo/../bar", "bar/../foo"));
214216
}
215-
217+
216218
@Test
217219
public void testRelativizeWithRelBase()
218220
{
@@ -229,15 +231,15 @@ public void testRelativizeWithRelBaseIsNormalized()
229231
assertEquals("foo/foo/", PathUtil.resolveRelativeReference("foo/", "foo/"));
230232
assertEquals("bar/foo", PathUtil.resolveRelativeReference("foo/..", "bar/foo"));
231233
}
232-
234+
233235
@Test
234236
public void testRelativizeFragment()
235237
{
236238
assertEquals("foo#bar", PathUtil.resolveRelativeReference("foo", "#bar"));
237239
assertEquals("foo/#bar", PathUtil.resolveRelativeReference("foo/", "#bar"));
238240
assertEquals("#bar", PathUtil.resolveRelativeReference(".", "#bar"));
239241
}
240-
242+
241243
@Test
242244
public void testRelativizeDecodes()
243245
{
@@ -255,4 +257,23 @@ public void testRemoveAnchor()
255257
assertEquals(urlWithoutAnchor, PathUtil.removeFragment(urlWithoutAnchor));
256258
}
257259

260+
@Test
261+
public void testRemoveWorkingDirectory()
262+
{
263+
String OLD_USER_DIR = System.getProperty("user.dir");
264+
265+
assertEquals(null, PathUtil.removeWorkingDirectory(null));
266+
assertEquals("", PathUtil.removeWorkingDirectory(""));
267+
268+
System.setProperty("user.dir", "/user");
269+
assertEquals("./epub", PathUtil.removeWorkingDirectory("/user/epub"));
270+
271+
assertEquals("/prefix/user/epub", PathUtil.removeWorkingDirectory("/prefix/user/epub"));
272+
273+
System.setProperty("user.dir", "/");
274+
assertEquals("/dir/epub", PathUtil.removeWorkingDirectory("/dir/epub"));
275+
276+
System.setProperty("user.dir", OLD_USER_DIR);
277+
}
278+
258279
}

0 commit comments

Comments
 (0)