Skip to content
Closed
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
@@ -0,0 +1,99 @@
/*
* Copyright 2025-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;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

class FileInputProviderTests {

@TempDir
Path tempDir;

@Test
void shouldReadSingleLine() throws Exception {
// given
File file = createTempFile("hello world");

// when / then
try (FileInputProvider provider = new FileInputProvider(file)) {
String input = provider.readInput();
assertEquals("hello world", input);
}
}

@Test
void shouldReturnNullOnEmptyFile() throws Exception {
// given
File file = createTempFile("");

// when / then
try (FileInputProvider provider = new FileInputProvider(file)) {
String input = provider.readInput();
assertNull(input);
}
}

@Test
void shouldHandleLineContinuation() throws Exception {
// given
File file = createTempFile("hello \\\nworld");

// when / then
try (FileInputProvider provider = new FileInputProvider(file)) {
String input = provider.readInput();
assertEquals("hello world", input);
}
}

@Test
void shouldReturnNullForCommentLine() throws Exception {
// given
File file = createTempFile("// this is a comment");

// when / then
try (FileInputProvider provider = new FileInputProvider(file)) {
String input = provider.readInput();
assertNull(input);
}
}

@Test
void shouldThrowForNonExistentFile() {
// given
File nonExistent = new File(tempDir.toFile(), "nonexistent.txt");

// when / then
assertThrows(FileNotFoundException.class, () -> new FileInputProvider(nonExistent));
}

private File createTempFile(String content) throws IOException {
Path file = tempDir.resolve("test-input.txt");
Files.writeString(file, content);
return file.toFile();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2025-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;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import org.springframework.shell.core.command.AbstractCommand;
import org.springframework.shell.core.command.CommandContext;
import org.springframework.shell.core.command.CommandRegistry;
import org.springframework.shell.core.command.DefaultCommandParser;
import org.springframework.shell.core.command.ExitStatus;

import static org.junit.jupiter.api.Assertions.assertTrue;

class NonInteractiveShellRunnerTests {

@TempDir
Path tempDir;

private CommandRegistry commandRegistry;

private DefaultCommandParser commandParser;

private StringWriter outputBuffer;

private NonInteractiveShellRunner runner;

@BeforeEach
void setUp() {
commandRegistry = new CommandRegistry();
commandParser = new DefaultCommandParser(commandRegistry);
outputBuffer = new StringWriter();

commandRegistry.registerCommand(new AbstractCommand("greet", "A greeting command") {
@Override
public ExitStatus doExecute(CommandContext commandContext) {
commandContext.outputWriter().println("Hello!");
commandContext.outputWriter().flush();
return ExitStatus.OK;
}
});

runner = new NonInteractiveShellRunner(commandParser, commandRegistry, new PrintWriter(outputBuffer));
}

@Test
void shouldExecuteSingleCommand() throws Exception {
// when
runner.run(new String[] { "greet" });

// then
assertTrue(outputBuffer.toString().contains("Hello!"));
}

@Test
void shouldHandleEmptyArgs() throws Exception {
// when - should not throw
runner.run(new String[] {});

// then - output should be empty (error is logged)
assertTrue(outputBuffer.toString().isEmpty());
}

@Test
void shouldExecuteScriptFile() throws Exception {
// given
Path scriptFile = tempDir.resolve("commands.txt");
Files.writeString(scriptFile, "greet\n");

// when
runner.run(new String[] { "@" + scriptFile.toAbsolutePath() });

// then
assertTrue(outputBuffer.toString().contains("Hello!"));
}

@Test
void shouldThrowForUnknownCommand() {
// when / then - CommandNotFoundException propagates from CommandExecutor
org.junit.jupiter.api.Assertions.assertThrows(
org.springframework.shell.core.command.CommandNotFoundException.class,
() -> runner.run(new String[] { "unknown" }));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2025-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.PrintWriter;
import java.io.StringWriter;

import org.junit.jupiter.api.Test;

import org.springframework.shell.core.InputReader;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ClearTests {

@Test
void executeShouldSendAnsiClearSequence() throws Exception {
// given
Clear clear = new Clear();
StringWriter stringWriter = new StringWriter();
ParsedInput parsedInput = ParsedInput.builder().commandName("clear").build();
CommandContext context = new CommandContext(parsedInput, new CommandRegistry(), new PrintWriter(stringWriter),
new InputReader() {
});

// when
ExitStatus exitStatus = clear.execute(context);

// then
assertEquals(ExitStatus.OK, exitStatus);
assertTrue(stringWriter.toString().contains("\033[H\033[2J"));
}

@Test
void shouldHaveCorrectMetadata() {
// given
Clear clear = new Clear();

// then
assertEquals("Clear the terminal screen", clear.getDescription());
assertEquals("Built-In Commands", clear.getGroup());
}

}
Loading