-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(java): support JSII_NODE setting
Users can customize the `node` runtime used by the jsii runtime for java by providing the `JSII_NODE` environment variable. Additionally, this corrects how the child process is spawned so that `JSII_NODE` and `JSII_RUNTIME` can contain spaces (previously, this would result in a spawn error). Added a test to verify the various scenarios work as intended. Fixes #4009
- Loading branch information
1 parent
6b2fd18
commit 32d31de
Showing
4 changed files
with
146 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--settings=user.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--settings=user.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/@jsii/java-runtime/project/src/test/java/software/amazon/jsii/JsiiRuntimeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package software.amazon.jsii; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.*; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
|
||
public final class JsiiRuntimeTest { | ||
@Test | ||
public void withNoCustomization() { | ||
final JsiiRuntime runtime = new JsiiRuntime(null, null); | ||
runtime.getClient().createObject("Object", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); | ||
runtime.terminate(); | ||
} | ||
|
||
@Test | ||
public void withCustomNode_Simple() { | ||
final String customNode = resolveNodeFromPath(); | ||
|
||
final JsiiRuntime runtime = new JsiiRuntime(null, customNode); | ||
runtime.getClient().createObject("Object", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); | ||
runtime.terminate(); | ||
} | ||
|
||
@Test | ||
public void withCustomNode_WithSpace() { | ||
final JsiiRuntime runtime = new JsiiRuntime(null, "node --max_old_space_size=1024"); | ||
runtime.getClient().createObject("Object", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); | ||
runtime.terminate(); | ||
} | ||
|
||
@Test | ||
public void withCustomRuntime_Simple() throws Exception { | ||
final Path launcher = Files.createTempFile("jsii-runtime", ".launcher.bat"); | ||
try (final Writer writer = new FileWriter(launcher.toFile())) { | ||
writer.write("node ./src/main/resources/software/amazon/jsii/bin/jsii-runtime.js\n"); | ||
} | ||
Assertions.assertTrue(launcher.toFile().setExecutable(true)); | ||
|
||
final JsiiRuntime runtime = new JsiiRuntime(launcher.toString(), null); | ||
runtime.getClient().createObject("Object", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); | ||
runtime.terminate(); | ||
} | ||
|
||
@Test | ||
public void withCustomRuntime_WithSpace() { | ||
final JsiiRuntime runtime = new JsiiRuntime("node ./src/main/resources/software/amazon/jsii/bin/jsii-runtime.js", null); | ||
runtime.getClient().createObject("Object", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); | ||
runtime.terminate(); | ||
} | ||
|
||
private static String resolveNodeFromPath() { | ||
try { | ||
final String[] command = System.getProperty("os.name").startsWith("Windows") | ||
? new String[]{"cmd.exe", "/d", "/s", "/c", "where node"} | ||
: new String[]{"sh", "-c", "command -v node"}; | ||
final Process process = Runtime.getRuntime().exec(command); | ||
try { | ||
final BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
return br.readLine(); | ||
} finally { | ||
process.waitFor(); | ||
} | ||
} catch (final IOException ioe) { | ||
throw new UncheckedIOException(ioe); | ||
} catch (final InterruptedException ie) { | ||
throw new RuntimeException(ie); | ||
} | ||
} | ||
} |