Skip to content

Commit b113c82

Browse files
committed
Issue #2 Ensuring features are parsed before formatters are initialised
1 parent 77a93d1 commit b113c82

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class RuntimeOptions {
3131
private final List<Object> lineFilters = new ArrayList<Object>();
3232
private final List<Formatter> formatters = new ArrayList<Formatter>();
3333
private final List<String> featurePaths = new ArrayList<String>();
34+
private final List<String> formatterNames = new ArrayList<String>();
3435
private final FormatterFactory formatterFactory;
3536
private URL dotCucumber;
3637
private boolean dryRun;
@@ -80,10 +81,9 @@ public RuntimeOptions(Env env, FormatterFactory formatterFactory, List<String> a
8081
}
8182
filters.addAll(lineFilters);
8283

83-
if (formatters.isEmpty()) {
84-
formatters.add(formatterFactory.create("progress"));
84+
if (formatterNames.isEmpty()) {
85+
formatterNames.add("progress");
8586
}
86-
setFormatterOptions();
8787
}
8888

8989
private void parse(List<String> args) {
@@ -107,7 +107,7 @@ private void parse(List<String> args) {
107107
} else if (arg.equals("--tags") || arg.equals("-t")) {
108108
parsedFilters.add(args.remove(0));
109109
} else if (arg.equals("--format") || arg.equals("-f")) {
110-
formatters.add(formatterFactory.create(args.remove(0)));
110+
formatterNames.add(args.remove(0));
111111
} else if (arg.equals("--dotcucumber")) {
112112
String urlOrPath = args.remove(0);
113113
dotCucumber = Utils.toURL(urlOrPath);
@@ -156,12 +156,24 @@ private void printUsage() {
156156
public List<CucumberFeature> cucumberFeatures(ResourceLoader resourceLoader) {
157157
return load(resourceLoader, featurePaths, filters, System.out);
158158
}
159+
160+
protected List<Formatter> formatters() {
161+
if (formatters.isEmpty()){
162+
for (String formatterName : formatterNames){
163+
final Formatter formatter = formatterFactory.create(formatterName);
164+
formatters.add(formatter);
165+
setMonochromeOnColorAwareFormatters(formatter);
166+
setStrictOnStrictAwareFormatters(formatter);
167+
}
168+
}
169+
return formatters;
170+
}
159171

160172
public Formatter formatter(ClassLoader classLoader) {
161173
return (Formatter) Proxy.newProxyInstance(classLoader, new Class<?>[]{Formatter.class}, new InvocationHandler() {
162174
@Override
163175
public Object invoke(Object target, Method method, Object[] args) throws Throwable {
164-
for (Formatter formatter : formatters) {
176+
for (Formatter formatter : formatters()) {
165177
Utils.invoke(formatter, method, 0, args);
166178
}
167179
return null;
@@ -183,13 +195,6 @@ public Object invoke(Object target, Method method, Object[] args) throws Throwab
183195
});
184196
}
185197

186-
private void setFormatterOptions() {
187-
for (Formatter formatter : formatters) {
188-
setMonochromeOnColorAwareFormatters(formatter);
189-
setStrictOnStrictAwareFormatters(formatter);
190-
}
191-
}
192-
193198
private void setMonochromeOnColorAwareFormatters(Formatter formatter) {
194199
if (formatter instanceof ColorAware) {
195200
ColorAware colorAware = (ColorAware) formatter;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void inherit_formatter_from_baseclass() {
105105
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(SubClassWithFormatter.class, new Class[]{CucumberOptions.class});
106106
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
107107

108-
List<Formatter> formatters = runtimeOptions.getFormatters();
108+
List<Formatter> formatters = runtimeOptions.formatters();
109109
assertEquals(2, formatters.size());
110110
assertTrue(formatters.get(0) instanceof PrettyFormatter);
111111
assertTrue(formatters.get(1) instanceof JSONFormatter);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void assigns_dotcucumber() throws MalformedURLException {
7676
@Test
7777
public void creates_formatter() {
7878
RuntimeOptions options = new RuntimeOptions(asList("--format", "html:some/dir", "--glue", "somewhere"));
79-
assertEquals("cucumber.runtime.formatter.HTMLFormatter", options.getFormatters().get(0).getClass().getName());
79+
assertEquals("cucumber.runtime.formatter.HTMLFormatter", options.formatters().get(0).getClass().getName());
8080
}
8181

8282
@Test
@@ -218,7 +218,8 @@ public void set_monochrome_on_color_aware_formatters() throws Exception {
218218
Formatter colorAwareFormatter = mock(Formatter.class, withSettings().extraInterfaces(ColorAware.class));
219219
when(factory.create("progress")).thenReturn(colorAwareFormatter);
220220

221-
new RuntimeOptions(new Env(), factory, asList("--monochrome", "--format", "progress"));
221+
RuntimeOptions options = new RuntimeOptions(new Env(), factory, asList("--monochrome", "--format", "progress"));
222+
options.formatters();
222223

223224
verify((ColorAware) colorAwareFormatter).setMonochrome(true);
224225
}
@@ -229,7 +230,8 @@ public void set_strict_on_strict_aware_formatters() throws Exception {
229230
Formatter strictAwareFormatter = mock(Formatter.class, withSettings().extraInterfaces(StrictAware.class));
230231
when(factory.create("junit:out/dir")).thenReturn(strictAwareFormatter);
231232

232-
new RuntimeOptions(new Env(), factory, asList("--strict", "--format", "junit:out/dir"));
233+
RuntimeOptions options = new RuntimeOptions(new Env(), factory, asList("--strict", "--format", "junit:out/dir"));
234+
options.formatters();
233235

234236
verify((StrictAware) strictAwareFormatter).setStrict(true);
235237
}

junit/src/main/java/cucumber/api/junit/Cucumber.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ public Cucumber(Class clazz) throws InitializationError, IOException {
6262
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
6363
runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
6464

65+
final List<CucumberFeature> cucumberFeatures = runtimeOptions.cucumberFeatures(resourceLoader);
6566
jUnitReporter = new JUnitReporter(runtimeOptions.reporter(classLoader), runtimeOptions.formatter(classLoader), runtimeOptions.isStrict());
66-
addChildren(runtimeOptions.cucumberFeatures(resourceLoader));
67+
addChildren(cucumberFeatures);
6768
}
6869

6970
@Override

junit/src/test/java/cucumber/runtime/junit/CucumberTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import static java.util.Collections.emptyList;
1818
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertFalse;
1920
import static org.junit.Assert.fail;
2021

2122
public class CucumberTest {
@@ -59,6 +60,16 @@ public void testThatParsingErrorsIsNicelyReported() throws Exception {
5960
assertEquals("Error parsing feature file cucumber/runtime/error/lexer_error.feature", e.getMessage());
6061
}
6162
}
63+
64+
@Test
65+
public void testThatFileIsNotCreatedOnParsingError() throws Exception {
66+
try {
67+
new Cucumber(FormatterWithLexerErrorFeature.class);
68+
fail("Expecting error");
69+
} catch (CucumberException e){
70+
assertFalse("File is created despite Lexor Error", new File("lexor_error_feature.json").exists());
71+
}
72+
}
6273

6374
@Test
6475
public void finds_no_features_when_explicit_feature_path_has_no_features() throws IOException, InitializationError {
@@ -110,4 +121,10 @@ private class ExplicitFeaturePathWithNoFeatures {
110121
private class LexerErrorFeature {
111122

112123
}
124+
125+
@CucumberOptions(features = {"classpath:cucumber/runtime/error/lexer_error.feature"}, format = {"json:lexor_error_feature.json"})
126+
private class FormatterWithLexerErrorFeature {
127+
128+
}
129+
113130
}

0 commit comments

Comments
 (0)