Skip to content

Commit

Permalink
CLEAN TEST move subcommand-related tests from HelpTest to HelpSubComm…
Browse files Browse the repository at this point in the history
…andTest
  • Loading branch information
remkop committed May 30, 2020
1 parent e742964 commit be8d239
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 193 deletions.
197 changes: 197 additions & 0 deletions src/test/java/picocli/HelpSubCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashSet;

import static java.lang.String.format;
import static org.junit.Assert.*;
import static picocli.CommandLine.*;
import static picocli.CommandLine.Model.*;
Expand Down Expand Up @@ -204,4 +208,197 @@ public void run() { }
assertEquals(String.format("Hi, colorScheme.ansi is OFF%n"), systemOutRule.getLog());
assertEquals(String.format("Hello, colorScheme.ansi is OFF%n"), systemErrRule.getLog());
}
@Test
public void testHelpSubcommandWithValidCommand() {
@Command(subcommands = {HelpTest.Sub.class, HelpCommand.class})
class App implements Runnable{ public void run(){}}

StringWriter sw = new StringWriter();
new CommandLine(new App())
.setOut(new PrintWriter(sw))
.setColorScheme(Help.defaultColorScheme(Help.Ansi.OFF))
.execute("help", "sub");

String expected = String.format("" +
"Usage: <main class> sub%n" +
"This is a subcommand%n");
assertEquals(expected, sw.toString());
}

@Test
public void testHelpSubcommandWithInvalidCommand() {
@Command(mixinStandardHelpOptions = true, subcommands = {HelpTest.Sub.class, HelpCommand.class})
class App implements Runnable{ public void run(){}}

StringWriter sw = new StringWriter();
new CommandLine(new App())
.setErr(new PrintWriter(sw))
.setColorScheme(Help.defaultColorScheme(Help.Ansi.OFF))
.execute("help", "abcd");

String expected = String.format("" +
"Unknown subcommand 'abcd'.%n" +
"Usage: <main class> [-hV] [COMMAND]%n" +
" -h, --help Show this help message and exit.%n" +
" -V, --version Print version information and exit.%n" +
"Commands:%n" +
" sub This is a subcommand%n" +
" help Displays help information about the specified command%n");
assertEquals(expected, sw.toString());
}

@Test
public void testHelpSubcommandWithHelpOption() {
@Command(subcommands = {HelpTest.Sub.class, HelpCommand.class})
class App implements Runnable{ public void run(){}}

StringWriter sw = new StringWriter();
new CommandLine(new App())
.setOut(new PrintWriter(sw))
.setColorScheme(Help.defaultColorScheme(Help.Ansi.OFF))
.execute("help", "-h");

String expected = String.format("" +
"Displays help information about the specified command%n" +
"%n" +
"Usage: <main class> help [-h] [COMMAND...]%n" +
"%n" +
"When no COMMAND is given, the usage help for the main command is displayed.%n" +
"If a COMMAND is specified, the help for that command is shown.%n" +
"%n" +
" [COMMAND...] The COMMAND to display the usage help message for.%n" +
" -h, --help Show usage help for the help command and exit.%n");
assertEquals(expected, sw.toString());

sw = new StringWriter();
new CommandLine(new App()).getSubcommands().get("help").usage(new PrintWriter(sw));
assertEquals(expected, sw.toString());
}

@Test
public void testHelpSubcommandWithoutCommand() {
@Command(mixinStandardHelpOptions = true, subcommands = {HelpTest.Sub.class, HelpCommand.class})
class App implements Runnable{ public void run(){}}

StringWriter sw = new StringWriter();
new CommandLine(new App())
.setOut(new PrintWriter(sw))
.setColorScheme(Help.defaultColorScheme(Help.Ansi.OFF))
.execute("help");

String expected = String.format("" +
"Usage: <main class> [-hV] [COMMAND]%n" +
" -h, --help Show this help message and exit.%n" +
" -V, --version Print version information and exit.%n" +
"Commands:%n" +
" sub This is a subcommand%n" +
" help Displays help information about the specified command%n");
assertEquals(expected, sw.toString());

sw = new StringWriter();
new CommandLine(new App()).usage(new PrintWriter(sw), Help.Ansi.OFF);
assertEquals(expected, sw.toString());
}

@Test
public void testHelpSubcommandRunDoesNothingIfParentNotSet() {
HelpCommand cmd = new HelpCommand();
cmd.run();
assertEquals("", this.systemOutRule.getLog());
}

@SuppressWarnings({"deprecation"})
@Test
public void testHelpSubcommandRunPrintsParentUsageIfParentSet() {
HelpCommand cmd = new HelpCommand();
CommandLine help = new CommandLine(cmd);
CommandSpec spec = CommandSpec.create().name("parent");
spec.usageMessage().description("the parent command");
spec.addSubcommand("parent", help);
new CommandLine(spec); // make sure parent spec has a CommandLine

cmd.init(help, Help.Ansi.OFF, System.out, System.err);
cmd.run();
String expected = String.format("" +
"Usage: parent [COMMAND]%n" +
"the parent command%n" +
"Commands:%n" +
" parent Displays help information about the specified command%n");
assertEquals(expected, this.systemOutRule.getLog());
}

@Test
public void testHelpSubcommand2RunPrintsParentUsageIfParentSet() {
HelpCommand cmd = new HelpCommand();
CommandLine help = new CommandLine(cmd);
CommandSpec spec = CommandSpec.create().name("parent");
spec.usageMessage().description("the parent command");
spec.addSubcommand("parent", help);
new CommandLine(spec); // make sure parent spec has a CommandLine

cmd.init(help, Help.defaultColorScheme(Help.Ansi.OFF), new PrintWriter(System.out), new PrintWriter(System.err));
cmd.run();
String expected = String.format("" +
"Usage: parent [COMMAND]%n" +
"the parent command%n" +
"Commands:%n" +
" parent Displays help information about the specified command%n");
assertEquals(expected, this.systemOutRule.getLog());
}

@Test
public void testUsageHelpForNestedSubcommands() {
@Command(name = "subsub", mixinStandardHelpOptions = true) class SubSub { }
@Command(name = "sub", subcommands = {SubSub.class}) class Sub { }
@Command(name = "main", subcommands = {Sub.class}) class App { }

CommandLine app = new CommandLine(new App(), new InnerClassFactory(this));
//ParseResult result = app.parseArgs("sub", "subsub", "--help");
//CommandLine.printHelpIfRequested(result);
CommandLine subsub = app.getSubcommands().get("sub").getSubcommands().get("subsub");

ByteArrayOutputStream baos = new ByteArrayOutputStream();
subsub.usage(new PrintStream(baos), Help.Ansi.OFF);

String expected = String.format("" +
"Usage: main sub subsub [-hV]%n" +
" -h, --help Show this help message and exit.%n" +
" -V, --version Print version information and exit.%n");
assertEquals(expected, baos.toString());
}

@Test
public void testUsageTextWithHiddenSubcommand() {
@Command(name = "foo", description = "This is a visible subcommand") class Foo { }
@Command(name = "bar", description = "This is a hidden subcommand", hidden = true) class Bar { }
@Command(name = "app", subcommands = {Foo.class, Bar.class}) class App { }

CommandLine app = new CommandLine(new App(), new InnerClassFactory(this));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
app.usage(new PrintStream(baos));

String expected = format("" +
"Usage: app [COMMAND]%n" +
"Commands:%n" +
" foo This is a visible subcommand%n");
assertEquals(expected, baos.toString());
}

@Test
public void testUsage_NoHeaderIfAllSubcommandHidden() {
@Command(name = "foo", description = "This is a foo sub-command", hidden = true) class Foo { }
@Command(name = "bar", description = "This is a foo sub-command", hidden = true) class Bar { }
@Command(name = "app", abbreviateSynopsis = true) class App { }

CommandLine app = new CommandLine(new App())
.addSubcommand("foo", new Foo())
.addSubcommand("bar", new Bar());

ByteArrayOutputStream baos = new ByteArrayOutputStream();
app.usage(new PrintStream(baos));

String expected = format("" +
"Usage: app [COMMAND]%n");
assertEquals(expected, baos.toString());
}
}
Loading

0 comments on commit be8d239

Please sign in to comment.