Skip to content

Commit 392d677

Browse files
authored
Fix : correctly resolve env parameters, so they are also accessible to onStart/Complete hooks (#1263)
* execute DefineVariablesCommand before the hooks so that env variable are also accessible to the hooks commands * fix comments * fix the case when multiple DefineVariablesCommand are present
1 parent 70e7ab3 commit 392d677

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ class Orchestra(
9898

9999
onFlowStart(commands)
100100

101+
executeDefineVariablesCommands(commands, config)
102+
// filter out DefineVariablesCommand to not execute it twice
103+
val filteredCommands = commands.filter { it.asCommand() !is DefineVariablesCommand }
104+
101105
config?.onFlowStart?.commands?.let {
102106
executeCommands(
103107
commands = it,
@@ -108,7 +112,7 @@ class Orchestra(
108112

109113
try {
110114
val flowSuccess = executeCommands(
111-
commands = commands,
115+
commands = filteredCommands,
112116
config = config,
113117
shouldReinitJsEngine = false,
114118
).also {
@@ -571,12 +575,16 @@ class Orchestra(
571575
}
572576

573577
private fun runSubFlow(commands: List<MaestroCommand>, config: MaestroConfig?, subflowConfig: MaestroConfig?): Boolean {
578+
executeDefineVariablesCommands(commands, config)
579+
// filter out DefineVariablesCommand to not execute it twice
580+
val filteredCommands = commands.filter { it.asCommand() !is DefineVariablesCommand }
581+
574582
subflowConfig?.onFlowStart?.commands?.let {
575583
executeSubflowCommands(it, config)
576584
}
577585

578586
try {
579-
return executeSubflowCommands(commands, config)
587+
return executeSubflowCommands(filteredCommands, config)
580588
} finally {
581589
subflowConfig?.onFlowComplete?.commands?.let {
582590
executeSubflowCommands(it, config)
@@ -998,6 +1006,16 @@ class Orchestra(
9981006
return true
9991007
}
10001008

1009+
private fun executeDefineVariablesCommands(commands: List<MaestroCommand>, config: MaestroConfig?) {
1010+
commands.filter { it.asCommand() is DefineVariablesCommand }.takeIf { it.isNotEmpty() }?.let {
1011+
executeCommands(
1012+
commands = it,
1013+
config = config,
1014+
shouldReinitJsEngine = false
1015+
)
1016+
}
1017+
}
1018+
10011019
private object CommandSkipped : Exception()
10021020

10031021
data class CommandMetadata(

maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2881,7 +2881,7 @@ class IntegrationTest {
28812881

28822882
@Test
28832883
fun `Case 106 - execute onFlowStart and onFlowComplete when js output is set with subflows`() {
2884-
// Given
2884+
// Given
28852885
val commands = readCommands("106_on_flow_start_complete_when_js_output_set_subflows")
28862886

28872887
val driver = driver {
@@ -2906,6 +2906,37 @@ class IntegrationTest {
29062906
).inOrder()
29072907
}
29082908

2909+
@Test
2910+
fun `Case 107 - execute defineVariablesCommand before onFlowStart and onFlowComplete are executed`() {
2911+
// Given
2912+
val commands = readCommands("107_define_variables_command_before_hooks")
2913+
2914+
val driver = driver {
2915+
}
2916+
driver.addInstalledApp("com.example.app")
2917+
val receivedLogs = mutableListOf<String>()
2918+
2919+
// when
2920+
Maestro(driver).use {
2921+
orchestra(
2922+
it,
2923+
onCommandMetadataUpdate = { _, metadata ->
2924+
receivedLogs += metadata.logMessages
2925+
}
2926+
).runFlow(commands)
2927+
}
2928+
2929+
// Then
2930+
assertThat(receivedLogs).containsExactly(
2931+
"com.example.app",
2932+
).inOrder()
2933+
driver.assertEvents(
2934+
listOf(
2935+
Event.LaunchApp("com.example.app")
2936+
)
2937+
)
2938+
}
2939+
29092940
private fun orchestra(
29102941
maestro: Maestro,
29112942
) = Orchestra(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
appId: ${MAESTRO_APP_ID}
2+
env:
3+
MAESTRO_APP_ID: com.example.app
4+
onFlowStart:
5+
- launchApp:
6+
appId: ${MAESTRO_APP_ID}
7+
---
8+
- evalScript: ${ console.log(MAESTRO_APP_ID); }

0 commit comments

Comments
 (0)