From 95c0e034c5b2a366e9b39b076eba3e360d366259 Mon Sep 17 00:00:00 2001 From: Tarek Belkahia Date: Tue, 17 Sep 2024 01:29:24 +0200 Subject: [PATCH] Fix script detection regexes --- .../main/java/maestro/orchestra/util/Env.kt | 4 ++-- .../kotlin/maestro/orchestra/util/EnvTest.kt | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/maestro-orchestra-models/src/main/java/maestro/orchestra/util/Env.kt b/maestro-orchestra-models/src/main/java/maestro/orchestra/util/Env.kt index 36562697af..44fbe1d4da 100644 --- a/maestro-orchestra-models/src/main/java/maestro/orchestra/util/Env.kt +++ b/maestro-orchestra-models/src/main/java/maestro/orchestra/util/Env.kt @@ -8,7 +8,7 @@ import maestro.orchestra.MaestroCommand object Env { fun String.evaluateScripts(jsEngine: JsEngine): String { - val result = "(? val script = match.groups[1]?.value ?: "" @@ -20,7 +20,7 @@ object Env { } return result - .replace("\\\\\\\$\\{([^\$]*)}".toRegex()) { match -> + .replace("\\\\\\\$\\{(.*?)}".toRegex()) { match -> match.value.substringAfter('\\') } } diff --git a/maestro-orchestra-models/src/test/kotlin/maestro/orchestra/util/EnvTest.kt b/maestro-orchestra-models/src/test/kotlin/maestro/orchestra/util/EnvTest.kt index 80b4fee6da..27dd79dfb1 100644 --- a/maestro-orchestra-models/src/test/kotlin/maestro/orchestra/util/EnvTest.kt +++ b/maestro-orchestra-models/src/test/kotlin/maestro/orchestra/util/EnvTest.kt @@ -3,10 +3,12 @@ package maestro.orchestra.util import com.google.common.truth.Truth.assertThat import java.io.File import kotlin.random.Random +import maestro.js.GraalJsEngine import maestro.orchestra.ApplyConfigurationCommand import maestro.orchestra.DefineVariablesCommand import maestro.orchestra.MaestroCommand import maestro.orchestra.MaestroConfig +import maestro.orchestra.util.Env.evaluateScripts import maestro.orchestra.util.Env.withDefaultEnvVars import maestro.orchestra.util.Env.withEnv import maestro.orchestra.util.Env.withInjectedShellEnvVars @@ -60,4 +62,20 @@ class EnvTest { assertThat(withEnv).containsExactly(defineVariables, applyConfig) } + + @Test + fun `evaluateScripts regex`() { + val engine = GraalJsEngine() + val inputs = listOf( + "${'$'}{console.log('Hello!')}", + "${'$'}{console.log('Hello Money! $')}", + "${'$'}{console.log('$')}", + ) + + val evaluated = inputs.map { it.evaluateScripts(engine) } + + // "undefined" is the expected output when evaluating console.log successfully + assertThat(evaluated).containsExactly("undefined", "undefined", "undefined") + } + }