Skip to content

Commit

Permalink
[grid] Reworking CLI commands to avoid direct use of System.out and S…
Browse files Browse the repository at this point in the history
…ystem.err

It is possible now to use custom streams for output. This allows to 1) redirect output programmatically, 2) write test that check output
  • Loading branch information
barancev committed Apr 13, 2020
1 parent b8ef89b commit 2728e60
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
4 changes: 3 additions & 1 deletion java/server/src/org/openqa/selenium/cli/CliCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

package org.openqa.selenium.cli;

import java.io.PrintStream;

public interface CliCommand {

String getName();

String getDescription();

Executable configure(String... args);
Executable configure(PrintStream out, PrintStream err, String... args);

default boolean isShown() {
return true;
Expand Down
45 changes: 31 additions & 14 deletions java/server/src/org/openqa/selenium/grid/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openqa.selenium.cli.WrappedPrintWriter;

import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
Expand All @@ -44,9 +45,25 @@ public class Main {

private static final Logger LOG = Logger.getLogger(Main.class.getName());

private final PrintStream out;
private final PrintStream err;
private final String[] args;

public static void main(String[] args) throws Exception {
new Main(System.out, System.err, args).go();
}

Main(PrintStream out, PrintStream err, String[] args) {
// It's not private to make it visible for tests
this.out = out;
this.err = err;
this.args = args;
}

void go() throws Exception {
// It's not private to make it visible for tests
if (args.length == 0) {
new Help(loadCommands(Main.class.getClassLoader())).configure().run();
new Help(loadCommands(Main.class.getClassLoader())).configure(out, err).run();
} else {
if ("--ext".equals(args[0])) {
if (args.length > 1) {
Expand Down Expand Up @@ -90,10 +107,10 @@ public static void main(String[] args) throws Exception {
System.arraycopy(args, 2, remainingArgs, 0, args.length - 2);
launch(remainingArgs, loader);
} else {
new Help(loadCommands(loader)).configure().run();
new Help(loadCommands(loader)).configure(out, err).run();
}
} else {
new Help(loadCommands(Main.class.getClassLoader())).configure().run();
new Help(loadCommands(Main.class.getClassLoader())).configure(out, err).run();
}

} else {
Expand All @@ -108,7 +125,7 @@ private static Set<CliCommand> loadCommands(ClassLoader loader) {
return commands;
}

private static void launch(String[] args, ClassLoader loader) throws Exception {
private void launch(String[] args, ClassLoader loader) throws Exception {
String commandName = args[0];
String[] remainingArgs = new String[args.length - 1];
System.arraycopy(args, 1, remainingArgs, 0, args.length - 1);
Expand All @@ -120,7 +137,7 @@ private static void launch(String[] args, ClassLoader loader) throws Exception {
.findFirst()
.orElse(new Help(commands));

command.configure(remainingArgs).run();
command.configure(out, err, remainingArgs).run();
}

private static class Help implements CliCommand {
Expand All @@ -143,7 +160,7 @@ public String getDescription() {
}

@Override
public Executable configure(String... args) {
public Executable configure(PrintStream out, PrintStream err, String... args) {
return () -> {
int longest = commands.stream()
.filter(CliCommand::isShown)
Expand All @@ -152,28 +169,28 @@ public Executable configure(String... args) {
.map(String::length)
.orElse(0) + 2; // two space padding either side

PrintWriter out = new WrappedPrintWriter(System.out, 72, 0);
out.append(getName()).append("\n\n");
out.append(getDescription()).append("\n").append("\n");
PrintWriter outWriter = new WrappedPrintWriter(out, 72, 0);
outWriter.append(getName()).append("\n\n");
outWriter.append(getDescription()).append("\n").append("\n");

int indent = Math.min(longest + 2, 25);
String format = " %-" + longest + "s";

PrintWriter indented = new WrappedPrintWriter(System.out, 72, indent);
PrintWriter indented = new WrappedPrintWriter(out, 72, indent);
commands.stream()
.filter(CliCommand::isShown)
.forEach(cmd -> indented.format(format, cmd.getName())
.append(cmd.getDescription())
.append("\n"));

out.write("\nFor each command, run with `--help` for command-specific help\n");
out.write("\nUse the `--ext` flag before the command name to specify an additional " +
outWriter.write("\nFor each command, run with `--help` for command-specific help\n");
outWriter.write("\nUse the `--ext` flag before the command name to specify an additional " +
"classpath to use with the server (for example, to provide additional " +
"commands, or to provide additional driver implementations). For example:\n");
out.write(String.format(
outWriter.write(String.format(
"\n java -jar selenium.jar --ext example.jar%sdir standalone --port 1234",
File.pathSeparator));
System.out.println("\n");
out.println("\n");
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.server.HelpFlags;

import java.io.PrintStream;
import java.util.LinkedHashSet;
import java.util.Set;

public abstract class TemplateGridCommand implements CliCommand {

@Override
public final Executable configure(String... args) {
public final Executable configure(PrintStream out, PrintStream err, String... args) {
Set<Object> allFlags = getFlagObjects();

HelpFlags helpFlags = new HelpFlags();
Expand All @@ -52,12 +53,12 @@ public final Executable configure(String... args) {
try {
commander.parse(args);
} catch (ParameterException e) {
System.err.println(e.getMessage());
err.println(e.getMessage());
commander.usage();
return;
}

if (helpFlags.displayHelp(commander, System.out)) {
if (helpFlags.displayHelp(commander, out)) {
return;
}

Expand All @@ -70,7 +71,7 @@ public final Executable configure(String... args) {

Config config = new CompoundConfig(allConfigs.toArray(new Config[0]));

if (helpFlags.dumpConfig(config, System.out)) {
if (helpFlags.dumpConfig(config, out)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import org.openqa.selenium.cli.CliCommand;
Expand All @@ -33,7 +34,6 @@
import java.io.PrintWriter;
import java.util.Collections;


@AutoService(CliCommand.class)
public class InfoCommand implements CliCommand {

Expand All @@ -45,7 +45,7 @@ public String getDescription() {
return "Prints information for commands and topics.";
}

public Executable configure(String... args) {
public Executable configure(PrintStream out, PrintStream err, String... args) {
HelpFlags help = new HelpFlags();
InfoFlags topic = new InfoFlags();

Expand All @@ -59,12 +59,12 @@ public Executable configure(String... args) {
try {
commander.parse(args);
} catch (ParameterException e) {
System.err.println(e.getMessage());
err.println(e.getMessage());
commander.usage();
return;
}

if (help.displayHelp(commander, System.out)) {
if (help.displayHelp(commander, out)) {
return;
}

Expand Down Expand Up @@ -92,11 +92,11 @@ public Executable configure(String... args) {
String path = getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + toDisplay;
String content = readContent(path);

PrintWriter out = new WrappedPrintWriter(System.out, 72, 0);
PrintWriter outWriter = new WrappedPrintWriter(out, 72, 0);

out.printf("\n%s\n%s\n\n", title, String.join("", Collections.nCopies(title.length(), "=")));
out.print(content);
out.println("\n\n");
outWriter.printf("\n%s\n%s\n\n", title, String.join("", Collections.nCopies(title.length(), "=")));
outWriter.print(content);
outWriter.println("\n\n");
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import java.util.Set;
import java.util.logging.Logger;


@AutoService(CliCommand.class)
public class DistributorServer extends TemplateGridCommand {

Expand Down

0 comments on commit 2728e60

Please sign in to comment.