From 9b1575fdad1153105d1d326644cc1121cd2943b7 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Mon, 30 Apr 2018 11:00:15 -0400 Subject: [PATCH] SQL: Teach the CLI to ignore empty commands 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 --- .../elasticsearch/xpack/sql/cli/CliRepl.java | 5 +++++ .../xpack/sql/cli/CliReplTests.java | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/CliRepl.java b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/CliRepl.java index f591ced77ec2d..956c62eaa1f9a 100644 --- a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/CliRepl.java +++ b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/CliRepl.java @@ -56,6 +56,11 @@ public void execute() { multiLine.setLength(0); } + // Skip empty commands + if (line.isEmpty()) { + continue; + } + // special case to handle exit if (isExit(line)) { cliTerminal.line().em("Bye!").ln(); diff --git a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliReplTests.java b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliReplTests.java index 2397418256ae9..31aa4749221fe 100644 --- a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliReplTests.java +++ b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliReplTests.java @@ -38,6 +38,28 @@ public void testBasicCliFunctionality() throws Exception { verifyNoMoreInteractions(mockCommand, mockSession); } + /** + * Test that empty commands are skipped. This includes commands that are + * just new lines. + */ + public void testEmptyNotSent() { + CliTerminal cliTerminal = new TestTerminal( + ";", + "", + "", + ";", + "exit;" + ); + + CliSession mockSession = mock(CliSession.class); + CliCommand mockCommand = mock(CliCommand.class); + + CliRepl cli = new CliRepl(cliTerminal, mockSession, mockCommand); + cli.execute(); + + verify(mockCommand, times(1)).handle(cliTerminal, mockSession, "logo"); + verifyNoMoreInteractions(mockSession, mockCommand); + } public void testFatalCliExceptionHandling() throws Exception { CliTerminal cliTerminal = new TestTerminal(