Skip to content

Commit

Permalink
Fix script detection regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
tokou committed Sep 16, 2024
1 parent ff0b9ec commit 95c0e03
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import maestro.orchestra.MaestroCommand
object Env {

fun String.evaluateScripts(jsEngine: JsEngine): String {
val result = "(?<!\\\\)\\\$\\{([^\$]*)}".toRegex()
val result = "(?<!\\\\)\\\$\\{(.*?)}".toRegex()
.replace(this) { match ->
val script = match.groups[1]?.value ?: ""

Expand All @@ -20,7 +20,7 @@ object Env {
}

return result
.replace("\\\\\\\$\\{([^\$]*)}".toRegex()) { match ->
.replace("\\\\\\\$\\{(.*?)}".toRegex()) { match ->
match.value.substringAfter('\\')
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}

}

0 comments on commit 95c0e03

Please sign in to comment.