Skip to content

Commit

Permalink
refactoring over #545 related to #520
Browse files Browse the repository at this point in the history
better unified file / path handling for files / jar zip file entries
  • Loading branch information
ptrthomas committed Oct 3, 2018
1 parent 061c572 commit 1b85b0d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
54 changes: 27 additions & 27 deletions karate-core/src/main/java/com/intuit/karate/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,23 +331,20 @@ public static void renameFileIfZeroBytes(String fileName) {
}
}

public static boolean isFile(Path path) {
return "file".equals(path.toUri().getScheme());
}

public static String toStandardPath(String path) {
if (path == null) {
return null;
}
return path.replace('\\', '/');
path = path.replace('\\', '/');
return path.startsWith("/") ? path.substring(1) : path;
}

public static String toRelativeClassPath(Path path, ClassLoader cl) {
if (!isFile(path)) {
if (isJarPath(path.toUri())) {
return CLASSPATH_COLON + toStandardPath(path.toString());
}
for (URL url : getAllClassPathUrls(cl)) {
Path rootPath = getPathFor(url);
Path rootPath = getPathFor(url, null);
if (path.startsWith(rootPath)) {
Path relativePath = rootPath.relativize(path);
return CLASSPATH_COLON + toStandardPath(relativePath.toString());
Expand All @@ -365,7 +362,7 @@ public static Path getPathContaining(Class clazz) {
Package p = clazz.getPackage();
String relative = p.getName().replace('.', '/');
URL url = clazz.getClassLoader().getResource(relative);
return getPathFor(url);
return getPathFor(url, null);
}

public static File getFileRelativeTo(Class clazz, String path) {
Expand All @@ -380,12 +377,8 @@ public static String toRelativeClassPath(Class clazz) {

public static Path fromRelativeClassPath(String relativePath, ClassLoader cl) {
relativePath = removePrefix(relativePath);
try {
return Paths.get(cl.getResource(relativePath).toURI());
} catch (Exception e) {
Path path = getPathFor(cl.getResource(relativePath));
return path.resolve(relativePath);
}
URL url = cl.getResource(relativePath);
return getPathFor(url, relativePath);
}

public static Path fromRelativeClassPath(String relativePath, Path parentPath) {
Expand All @@ -411,13 +404,23 @@ public static List<Resource> scanForFeatureFiles(List<String> paths, ClassLoader
return list;
}

private static Path getPathFor(URL url) {
public static boolean isJarPath(URI uri) {
return uri.toString().contains("!/");
}

private static Path getPathFor(URL url, String relativePath) {
try {
if (url.toString().contains("!/")) {
FileSystem fs = getFileSystem(url.toURI());
return fs.getRootDirectories().iterator().next();
URI uri = url.toURI();
if (isJarPath(uri)) {
FileSystem fs = getFileSystem(uri);
Path path = fs.getRootDirectories().iterator().next();
if (relativePath != null) {
return path.resolve(relativePath);
} else {
return path;
}
} else {
return Paths.get(url.toURI());
return Paths.get(uri);
}
} catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -433,11 +436,11 @@ public static List<URL> getAllClassPathUrls(ClassLoader classLoader) {
list.add(url);
}
if (classLoader instanceof URLClassLoader) {
for (URL u : ((URLClassLoader) classLoader).getURLs()) {
for (URL u : ((URLClassLoader) classLoader).getURLs()) {
URL url = new URL("jar:" + u + "!/");
list.add(url);
}
}
}
}
return list;
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -476,7 +479,7 @@ private static void collectFeatureFiles(URL url, String searchPath, List<Resourc
Path rootPath;
Path search;
if (classpath) {
rootPath = getPathFor(url);
rootPath = getPathFor(url, null);
search = rootPath.resolve(searchPath);
} else {
rootPath = new File(".").getAbsoluteFile().toPath();
Expand All @@ -494,15 +497,12 @@ private static void collectFeatureFiles(URL url, String searchPath, List<Resourc
if (fileName != null && fileName.toString().endsWith(".feature")) {
String relativePath = rootPath.relativize(path.toAbsolutePath()).toString();
relativePath = relativePath.replaceAll("[.]{2,}", "");
if (relativePath.charAt(0) == '/') {
relativePath = relativePath.substring(1);
}
String prefix = classpath ? CLASSPATH_COLON : "";
files.add(new Resource(path, prefix + toStandardPath(relativePath)));
}
}
}

public static enum Platform {
WINDOWS,
MAC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public void testRunningFromJarFile() throws Exception {
list = FileUtils.scanForFeatureFiles(Collections.singletonList("classpath:demo/jar1/caller.feature"), cl);
assertEquals(1, list.size());
Resource resource = list.get(0);
assertTrue(FileUtils.isJarPath(resource.getPath().toUri()));
Path path = FileUtils.fromRelativeClassPath("classpath:demo/jar1/caller.feature", cl);
String relativePath = FileUtils.toRelativeClassPath(path, cl);
assertEquals("classpath:demo/jar1/caller.feature", relativePath);
Feature feature = FeatureParser.parse(resource);
Map<String, Object> map = Runner.runFeature(feature, null, false);
assertEquals(true, map.get("success"));
Expand All @@ -53,13 +57,13 @@ public void testRunningFromJarFile() throws Exception {
@Test
public void testFileUtilsForJarFile() throws Exception {
File file = new File("src/test/java/common.feature");
assertTrue(FileUtils.isFile(file.toPath()));
assertTrue(!FileUtils.isJarPath(file.toPath().toUri()));
ClassLoader cl = getJarClassLoader();
Class main = cl.loadClass("demo.jar1.Main");
Path path = FileUtils.getPathContaining(main);
assertFalse(FileUtils.isFile(path));
assertFalse(!FileUtils.isJarPath(path.toUri()));
String relativePath = FileUtils.toRelativeClassPath(path, cl);
assertEquals("classpath:/", relativePath);
assertEquals("classpath:", relativePath); // TODO doesn't matter but fix in future if possible
path = FileUtils.fromRelativeClassPath("classpath:demo/jar1", cl);
assertEquals(path.toString(), "/demo/jar1");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Feature: browser automation

Background:
* configure driver = { type: 'chrome', executable: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' }
# * configure driver = { type: 'chrome', executable: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' }
# * configure driver = { type: 'chromedriver', port: 9515, executable: '/Users/pthomas3/dev/webdriver/chromedriver' }
# * configure driver = { type: 'geckodriver', port: 4444, executable: '/Users/pthomas3/dev/webdriver/geckodriver' }
# * configure driver = { type: 'mswebdriver', port: 17556, executable: 'C:/Users/pthomas3/Downloads/MicrosoftWebDriver.exe' }
# * configure driver = { type: 'safaridriver', port: 4444, executable: 'safaridriver' }
* configure driver = { type: 'safaridriver', port: 4444, executable: 'safaridriver' }

Scenario: try to login to github
and then do a google search
Expand Down

0 comments on commit 1b85b0d

Please sign in to comment.