-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33304 from iocanel/fix-cli-plugin-args
Fix argument parsing for CLI plugins
- Loading branch information
Showing
3 changed files
with
263 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
devtools/cli/src/test/java/io/quarkus/cli/plugin/PluginCommandFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package io.quarkus.cli.plugin; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import picocli.CommandLine; | ||
|
||
public class PluginCommandFactoryTest { | ||
|
||
final PluginCommandFactory factory = new PluginCommandFactory(); | ||
|
||
@Test | ||
void testNoArgs() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)); | ||
//when | ||
commandLine.execute(new String[0]); | ||
//thew | ||
assertEquals(0, rootCommand.getArguments().size()); | ||
} | ||
|
||
@Test | ||
void testSingleArg() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)); | ||
//when | ||
commandLine.execute("hello"); | ||
//then | ||
assertEquals("hello", rootCommand.getArguments().get(0)); | ||
} | ||
|
||
@Test | ||
void testMultiArgs() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)); | ||
//when | ||
commandLine.execute("one", "two", "three"); | ||
//then | ||
assertEquals("one", rootCommand.getArguments().get(0)); | ||
assertEquals("two", rootCommand.getArguments().get(1)); | ||
assertEquals("three", rootCommand.getArguments().get(2)); | ||
} | ||
|
||
@Test | ||
void testMultiArgsAndOptions() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)); | ||
//when | ||
commandLine.execute("one", "two", "three", "--depth", "5"); | ||
//then | ||
assertEquals("one", rootCommand.getArguments().get(0)); | ||
assertEquals("two", rootCommand.getArguments().get(1)); | ||
assertEquals("three", rootCommand.getArguments().get(2)); | ||
assertEquals("--depth", rootCommand.getArguments().get(3)); | ||
assertEquals("5", rootCommand.getArguments().get(4)); | ||
} | ||
|
||
@Test | ||
void testSubCommandNoArgs() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
TestCommand subCommand = new TestCommand(); | ||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)) | ||
.addSubcommand("sub", new CommandLine(factory.createCommandSpec("sub command").apply(subCommand))); | ||
|
||
//when | ||
commandLine.execute(new String[0]); | ||
//thew | ||
assertEquals(0, rootCommand.getArguments().size()); | ||
assertEquals(0, subCommand.getArguments().size()); | ||
} | ||
|
||
@Test | ||
void testSubSingleArg() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
TestCommand subCommand = new TestCommand(); | ||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)) | ||
.addSubcommand("sub", new CommandLine(factory.createCommandSpec("sub command").apply(subCommand))); | ||
|
||
//when | ||
commandLine.execute("sub", "hello"); | ||
//then | ||
assertEquals("hello", subCommand.getArguments().get(0)); | ||
} | ||
|
||
@Test | ||
void testSubMultiArgs() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
TestCommand subCommand = new TestCommand(); | ||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)) | ||
.addSubcommand("sub", new CommandLine(factory.createCommandSpec("sub command").apply(subCommand))); | ||
//when | ||
commandLine.execute("sub", "one", "two", "three"); | ||
//then | ||
assertEquals("one", subCommand.getArguments().get(0)); | ||
assertEquals("two", subCommand.getArguments().get(1)); | ||
assertEquals("three", subCommand.getArguments().get(2)); | ||
} | ||
|
||
@Test | ||
void testSubMultiArgsAndOptions() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
TestCommand subCommand = new TestCommand(); | ||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)) | ||
.addSubcommand("sub", new CommandLine(factory.createCommandSpec("sub command").apply(subCommand))); | ||
//when | ||
commandLine.execute("sub", "one", "two", "three", "--depth", "5"); | ||
//then | ||
assertEquals("one", subCommand.getArguments().get(0)); | ||
assertEquals("two", subCommand.getArguments().get(1)); | ||
assertEquals("three", subCommand.getArguments().get(2)); | ||
assertEquals("--depth", subCommand.getArguments().get(3)); | ||
assertEquals("5", subCommand.getArguments().get(4)); | ||
} | ||
|
||
@Test | ||
void testSecLevelSubCommandNoArgs() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
TestCommand subCommand = new TestCommand(); | ||
TestCommand secondLevelSubCommand = new TestCommand(); | ||
|
||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)) | ||
.addSubcommand("sub", new CommandLine(factory.createCommandSpec("sub command").apply(subCommand)) | ||
.addSubcommand("sec-sub", new CommandLine( | ||
factory.createCommandSpec("secon level sub command").apply(secondLevelSubCommand)))); | ||
|
||
//when | ||
commandLine.execute(new String[0]); | ||
//thew | ||
assertEquals(0, rootCommand.getArguments().size()); | ||
assertEquals(0, subCommand.getArguments().size()); | ||
assertEquals(0, secondLevelSubCommand.getArguments().size()); | ||
} | ||
|
||
@Test | ||
void testSecLevelSubSingleArg() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
TestCommand subCommand = new TestCommand(); | ||
TestCommand secondLevelSubCommand = new TestCommand(); | ||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)) | ||
.addSubcommand("sub", new CommandLine(factory.createCommandSpec("sub command").apply(subCommand)) | ||
.addSubcommand("sec-sub", new CommandLine( | ||
factory.createCommandSpec("secon level sub command").apply(secondLevelSubCommand)))); | ||
|
||
//when | ||
commandLine.execute("sub", "sec-sub", "hello"); | ||
//then | ||
assertEquals("hello", secondLevelSubCommand.getArguments().get(0)); | ||
} | ||
|
||
@Test | ||
void testSecLevelSubMultiArgs() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
TestCommand subCommand = new TestCommand(); | ||
TestCommand secondLevelSubCommand = new TestCommand(); | ||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)) | ||
.addSubcommand("sub", new CommandLine(factory.createCommandSpec("sub command").apply(subCommand)) | ||
.addSubcommand("sec-sub", new CommandLine( | ||
factory.createCommandSpec("secon level sub command").apply(secondLevelSubCommand)))); | ||
//when | ||
commandLine.execute("sub", "sec-sub", "one", "two", "three"); | ||
//then | ||
assertEquals("one", secondLevelSubCommand.getArguments().get(0)); | ||
assertEquals("two", secondLevelSubCommand.getArguments().get(1)); | ||
assertEquals("three", secondLevelSubCommand.getArguments().get(2)); | ||
} | ||
|
||
@Test | ||
void testSecLevelSubMultiArgsAndOptions() { | ||
//given | ||
TestCommand rootCommand = new TestCommand(); | ||
TestCommand subCommand = new TestCommand(); | ||
TestCommand secondLevelSubCommand = new TestCommand(); | ||
CommandLine commandLine = new CommandLine(factory.createCommandSpec("rootCommand command").apply(rootCommand)) | ||
.addSubcommand("sub", new CommandLine(factory.createCommandSpec("sub command").apply(subCommand)) | ||
.addSubcommand("sec-sub", new CommandLine( | ||
factory.createCommandSpec("secon level sub command").apply(secondLevelSubCommand)))); | ||
//when | ||
commandLine.execute("sub", "sec-sub", "one", "two", "three", "--depth", "5"); | ||
//then | ||
assertEquals("one", secondLevelSubCommand.getArguments().get(0)); | ||
assertEquals("two", secondLevelSubCommand.getArguments().get(1)); | ||
assertEquals("three", secondLevelSubCommand.getArguments().get(2)); | ||
assertEquals("--depth", secondLevelSubCommand.getArguments().get(3)); | ||
assertEquals("5", secondLevelSubCommand.getArguments().get(4)); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
devtools/cli/src/test/java/io/quarkus/cli/plugin/TestCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.quarkus.cli.plugin; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import io.quarkus.cli.common.OutputOptionMixin; | ||
import picocli.CommandLine; | ||
import picocli.CommandLine.Command; | ||
|
||
@Command | ||
public class TestCommand implements PluginCommand { | ||
|
||
private static final String DEFAULT_CMD = "cmd"; | ||
|
||
private final String cmd; | ||
private final List<String> arguments = new ArrayList<>(); | ||
|
||
public TestCommand() { | ||
this(DEFAULT_CMD); | ||
} | ||
|
||
public TestCommand(String cmd) { | ||
this.cmd = cmd; | ||
} | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
System.out.println("Calling " + cmd + " " + String.join(" ", arguments)); | ||
return CommandLine.ExitCode.OK; | ||
} | ||
|
||
@Override | ||
public List<String> getCommand() { | ||
return List.of(cmd); | ||
} | ||
|
||
@Override | ||
public List<String> getArguments() { | ||
return arguments; | ||
} | ||
|
||
@Override | ||
public void useArguments(List<String> arguments) { | ||
this.arguments.clear(); | ||
this.arguments.addAll(arguments); | ||
} | ||
|
||
@Override | ||
public OutputOptionMixin getOutput() { | ||
return null; | ||
} | ||
} |