Skip to content

Commit 0e25e94

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 0e25e94

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

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

Lines changed: 5 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.
@@ -59,9 +60,12 @@ public FileInputProvider(File file) throws FileNotFoundException {
5960
sb.append(line.replaceFirst(BACKSLASH_AT_EOL_REGEX, "$1 "));
6061
}
6162
while (continued);
62-
if (line == null || isComment(line)) {
63+
if (line == null) {
6364
return null;
6465
}
66+
else if (!StringUtils.hasLength(line) || isComment(line)) {
67+
return readInput();
68+
}
6569
else {
6670
return sb.toString();
6771
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
class FileInputProviderTests {
14+
15+
@TempDir(cleanup = CleanupMode.ALWAYS)
16+
private File tempDir;
17+
18+
@Test
19+
void testReadInput() throws Exception {
20+
// given
21+
File inputFile = new File(tempDir, "input.txt");
22+
String inputContent = """
23+
echo Hello World
24+
// This is a comment
25+
echo Line 1\\
26+
Line 2
27+
28+
echo Line 3""";
29+
Files.writeString(inputFile.toPath(), inputContent);
30+
31+
// when & then
32+
try (FileInputProvider inputProvider = new FileInputProvider(inputFile)) {
33+
assertEquals("echo Hello World", inputProvider.readInput());
34+
assertEquals("echo Line 1 Line 2", inputProvider.readInput());
35+
assertEquals("echo Line 3", inputProvider.readInput());
36+
assertNull(inputProvider.readInput());
37+
}
38+
}
39+
40+
}

0 commit comments

Comments
 (0)