Fix empty command chain causing NullPointerException (#1931)

* Fix empty command chain causing NullPointerException

* Add test for empty command input
This commit is contained in:
Spanner 2023-09-01 21:14:18 +00:00 committed by GitHub
parent 2b8beadd3e
commit 3645d4311d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View File

@ -101,18 +101,19 @@ final class CommandParserImpl implements CommandParser {
NodeResult result = parseNode(parent, chain, reader); NodeResult result = parseNode(parent, chain, reader);
chain = result.chain; chain = result.chain;
if (result.argumentResult instanceof ArgumentResult.Success<?>) {
NodeResult lastNodeResult = chain.nodeResults.peekLast(); NodeResult lastNodeResult = chain.nodeResults.peekLast();
if (lastNodeResult == null) return UnknownCommandResult.INSTANCE;
Node lastNode = lastNodeResult.node; Node lastNode = lastNodeResult.node;
if (result.argumentResult instanceof ArgumentResult.Success<?>) {
CommandExecutor executor = nullSafeGetter(lastNode.execution(), Graph.Execution::executor); CommandExecutor executor = nullSafeGetter(lastNode.execution(), Graph.Execution::executor);
if (executor != null) return ValidCommand.executor(input, chain, executor); if (executor != null) return ValidCommand.executor(input, chain, executor);
} }
// If here, then the command failed or didn't have an executor // If here, then the command failed or didn't have an executor
// Look for a default executor, or give up if we got nowhere // Look for a default executor, or give up if we got nowhere
NodeResult lastNode = chain.nodeResults.peekLast(); if (lastNode.equals(parent)) return UnknownCommandResult.INSTANCE;
if (lastNode.node.equals(parent)) return UnknownCommandResult.INSTANCE;
if (chain.defaultExecutor != null) { if (chain.defaultExecutor != null) {
return ValidCommand.defaultExecutor(input, chain); return ValidCommand.defaultExecutor(input, chain);
} }

View File

@ -13,6 +13,12 @@ import static org.junit.jupiter.api.Assertions.*;
public class CommandParseTest { public class CommandParseTest {
@Test
public void emptyCommand() {
var graph = Graph.merge(Graph.builder(Literal("foo"), createExecutor(new AtomicBoolean())).build());
assertUnknown(graph, "");
}
@Test @Test
public void singleParameterlessCommand() { public void singleParameterlessCommand() {
final AtomicBoolean b = new AtomicBoolean(); final AtomicBoolean b = new AtomicBoolean();