From f059eb5340a7c4e21c134028d782d0e6b043027c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rasmusson?= Date: Sun, 31 May 2015 19:25:43 +0200 Subject: [PATCH] Make the Rerun formatter consistent with the exit code The exit code is zero => the Rerun formatter output is empty The exit code is non-zero => the Rerun formatter output is not empty --- .../runtime/formatter/RerunFormatter.java | 22 ++++--- .../runtime/formatter/RerunFormatterTest.java | 62 +++++++++++++++---- 2 files changed, 64 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/cucumber/runtime/formatter/RerunFormatter.java b/core/src/main/java/cucumber/runtime/formatter/RerunFormatter.java index f5742e1e99..e7bdad1cf9 100644 --- a/core/src/main/java/cucumber/runtime/formatter/RerunFormatter.java +++ b/core/src/main/java/cucumber/runtime/formatter/RerunFormatter.java @@ -12,8 +12,8 @@ import gherkin.formatter.model.ScenarioOutline; import gherkin.formatter.model.Step; +import java.util.ArrayList; import java.util.HashMap; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -23,12 +23,13 @@ * Formatter for reporting all failed features and print their locations * Failed means: (failed, undefined, pending) test result */ -class RerunFormatter implements Formatter, Reporter { +class RerunFormatter implements Formatter, Reporter, StrictAware { private final NiceAppendable out; private String featureLocation; private Scenario scenario; private boolean isTestFailed = false; - private Map> featureAndFailedLinesMapping = new HashMap>(); + private Map> featureAndFailedLinesMapping = new HashMap>(); + private boolean isStrict = false; public RerunFormatter(Appendable out) { this.out = new NiceAppendable(out); @@ -78,9 +79,9 @@ public void done() { } private void reportFailedScenarios() { - Set>> entries = featureAndFailedLinesMapping.entrySet(); + Set>> entries = featureAndFailedLinesMapping.entrySet(); boolean firstFeature = true; - for (Map.Entry> entry : entries) { + for (Map.Entry> entry : entries) { if (entry.getValue().size() > 0) { if (!firstFeature) { out.append(" "); @@ -127,13 +128,13 @@ public void result(Result result) { private boolean isTestFailed(Result result) { String status = result.getStatus(); - return Result.FAILED.equals(status) || Result.UNDEFINED.getStatus().equals(status) || "pending".equals(status); + return Result.FAILED.equals(status) || isStrict && (Result.UNDEFINED.getStatus().equals(status) || "pending".equals(status)); } private void recordTestFailed() { - LinkedHashSet failedScenarios = this.featureAndFailedLinesMapping.get(featureLocation); + ArrayList failedScenarios = this.featureAndFailedLinesMapping.get(featureLocation); if (failedScenarios == null) { - failedScenarios = new LinkedHashSet(); + failedScenarios = new ArrayList(); this.featureAndFailedLinesMapping.put(featureLocation, failedScenarios); } @@ -158,4 +159,9 @@ public void embedding(String mimeType, byte[] data) { @Override public void write(String text) { } + + @Override + public void setStrict(boolean strict) { + isStrict = strict; + } } diff --git a/core/src/test/java/cucumber/runtime/formatter/RerunFormatterTest.java b/core/src/test/java/cucumber/runtime/formatter/RerunFormatterTest.java index 98225c25a9..2722c1700f 100755 --- a/core/src/test/java/cucumber/runtime/formatter/RerunFormatterTest.java +++ b/core/src/test/java/cucumber/runtime/formatter/RerunFormatterTest.java @@ -19,23 +19,45 @@ public class RerunFormatterTest { @Test - public void should_leave_report_empty_when_no_scenario_fails() throws Throwable { + public void should_leave_report_empty_when_exit_code_is_zero() throws Throwable { CucumberFeature feature = TestHelper.feature("path/test.feature", "" + "Feature: feature name\n" + - " Scenario: scenario name\n" + - " Given first step\n" + - " When second step\n" + - " Then third step\n"); + " Scenario: passed scenario\n" + + " Given passed step\n" + + " Scenario: pending scenario\n" + + " Given pending step\n" + + " Scenario: undefined scenario\n" + + " Given undefined step\n"); Map stepsToResult = new HashMap(); - stepsToResult.put("first step", result("passed")); - stepsToResult.put("second step", result("passed")); - stepsToResult.put("third step", result("passed")); + stepsToResult.put("passed step", result("passed")); + stepsToResult.put("pending step", result("pending")); + stepsToResult.put("undefined step", result("undefined")); String formatterOutput = runFeatureWithRerunFormatter(feature, stepsToResult); assertEquals("", formatterOutput); } + @Test + public void should_put_data_in_report_when_exit_code_is_non_zero() throws Throwable { + CucumberFeature feature = TestHelper.feature("path/test.feature", "" + + "Feature: feature name\n" + + " Scenario: failed scenario\n" + + " Given failed step\n" + + " Scenario: pending scenario\n" + + " Given pending step\n" + + " Scenario: undefined scenario\n" + + " Given undefined step\n"); + Map stepsToResult = new HashMap(); + stepsToResult.put("failed step", result("failed")); + stepsToResult.put("pending step", result("pending")); + stepsToResult.put("undefined step", result("undefined")); + + String formatterOutput = runFeatureWithRerunFormatter(feature, stepsToResult, strict(true)); + + assertEquals("path/test.feature:2:4:6", formatterOutput); + } + @Test public void should_use_scenario_location_when_scenario_step_fails() throws Throwable { CucumberFeature feature = TestHelper.feature("path/test.feature", "" + @@ -180,26 +202,42 @@ public void should_one_entry_for_each_failing_feature() throws Throwable { private String runFeatureWithRerunFormatter(final CucumberFeature feature, final Map stepsToResult) throws Throwable { - return runFeatureWithRerunFormatter(feature, stepsToResult, Collections.>emptyList()); + return runFeatureWithRerunFormatter(feature, stepsToResult, Collections.>emptyList(), false); + } + + private String runFeatureWithRerunFormatter(final CucumberFeature feature, final Map stepsToResult, boolean isStrict) + throws Throwable { + return runFeatureWithRerunFormatter(feature, stepsToResult, Collections.>emptyList(), isStrict); } private String runFeatureWithRerunFormatter(final CucumberFeature feature, final Map stepsToResult, final List> hooks) throws Throwable { - return runFeaturesWithRerunFormatter(Arrays.asList(feature), stepsToResult, hooks); + return runFeaturesWithRerunFormatter(Arrays.asList(feature), stepsToResult, hooks, strict(false)); + } + + private String runFeatureWithRerunFormatter(final CucumberFeature feature, final Map stepsToResult, + final List> hooks, boolean isStrict) throws Throwable { + return runFeaturesWithRerunFormatter(Arrays.asList(feature), stepsToResult, hooks, isStrict); } private String runFeaturesWithRerunFormatter(final List features, final Map stepsToResult) throws Throwable { - return runFeaturesWithRerunFormatter(features, stepsToResult, Collections.>emptyList()); + return runFeaturesWithRerunFormatter(features, stepsToResult, Collections.>emptyList(), strict(false)); } private String runFeaturesWithRerunFormatter(final List features, final Map stepsToResult, - final List> hooks) throws Throwable { + final List> hooks, boolean isStrict) throws Throwable { final StringBuffer buffer = new StringBuffer(); final RerunFormatter rerunFormatter = new RerunFormatter(buffer); + if (isStrict) { + rerunFormatter.setStrict(isStrict); + } final long stepHookDuration = 0; TestHelper.runFeaturesWithFormatter(features, stepsToResult, hooks, stepHookDuration, rerunFormatter, rerunFormatter); return buffer.toString(); } + private boolean strict(boolean value) { + return value; + } }