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.
@@ -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 }
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+ 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+ }
You can’t perform that action at this time.
0 commit comments