From e59c94ae629b46cc5517eb4ffbce6bd1886f0388 Mon Sep 17 00:00:00 2001 From: David Pilar Date: Mon, 4 May 2026 13:30:47 +0200 Subject: [PATCH] Add missing space in error message between command and subcommands Signed-off-by: David Pilar --- .../shell/core/InteractiveShellRunner.java | 2 +- .../core/InteractiveShellRunnerTests.java | 108 ++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 spring-shell-core/src/test/java/org/springframework/shell/core/InteractiveShellRunnerTests.java diff --git a/spring-shell-core/src/main/java/org/springframework/shell/core/InteractiveShellRunner.java b/spring-shell-core/src/main/java/org/springframework/shell/core/InteractiveShellRunner.java index dda809439..48207855e 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/core/InteractiveShellRunner.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/core/InteractiveShellRunner.java @@ -118,7 +118,7 @@ public void run(String[] args) throws Exception { while (cause != null && cause.getCause() != null) { cause = cause.getCause(); } - String errorMessage = "Unable to run command " + parsedInput.commandName() + String errorMessage = "Unable to run command " + parsedInput.commandName() + " " + String.join(" ", parsedInput.subCommands()); if (cause != null && cause.getMessage() != null) { errorMessage += ": " + cause.getMessage(); diff --git a/spring-shell-core/src/test/java/org/springframework/shell/core/InteractiveShellRunnerTests.java b/spring-shell-core/src/test/java/org/springframework/shell/core/InteractiveShellRunnerTests.java new file mode 100644 index 000000000..ed631531a --- /dev/null +++ b/spring-shell-core/src/test/java/org/springframework/shell/core/InteractiveShellRunnerTests.java @@ -0,0 +1,108 @@ +package org.springframework.shell.core; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import org.junit.jupiter.api.Test; + +import org.springframework.shell.core.command.Command; +import org.springframework.shell.core.command.CommandContext; +import org.springframework.shell.core.command.CommandParser; +import org.springframework.shell.core.command.CommandRegistry; +import org.springframework.shell.core.command.ParsedInput; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author David Pilar + */ +class InteractiveShellRunnerTests { + + @Test + void errorMessageHasSpaceBetweenCommandAndSubCommands() throws Exception { + // given: a command "foo bar" that throws when executed + Consumer failingExecutor = ctx -> { + throw new RuntimeException("boom"); + }; + Command failingCommand = Command.builder().name("foo is bar").execute(failingExecutor); + CommandRegistry registry = new CommandRegistry(); + registry.registerCommand(failingCommand); + + ParsedInput parsedInput = ParsedInput.builder() + .commandName("foo") + .addSubCommand("is") + .addSubCommand("bar") + .build(); + CommandParser parser = input -> parsedInput; + + InputProvider inputProvider = new SingleLineInputProvider("foo is bar"); + RecordingShellRunner runner = new RecordingShellRunner(inputProvider, parser, registry); + + // when + runner.run(new String[0]); + + // then + assertThat(runner.printed).anyMatch(line -> line.startsWith("Unable to run command foo is bar")); + assertThat(runner.printed).noneMatch(line -> line.contains("Unable to run command foois bar")); + assertThat(runner.printed).noneMatch(line -> line.contains("Unable to run command fooisbar")); + assertThat(runner.printed).noneMatch(line -> line.contains("Unable to run command foo isbar")); + } + + private static class SingleLineInputProvider implements InputProvider { + + private final String line; + + private boolean consumed; + + SingleLineInputProvider(String line) { + this.line = line; + } + + @Override + public String readInput() { + if (consumed) { + return null; + } + consumed = true; + return line; + } + + } + + private static class RecordingShellRunner extends InteractiveShellRunner { + + private final List printed = new ArrayList<>(); + + private final PrintWriter writer = new PrintWriter(new StringWriter()); + + RecordingShellRunner(InputProvider inputProvider, CommandParser commandParser, + CommandRegistry commandRegistry) { + super(inputProvider, commandParser, commandRegistry); + } + + @Override + public void print(String message) { + printed.add(message); + } + + @Override + public void flush() { + } + + @Override + public PrintWriter getWriter() { + return writer; + } + + @Override + public InputReader getReader() { + return new InputReader() { + }; + } + + } + +}