Skip to content

Commit 8c0231b

Browse files
authored
fix: handle when FileSystem already exists (#1140)
1 parent c1891cb commit 8c0231b

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed

.github/workflows/test_cli.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ jobs:
2929
- name: Test CLI version
3030
shell: bash
3131
run: tools/test-cli-version/test.sh
32+
- name: Test CLI Fatjar
33+
shell: bash
34+
run: tools/test-cli-fatjar/test.sh

driver-bundle/src/main/java/com/microsoft/playwright/impl/driver/jar/DriverJar.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,25 @@ private static boolean isExecutable(Path filePath) {
106106
return name.endsWith(".sh") || name.endsWith(".exe") || !name.contains(".");
107107
}
108108

109-
void extractDriverToTempDir() throws URISyntaxException, IOException {
109+
private FileSystem initFileSystem(URI uri) throws IOException {
110+
try {
111+
return FileSystems.newFileSystem(uri, Collections.emptyMap());
112+
} catch (FileSystemAlreadyExistsException e) {
113+
return null;
114+
}
115+
}
116+
117+
public static URI getDriverResourceURI() throws URISyntaxException {
110118
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
111-
URI originalUri = classloader.getResource(
112-
"driver/" + platformDir()).toURI();
119+
return classloader.getResource("driver/" + platformDir()).toURI();
120+
}
121+
122+
void extractDriverToTempDir() throws URISyntaxException, IOException {
123+
URI originalUri = getDriverResourceURI();
113124
URI uri = maybeExtractNestedJar(originalUri);
114125

115126
// Create zip filesystem if loading from jar.
116-
try (FileSystem fileSystem = "jar".equals(uri.getScheme()) ? FileSystems.newFileSystem(uri, Collections.emptyMap()) : null) {
127+
try (FileSystem fileSystem = "jar".equals(uri.getScheme()) ? initFileSystem(uri) : null) {
117128
Path srcRoot = Paths.get(uri);
118129
// jar file system's .relativize gives wrong results when used with
119130
// spring-boot-maven-plugin, convert to the default filesystem to

tools/test-cli-fatjar/pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.microsoft.playwright</groupId>
6+
<artifactId>test-cli-fatjar</artifactId>
7+
<version>1.29.0-SNAPSHOT</version>
8+
<name>Test Playwright Command Line FatJar</name>
9+
<properties>
10+
<compiler.version>1.8</compiler.version>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
</properties>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<version>3.1</version>
19+
<configuration>
20+
<source>${compiler.version}</source>
21+
<target>${compiler.version}</target>
22+
</configuration>
23+
</plugin>
24+
</plugins>
25+
</build>
26+
<dependencies>
27+
<dependency>
28+
<groupId>com.microsoft.playwright</groupId>
29+
<artifactId>playwright</artifactId>
30+
<version>${project.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.microsoft.playwright</groupId>
34+
<artifactId>driver-bundle</artifactId>
35+
<version>${project.version}</version>
36+
</dependency>
37+
</dependencies>
38+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.microsoft.playwright.testclifatjar;
2+
3+
import com.microsoft.playwright.Browser;
4+
import com.microsoft.playwright.Page;
5+
import com.microsoft.playwright.Playwright;
6+
import com.microsoft.playwright.impl.driver.Driver;
7+
import com.microsoft.playwright.impl.driver.jar.DriverJar;
8+
9+
import java.io.IOException;
10+
import java.net.URI;
11+
import java.net.URISyntaxException;
12+
import java.nio.file.FileSystem;
13+
import java.nio.file.FileSystems;
14+
import java.util.Collections;
15+
16+
public class TestApp {
17+
public static void main(String[] args) throws IOException, URISyntaxException {
18+
URI uri = DriverJar.getDriverResourceURI();
19+
FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap());
20+
if (fs == null) {
21+
throw new RuntimeException();
22+
}
23+
try (Playwright playwright = Playwright.create()) {
24+
Browser browser = playwright.chromium().launch();
25+
Page page = browser.newPage();
26+
}
27+
}
28+
}

tools/test-cli-fatjar/test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set +x
5+
6+
trap "cd $(pwd -P)" EXIT
7+
cd "$(dirname $0)"
8+
9+
echo "Running TestApp..."
10+
mvn compile exec:java -e -Dexec.mainClass=com.microsoft.playwright.testclifatjar.TestApp

0 commit comments

Comments
 (0)