Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extraGlue option to @CucumberOptions #1439

Merged
merged 1 commit into from Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/src/main/java/cucumber/api/CucumberOptions.java
Expand Up @@ -31,6 +31,11 @@
*/
String[] glue() default {};

/**
* @return where to look for glue code (stepdefs and hooks), in addition to default search path
*/
String[] extraGlue() default {};

/**
* @return what tags in the features should be executed
*/
Expand Down
28 changes: 21 additions & 7 deletions core/src/main/java/cucumber/runtime/RuntimeOptionsFactory.java
@@ -1,7 +1,6 @@
package cucumber.runtime;

import cucumber.api.CucumberOptions;
import cucumber.runtime.formatter.PluginFactory;
import cucumber.runtime.io.MultiLoader;

import java.util.ArrayList;
Expand All @@ -13,7 +12,7 @@
public class RuntimeOptionsFactory {
private final Class clazz;
private boolean featuresSpecified = false;
private boolean glueSpecified = false;
private boolean overridingGlueSpecified = false;

public RuntimeOptionsFactory(Class clazz) {
this.clazz = clazz;
Expand Down Expand Up @@ -43,7 +42,7 @@ private List<String> buildArgsFromOptions() {
}
}
addDefaultFeaturePathIfNoFeaturePathIsSpecified(args, clazz);
addDefaultGlueIfNoGlueIsSpecified(args, clazz);
addDefaultGlueIfNoOverridingGlueIsSpecified(args, clazz);
return args;
}

Expand Down Expand Up @@ -101,15 +100,30 @@ private void addDefaultFeaturePathIfNoFeaturePathIsSpecified(List<String> args,
}

private void addGlue(CucumberOptions options, List<String> args) {
for (String glue : options.glue()) {
boolean hasExtraGlue = options.extraGlue().length > 0;
boolean hasGlue = options.glue().length > 0;

if (hasExtraGlue && hasGlue) {
throw new CucumberException("glue and extraGlue cannot be specified at the same time");
}

String[] gluePaths = {};
if (hasExtraGlue) {
gluePaths = options.extraGlue();
}
if (hasGlue) {
gluePaths = options.glue();
overridingGlueSpecified = true;
}

for (String glue : gluePaths) {
args.add("--glue");
args.add(glue);
glueSpecified = true;
}
}

private void addDefaultGlueIfNoGlueIsSpecified(List<String> args, Class clazz) {
if (!glueSpecified) {
private void addDefaultGlueIfNoOverridingGlueIsSpecified(List<String> args, Class clazz) {
if (!overridingGlueSpecified) {
args.add("--glue");
args.add(MultiLoader.CLASSPATH_SCHEME + packagePath(clazz));
}
Expand Down
63 changes: 63 additions & 0 deletions core/src/test/java/cucumber/runtime/RuntimeOptionsFactoryTest.java
Expand Up @@ -150,6 +150,44 @@ private void assertPluginExists(List<Plugin> plugins, String pluginName) {
assertTrue(pluginName + " not found among the plugins", found);
}

@Test
public void create_with_glue() {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(ClassWithGlue.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();

assertEquals(asList("app.features.user.registration", "app.features.hooks"), runtimeOptions.getGlue());
}

@Test
public void create_with_extra_glue() {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(ClassWithExtraGlue.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();

assertEquals(asList("app.features.hooks", "classpath:cucumber/runtime"), runtimeOptions.getGlue());
}

@Test
public void create_with_extra_glue_in_subclass_of_extra_glue() {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(SubClassWithExtraGlueOfExtraGlue.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();

assertEquals(asList("app.features.user.hooks", "app.features.hooks", "classpath:cucumber/runtime"), runtimeOptions.getGlue());
}

@Test
public void create_with_extra_glue_in_subclass_of_glue() {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(SubClassWithExtraGlueOfGlue.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();

assertEquals(asList("app.features.user.hooks", "app.features.user.registration", "app.features.hooks"), runtimeOptions.getGlue());
}

@Test(expected = CucumberException.class)
public void cannot_create_with_glue_and_extra_glue() {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(ClassWithGlueAndExtraGlue.class);
runtimeOptionsFactory.create();
}


@CucumberOptions(snippets = SnippetType.CAMELCASE)
private static class Snippets {
Expand Down Expand Up @@ -218,4 +256,29 @@ private static class ClassWithJunitOption {
// empty
}

@CucumberOptions(glue = {"app.features.user.registration", "app.features.hooks"})
private static class ClassWithGlue {
// empty
}

@CucumberOptions(extraGlue = {"app.features.hooks"})
private static class ClassWithExtraGlue {
// empty
}

@CucumberOptions(extraGlue = {"app.features.user.hooks"})
private static class SubClassWithExtraGlueOfExtraGlue extends ClassWithExtraGlue {
// empty
}

@CucumberOptions(extraGlue = {"app.features.user.hooks"})
private static class SubClassWithExtraGlueOfGlue extends ClassWithGlue {
// empty
}

@CucumberOptions(extraGlue = {"app.features.hooks"}, glue = {"app.features.user.registration"})
private static class ClassWithGlueAndExtraGlue {
// empty
}

}