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 0841300d7..5d9ac2744 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 @@ -47,6 +47,14 @@ public FileInputProvider(File file) throws FileNotFoundException { this.reader = new BufferedReader(new FileReader(file)); } + /** + * Create a new {@link FileInputProvider} instance. + * @param reader the reader to read input from + */ + public FileInputProvider(Reader reader) { + this.reader = new BufferedReader(reader); + } + @Override @Nullable public String readInput() throws IOException { StringBuilder sb = new StringBuilder(); diff --git a/spring-shell-core/src/main/java/org/springframework/shell/core/command/Script.java b/spring-shell-core/src/main/java/org/springframework/shell/core/command/Script.java index a08ea64b2..139e4fd1b 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/core/command/Script.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/core/command/Script.java @@ -16,9 +16,13 @@ package org.springframework.shell.core.command; import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStreamReader; import java.util.List; import java.util.Objects; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.Resource; import org.springframework.shell.core.FileInputProvider; import org.springframework.shell.core.NonInteractiveShellRunner; @@ -68,7 +72,7 @@ public ExitStatus doExecute(CommandContext commandContext) throws Exception { .orElseThrow(() -> new IllegalArgumentException( "Script command expects option --file or -f with exactly one argument: the absolute path to the script file to execute.")); File file = new File(Objects.requireNonNull(scriptFile)); - try (FileInputProvider inputProvider = new FileInputProvider(file)) { + try (FileInputProvider inputProvider = createInputProvider(file, scriptFile)) { String input; while ((input = inputProvider.readInput()) != null) { executeCommand(commandContext, input); @@ -77,6 +81,25 @@ public ExitStatus doExecute(CommandContext commandContext) throws Exception { return ExitStatus.OK; } + /** + * Create the {@link FileInputProvider} for the given script reference. An existing + * file is used as-is (backward compatible); otherwise the reference is resolved as a + * Spring resource (e.g. {@code classpath:} or {@code file:}). + * @param file the script reference as a file + * @param scriptFile the raw script reference + * @return the input provider for the script + */ + private FileInputProvider createInputProvider(File file, String scriptFile) throws Exception { + if (file.exists()) { + return new FileInputProvider(file); + } + Resource resource = new DefaultResourceLoader().getResource(scriptFile); + if (!resource.exists()) { + throw new FileNotFoundException("Script file does not exist: " + scriptFile); + } + return new FileInputProvider(new InputStreamReader(resource.getInputStream())); + } + private void executeCommand(CommandContext commandContext, String input) throws Exception { String[] commandTokens = input.split(" "); NonInteractiveShellRunner shellRunner = new NonInteractiveShellRunner(this.commandParser, 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 index 87234abdb..d0c5ed3e8 100644 --- 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 @@ -20,6 +20,7 @@ import org.junit.jupiter.api.io.TempDir; import java.io.File; +import java.io.StringReader; import java.nio.file.Files; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -55,4 +56,24 @@ void testReadInput() throws Exception { } } -} \ No newline at end of file + @Test + void testReadInputFromReader() throws Exception { + // given + String inputContent = """ + echo Hello World + // This is a comment + echo Line 1\\ + Line 2 + + echo Line 3"""; + + // when & then + try (FileInputProvider inputProvider = new FileInputProvider(new StringReader(inputContent))) { + assertEquals("echo Hello World", inputProvider.readInput()); + assertEquals("echo Line 1 Line 2", inputProvider.readInput()); + assertEquals("echo Line 3", inputProvider.readInput()); + assertNull(inputProvider.readInput()); + } + } + +} diff --git a/spring-shell-core/src/test/java/org/springframework/shell/core/command/ScriptTests.java b/spring-shell-core/src/test/java/org/springframework/shell/core/command/ScriptTests.java new file mode 100644 index 000000000..277bd847a --- /dev/null +++ b/spring-shell-core/src/test/java/org/springframework/shell/core/command/ScriptTests.java @@ -0,0 +1,121 @@ +/* + * Copyright 2026-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell.core.command; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.nio.file.Files; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.CleanupMode; +import org.junit.jupiter.api.io.TempDir; + +import org.springframework.shell.core.InputReader; + +/** + * Tests for {@link Script}. + * + * @author Ezequiel Primon + */ +class ScriptTests { + + @TempDir(cleanup = CleanupMode.ALWAYS) + private File tempDir; + + private CommandRegistry commandRegistry; + + private StringWriter stringWriter; + + private PrintWriter outputWriter; + + private InputReader inputReader = new InputReader() { + }; + + @BeforeEach + void setUp() { + this.commandRegistry = new CommandRegistry(); + Command sayHello = Command.builder() + .name("say-hello") + .description("Say hello") + .group("Test") + .execute(commandContext -> { + commandContext.outputWriter().println("Hello World"); + }); + this.commandRegistry.registerCommand(sayHello); + this.stringWriter = new StringWriter(); + this.outputWriter = new PrintWriter(this.stringWriter); + } + + @Test + void testScriptFromClasspathResource() throws Exception { + // when + executeScript("classpath:scripts/test-script.txt"); + + // then + Assertions.assertEquals("Hello World\nHello World\n", normalize(this.stringWriter.toString())); + } + + @Test + void testScriptFromAbsoluteFilePath() throws Exception { + // given + File scriptFile = new File(this.tempDir, "script.txt"); + Files.writeString(scriptFile.toPath(), "say-hello\n// This is a comment\nsay-hello\n"); + + // when + executeScript(scriptFile.getAbsolutePath()); + + // then + Assertions.assertEquals("Hello World\nHello World\n", normalize(this.stringWriter.toString())); + } + + @Test + void testScriptFromFileUrl() throws Exception { + // given + File scriptFile = new File(this.tempDir, "script.txt"); + Files.writeString(scriptFile.toPath(), "say-hello\n// This is a comment\nsay-hello\n"); + + // when + executeScript(scriptFile.toURI().toString()); + + // then + Assertions.assertEquals("Hello World\nHello World\n", normalize(this.stringWriter.toString())); + } + + @Test + void testScriptWithMissingFile() { + // when & then + Assertions.assertThrows(FileNotFoundException.class, () -> executeScript("does-not-exist.txt")); + } + + private void executeScript(String scriptRef) throws Exception { + CommandOption fileOption = CommandOption.with().shortName('f').longName("file").value(scriptRef).build(); + ParsedInput parsedInput = ParsedInput.builder().commandName("script").addOption(fileOption).build(); + CommandContext commandContext = new CommandContext(parsedInput, this.commandRegistry, this.outputWriter, + this.inputReader); + Script script = new Script(this.commandRegistry); + script.execute(commandContext); + this.outputWriter.flush(); + } + + private String normalize(String output) { + return output.replaceAll("\\R", "\n"); + } + +} diff --git a/spring-shell-core/src/test/resources/scripts/test-script.txt b/spring-shell-core/src/test/resources/scripts/test-script.txt new file mode 100644 index 000000000..ed6ea3f64 --- /dev/null +++ b/spring-shell-core/src/test/resources/scripts/test-script.txt @@ -0,0 +1,3 @@ +say-hello +// This is a comment +say-hello