Skip to content

Commit

Permalink
Fixes bug when running empty foreachs
Browse files Browse the repository at this point in the history
This commit fixes a bug when a foreach has no iteration elements.
  • Loading branch information
lipido committed Oct 21, 2020
1 parent bf81e64 commit 6042fd0
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>org.sing_group</groupId>
<artifactId>compi</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<!--
WARNING: change version using (in the parent project):
mvn versions:set -DnewVersion=[new_version]
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>org.sing_group</groupId>
<artifactId>compi</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<!--
WARNING: change version using (in the parent project):
mvn versions:set -DnewVersion=[new_version]
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/org/sing_group/compi/core/CompiApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

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

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -203,6 +204,7 @@ public void run()
if (this.executionLog.taskWasFinished(taskToRun)) {
taskToRun.setSkipped(true);
}

try {
taskManager.setRunning(taskToRun);
} catch (RuntimeException e) {
Expand All @@ -219,6 +221,7 @@ public void run()
});
continue;
}

if (taskToRun instanceof Foreach && !(taskToRun instanceof ForeachIteration)) {
loopCounterOfTask.put(
taskToRun, new AtomicInteger(
Expand Down Expand Up @@ -677,12 +680,12 @@ public OutputStream getOutputStream() {

@Override
public InputStream getInputStream() {
return null;
return new ByteArrayInputStream(new byte[0]);
}

@Override
public InputStream getErrorStream() {
return null;
return new ByteArrayInputStream(new byte[0]);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public ListLoopValuesGenerator(VariableResolver resolver, Foreach foreach) {
*/
@Override
protected List<String> getValuesFromResolvedSource(String source) {
final String[] sourceStrings = source.split(",");

final String[] sourceStrings = source.length() == 0 ? new String[0] : source.split(",");
for (final String s : sourceStrings) {
this.toExecute.add(s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ public String resolveAllVariables(String text, Task task) {

private String getFileContents(File resolvedTextFile) throws FileNotFoundException {
try (Scanner resultsScanner = new Scanner(resolvedTextFile)) {
return resultsScanner.useDelimiter("\\Z").next();
if (resultsScanner.useDelimiter("\\Z").hasNext()) {
return resultsScanner.useDelimiter("\\Z").next();
} else {
return "";
}
}
}
}
19 changes: 19 additions & 0 deletions core/src/test/java/org/sing_group/compi/tests/PipelineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,25 @@ public void testPipelineLoop() throws Exception {
}
}

@Test
public void testPipelineEmptyLoop() throws Exception {
final String pipelineFile = ClassLoader.getSystemResource("testEmptyForeach.xml").getFile();

final CompiApp compi =
new CompiApp(
forPipeline(fromFile(new File(pipelineFile)), new File(pipelineFile))
.build()
);

TestExecutionHandler handler = new TestExecutionHandler();
compi.addTaskExecutionHandler(handler);

compi.run();

assertTrue("empty loop does not finish", handler.getFinishedTasksIncludingLoopChildren().contains("ID-1"));

}

@Test
public void testPipelineIterationBindedLoop() throws Exception {
final String pipelineFile = ClassLoader.getSystemResource("testPipelineIterationBindedLoops.xml").getFile();
Expand Down
31 changes: 31 additions & 0 deletions core/src/test/resources/testEmptyForeach.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
#%L
Compi Core
%%
Copyright (C) 2016 - 2018 Daniel Glez-Peña, Osvaldo Graña-Castro, Hugo
López-Fernández, Jesús Álvarez Casanova
%%
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#L%
-->

<pipeline xmlns="http://www.sing-group.org/compi/pipeline-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<version>1.0</version>
<tasks>
<foreach id="ID-1" as="param" of="list" in="">
sleep $param
</foreach>
</tasks>
</pipeline>
2 changes: 1 addition & 1 deletion dk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>org.sing_group</groupId>
<artifactId>compi</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<!--
WARNING: change version using (in the parent project):
mvn versions:set -DnewVersion=[new_version]
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>org.sing_group</groupId>
<artifactId>compi</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<!--
WARNING: change version using (in the parent project):
mvn versions:set -DnewVersion=[new_version]
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>org.sing_group</groupId>
<artifactId>compi</artifactId>
<packaging>pom</packaging>
<version>1.3.2</version>
<version>1.3.3</version>
<!-- WARNING: change version using (in the parent project): mvn versions:set -DnewVersion=[new_version] mvn versions:commit This will change the version
in all modules at-once -->

Expand Down

0 comments on commit 6042fd0

Please sign in to comment.