Skip to content

Commit 8c8aa70

Browse files
committed
Fix empty and commented lines within script execution #1315 #1316
Signed-off-by: David Pilar <david@czpilar.net>
1 parent d714902 commit 8c8aa70

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

spring-shell-core/src/main/java/org/springframework/shell/core/FileInputProvider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.*;
2020

2121
import org.jspecify.annotations.Nullable;
22+
import org.springframework.util.StringUtils;
2223

2324
/**
2425
* An {@link InputProvider} that reads input from a file.
@@ -29,6 +30,7 @@
2930
* @author Eric Bottard
3031
* @author Piotr Olaszewski
3132
* @author Mahmoud Ben Hassine
33+
* @author David Pilar
3234
*/
3335
public class FileInputProvider implements InputProvider, AutoCloseable {
3436

@@ -59,9 +61,12 @@ public FileInputProvider(File file) throws FileNotFoundException {
5961
sb.append(line.replaceFirst(BACKSLASH_AT_EOL_REGEX, "$1 "));
6062
}
6163
while (continued);
62-
if (line == null || isComment(line)) {
64+
if (line == null) {
6365
return null;
6466
}
67+
else if (!StringUtils.hasLength(line) || isComment(line)) {
68+
return readInput();
69+
}
6570
else {
6671
return sb.toString();
6772
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.springframework.shell.core;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.io.CleanupMode;
5+
import org.junit.jupiter.api.io.TempDir;
6+
7+
import java.io.File;
8+
import java.nio.file.Files;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertNull;
12+
13+
/**
14+
* @author David Pilar
15+
*/
16+
class FileInputProviderTests {
17+
18+
@TempDir(cleanup = CleanupMode.ALWAYS)
19+
private File tempDir;
20+
21+
@Test
22+
void testReadInput() throws Exception {
23+
// given
24+
File inputFile = new File(tempDir, "input.txt");
25+
String inputContent = """
26+
echo Hello World
27+
// This is a comment
28+
echo Line 1\\
29+
Line 2
30+
31+
echo Line 3""";
32+
Files.writeString(inputFile.toPath(), inputContent);
33+
34+
// when & then
35+
try (FileInputProvider inputProvider = new FileInputProvider(inputFile)) {
36+
assertEquals("echo Hello World", inputProvider.readInput());
37+
assertEquals("echo Line 1 Line 2", inputProvider.readInput());
38+
assertEquals("echo Line 3", inputProvider.readInput());
39+
assertNull(inputProvider.readInput());
40+
}
41+
}
42+
43+
}

0 commit comments

Comments
 (0)