Skip to content

Commit 532be3e

Browse files
ekalinmpkorstanje
authored andcommitted
[Core] Added extraGlue option to @CucumberOptions (cucumber#1439)
This option works similarly to glue, specifing packages in which to search for steps and hooks, but the default search path (the current package) is included in the list of glue packages, whereas glue includes only the explicitly listed packages. Closes cucumber#1438.
1 parent 1c33961 commit 532be3e

File tree

3 files changed

+89
-7
lines changed

3 files changed

+89
-7
lines changed

core/src/main/java/cucumber/api/CucumberOptions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
*/
3232
String[] glue() default {};
3333

34+
/**
35+
* @return where to look for glue code (stepdefs and hooks), in addition to default search path
36+
*/
37+
String[] extraGlue() default {};
38+
3439
/**
3540
* @return what tags in the features should be executed
3641
*/

core/src/main/java/cucumber/runtime/RuntimeOptionsFactory.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cucumber.runtime;
22

33
import cucumber.api.CucumberOptions;
4-
import cucumber.runtime.formatter.PluginFactory;
54
import cucumber.runtime.io.MultiLoader;
65

76
import java.util.ArrayList;
@@ -13,7 +12,7 @@
1312
public class RuntimeOptionsFactory {
1413
private final Class clazz;
1514
private boolean featuresSpecified = false;
16-
private boolean glueSpecified = false;
15+
private boolean overridingGlueSpecified = false;
1716

1817
public RuntimeOptionsFactory(Class clazz) {
1918
this.clazz = clazz;
@@ -43,7 +42,7 @@ private List<String> buildArgsFromOptions() {
4342
}
4443
}
4544
addDefaultFeaturePathIfNoFeaturePathIsSpecified(args, clazz);
46-
addDefaultGlueIfNoGlueIsSpecified(args, clazz);
45+
addDefaultGlueIfNoOverridingGlueIsSpecified(args, clazz);
4746
return args;
4847
}
4948

@@ -101,15 +100,30 @@ private void addDefaultFeaturePathIfNoFeaturePathIsSpecified(List<String> args,
101100
}
102101

103102
private void addGlue(CucumberOptions options, List<String> args) {
104-
for (String glue : options.glue()) {
103+
boolean hasExtraGlue = options.extraGlue().length > 0;
104+
boolean hasGlue = options.glue().length > 0;
105+
106+
if (hasExtraGlue && hasGlue) {
107+
throw new CucumberException("glue and extraGlue cannot be specified at the same time");
108+
}
109+
110+
String[] gluePaths = {};
111+
if (hasExtraGlue) {
112+
gluePaths = options.extraGlue();
113+
}
114+
if (hasGlue) {
115+
gluePaths = options.glue();
116+
overridingGlueSpecified = true;
117+
}
118+
119+
for (String glue : gluePaths) {
105120
args.add("--glue");
106121
args.add(glue);
107-
glueSpecified = true;
108122
}
109123
}
110124

111-
private void addDefaultGlueIfNoGlueIsSpecified(List<String> args, Class clazz) {
112-
if (!glueSpecified) {
125+
private void addDefaultGlueIfNoOverridingGlueIsSpecified(List<String> args, Class clazz) {
126+
if (!overridingGlueSpecified) {
113127
args.add("--glue");
114128
args.add(MultiLoader.CLASSPATH_SCHEME + packagePath(clazz));
115129
}

core/src/test/java/cucumber/runtime/RuntimeOptionsFactoryTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,44 @@ private void assertPluginExists(List<Plugin> plugins, String pluginName) {
150150
assertTrue(pluginName + " not found among the plugins", found);
151151
}
152152

153+
@Test
154+
public void create_with_glue() {
155+
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(ClassWithGlue.class);
156+
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
157+
158+
assertEquals(asList("app.features.user.registration", "app.features.hooks"), runtimeOptions.getGlue());
159+
}
160+
161+
@Test
162+
public void create_with_extra_glue() {
163+
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(ClassWithExtraGlue.class);
164+
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
165+
166+
assertEquals(asList("app.features.hooks", "classpath:cucumber/runtime"), runtimeOptions.getGlue());
167+
}
168+
169+
@Test
170+
public void create_with_extra_glue_in_subclass_of_extra_glue() {
171+
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(SubClassWithExtraGlueOfExtraGlue.class);
172+
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
173+
174+
assertEquals(asList("app.features.user.hooks", "app.features.hooks", "classpath:cucumber/runtime"), runtimeOptions.getGlue());
175+
}
176+
177+
@Test
178+
public void create_with_extra_glue_in_subclass_of_glue() {
179+
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(SubClassWithExtraGlueOfGlue.class);
180+
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
181+
182+
assertEquals(asList("app.features.user.hooks", "app.features.user.registration", "app.features.hooks"), runtimeOptions.getGlue());
183+
}
184+
185+
@Test(expected = CucumberException.class)
186+
public void cannot_create_with_glue_and_extra_glue() {
187+
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(ClassWithGlueAndExtraGlue.class);
188+
runtimeOptionsFactory.create();
189+
}
190+
153191

154192
@CucumberOptions(snippets = SnippetType.CAMELCASE)
155193
private static class Snippets {
@@ -218,4 +256,29 @@ private static class ClassWithJunitOption {
218256
// empty
219257
}
220258

259+
@CucumberOptions(glue = {"app.features.user.registration", "app.features.hooks"})
260+
private static class ClassWithGlue {
261+
// empty
262+
}
263+
264+
@CucumberOptions(extraGlue = {"app.features.hooks"})
265+
private static class ClassWithExtraGlue {
266+
// empty
267+
}
268+
269+
@CucumberOptions(extraGlue = {"app.features.user.hooks"})
270+
private static class SubClassWithExtraGlueOfExtraGlue extends ClassWithExtraGlue {
271+
// empty
272+
}
273+
274+
@CucumberOptions(extraGlue = {"app.features.user.hooks"})
275+
private static class SubClassWithExtraGlueOfGlue extends ClassWithGlue {
276+
// empty
277+
}
278+
279+
@CucumberOptions(extraGlue = {"app.features.hooks"}, glue = {"app.features.user.registration"})
280+
private static class ClassWithGlueAndExtraGlue {
281+
// empty
282+
}
283+
221284
}

0 commit comments

Comments
 (0)