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

feat: added require to load scripts using CommonJs #2272

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
22 changes: 19 additions & 3 deletions maestro-client/src/main/java/maestro/js/GraalJsEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import java.io.ByteArrayOutputStream
import java.util.concurrent.TimeUnit
import java.util.logging.Handler
import java.util.logging.LogRecord
import java.nio.file.Paths
import java.nio.file.Path
import kotlin.time.Duration.Companion.minutes

private val NULL_HANDLER = object : Handler() {
Expand Down Expand Up @@ -70,10 +72,20 @@ class GraalJsEngine(
): Value {
envBinding.putAll(env)
val source = Source.newBuilder("js", script, sourceName).build()
return createContext().eval(source)
val sourceDir = getSourceDirectory(sourceName)
return createContext(sourceDir).eval(source)
}

private fun createContext(): Context {
private fun getSourceDirectory(sourceName: String): String {
return envBinding["MAESTRO_YAML_DIR"] ?: resolveScriptPath(sourceName).parent.toString()
}

private fun resolveScriptPath(sourceName: String): Path {
val path = Paths.get(sourceName)
return if (path.isAbsolute) path else Paths.get(System.getProperty("user.dir"), sourceName)
}

private fun createContext(moduleDir: String): Context {
val outputStream = object : ByteArrayOutputStream() {
override fun flush() {
super.flush()
Expand All @@ -85,6 +97,10 @@ class GraalJsEngine(

val context = Context.newBuilder("js")
.option("js.strict", "true")
.option("js.commonjs-require", "true")
.option("js.commonjs-require-cwd", moduleDir)
.allowExperimentalOptions(true)
.allowIO(true)
.logHandler(NULL_HANDLER)
.out(outputStream)
.build()
Expand Down Expand Up @@ -121,4 +137,4 @@ class GraalJsEngine(

return context
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ data class YamlFluentCommand(
RunScriptCommand(
script = resolvePath(flowPath, runScript.file)
.readText(),
env = runScript.env,
env = runScript.env + mapOf(
"MAESTRO_YAML_DIR" to flowPath.parent.toString()
),
sourceDescription = runScript.file,
condition = runScript.`when`?.toCondition(),
label = runScript.label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,10 @@ internal class YamlCommandReaderTest {
script = "const myNumber = 1 + 1;",
condition = null,
sourceDescription = "023_runScript_test.js",
label = "Run some special calculations"
label = "Run some special calculations",
env = mapOf(
"MAESTRO_YAML_DIR" to Paths.get("build/resources/test/YamlCommandReaderTest").toAbsolutePath().toString()
)
),
ScrollCommand(
label = "Scroll down"
Expand Down
24 changes: 24 additions & 0 deletions maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3218,6 +3218,30 @@ class IntegrationTest {
)
}

@Test
fun `Case 121 - CommonJS require functionality`() {
// Given
val commands = readCommands("121_require_module")
val driver = driver { }

val receivedLogs = mutableListOf<String>()

// When
Maestro(driver).use {
orchestra(
it,
onCommandMetadataUpdate = { _, metadata ->
receivedLogs += metadata.logMessages
}
).runFlow(commands)
}

// Then
assertThat(receivedLogs).containsExactly(
"{\"name\":\"myrequire\",\"version\":\"1.0.0\"}"
).inOrder()
}

private fun orchestra(
maestro: Maestro,
) = Orchestra(
Expand Down
4 changes: 4 additions & 0 deletions maestro-test/src/test/resources/121-myrequire.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
name: "myrequire",
version: "1.0.0"
};
2 changes: 2 additions & 0 deletions maestro-test/src/test/resources/121-testrequire.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const myrequire = require("./121-myrequire.js");
console.log(JSON.stringify(myrequire));
5 changes: 5 additions & 0 deletions maestro-test/src/test/resources/121_require_module.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
appId: com.other.app
jsEngine: graaljs
---
- runScript:
file: 121-testrequire.js
Loading