Skip to content

Commit d88aa0f

Browse files
committed
Call all formatters, also with the IntelliJ's one. Fixes #803.
The IntelliJ formatter does not implement the startOfScenarioLifeCycle and the endOfScenarioLifeCycle methods. Also when it is throwing an exception on these methods, all other formatters used need to be called on these methods.
1 parent 3fc886e commit d88aa0f

File tree

3 files changed

+132
-13
lines changed

3 files changed

+132
-13
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,14 @@ public <T> T pluginProxy(ClassLoader classLoader, final Class<T> type) {
240240
public Object invoke(Object target, Method method, Object[] args) throws Throwable {
241241
for (Object plugin : getPlugins()) {
242242
if (type.isInstance(plugin)) {
243-
Utils.invoke(plugin, method, 0, args);
243+
try {
244+
Utils.invoke(plugin, method, 0, args);
245+
} catch (Throwable t) {
246+
if (!method.getName().equals("startOfScenarioLifeCycle") && !method.getName().equals("endOfScenarioLifeCycle")) {
247+
// IntelliJ has its own formatter which doesn't yet implement these methods.
248+
throw t;
249+
}
250+
}
244251
}
245252
}
246253
return null;

core/src/main/java/cucumber/runtime/model/CucumberScenario.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,15 @@ public CucumberBackground getCucumberBackground() {
3636
public void run(Formatter formatter, Reporter reporter, Runtime runtime) {
3737
Set<Tag> tags = tagsAndInheritedTags();
3838
runtime.buildBackendWorlds(reporter, tags, scenario);
39-
try {
40-
formatter.startOfScenarioLifeCycle((Scenario) getGherkinModel());
41-
} catch (Throwable ignore) {
42-
// IntelliJ has its own formatter which doesn't yet implement this.
43-
}
39+
formatter.startOfScenarioLifeCycle((Scenario) getGherkinModel());
4440
runtime.runBeforeHooks(reporter, tags);
4541

4642
runBackground(formatter, reporter, runtime);
4743
format(formatter);
4844
runSteps(reporter, runtime);
4945

5046
runtime.runAfterHooks(reporter, tags);
51-
try {
52-
formatter.endOfScenarioLifeCycle((Scenario) getGherkinModel());
53-
} catch (Throwable ignore) {
54-
// IntelliJ has its own formatter which doesn't yet implement this.
55-
}
47+
formatter.endOfScenarioLifeCycle((Scenario) getGherkinModel());
5648
runtime.disposeBackendWorlds();
5749
}
5850

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

+122-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package cucumber.runtime;
22

3+
import gherkin.formatter.Reporter;
4+
import gherkin.formatter.model.Background;
5+
import gherkin.formatter.model.Examples;
6+
import gherkin.formatter.model.Feature;
7+
import gherkin.formatter.model.Match;
8+
import gherkin.formatter.model.Result;
9+
import gherkin.formatter.model.ScenarioOutline;
10+
import gherkin.formatter.model.Step;
11+
import cucumber.runtime.formatter.FormatterSpy;
312
import cucumber.api.SnippetType;
413
import cucumber.runtime.formatter.ColorAware;
514
import cucumber.runtime.formatter.PluginFactory;
@@ -13,10 +22,9 @@
1322
import java.io.ByteArrayInputStream;
1423
import java.io.IOException;
1524
import java.io.UnsupportedEncodingException;
16-
import java.net.MalformedURLException;
17-
import java.net.URL;
1825
import java.util.Arrays;
1926
import java.util.Collections;
27+
import java.util.HashMap;
2028
import java.util.List;
2129
import java.util.Properties;
2230
import java.util.regex.Pattern;
@@ -296,6 +304,35 @@ public void applies_line_filters_only_to_own_feature() throws Exception {
296304
assertOnlyScenarioName(features.get(1), "scenario_2_2");
297305
}
298306

307+
@Test
308+
public void handles_formatters_missing_startOfScenarioLifeCycle_endOfScenarioLifeCycle() throws Throwable {
309+
CucumberFeature feature = TestHelper.feature("path/test.feature", "" +
310+
"Feature: feature name\n" +
311+
" Scenario: scenario name\n" +
312+
" Given step\n");
313+
314+
FormatterSpy formatterSpy = new FormatterSpy();
315+
RuntimeOptions runtimeOptions = new RuntimeOptions("");
316+
runtimeOptions.addPlugin(new FormatterMissingLifecycleMethods());
317+
runtimeOptions.addPlugin(formatterSpy);
318+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
319+
TestHelper.runFeatureWithFormatter(feature, new HashMap<String, String>(),
320+
runtimeOptions.formatter(classLoader), runtimeOptions.reporter(classLoader));
321+
322+
assertEquals("" +
323+
"uri\n" +
324+
"feature\n" +
325+
" startOfScenarioLifeCycle\n" +
326+
" scenario\n" +
327+
" step\n" +
328+
" match\n" +
329+
" result\n" +
330+
" endOfScenarioLifeCycle\n" +
331+
"eof\n" +
332+
"done\n" +
333+
"close\n", formatterSpy.toString());
334+
}
335+
299336
private void assertOnlyScenarioName(CucumberFeature feature, String scenarioName) {
300337
assertEquals("Wrong number of scenarios loaded for feature", 1, feature.getFeatureElements().size());
301338
assertEquals("Scenario: " + scenarioName, feature.getFeatureElements().get(0).getVisualName());
@@ -309,3 +346,86 @@ private void mockResource(ResourceLoader resourceLoader, String featurePath, Str
309346
when(resourceLoader.resources(featurePath, ".feature")).thenReturn(asList(resource1));
310347
}
311348
}
349+
350+
class FormatterMissingLifecycleMethods implements Formatter, Reporter {
351+
@Override
352+
public void startOfScenarioLifeCycle(gherkin.formatter.model.Scenario arg0) {
353+
throw new NoSuchMethodError(); // simulate that this method is not implemented
354+
}
355+
356+
@Override
357+
public void endOfScenarioLifeCycle(gherkin.formatter.model.Scenario arg0) {
358+
throw new NoSuchMethodError(); // simulate that this method is not implemented
359+
}
360+
361+
@Override
362+
public void after(Match arg0, Result arg1) {
363+
}
364+
365+
@Override
366+
public void before(Match arg0, Result arg1) {
367+
}
368+
369+
@Override
370+
public void embedding(String arg0, byte[] arg1) {
371+
}
372+
373+
@Override
374+
public void match(Match arg0) {
375+
}
376+
377+
@Override
378+
public void result(Result arg0) {
379+
}
380+
381+
@Override
382+
public void write(String arg0) {
383+
}
384+
385+
@Override
386+
public void background(Background arg0) {
387+
}
388+
389+
@Override
390+
public void close() {
391+
}
392+
393+
@Override
394+
public void done() {
395+
}
396+
397+
@Override
398+
public void eof() {
399+
}
400+
401+
@Override
402+
public void examples(Examples arg0) {
403+
}
404+
405+
@Override
406+
public void feature(Feature arg0) {
407+
408+
}
409+
410+
@Override
411+
public void scenario(gherkin.formatter.model.Scenario arg0) {
412+
413+
}
414+
415+
@Override
416+
public void scenarioOutline(ScenarioOutline arg0) {
417+
}
418+
419+
@Override
420+
public void step(Step arg0) {
421+
}
422+
423+
@Override
424+
public void syntaxError(String arg0, String arg1, List<String> arg2, String arg3, Integer arg4) {
425+
}
426+
427+
@Override
428+
public void uri(String arg0) {
429+
}
430+
431+
}

0 commit comments

Comments
 (0)