Skip to content

Commit f1da897

Browse files
authored
fix root command unregister (#389)
1 parent 9a298d0 commit f1da897

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

cloud-core/src/main/java/cloud/commandframework/CommandManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public void deleteRootCommand(final @NonNull String rootCommand) throws CloudCap
545545
this.commandRegistrationHandler.unregisterRootCommand((StaticArgument<?>) node.getValue());
546546

547547
// We then delete it from the tree.
548-
this.commandTree.deleteRecursively(node);
548+
this.commandTree.deleteRecursively(node, true);
549549

550550
// And lastly we re-build the entire tree.
551551
this.commandTree.verifyAndRegister();

cloud-core/src/main/java/cloud/commandframework/CommandTree.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -952,23 +952,22 @@ private void checkAmbiguity(final @NonNull Node<@Nullable CommandArgument<C, ?>>
952952
return null;
953953
}
954954

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

960-
// We need to remove it from the tree.
961-
this.removeNode(node);
960+
this.removeNode(node, root);
962961
}
963962

964-
private boolean removeNode(final @NonNull Node<@Nullable CommandArgument<C, ?>> node) {
965-
if (this.getRootNodes().contains(node)) {
966-
this.internalTree.removeChild(node);
963+
private boolean removeNode(final @NonNull Node<@Nullable CommandArgument<C, ?>> node, final boolean root) {
964+
if (root) {
965+
// root command node - remove it from the root tree
966+
return this.internalTree.removeChild(node);
967967
} else {
968+
// child node - remove it from the parent node
968969
return node.getParent().removeChild(node);
969970
}
970-
971-
return false;
972971
}
973972

974973
/**

cloud-core/src/test/java/cloud/commandframework/CommandDeletionTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,29 @@ void deleteIntermediateCommand() {
146146

147147
assertThat(this.commandManager.commandTree().getRootNodes()).isEmpty();
148148
}
149+
150+
@Test
151+
void deleteCommandWithSameArgumentNameAsRootCommand() {
152+
// Arrange
153+
this.commandManager.command(this.commandManager.commandBuilder("test").build());
154+
this.commandManager.command(this.commandManager.commandBuilder("hello").literal("test").build());
155+
156+
// Pre-assert.
157+
this.commandManager.executeCommand(new TestCommandSender(), "test").join();
158+
this.commandManager.executeCommand(new TestCommandSender(), "hello test").join();
159+
160+
// Act
161+
this.commandManager.deleteRootCommand("hello");
162+
163+
// Assert
164+
this.commandManager.executeCommand(new TestCommandSender(), "test").join();
165+
final CompletionException completionException = assertThrows(
166+
CompletionException.class,
167+
() -> this.commandManager.executeCommand(new TestCommandSender(), "hello").join()
168+
);
169+
assertThat(completionException).hasCauseThat().isInstanceOf(NoSuchCommandException.class);
170+
171+
assertThat(this.commandManager.suggest(new TestCommandSender(), "")).contains("test");
172+
assertThat(this.commandManager.commandTree().getRootNodes()).hasSize(1);
173+
}
149174
}

0 commit comments

Comments
 (0)