Skip to content

Commit 6042fd0

Browse files
committed
Fixes bug when running empty foreachs
This commit fixes a bug when a foreach has no iteration elements.
1 parent bf81e64 commit 6042fd0

File tree

10 files changed

+67
-9
lines changed

10 files changed

+67
-9
lines changed

cli/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>org.sing_group</groupId>
1111
<artifactId>compi</artifactId>
12-
<version>1.3.2</version>
12+
<version>1.3.3</version>
1313
<!--
1414
WARNING: change version using (in the parent project):
1515
mvn versions:set -DnewVersion=[new_version]

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<parent>
1111
<groupId>org.sing_group</groupId>
1212
<artifactId>compi</artifactId>
13-
<version>1.3.2</version>
13+
<version>1.3.3</version>
1414
<!--
1515
WARNING: change version using (in the parent project):
1616
mvn versions:set -DnewVersion=[new_version]

core/src/main/java/org/sing_group/compi/core/CompiApp.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import static org.sing_group.compi.core.loops.ForeachIteration.createIterationForForeach;
2424

25+
import java.io.ByteArrayInputStream;
2526
import java.io.File;
2627
import java.io.IOException;
2728
import java.io.InputStream;
@@ -203,6 +204,7 @@ public void run()
203204
if (this.executionLog.taskWasFinished(taskToRun)) {
204205
taskToRun.setSkipped(true);
205206
}
207+
206208
try {
207209
taskManager.setRunning(taskToRun);
208210
} catch (RuntimeException e) {
@@ -219,6 +221,7 @@ public void run()
219221
});
220222
continue;
221223
}
224+
222225
if (taskToRun instanceof Foreach && !(taskToRun instanceof ForeachIteration)) {
223226
loopCounterOfTask.put(
224227
taskToRun, new AtomicInteger(
@@ -677,12 +680,12 @@ public OutputStream getOutputStream() {
677680

678681
@Override
679682
public InputStream getInputStream() {
680-
return null;
683+
return new ByteArrayInputStream(new byte[0]);
681684
}
682685

683686
@Override
684687
public InputStream getErrorStream() {
685-
return null;
688+
return new ByteArrayInputStream(new byte[0]);
686689
}
687690

688691
@Override

core/src/main/java/org/sing_group/compi/core/loops/ListLoopValuesGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public ListLoopValuesGenerator(VariableResolver resolver, Foreach foreach) {
5050
*/
5151
@Override
5252
protected List<String> getValuesFromResolvedSource(String source) {
53-
final String[] sourceStrings = source.split(",");
53+
54+
final String[] sourceStrings = source.length() == 0 ? new String[0] : source.split(",");
5455
for (final String s : sourceStrings) {
5556
this.toExecute.add(s);
5657
}

core/src/main/java/org/sing_group/compi/core/resolver/VariableResolverUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ public String resolveAllVariables(String text, Task task) {
131131

132132
private String getFileContents(File resolvedTextFile) throws FileNotFoundException {
133133
try (Scanner resultsScanner = new Scanner(resolvedTextFile)) {
134-
return resultsScanner.useDelimiter("\\Z").next();
134+
if (resultsScanner.useDelimiter("\\Z").hasNext()) {
135+
return resultsScanner.useDelimiter("\\Z").next();
136+
} else {
137+
return "";
138+
}
135139
}
136140
}
137141
}

core/src/test/java/org/sing_group/compi/tests/PipelineTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,25 @@ public void testPipelineLoop() throws Exception {
281281
}
282282
}
283283

284+
@Test
285+
public void testPipelineEmptyLoop() throws Exception {
286+
final String pipelineFile = ClassLoader.getSystemResource("testEmptyForeach.xml").getFile();
287+
288+
final CompiApp compi =
289+
new CompiApp(
290+
forPipeline(fromFile(new File(pipelineFile)), new File(pipelineFile))
291+
.build()
292+
);
293+
294+
TestExecutionHandler handler = new TestExecutionHandler();
295+
compi.addTaskExecutionHandler(handler);
296+
297+
compi.run();
298+
299+
assertTrue("empty loop does not finish", handler.getFinishedTasksIncludingLoopChildren().contains("ID-1"));
300+
301+
}
302+
284303
@Test
285304
public void testPipelineIterationBindedLoop() throws Exception {
286305
final String pipelineFile = ClassLoader.getSystemResource("testPipelineIterationBindedLoops.xml").getFile();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
#%L
4+
Compi Core
5+
%%
6+
Copyright (C) 2016 - 2018 Daniel Glez-Peña, Osvaldo Graña-Castro, Hugo
7+
López-Fernández, Jesús Álvarez Casanova
8+
%%
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
#L%
21+
-->
22+
23+
<pipeline xmlns="http://www.sing-group.org/compi/pipeline-1.0"
24+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
25+
<version>1.0</version>
26+
<tasks>
27+
<foreach id="ID-1" as="param" of="list" in="">
28+
sleep $param
29+
</foreach>
30+
</tasks>
31+
</pipeline>

dk/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>org.sing_group</groupId>
1111
<artifactId>compi</artifactId>
12-
<version>1.3.2</version>
12+
<version>1.3.3</version>
1313
<!--
1414
WARNING: change version using (in the parent project):
1515
mvn versions:set -DnewVersion=[new_version]

e2e-tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>org.sing_group</groupId>
1111
<artifactId>compi</artifactId>
12-
<version>1.3.2</version>
12+
<version>1.3.3</version>
1313
<!--
1414
WARNING: change version using (in the parent project):
1515
mvn versions:set -DnewVersion=[new_version]

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>org.sing_group</groupId>
77
<artifactId>compi</artifactId>
88
<packaging>pom</packaging>
9-
<version>1.3.2</version>
9+
<version>1.3.3</version>
1010
<!-- WARNING: change version using (in the parent project): mvn versions:set -DnewVersion=[new_version] mvn versions:commit This will change the version
1111
in all modules at-once -->
1212

0 commit comments

Comments
 (0)