Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,6 +30,7 @@
* @author Eric Bottard
* @author Piotr Olaszewski
* @author Mahmoud Ben Hassine
* @author David Pilar
*/
public class FileInputProvider implements InputProvider, AutoCloseable {

Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

}