Skip to content

Commit

Permalink
Add --add-plugin option and clobber when --plugin is used
Browse files Browse the repository at this point in the history
When using --plugin the plugins of that type defined from a different
source is clobbered. Using -Dcucumber.options="--plugin pretty" would
clobber the formatter/reporter plugins defined in `@CucumberOptions`.
--add-plugin does not clobber any plugins defined from a different
source.
  • Loading branch information
brasmusson committed Apr 28, 2015
1 parent d6ef4f8 commit 0ac2df0
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 15 deletions.
73 changes: 59 additions & 14 deletions core/src/main/java/cucumber/runtime/RuntimeOptions.java
Expand Up @@ -96,6 +96,7 @@ private void parse(List<String> args) {
List<Object> parsedFilters = new ArrayList<Object>();
List<String> parsedFeaturePaths = new ArrayList<String>();
List<String> parsedGlue = new ArrayList<String>();
ParsedPluginData parsedPluginData = new ParsedPluginData();

while (!args.isEmpty()) {
String arg = args.remove(0).trim();
Expand All @@ -114,11 +115,11 @@ private void parse(List<String> args) {
parsedGlue.add(gluePath);
} else if (arg.equals("--tags") || arg.equals("-t")) {
parsedFilters.add(args.remove(0));
} else if (arg.equals("--plugin") || arg.equals("-p")) {
addPluginName(args.remove(0));
} else if (arg.equals("--plugin") || arg.equals("--add-plugin") || arg.equals("-p")) {
parsedPluginData.addPluginName(args.remove(0), arg.equals("--add-plugin"));
} else if (arg.equals("--format") || arg.equals("-f")) {
System.err.println("WARNING: Cucumber-JVM's --format option is deprecated. Please use --plugin instead.");
addPluginName(args.remove(0));
parsedPluginData.addPluginName(args.remove(0), true);
} else if (arg.equals("--no-dry-run") || arg.equals("--dry-run") || arg.equals("-d")) {
dryRun = !arg.startsWith("--no-");
} else if (arg.equals("--no-strict") || arg.equals("--strict") || arg.equals("-s")) {
Expand Down Expand Up @@ -150,22 +151,15 @@ private void parse(List<String> args) {
featurePaths.clear();
featurePaths.addAll(parsedFeaturePaths);
}

if (!parsedGlue.isEmpty()) {
glue.clear();
glue.addAll(parsedGlue);
}
}

private void addPluginName(String name) {
if (PluginFactory.isFormatterName(name)) {
pluginFormatterNames.add(name);
} else if (PluginFactory.isStepDefinitionResporterName(name)) {
pluginStepDefinitionReporterNames.add(name);
} else if (PluginFactory.isSummaryPrinterName(name)) {
pluginSummaryPrinterNames.add(name);
} else {
throw new CucumberException("Unrecognized plugin: " + name);
}
parsedPluginData.updatePluginFormatterNames(pluginFormatterNames);
parsedPluginData.updatePluginStepDefinitionReporterNames(pluginStepDefinitionReporterNames);
parsedPluginData.updatePluginSummaryPrinterNames(pluginSummaryPrinterNames);
}

private boolean haveLineFilters(List<String> parsedFeaturePaths) {
Expand Down Expand Up @@ -332,3 +326,54 @@ public SnippetType getSnippetType() {
return snippetType;
}
}

class ParsedPluginData {
ParsedOptionNames formatterNames = new ParsedOptionNames();
ParsedOptionNames stepDefinitionReporterNames = new ParsedOptionNames();
ParsedOptionNames summaryPrinterNames = new ParsedOptionNames();

public void addPluginName(String name, boolean isAddPlugin) {
if (PluginFactory.isFormatterName(name)) {
formatterNames.addName(name, isAddPlugin);
} else if (PluginFactory.isStepDefinitionResporterName(name)) {
stepDefinitionReporterNames.addName(name, isAddPlugin);
} else if (PluginFactory.isSummaryPrinterName(name)) {
summaryPrinterNames.addName(name, isAddPlugin);
} else {
throw new CucumberException("Unrecognized plugin: " + name);
}
}

public void updatePluginFormatterNames(List<String> pluginFormatterNames) {
formatterNames.updateNameList(pluginFormatterNames);
}

public void updatePluginStepDefinitionReporterNames(List<String> pluginStepDefinitionReporterNames) {
stepDefinitionReporterNames.updateNameList(pluginStepDefinitionReporterNames);
}

public void updatePluginSummaryPrinterNames(List<String> pluginSummaryPrinterNames) {
summaryPrinterNames.updateNameList(pluginSummaryPrinterNames);
}
}

class ParsedOptionNames {
private List<String> names = new ArrayList<String>();
private boolean clobber = false;

public void addName(String name, boolean isAddOption) {
names.add(name);
if (!isAddOption) {
clobber = true;
}
}

public void updateNameList(List<String> nameList) {
if (!names.isEmpty()) {
if (clobber) {
nameList.clear();
}
nameList.addAll(names);
}
}
}
5 changes: 4 additions & 1 deletion core/src/main/resources/cucumber/api/cli/USAGE.txt
Expand Up @@ -4,13 +4,16 @@ Options:

-g, --glue PATH Where glue code (step definitions, hooks
and plugins) are loaded from.
-p, --plugin PLUGIN[:PATH_OR_URL] Register a plugin.
-p, --[add-]plugin PLUGIN[:PATH_OR_URL]
Register a plugin.
Built-in formatter PLUGIN types: junit,
html, pretty, progress, json, usage, rerun,
testng. Built-in summary PLUGIN types:
default_summary, null_summary. PLUGIN can
also be a fully qualified class name, allowing
registration of 3rd party plugins.
--add-plugin does not clobber plugins of that
type defined from a different source.
-f, --format FORMAT[:PATH_OR_URL] Deprecated. Use --plugin instead.
-t, --tags TAG_EXPRESSION Only run scenarios tagged with tags matching
TAG_EXPRESSION.
Expand Down
45 changes: 45 additions & 0 deletions core/src/test/java/cucumber/runtime/RuntimeOptionsTest.java
Expand Up @@ -244,6 +244,51 @@ public void clobbers_line_filters_from_cli_if_features_specified_in_cucumber_opt
assertEquals(asList("@keep_this"), runtimeOptions.getFilters());
}

@Test
public void clobbers_formatter_plugins_from_cli_if_formatters_specified_in_cucumber_options_property() {
Properties properties = new Properties();
properties.setProperty("cucumber.options", "--plugin pretty");
RuntimeOptions options = new RuntimeOptions(new Env(properties), asList("--plugin", "html:some/dir", "--glue", "somewhere"));
assertPluginExists(options.getPlugins(), "cucumber.runtime.formatter.CucumberPrettyFormatter");
assertPluginNotExists(options.getPlugins(), "cucumber.runtime.formatter.HTMLFormatter");
}

@Test
public void adds_to_formatter_plugins_with_add_plugin_option() {
Properties properties = new Properties();
properties.setProperty("cucumber.options", "--add-plugin pretty");
RuntimeOptions options = new RuntimeOptions(new Env(properties), asList("--plugin", "html:some/dir", "--glue", "somewhere"));
assertPluginExists(options.getPlugins(), "cucumber.runtime.formatter.HTMLFormatter");
assertPluginExists(options.getPlugins(), "cucumber.runtime.formatter.CucumberPrettyFormatter");
}

@Test
public void clobbers_summary_plugins_from_cli_if_summary_printer_specified_in_cucumber_options_property() {
Properties properties = new Properties();
properties.setProperty("cucumber.options", "--plugin default_summary");
RuntimeOptions options = new RuntimeOptions(new Env(properties), asList("--plugin", "null_summary", "--glue", "somewhere"));
assertPluginExists(options.getPlugins(), "cucumber.runtime.DefaultSummaryPrinter");
assertPluginNotExists(options.getPlugins(), "cucumber.runtime.NullSummaryPrinter");
}

@Test
public void adds_to_summary_plugins_with_add_plugin_option() {
Properties properties = new Properties();
properties.setProperty("cucumber.options", "--add-plugin default_summary");
RuntimeOptions options = new RuntimeOptions(new Env(properties), asList("--plugin", "null_summary", "--glue", "somewhere"));
assertPluginExists(options.getPlugins(), "cucumber.runtime.NullSummaryPrinter");
assertPluginExists(options.getPlugins(), "cucumber.runtime.DefaultSummaryPrinter");
}

@Test
public void does_not_clobber_plugins_of_different_type_when_specifying_plugins_in_cucumber_options_property() {
Properties properties = new Properties();
properties.setProperty("cucumber.options", "--plugin default_summary");
RuntimeOptions options = new RuntimeOptions(new Env(properties), asList("--plugin", "pretty", "--glue", "somewhere"));
assertPluginExists(options.getPlugins(), "cucumber.runtime.formatter.CucumberPrettyFormatter");
assertPluginExists(options.getPlugins(), "cucumber.runtime.DefaultSummaryPrinter");
}

@Test
public void allows_removal_of_strict_in_cucumber_options_property() {
Properties properties = new Properties();
Expand Down

0 comments on commit 0ac2df0

Please sign in to comment.