Skip to content

Commit aff7c12

Browse files
committed
Add test to return options
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent 38b7acf commit aff7c12

File tree

5 files changed

+116
-23
lines changed

5 files changed

+116
-23
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/executors/RunShellExecutor.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,7 @@ public void init(RunShell taskConfiguration, WorkflowDefinition definition) {
104104
Process process = processBuilder.start();
105105

106106
if (taskConfiguration.isAwait()) {
107-
StringBuilder stdout = new StringBuilder();
108-
StringBuilder stderr = new StringBuilder();
109-
110-
process.getInputStream().transferTo(the(stdout));
111-
process.getErrorStream().transferTo(the(stderr));
112-
int exitCode = process.waitFor();
113-
114-
RunTaskConfiguration.ProcessReturnType returnType = taskConfiguration.getReturn();
115-
116-
WorkflowModelFactory modelFactory = definition.application().modelFactory();
117-
118-
return switch (returnType) {
119-
case ALL ->
120-
modelFactory.fromAny(
121-
new ProcessResult(
122-
exitCode, stdout.toString().trim(), stderr.toString().trim()));
123-
case NONE -> modelFactory.fromNull();
124-
case CODE -> modelFactory.from(exitCode);
125-
case STDOUT -> modelFactory.from(stdout.toString().trim());
126-
case STDERR -> modelFactory.from(stderr.toString().trim());
127-
};
107+
return executeAwaiting(taskConfiguration, definition, process);
128108
} else {
129109
return input;
130110
}
@@ -135,6 +115,31 @@ public void init(RunShell taskConfiguration, WorkflowDefinition definition) {
135115
};
136116
}
137117

118+
private WorkflowModel executeAwaiting(
119+
RunShell taskConfiguration, WorkflowDefinition definition, Process process)
120+
throws IOException, InterruptedException {
121+
StringBuilder stdout = new StringBuilder();
122+
StringBuilder stderr = new StringBuilder();
123+
124+
process.getInputStream().transferTo(the(stdout));
125+
process.getErrorStream().transferTo(the(stderr));
126+
int exitCode = process.waitFor();
127+
128+
RunTaskConfiguration.ProcessReturnType returnType = taskConfiguration.getReturn();
129+
130+
WorkflowModelFactory modelFactory = definition.application().modelFactory();
131+
132+
return switch (returnType) {
133+
case ALL ->
134+
modelFactory.fromAny(
135+
new ProcessResult(exitCode, stdout.toString().trim(), stderr.toString().trim()));
136+
case NONE -> modelFactory.fromNull();
137+
case CODE -> modelFactory.from(exitCode);
138+
case STDOUT -> modelFactory.from(stdout.toString().trim());
139+
case STDERR -> modelFactory.from(stderr.toString().trim());
140+
};
141+
}
142+
138143
@Override
139144
public boolean accept(Class<? extends RunTaskConfiguration> clazz) {
140145
return RunShell.class.equals(clazz);

impl/test/src/test/java/io/serverlessworkflow/impl/test/shell/RunTaskExecutorTest.java renamed to impl/test/src/test/java/io/serverlessworkflow/impl/test/RunShellExecutorTest.java

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.serverlessworkflow.impl.test.shell;
16+
package io.serverlessworkflow.impl.test;
1717

1818
import io.serverlessworkflow.api.WorkflowReader;
1919
import io.serverlessworkflow.api.types.Workflow;
@@ -27,7 +27,7 @@
2727
import org.assertj.core.api.SoftAssertions;
2828
import org.junit.jupiter.api.Test;
2929

30-
public class RunTaskExecutorTest {
30+
public class RunShellExecutorTest {
3131

3232
@Test
3333
void testEcho() throws IOException {
@@ -140,6 +140,61 @@ void testAwaitBehavior() throws IOException {
140140
}
141141
}
142142

143+
@Test
144+
void testStderr() throws IOException {
145+
Workflow workflow =
146+
WorkflowReader.readWorkflowFromClasspath("workflows-samples/run-shell/echo-stderr.yaml");
147+
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
148+
Map<String, String> inputMap = Map.of();
149+
150+
WorkflowModel outputModel =
151+
appl.workflowDefinition(workflow).instance(inputMap).start().join();
152+
153+
SoftAssertions.assertSoftly(
154+
softly -> {
155+
softly.assertThat(outputModel.asText()).isPresent();
156+
softly.assertThat(outputModel.asText().get()).isNotEmpty();
157+
softly.assertThat(outputModel.asText().get()).contains("ls: cannot access");
158+
softly.assertThat(outputModel.asText().get()).contains("No such file or directory");
159+
});
160+
}
161+
}
162+
163+
@Test
164+
void testExitCode() throws IOException {
165+
Workflow workflow =
166+
WorkflowReader.readWorkflowFromClasspath("workflows-samples/run-shell/echo-exitcode.yaml");
167+
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
168+
Map<String, String> inputMap = Map.of();
169+
170+
WorkflowModel outputModel =
171+
appl.workflowDefinition(workflow).instance(inputMap).start().join();
172+
173+
SoftAssertions.assertSoftly(
174+
softly -> {
175+
softly.assertThat(outputModel.asNumber()).isPresent();
176+
softly.assertThat(outputModel.asNumber().get()).isNotEqualTo(0);
177+
});
178+
}
179+
}
180+
181+
@Test
182+
void testNone() throws IOException {
183+
Workflow workflow =
184+
WorkflowReader.readWorkflowFromClasspath("workflows-samples/run-shell/echo-none.yaml");
185+
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
186+
Map<String, String> inputMap = Map.of();
187+
188+
WorkflowModel outputModel =
189+
appl.workflowDefinition(workflow).instance(inputMap).start().join();
190+
191+
SoftAssertions.assertSoftly(
192+
softly -> {
193+
softly.assertThat(outputModel.asJavaObject()).isNull();
194+
});
195+
}
196+
}
197+
143198
record Input(User user) {}
144199

145200
record User(String name) {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
document:
2+
dsl: '1.0.1'
3+
namespace: test
4+
name: run-shell-example
5+
version: '0.1.0'
6+
do:
7+
- runShell:
8+
run:
9+
shell:
10+
command: 'ls /nonexistent_directory'
11+
return: code
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
document:
2+
dsl: '1.0.1'
3+
namespace: test
4+
name: run-shell-example
5+
version: '0.1.0'
6+
do:
7+
- runShell:
8+
run:
9+
shell:
10+
command: 'echo "Serverless Workflow"'
11+
return: none
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
document:
2+
dsl: '1.0.1'
3+
namespace: test
4+
name: run-shell-example
5+
version: '0.1.0'
6+
do:
7+
- runShell:
8+
run:
9+
shell:
10+
command: 'ls /nonexistent_directory'
11+
return: stderr

0 commit comments

Comments
 (0)