diff --git a/spring-shell-core/src/main/java/org/springframework/shell/jline/FileInputProvider.java b/spring-shell-core/src/main/java/org/springframework/shell/jline/FileInputProvider.java index 6fb84753d..3279af808 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/jline/FileInputProvider.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/jline/FileInputProvider.java @@ -20,6 +20,7 @@ import org.jline.reader.Parser; import org.springframework.shell.Input; import org.springframework.shell.InputProvider; +import org.springframework.util.StringUtils; import java.io.BufferedReader; import java.io.Closeable; @@ -33,6 +34,7 @@ * of line to signal line continuation.

* * @author Eric Bottard + * @author David Pilar */ public class FileInputProvider implements InputProvider, Closeable { @@ -63,9 +65,13 @@ public Input readInput() { } catch (IOException e) { throw new RuntimeException(e); } - if (line == null || isComment(line)) { + if (line == null) { return null; - } else { + } + else if (!StringUtils.hasLength(line) || isComment(line)) { + return readInput(); + } + else { ParsedLine parsedLine = parser.parse(sb.toString(), sb.toString().length()); return new ParsedLineInput(parsedLine); } diff --git a/spring-shell-core/src/test/java/org/springframework/shell/jline/FileInputProviderTests.java b/spring-shell-core/src/test/java/org/springframework/shell/jline/FileInputProviderTests.java index 317649878..2eb482367 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/jline/FileInputProviderTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/jline/FileInputProviderTests.java @@ -17,15 +17,20 @@ import org.jline.reader.EOFError; import org.jline.reader.impl.DefaultParser; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.shell.Input; + import java.io.Reader; import java.io.StringReader; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -98,4 +103,27 @@ void shouldNotThrowOnUnclosedQuoteExtendedParser(String line) { fileInputProvider.readInput(); }); } + + @Test + void shouldSkipEmptyAndCommentedLines() { + String inputContent = """ + echo Hello World + // This is a comment + echo Line 1\\ + Line 2 + + echo Line 3"""; + Reader reader = new StringReader(inputContent); + fileInputProvider = new FileInputProvider(reader, springParser); + + Input first = fileInputProvider.readInput(); + Input second = fileInputProvider.readInput(); + Input third = fileInputProvider.readInput(); + Input fourth = fileInputProvider.readInput(); + + assertEquals("echo Hello World", first.rawText()); + assertEquals("echo Line 1 Line 2", second.rawText()); + assertEquals("echo Line 3", third.rawText()); + assertNull(fourth); + } } \ No newline at end of file