changeset 897:72638d2844fa

Don't crash shell if there is no history match The shell crashes if a history expansion expression (such as '!connect') is used and no item in the history matches. Modify the shell to print the error and continue working. Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-January/005035.html
author Omair Majid <omajid@redhat.com>
date Tue, 08 Jan 2013 10:39:19 -0500
parents 3cc2f2ea31b3
children 5a2d3c43b551 0289117ee9ef
files client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ShellCommand.java distribution/src/test/java/com/redhat/thermostat/distribution/CliTest.java
diffstat 2 files changed, 31 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ShellCommand.java	Mon Jan 07 15:51:53 2013 -0500
+++ b/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ShellCommand.java	Tue Jan 08 10:39:19 2013 -0500
@@ -54,6 +54,7 @@
 
 import com.redhat.thermostat.common.cli.CommandContext;
 import com.redhat.thermostat.common.cli.CommandException;
+import com.redhat.thermostat.common.cli.Console;
 import com.redhat.thermostat.common.cli.SimpleCommand;
 import com.redhat.thermostat.common.config.ConfigUtils;
 import com.redhat.thermostat.common.config.InvalidConfigurationException;
@@ -129,11 +130,23 @@
         if (history != null) {
             reader.setHistory(history);
         }
-        while (handleConsoleInput(reader)) { /* no-op; the loop conditional performs the action */ }
+        while (handleConsoleInput(reader, ctx.getConsole())) { /* no-op; the loop conditional performs the action */ }
     }
 
-    private boolean handleConsoleInput(ConsoleReader reader) throws IOException, CommandException {
-        String line = reader.readLine(PROMPT);
+    /**
+     * @return true if the shell should continue accepting more input or false if the shell should quit
+     */
+    private boolean handleConsoleInput(ConsoleReader reader, Console console) throws IOException, CommandException {
+        String line;
+        try {
+            line = reader.readLine(PROMPT);
+        } catch (IllegalArgumentException iae) {
+            if (iae.getMessage().endsWith(": event not found")) {
+                console.getError().println(iae.getMessage());
+                return true;
+            }
+            throw iae;
+        }
         if (line == null) {
             return false;
         }
--- a/distribution/src/test/java/com/redhat/thermostat/distribution/CliTest.java	Mon Jan 07 15:51:53 2013 -0500
+++ b/distribution/src/test/java/com/redhat/thermostat/distribution/CliTest.java	Tue Jan 08 10:39:19 2013 -0500
@@ -46,6 +46,7 @@
 import org.junit.Test;
 
 import expectj.Spawn;
+import expectj.TimeoutException;
 
 /**
  * Integration tests to exercise the basics of the thermostat command line.
@@ -160,6 +161,20 @@
     }
 
     @Test
+    public void testUnrecognizedEventsInShell() throws IOException, TimeoutException {
+        // test '!' events
+        Spawn shell = spawnThermostat("shell");
+
+        shell.expect(SHELL_PROMPT);
+        shell.send("what!?!\n");
+        shell.expect(SHELL_PROMPT);
+        shell.send("exit\n");
+
+        assertTrue(shell.getCurrentStandardErrContents().contains("!?!: event not found"));
+        assertNoExceptions(shell.getCurrentStandardOutContents(), shell.getCurrentStandardErrContents());
+    }
+
+    @Test
     public void testInvalidCommand() throws Exception {
         Spawn shell = spawnThermostat("foobar", "baz");