# HG changeset patch # User Omair Majid # Date 1357659559 18000 # Node ID 72638d2844fa7e8e005fa32e27de6b12de48d868 # Parent 3cc2f2ea31b35b2c4b6e5831966b0bc62d20bdde 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 diff -r 3cc2f2ea31b3 -r 72638d2844fa client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ShellCommand.java --- 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; } diff -r 3cc2f2ea31b3 -r 72638d2844fa distribution/src/test/java/com/redhat/thermostat/distribution/CliTest.java --- 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");