SHL-103 Create simple way to test the execution of shell commands

This commit is contained in:
mpollack
2013-07-23 12:00:29 -04:00
parent 4f885ca2ee
commit 209fc84a70
6 changed files with 19 additions and 14 deletions

View File

@@ -39,6 +39,7 @@ dependencies {
// Testing
testCompile "junit:junit:$junitVersion"
testCompile "org.mockito:mockito-core:$mockitoVersion"
testCompile "uk.co.modular-it:hamcrest-date:$hamcrestDateVersion"
}
sourceCompatibility = 1.5

View File

@@ -14,6 +14,7 @@ jansiVersion=1.8
# Testing
junitVersion = 4.10
mockitoVersion = 1.8.5
hamcrestDateVersion = 0.9.3
# --------------------
# Project wide version

View File

@@ -66,6 +66,10 @@ public class Bootstrap {
System.exit(exitShellRequest.getExitCode());
}
public Bootstrap() {
this(null, CONTEXT_PATH);
}
public Bootstrap(String[] args) throws IOException {
this(args, CONTEXT_PATH);
}
@@ -150,7 +154,7 @@ public class Bootstrap {
}
protected ExitShellRequest run() {
public ExitShellRequest run() {
String[] commandsToExecuteAndThenQuit = commandLine.getShellCommandsToExecute();
// The shell is used
@@ -162,7 +166,7 @@ public class Bootstrap {
exitShellRequest = ExitShellRequest.FATAL_EXIT;
for (String cmd : commandsToExecuteAndThenQuit) {
successful = shell.executeCommand(cmd);
successful = shell.executeCommand(cmd).isSuccess();
if (!successful)
break;
}
@@ -191,7 +195,7 @@ public class Bootstrap {
return exitShellRequest;
}
JLineShellComponent getJLineShellComponent() {
public JLineShellComponent getJLineShellComponent() {
return ctx.getBean("shell", JLineShellComponent.class);
}
}

View File

@@ -160,10 +160,10 @@ public abstract class AbstractShell extends AbstractShellStatusPublisher impleme
* This method can be overridden by sub-classes to pre-process script lines.
*/
protected boolean executeScriptLine(final String line) {
return executeCommand(line);
return executeCommand(line).isSuccess();
}
public boolean executeCommand(String line) {
public CommandResult executeCommand(String line) {
// Another command was attempted
setShellStatus(ShellStatus.Status.PARSING);
@@ -198,7 +198,7 @@ public abstract class AbstractShell extends AbstractShellStatusPublisher impleme
}
if (inBlockComment) {
if (!line.contains("*/")) {
return true;
return new CommandResult(true);
}
blockCommentFinish();
line = line.substring(line.lastIndexOf("*/") + 2);
@@ -212,11 +212,11 @@ public abstract class AbstractShell extends AbstractShellStatusPublisher impleme
line = line.replace('\t', ' ');
if ("".equals(line.trim())) {
setShellStatus(Status.EXECUTION_SUCCESS);
return true;
return new CommandResult(true);
}
parseResult = getParser().parse(line);
if (parseResult == null) {
return false;
return new CommandResult(false);
}
setShellStatus(Status.EXECUTING);
@@ -234,14 +234,14 @@ public abstract class AbstractShell extends AbstractShellStatusPublisher impleme
logCommandIfRequired(line, true);
setShellStatus(Status.EXECUTION_SUCCESS, line, parseResult);
return true;
return new CommandResult(true, result, null);
} catch (RuntimeException e) {
setShellStatus(Status.EXECUTION_FAILED, line, parseResult);
// We rely on execution strategy to log it
try {
logCommandIfRequired(line, false);
} catch (Exception ignored) {}
return false;
return new CommandResult(false, null, e);
} finally {
setShellStatus(Status.USER_INPUT);
}
@@ -340,8 +340,7 @@ public abstract class AbstractShell extends AbstractShellStatusPublisher impleme
@CliCommand(value = { "date" }, help = "Displays the local date and time")
public String date() {
return DateFormat.getDateTimeInstance(
DateFormat.FULL, DateFormat.FULL,Locale.US)
return DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,Locale.US)
.format(new Date());
}

View File

@@ -145,7 +145,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker,
String rooArgs = System.getProperty("roo.args");
if (rooArgs != null && !"".equals(rooArgs)) {
setShellStatus(Status.USER_INPUT);
boolean success = executeCommand(rooArgs);
boolean success = executeCommand(rooArgs).isSuccess();
if (exitShellRequest == null) {
// The command itself did not specify an exit shell code, so we'll fall back to something sensible here
executeCommand("quit"); // ROO-839

View File

@@ -58,7 +58,7 @@ public interface Shell extends ShellStatusProvider, ShellPromptAccessor {
* @param line to execute (required)
* @return true if the command was successful, false if there was an exception
*/
boolean executeCommand(String line);
CommandResult executeCommand(String line);
/**
* Indicates the shell should switch into a lower-level development mode. The exact meaning varies by