diff --git a/spring-shell-core/src/main/java/org/springframework/shell/core/FileInputProvider.java b/spring-shell-core/src/main/java/org/springframework/shell/core/FileInputProvider.java index 21ad569af..0841300d7 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/core/FileInputProvider.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/core/FileInputProvider.java @@ -19,6 +19,7 @@ import java.io.*; import org.jspecify.annotations.Nullable; +import org.springframework.util.StringUtils; /** * An {@link InputProvider} that reads input from a file. @@ -29,6 +30,7 @@ * @author Eric Bottard * @author Piotr Olaszewski * @author Mahmoud Ben Hassine + * @author David Pilar */ public class FileInputProvider implements InputProvider, AutoCloseable { @@ -59,9 +61,12 @@ public FileInputProvider(File file) throws FileNotFoundException { sb.append(line.replaceFirst(BACKSLASH_AT_EOL_REGEX, "$1 ")); } while (continued); - if (line == null || isComment(line)) { + if (line == null) { return null; } + else if (!StringUtils.hasLength(line) || isComment(line)) { + return readInput(); + } else { return sb.toString(); } diff --git a/spring-shell-core/src/test/java/org/springframework/shell/core/FileInputProviderTests.java b/spring-shell-core/src/test/java/org/springframework/shell/core/FileInputProviderTests.java new file mode 100644 index 000000000..e07dd7d3d --- /dev/null +++ b/spring-shell-core/src/test/java/org/springframework/shell/core/FileInputProviderTests.java @@ -0,0 +1,43 @@ +package org.springframework.shell.core; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.CleanupMode; +import org.junit.jupiter.api.io.TempDir; + +import java.io.File; +import java.nio.file.Files; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * @author David Pilar + */ +class FileInputProviderTests { + + @TempDir(cleanup = CleanupMode.ALWAYS) + private File tempDir; + + @Test + void testReadInput() throws Exception { + // given + File inputFile = new File(tempDir, "input.txt"); + String inputContent = """ + echo Hello World + // This is a comment + echo Line 1\\ + Line 2 + + echo Line 3"""; + Files.writeString(inputFile.toPath(), inputContent); + + // when & then + try (FileInputProvider inputProvider = new FileInputProvider(inputFile)) { + assertEquals("echo Hello World", inputProvider.readInput()); + assertEquals("echo Line 1 Line 2", inputProvider.readInput()); + assertEquals("echo Line 3", inputProvider.readInput()); + assertNull(inputProvider.readInput()); + } + } + +} \ No newline at end of file