Skip to content

Commit 9571568

Browse files
committed
Allow quit command to be hidden from help.
1 parent fa29874 commit 9571568

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

spring-shell-core/src/main/java/org/springframework/shell/core/command/AbstractCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public abstract class AbstractCommand implements Command {
4646

4747
private final String group;
4848

49-
private final boolean hidden;
49+
private boolean hidden;
5050

5151
private AvailabilityProvider availabilityProvider = AvailabilityProvider.alwaysAvailable();
5252

@@ -105,6 +105,10 @@ public boolean isHidden() {
105105
return this.hidden;
106106
}
107107

108+
public void setHidden(boolean hidden) {
109+
this.hidden = hidden;
110+
}
111+
108112
@Override
109113
public List<String> getAliases() {
110114
return this.aliases;

spring-shell-core/src/test/java/org/springframework/shell/core/command/HelpTests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.junit.jupiter.api.Assertions;
1919
import org.junit.jupiter.api.Test;
2020
import org.springframework.shell.core.InputReader;
21+
import org.springframework.shell.core.utils.Utils;
2122

2223
import java.io.PrintWriter;
2324
import java.io.StringWriter;
@@ -51,6 +52,35 @@ void testDefaultHelpMessage() throws Exception {
5152
Assertions.assertEquals(expectedOutput.replaceAll("\\R", "\n"), actualOutput.replaceAll("\\R", "\n"));
5253
}
5354

55+
@Test
56+
void testDefaultHelpMessageWithoutQuit() throws Exception {
57+
// given
58+
CommandRegistry commandRegistry = new CommandRegistry();
59+
StringWriter stringWriter = new StringWriter();
60+
PrintWriter outputWriter = new PrintWriter(stringWriter);
61+
InputReader inputReader = new InputReader() {
62+
};
63+
ParsedInput parsedInput = ParsedInput.builder().build();
64+
CommandContext commandContext = new CommandContext(parsedInput, commandRegistry, outputWriter, inputReader);
65+
66+
((AbstractCommand) Utils.QUIT_COMMAND).setHidden(true);
67+
68+
// when
69+
Help help = new Help();
70+
help.execute(commandContext);
71+
72+
// then
73+
String actualOutput = stringWriter.toString();
74+
String expectedOutput = """
75+
AVAILABLE COMMANDS
76+
77+
78+
""";
79+
Assertions.assertEquals(expectedOutput.replaceAll("\\R", "\n"), actualOutput.replaceAll("\\R", "\n"));
80+
81+
((AbstractCommand) Utils.QUIT_COMMAND).setHidden(false);
82+
}
83+
5484
@Test
5585
void testHelpMessageForCommand() throws Exception {
5686
// given
@@ -352,4 +382,4 @@ void testHelpMessageForCommandAlias() throws Exception {
352382
Assertions.assertEquals(expectedOutput.replaceAll("\\R", "\n"), actualOutput.replaceAll("\\R", "\n"));
353383
}
354384

355-
}
385+
}

0 commit comments

Comments
 (0)