Skip to content

Commit abe797b

Browse files
authored
SQL: Teach the CLI to ignore empty commands (#30265)
Cause the CLI to ignore commands that are empty or consist only of newlines. This is a fairly standard thing for SQL CLIs to do. It looks like: ``` sql> ; sql> | | ; sql> exit; Bye! ``` I think I *could* have implemented this with a `CliCommand` that throws out empty string but it felt simpler to bake it in to the `CliRepl`. Closes #30000
1 parent 7933f5e commit abe797b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/CliRepl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public void execute() {
5656
multiLine.setLength(0);
5757
}
5858

59+
// Skip empty commands
60+
if (line.isEmpty()) {
61+
continue;
62+
}
63+
5964
// special case to handle exit
6065
if (isExit(line)) {
6166
cliTerminal.line().em("Bye!").ln();

x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliReplTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ public void testBasicCliFunctionality() throws Exception {
3838
verifyNoMoreInteractions(mockCommand, mockSession);
3939
}
4040

41+
/**
42+
* Test that empty commands are skipped. This includes commands that are
43+
* just new lines.
44+
*/
45+
public void testEmptyNotSent() {
46+
CliTerminal cliTerminal = new TestTerminal(
47+
";",
48+
"",
49+
"",
50+
";",
51+
"exit;"
52+
);
53+
54+
CliSession mockSession = mock(CliSession.class);
55+
CliCommand mockCommand = mock(CliCommand.class);
56+
57+
CliRepl cli = new CliRepl(cliTerminal, mockSession, mockCommand);
58+
cli.execute();
59+
60+
verify(mockCommand, times(1)).handle(cliTerminal, mockSession, "logo");
61+
verifyNoMoreInteractions(mockSession, mockCommand);
62+
}
4163

4264
public void testFatalCliExceptionHandling() throws Exception {
4365
CliTerminal cliTerminal = new TestTerminal(

0 commit comments

Comments
 (0)