Skip to content

The RuntimeOptionsFactory should add default feature path, glue path and formatter once #636

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions core/src/main/java/cucumber/runtime/RuntimeOptionsFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
public class RuntimeOptionsFactory {
private final Class clazz;
private final Class<? extends Annotation>[] annotationClasses;
private boolean featuresSpecified = false;
private boolean glueSpecified = false;
private boolean formatSpecified = false;

public RuntimeOptionsFactory(Class clazz, Class<? extends Annotation>[] annotationClasses) {
this.clazz = clazz;
Expand All @@ -39,11 +42,14 @@ private List<String> buildArgsFromOptions() {
addName(options, args);
addDotCucumber(options, args);
addSnippets(options, args);
addGlue(optionsArray, args);
addFeatures(optionsArray, args);
}
}
addGlue(optionsArray, args, classWithOptions);
addFeatures(optionsArray, args, classWithOptions);
}
addDefaultFeaturePathIfNoFeaturePathIsSpecified(args, clazz);
addDefaultGlueIfNoGlueIsSpecified(args, clazz);
addDefaultFormatIfNoFormatIsSpecified(args);
return args;
}

Expand Down Expand Up @@ -92,37 +98,46 @@ private void addFormats(Annotation options, List<String> args) {
args.add("--format");
args.add(format);
}
} else {
formatSpecified = true;
}
}

private void addDefaultFormatIfNoFormatIsSpecified(List<String> args) {
if (!formatSpecified) {
args.add("--format");
args.add("null");
}
}

private void addFeatures(Annotation[] optionsArray, List<String> args, Class clazz) {
boolean specified = false;
private void addFeatures(Annotation[] optionsArray, List<String> args) {
for (Annotation options : optionsArray) {
if (options != null && this.<String[]>invoke(options, "features").length != 0) {
Collections.addAll(args, this.<String[]>invoke(options, "features"));
specified = true;
featuresSpecified = true;
}
}
if (!specified) {
}

private void addDefaultFeaturePathIfNoFeaturePathIsSpecified(List<String> args, Class clazz) {
if (!featuresSpecified) {
args.add(MultiLoader.CLASSPATH_SCHEME + packagePath(clazz));
}
}

private void addGlue(Annotation[] optionsArray, List<String> args, Class clazz) {
boolean specified = false;
private void addGlue(Annotation[] optionsArray, List<String> args) {
for (Annotation options : optionsArray) {
if (options != null && this.<String[]>invoke(options, "glue").length != 0) {
for (String glue : this.<String[]>invoke(options, "glue")) {
args.add("--glue");
args.add(glue);
}
specified = true;
glueSpecified = true;
}
}
if (!specified) {
}

private void addDefaultGlueIfNoGlueIsSpecified(List<String> args, Class clazz) {
if (!glueSpecified) {
args.add("--glue");
args.add(MultiLoader.CLASSPATH_SCHEME + packagePath(clazz));
}
Expand Down
16 changes: 16 additions & 0 deletions core/src/test/java/cucumber/runtime/RuntimeOptionsFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public void create_without_options() throws Exception {
assertFalse(runtimeOptions.isStrict());
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getFeaturePaths());
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getGlue());
assertEquals(1, runtimeOptions.getFormatters().size());
assertEquals("cucumber.runtime.formatter.NullFormatter", runtimeOptions.getFormatters().get(0).getClass().getName());
}

@Test
public void create_without_options_with_base_class_without_options() throws Exception {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(WithoutOptionsWithBaseClassWithoutOptions.class, new Class[]{CucumberOptions.class});
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getFeaturePaths());
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getGlue());
assertEquals(1, runtimeOptions.getFormatters().size());
assertEquals("cucumber.runtime.formatter.NullFormatter", runtimeOptions.getFormatters().get(0).getClass().getName());
}

@Test
Expand Down Expand Up @@ -158,6 +170,10 @@ static class WithoutOptions {
// empty
}

static class WithoutOptionsWithBaseClassWithoutOptions extends WithoutOptions {
// empty
}

@CucumberOptions(format = "pretty")
static class SubClassWithFormatter extends BaseClassWithFormatter {
// empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ public TestNGCucumberRunner(Class clazz) {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(clazz, new Class[]{CucumberOptions.class});
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();

// remove duplicates from glue path.
List<String> uniqueGlue = new ArrayList<String>(new HashSet<String>(runtimeOptions.getGlue()));
runtimeOptions.getGlue().clear();
runtimeOptions.getGlue().addAll(uniqueGlue);

TestNgReporter reporter = new TestNgReporter(System.out);
runtimeOptions.getFormatters().add(reporter);
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
Expand Down