Skip to content

Commit f597f44

Browse files
committed
Fix redundant error message when calling unavailable commands
Resolves #1387
1 parent db5cf48 commit f597f44

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

‎spring-shell-core/src/main/java/org/springframework/shell/core/InteractiveShellRunner.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ private void doRun() {
125125
CommandContext commandContext = new CommandContext(parsedInput, this.commandRegistry, getWriter(),
126126
getReader());
127127
ExitStatus exitStatus = this.commandExecutor.execute(commandContext);
128-
if (ExitStatus.OK.code() != exitStatus.code()) { // business error
128+
boolean businessError = ExitStatus.OK.code() != exitStatus.code();
129+
// the command itself already prints a dedicated availability message
130+
boolean alreadyReported = ExitStatus.AVAILABILITY_ERROR.code() == exitStatus.code();
131+
if (businessError && !alreadyReported) {
129132
print("Error while executing command " + parsedInput.commandName() + ": "
130133
+ exitStatus.description());
131134
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.core;
17+
18+
import java.io.PrintWriter;
19+
import java.io.StringWriter;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import java.util.Set;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.shell.core.command.AbstractCommand;
27+
import org.springframework.shell.core.command.Command;
28+
import org.springframework.shell.core.command.CommandContext;
29+
import org.springframework.shell.core.command.CommandParser;
30+
import org.springframework.shell.core.command.CommandRegistry;
31+
import org.springframework.shell.core.command.ExitStatus;
32+
import org.springframework.shell.core.command.ParsedInput;
33+
import org.springframework.shell.core.command.availability.Availability;
34+
import org.springframework.shell.core.command.availability.AvailabilityProvider;
35+
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.junit.jupiter.api.Assertions.assertFalse;
38+
import static org.mockito.Mockito.mock;
39+
40+
/**
41+
* Tests for {@link InteractiveShellRunner}.
42+
*
43+
* @author Mahmoud Ben Hassine
44+
*/
45+
class InteractiveShellRunnerTests {
46+
47+
@Test
48+
void testUnavailableCommandDoesNotAlsoPrintGenericErrorMessage() throws Exception {
49+
// given
50+
Command command = new AbstractCommand("test", "A test command") {
51+
52+
@Override
53+
public AvailabilityProvider getAvailabilityProvider() {
54+
return AvailabilityProvider.of(Availability.unavailable("you are not logged in"));
55+
}
56+
57+
@Override
58+
public ExitStatus doExecute(CommandContext commandContext) {
59+
return ExitStatus.OK;
60+
}
61+
};
62+
CommandRegistry commandRegistry = new CommandRegistry(Set.of(command));
63+
CommandParser commandParser = input -> ParsedInput.builder().commandName(input).build();
64+
List<String> lines = new ArrayList<>(List.of("test"));
65+
InputProvider inputProvider = () -> lines.isEmpty() ? null : lines.remove(0);
66+
StringWriter outputWriter = new StringWriter();
67+
List<String> printedMessages = new ArrayList<>();
68+
InteractiveShellRunner shellRunner = new InteractiveShellRunner(inputProvider, commandParser, commandRegistry) {
69+
70+
@Override
71+
public void print(String message) {
72+
printedMessages.add(message);
73+
}
74+
75+
@Override
76+
public void flush() {
77+
}
78+
79+
@Override
80+
public PrintWriter getWriter() {
81+
return new PrintWriter(outputWriter);
82+
}
83+
84+
@Override
85+
public InputReader getReader() {
86+
return mock(InputReader.class);
87+
}
88+
};
89+
90+
// when
91+
shellRunner.run(new String[0]);
92+
93+
// then
94+
assertEquals("Command 'test' exists but is not currently available because you are not logged in",
95+
outputWriter.toString().trim());
96+
assertFalse(printedMessages.stream().anyMatch(message -> message.startsWith("Error while executing command")));
97+
}
98+
99+
}

0 commit comments

Comments
 (0)