Skip to content

Commit 44d42c3

Browse files
committed
Match paths with regex instead of glob to eliminate false matches
1 parent 02e69ab commit 44d42c3

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/main/java/com/nordstrom/common/file/PathUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static Path getNextPath(Path targetPath, String baseName, String extensio
8080
throw new IllegalArgumentException("[extension] must specify a non-empty string");
8181
}
8282

83-
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + baseName + "*." + extension);
83+
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("regex:" + baseName + "(-\\d+)?\\." + extension);
8484

8585
try (Stream<Path> stream = Files.walk(targetPath, 1)) {
8686
int ext = extension.length() + 1;

src/test/java/com/nordstrom/common/file/PathUtilsTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ public void testNextPath() throws IOException {
3030
Files.createDirectories(targetPath);
3131
}
3232

33-
Path path1 = PathUtils.getNextPath(targetPath, "test", "txt");
34-
assertEquals(path1.getFileName().toString(), "test.txt");
33+
Path path1 = PathUtils.getNextPath(targetPath, "testNextPath", "txt");
34+
assertEquals(path1.getFileName().toString(), "testNextPath.txt");
3535

3636
path1.toFile().createNewFile();
37-
Path path2 = PathUtils.getNextPath(targetPath, "test", "txt");
38-
assertEquals(path2.getFileName().toString(), "test-2.txt");
37+
Path path2 = PathUtils.getNextPath(targetPath, "testNextPath", "txt");
38+
assertEquals(path2.getFileName().toString(), "testNextPath-2.txt");
39+
Path path3 = PathUtils.getNextPath(targetPath, "test", "txt");
40+
assertEquals(path3.getFileName().toString(), "test.txt");
3941
}
4042

4143
private Path getOutputPath() {

0 commit comments

Comments
 (0)