Skip to content

Commit

Permalink
fix root command unregister (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
derklaro authored Aug 26, 2022
1 parent 9a298d0 commit f1da897
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ public void deleteRootCommand(final @NonNull String rootCommand) throws CloudCap
this.commandRegistrationHandler.unregisterRootCommand((StaticArgument<?>) node.getValue());

// We then delete it from the tree.
this.commandTree.deleteRecursively(node);
this.commandTree.deleteRecursively(node, true);

// And lastly we re-build the entire tree.
this.commandTree.verifyAndRegister();
Expand Down
17 changes: 8 additions & 9 deletions cloud-core/src/main/java/cloud/commandframework/CommandTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -952,23 +952,22 @@ private void checkAmbiguity(final @NonNull Node<@Nullable CommandArgument<C, ?>>
return null;
}

void deleteRecursively(final @NonNull Node<@Nullable CommandArgument<C, ?>> node) {
void deleteRecursively(final @NonNull Node<@Nullable CommandArgument<C, ?>> node, final boolean root) {
for (final Node<@Nullable CommandArgument<C, ?>> child : new ArrayList<>(node.children)) {
this.deleteRecursively(child);
this.deleteRecursively(child, false);
}

// We need to remove it from the tree.
this.removeNode(node);
this.removeNode(node, root);
}

private boolean removeNode(final @NonNull Node<@Nullable CommandArgument<C, ?>> node) {
if (this.getRootNodes().contains(node)) {
this.internalTree.removeChild(node);
private boolean removeNode(final @NonNull Node<@Nullable CommandArgument<C, ?>> node, final boolean root) {
if (root) {
// root command node - remove it from the root tree
return this.internalTree.removeChild(node);
} else {
// child node - remove it from the parent node
return node.getParent().removeChild(node);
}

return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,29 @@ void deleteIntermediateCommand() {

assertThat(this.commandManager.commandTree().getRootNodes()).isEmpty();
}

@Test
void deleteCommandWithSameArgumentNameAsRootCommand() {
// Arrange
this.commandManager.command(this.commandManager.commandBuilder("test").build());
this.commandManager.command(this.commandManager.commandBuilder("hello").literal("test").build());

// Pre-assert.
this.commandManager.executeCommand(new TestCommandSender(), "test").join();
this.commandManager.executeCommand(new TestCommandSender(), "hello test").join();

// Act
this.commandManager.deleteRootCommand("hello");

// Assert
this.commandManager.executeCommand(new TestCommandSender(), "test").join();
final CompletionException completionException = assertThrows(
CompletionException.class,
() -> this.commandManager.executeCommand(new TestCommandSender(), "hello").join()
);
assertThat(completionException).hasCauseThat().isInstanceOf(NoSuchCommandException.class);

assertThat(this.commandManager.suggest(new TestCommandSender(), "")).contains("test");
assertThat(this.commandManager.commandTree().getRootNodes()).hasSize(1);
}
}

0 comments on commit f1da897

Please sign in to comment.