Skip to content

Commit 61e6cfc

Browse files
committed
Merge branch 'rerun-formatter-exit-code'
2 parents 13ba17a + 4de216b commit 61e6cfc

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

core/src/main/java/cucumber/runtime/formatter/RerunFormatter.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import gherkin.formatter.model.ScenarioOutline;
1313
import gherkin.formatter.model.Step;
1414

15+
import java.util.ArrayList;
1516
import java.util.HashMap;
16-
import java.util.LinkedHashSet;
1717
import java.util.List;
1818
import java.util.Map;
1919
import java.util.Set;
@@ -23,12 +23,13 @@
2323
* Formatter for reporting all failed features and print their locations
2424
* Failed means: (failed, undefined, pending) test result
2525
*/
26-
class RerunFormatter implements Formatter, Reporter {
26+
class RerunFormatter implements Formatter, Reporter, StrictAware {
2727
private final NiceAppendable out;
2828
private String featureLocation;
2929
private Scenario scenario;
3030
private boolean isTestFailed = false;
31-
private Map<String, LinkedHashSet<Integer>> featureAndFailedLinesMapping = new HashMap<String, LinkedHashSet<Integer>>();
31+
private Map<String, ArrayList<Integer>> featureAndFailedLinesMapping = new HashMap<String, ArrayList<Integer>>();
32+
private boolean isStrict = false;
3233

3334
public RerunFormatter(Appendable out) {
3435
this.out = new NiceAppendable(out);
@@ -78,9 +79,9 @@ public void done() {
7879
}
7980

8081
private void reportFailedScenarios() {
81-
Set<Map.Entry<String, LinkedHashSet<Integer>>> entries = featureAndFailedLinesMapping.entrySet();
82+
Set<Map.Entry<String, ArrayList<Integer>>> entries = featureAndFailedLinesMapping.entrySet();
8283
boolean firstFeature = true;
83-
for (Map.Entry<String, LinkedHashSet<Integer>> entry : entries) {
84+
for (Map.Entry<String, ArrayList<Integer>> entry : entries) {
8485
if (entry.getValue().size() > 0) {
8586
if (!firstFeature) {
8687
out.append(" ");
@@ -127,13 +128,13 @@ public void result(Result result) {
127128

128129
private boolean isTestFailed(Result result) {
129130
String status = result.getStatus();
130-
return Result.FAILED.equals(status) || Result.UNDEFINED.getStatus().equals(status) || "pending".equals(status);
131+
return Result.FAILED.equals(status) || isStrict && (Result.UNDEFINED.getStatus().equals(status) || "pending".equals(status));
131132
}
132133

133134
private void recordTestFailed() {
134-
LinkedHashSet<Integer> failedScenarios = this.featureAndFailedLinesMapping.get(featureLocation);
135+
ArrayList<Integer> failedScenarios = this.featureAndFailedLinesMapping.get(featureLocation);
135136
if (failedScenarios == null) {
136-
failedScenarios = new LinkedHashSet<Integer>();
137+
failedScenarios = new ArrayList<Integer>();
137138
this.featureAndFailedLinesMapping.put(featureLocation, failedScenarios);
138139
}
139140

@@ -158,4 +159,9 @@ public void embedding(String mimeType, byte[] data) {
158159
@Override
159160
public void write(String text) {
160161
}
162+
163+
@Override
164+
public void setStrict(boolean strict) {
165+
isStrict = strict;
166+
}
161167
}

core/src/test/java/cucumber/runtime/formatter/RerunFormatterTest.java

+50-12
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,45 @@
1919
public class RerunFormatterTest {
2020

2121
@Test
22-
public void should_leave_report_empty_when_no_scenario_fails() throws Throwable {
22+
public void should_leave_report_empty_when_exit_code_is_zero() throws Throwable {
2323
CucumberFeature feature = TestHelper.feature("path/test.feature", "" +
2424
"Feature: feature name\n" +
25-
" Scenario: scenario name\n" +
26-
" Given first step\n" +
27-
" When second step\n" +
28-
" Then third step\n");
25+
" Scenario: passed scenario\n" +
26+
" Given passed step\n" +
27+
" Scenario: pending scenario\n" +
28+
" Given pending step\n" +
29+
" Scenario: undefined scenario\n" +
30+
" Given undefined step\n");
2931
Map<String, Result> stepsToResult = new HashMap<String, Result>();
30-
stepsToResult.put("first step", result("passed"));
31-
stepsToResult.put("second step", result("passed"));
32-
stepsToResult.put("third step", result("passed"));
32+
stepsToResult.put("passed step", result("passed"));
33+
stepsToResult.put("pending step", result("pending"));
34+
stepsToResult.put("undefined step", result("undefined"));
3335

3436
String formatterOutput = runFeatureWithRerunFormatter(feature, stepsToResult);
3537

3638
assertEquals("", formatterOutput);
3739
}
3840

41+
@Test
42+
public void should_put_data_in_report_when_exit_code_is_non_zero() throws Throwable {
43+
CucumberFeature feature = TestHelper.feature("path/test.feature", "" +
44+
"Feature: feature name\n" +
45+
" Scenario: failed scenario\n" +
46+
" Given failed step\n" +
47+
" Scenario: pending scenario\n" +
48+
" Given pending step\n" +
49+
" Scenario: undefined scenario\n" +
50+
" Given undefined step\n");
51+
Map<String, Result> stepsToResult = new HashMap<String, Result>();
52+
stepsToResult.put("failed step", result("failed"));
53+
stepsToResult.put("pending step", result("pending"));
54+
stepsToResult.put("undefined step", result("undefined"));
55+
56+
String formatterOutput = runFeatureWithRerunFormatter(feature, stepsToResult, strict(true));
57+
58+
assertEquals("path/test.feature:2:4:6", formatterOutput);
59+
}
60+
3961
@Test
4062
public void should_use_scenario_location_when_scenario_step_fails() throws Throwable {
4163
CucumberFeature feature = TestHelper.feature("path/test.feature", "" +
@@ -180,26 +202,42 @@ public void should_one_entry_for_each_failing_feature() throws Throwable {
180202

181203
private String runFeatureWithRerunFormatter(final CucumberFeature feature, final Map<String, Result> stepsToResult)
182204
throws Throwable {
183-
return runFeatureWithRerunFormatter(feature, stepsToResult, Collections.<SimpleEntry<String, Result>>emptyList());
205+
return runFeatureWithRerunFormatter(feature, stepsToResult, Collections.<SimpleEntry<String, Result>>emptyList(), false);
206+
}
207+
208+
private String runFeatureWithRerunFormatter(final CucumberFeature feature, final Map<String, Result> stepsToResult, boolean isStrict)
209+
throws Throwable {
210+
return runFeatureWithRerunFormatter(feature, stepsToResult, Collections.<SimpleEntry<String, Result>>emptyList(), isStrict);
184211
}
185212

186213
private String runFeatureWithRerunFormatter(final CucumberFeature feature, final Map<String, Result> stepsToResult,
187214
final List<SimpleEntry<String, Result>> hooks) throws Throwable {
188-
return runFeaturesWithRerunFormatter(Arrays.asList(feature), stepsToResult, hooks);
215+
return runFeaturesWithRerunFormatter(Arrays.asList(feature), stepsToResult, hooks, strict(false));
216+
}
217+
218+
private String runFeatureWithRerunFormatter(final CucumberFeature feature, final Map<String, Result> stepsToResult,
219+
final List<SimpleEntry<String, Result>> hooks, boolean isStrict) throws Throwable {
220+
return runFeaturesWithRerunFormatter(Arrays.asList(feature), stepsToResult, hooks, isStrict);
189221
}
190222

191223
private String runFeaturesWithRerunFormatter(final List<CucumberFeature> features, final Map<String, Result> stepsToResult)
192224
throws Throwable {
193-
return runFeaturesWithRerunFormatter(features, stepsToResult, Collections.<SimpleEntry<String, Result>>emptyList());
225+
return runFeaturesWithRerunFormatter(features, stepsToResult, Collections.<SimpleEntry<String, Result>>emptyList(), strict(false));
194226
}
195227

196228
private String runFeaturesWithRerunFormatter(final List<CucumberFeature> features, final Map<String, Result> stepsToResult,
197-
final List<SimpleEntry<String, Result>> hooks) throws Throwable {
229+
final List<SimpleEntry<String, Result>> hooks, boolean isStrict) throws Throwable {
198230
final StringBuffer buffer = new StringBuffer();
199231
final RerunFormatter rerunFormatter = new RerunFormatter(buffer);
232+
if (isStrict) {
233+
rerunFormatter.setStrict(isStrict);
234+
}
200235
final long stepHookDuration = 0;
201236
TestHelper.runFeaturesWithFormatter(features, stepsToResult, hooks, stepHookDuration, rerunFormatter, rerunFormatter);
202237
return buffer.toString();
203238
}
204239

240+
private boolean strict(boolean value) {
241+
return value;
242+
}
205243
}

0 commit comments

Comments
 (0)