File tree Expand file tree Collapse file tree
main/java/org/springframework/shell/core
test/java/org/springframework/shell/core Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1919import java .io .*;
2020
2121import org .jspecify .annotations .Nullable ;
22+ import org .springframework .util .StringUtils ;
2223
2324/**
2425 * An {@link InputProvider} that reads input from a file.
2930 * @author Eric Bottard
3031 * @author Piotr Olaszewski
3132 * @author Mahmoud Ben Hassine
33+ * @author David Pilar
3234 */
3335public 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 }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments