fix: support multiple source dirs (#809)
This commit is contained in:
@@ -23,7 +23,6 @@ import com.microsoft.playwright.PlaywrightException;
|
||||
import com.microsoft.playwright.TimeoutError;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -85,8 +84,7 @@ public class Connection {
|
||||
}
|
||||
this.transport = transport;
|
||||
root = new Root(this);
|
||||
String srcRoot = System.getenv("PLAYWRIGHT_JAVA_SRC");
|
||||
stackTraceCollector = (srcRoot == null) ? null: new StackTraceCollector(Paths.get(srcRoot));
|
||||
stackTraceCollector = StackTraceCollector.createFromEnv();
|
||||
}
|
||||
|
||||
boolean isCollectingStacks() {
|
||||
|
||||
@@ -23,15 +23,30 @@ import com.microsoft.playwright.PlaywrightException;
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class StackTraceCollector {
|
||||
private final Path srcDir;
|
||||
private final List<Path> srcDirs;
|
||||
private final Map<Path, String> classToSourceCache = new HashMap<>();
|
||||
|
||||
StackTraceCollector(Path srcDir) {
|
||||
if (!Files.exists(srcDir.toAbsolutePath())) {
|
||||
throw new PlaywrightException("Source location doesn't exist: '" + srcDir.toAbsolutePath() + "'");
|
||||
static StackTraceCollector createFromEnv() {
|
||||
String srcRoots = System.getenv("PLAYWRIGHT_JAVA_SRC");
|
||||
if (srcRoots == null) {
|
||||
return null;
|
||||
}
|
||||
this.srcDir = srcDir;
|
||||
List<Path> srcDirs = Arrays.stream(srcRoots.split(File.pathSeparator)).map(p -> Paths.get(p)).collect(Collectors.toList());
|
||||
for (Path srcDir: srcDirs) {
|
||||
if (!Files.exists(srcDir.toAbsolutePath())) {
|
||||
throw new PlaywrightException("Source location specified in PLAYWRIGHT_JAVA_SRC doesn't exist: '" + srcDir.toAbsolutePath() + "'");
|
||||
}
|
||||
}
|
||||
return new StackTraceCollector(srcDirs);
|
||||
}
|
||||
|
||||
private StackTraceCollector(List<Path> srcDirs) {
|
||||
this.srcDirs = srcDirs;
|
||||
}
|
||||
|
||||
private String sourceFile(StackTraceElement frame) {
|
||||
@@ -47,7 +62,26 @@ class StackTraceCollector {
|
||||
if (file == null) {
|
||||
return "";
|
||||
}
|
||||
return srcDir.resolve(pkg).resolve(file).toString();
|
||||
return resolveSourcePath(Paths.get(pkg).resolve(file));
|
||||
}
|
||||
|
||||
private String resolveSourcePath(Path relativePath) {
|
||||
String path = classToSourceCache.get(relativePath);
|
||||
if (path == null) {
|
||||
for (Path dir : srcDirs) {
|
||||
Path absolutePath = dir.resolve(relativePath);
|
||||
if (Files.exists(absolutePath)) {
|
||||
path = absolutePath.toString();
|
||||
classToSourceCache.put(relativePath, path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (path == null) {
|
||||
path = "";
|
||||
classToSourceCache.put(relativePath, path);
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
JsonArray currentStackTrace() {
|
||||
|
||||
@@ -119,7 +119,9 @@ public class TestTracing extends TestBase {
|
||||
assertEquals(1, sources.size());
|
||||
|
||||
String path = getClass().getName().replace('.', File.separatorChar);
|
||||
Path sourceFile = Paths.get(System.getenv("PLAYWRIGHT_JAVA_SRC"), path + ".java");
|
||||
String[] srcRoots = System.getenv("PLAYWRIGHT_JAVA_SRC").split(File.pathSeparator);
|
||||
// Resolve in the last specified source dir.
|
||||
Path sourceFile = Paths.get(srcRoots[srcRoots.length - 1], path + ".java");
|
||||
byte[] thisFile = Files.readAllBytes(sourceFile);
|
||||
assertEquals(new String(thisFile, UTF_8), new String(sources.values().iterator().next(), UTF_8));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user