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

#1637 [Graaljs] allow import #2271

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions maestro-client/src/main/java/maestro/js/GraalJsEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class GraalJsEngine(
protocols = listOf(Protocol.HTTP_1_1)
),
platform: String = "unknown",
private val allowIo: Boolean = false,
) : JsEngine {

private val openContexts = HashSet<Context>()
Expand Down Expand Up @@ -84,6 +85,7 @@ class GraalJsEngine(
}

val context = Context.newBuilder("js")
.allowIO(allowIo)
.option("js.strict", "true")
.logHandler(NULL_HANDLER)
.out(outputStream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,12 @@ class Orchestra(
config?.ext?.get("jsEngine") == "graaljs" || System.getenv("MAESTRO_USE_GRAALJS") == "true"
val platform = maestro.cachedDeviceInfo.platform.toString().lowercase()
jsEngine = if (shouldUseGraalJs) {
httpClient?.let { GraalJsEngine(it, platform) } ?: GraalJsEngine(platform = platform)
val shouldAllowIo =
config?.ext?.get("graalJsAllowIo") == true ||
System.getenv("MAESTRO_GRAALJS_ALLOW_IO") == "true"
httpClient?.let { GraalJsEngine(it, platform, shouldAllowIo) }
?: GraalJsEngine(platform = platform, allowIo = shouldAllowIo
)
} else {
httpClient?.let { RhinoJsEngine(it, platform) } ?: RhinoJsEngine(platform = platform)
}
Expand Down
41 changes: 40 additions & 1 deletion maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package maestro.test

import com.google.common.truth.Truth.assertThat
import com.oracle.truffle.js.nodes.function.EvalNode
import maestro.KeyCode
import maestro.Maestro
import maestro.MaestroException
Expand All @@ -28,9 +27,11 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.awt.Color
import java.io.File
import java.io.IOException
import java.nio.file.Paths
import kotlin.system.measureTimeMillis
import maestro.orchestra.util.Env.withDefaultEnvVars
import org.graalvm.polyglot.PolyglotException

class IntegrationTest {

Expand Down Expand Up @@ -3218,6 +3219,44 @@ class IntegrationTest {
)
}

@Test
fun `Case 120 - GraalJs import`() {
// given
val commands = readCommands("120_graaljs_import")
val driver = driver { }
val receivedLogs = mutableMapOf<MaestroCommand, List<String>>()

// when
Maestro(driver).use {
orchestra(
maestro = it,
onCommandMetadataUpdate = { command, metadata ->
receivedLogs[command] = metadata.logMessages
},
).runFlow(commands)
}

// then
assertThat(receivedLogs.values.flatten()).containsExactly(
"From script.mjs",
"From foo.mjs",
)
}

@Test
fun `Case 120 - GraalJs import disallowed`() {
assertThrows<PolyglotException> {
// given
val commands = readCommands("120_graaljs_import_disallowed")
val driver = driver { }

// when
Maestro(driver).use {
orchestra(it).runFlow(commands)
}
}
}

private fun orchestra(
maestro: Maestro,
) = Orchestra(
Expand Down
1 change: 1 addition & 0 deletions maestro-test/src/test/resources/120_foo.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function() { console.log('From foo.mjs') }
5 changes: 5 additions & 0 deletions maestro-test/src/test/resources/120_graaljs_import.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
appId: com.example.app
jsEngine: graaljs
graalJsAllowIo: true
---
- runScript: 120_script.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
appId: com.example.app
jsEngine: graaljs
---
- runScript: 120_script.mjs
3 changes: 3 additions & 0 deletions maestro-test/src/test/resources/120_script.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import foo from 'src/test/resources/120_foo.mjs'
foo()
console.log("From script.mjs")
Loading