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

Handle absolute path in getAll method DAT-11890 #3369

Merged
merged 8 commits into from Oct 19, 2022
Expand Up @@ -2,6 +2,7 @@

import liquibase.Scope;
import liquibase.logging.Logger;
import liquibase.util.FileUtil;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -48,7 +49,20 @@ public List<Resource> getAll(String path) throws IOException {

private String standardizePath(String path) {
try {
return new File(path).toPath().normalize().toString().replace("\\", "/").replaceFirst("^/", "");
//
// Flip the separators to Linux-style and replace the first separator
// If then root path is the absolute path for Linux or Windows then return that result
//
String rootPath = getRootPath().toString();
String result = new File(path).toPath().normalize().toString().replace("\\", "/").replaceFirst("^/", "");
if (rootPath.equals("/") || rootPath.equals("\\")) {
return result;
}

//
// Strip off any Windows drive prefix and return the result
//
return result.replaceFirst("^\\w:/","");
} catch (InvalidPathException e) {
Scope.getCurrentScope().getLog(getClass()).warning("Failed to standardize path " + path, e);
return path;
Expand Down
3 changes: 1 addition & 2 deletions liquibase-core/src/main/java/liquibase/util/FileUtil.java
Expand Up @@ -52,11 +52,10 @@ public static String getFileNotFoundMessage(String physicalChangeLogLocation) {
return message;
}

public static boolean isAbsolute(String path) throws IOException {
public static boolean isAbsolute(String path) {
if (path == null) {
return false;
}
return new File(path).isAbsolute();
}

}
Expand Up @@ -93,14 +93,15 @@ class DirectoryResourceAccessorTest extends Specification {
simpleTestAccessor.getAll(path)*.getPath() == expected

where:
path | expected
"liquibase/resource/DirectoryResourceAccessorTest.class" | ["liquibase/resource/DirectoryResourceAccessorTest.class"]
"/liquibase/resource/DirectoryResourceAccessorTest.class" | ["liquibase/resource/DirectoryResourceAccessorTest.class"]
"liquibase\\resource\\DirectoryResourceAccessorTest.class" | ["liquibase/resource/DirectoryResourceAccessorTest.class"]
"\\liquibase\\resource\\DirectoryResourceAccessorTest.class" | ["liquibase/resource/DirectoryResourceAccessorTest.class"]
"com/example/file with space.txt" | ["com/example/file with space.txt"]
"com\\example\\file with space.txt" | ["com/example/file with space.txt"]
"com/example/file with space.txt" | ["com/example/file with space.txt"]
path | expected
"liquibase/resource/DirectoryResourceAccessorTest.class" | ["liquibase/resource/DirectoryResourceAccessorTest.class"]
"/liquibase/resource/DirectoryResourceAccessorTest.class" | ["liquibase/resource/DirectoryResourceAccessorTest.class"]
"liquibase\\resource\\DirectoryResourceAccessorTest.class" | ["liquibase/resource/DirectoryResourceAccessorTest.class"]
"\\liquibase\\resource\\DirectoryResourceAccessorTest.class" | ["liquibase/resource/DirectoryResourceAccessorTest.class"]
"com/example/file with space.txt" | ["com/example/file with space.txt"]
"com\\example\\file with space.txt" | ["com/example/file with space.txt"]
"com/example/file with space.txt" | ["com/example/file with space.txt"]
"c:\\liquibase\\resource\\DirectoryResourceAccessorTest.class" | ["liquibase/resource/DirectoryResourceAccessorTest.class"]
}

def "getAll returns empty if nothing matches"() {
Expand Down