Skip to content

Commit 29ddc11

Browse files
czpilarfmbenhassine
authored andcommitted
Fix empty and commented lines within script execution
Resolves #1315 Resolves #1316 Signed-off-by: David Pilar <david@czpilar.net>
1 parent db567c2 commit 29ddc11

3 files changed

Lines changed: 65 additions & 2 deletions

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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.core;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.io.CleanupMode;
20+
import org.junit.jupiter.api.io.TempDir;
21+
22+
import java.io.File;
23+
import java.nio.file.Files;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertNull;
27+
28+
/**
29+
* @author David Pilar
30+
*/
31+
class FileInputProviderTests {
32+
33+
@TempDir(cleanup = CleanupMode.ALWAYS)
34+
private File tempDir;
35+
36+
@Test
37+
void testReadInput() throws Exception {
38+
// given
39+
File inputFile = new File(tempDir, "input.txt");
40+
String inputContent = """
41+
echo Hello World
42+
// This is a comment
43+
echo Line 1\\
44+
Line 2
45+
46+
echo Line 3""";
47+
Files.writeString(inputFile.toPath(), inputContent);
48+
49+
// when & then
50+
try (FileInputProvider inputProvider = new FileInputProvider(inputFile)) {
51+
assertEquals("echo Hello World", inputProvider.readInput());
52+
assertEquals("echo Line 1 Line 2", inputProvider.readInput());
53+
assertEquals("echo Line 3", inputProvider.readInput());
54+
assertNull(inputProvider.readInput());
55+
}
56+
}
57+
58+
}

spring-shell-docs/modules/ROOT/pages/commands/builtin/script.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ This command expects a file absolute path as an option, which points to the scri
1010
$>script --file /absolute/path/to/your/script.txt
1111
----
1212

13-
The script file should contain one command per line, and the commands will be executed in the order they appear in the file. Comments can be added to the script file by starting a line with a `#` character.
13+
The script file should contain one command per line, and the commands will be executed in the order they appear in the file. Comments can be added to the script file by starting a line with `//` characters.

0 commit comments

Comments
 (0)