From a98e7b2d090d0de557437e65d30dcf15208ad6f2 Mon Sep 17 00:00:00 2001 From: "M.P. Korstanje" Date: Thu, 23 Apr 2020 14:20:47 +0200 Subject: [PATCH] [Core] Include all fields in JsonFormatters failure feature Consumers of the json output expect all fields to be present. To avoid breaking consumers we include all fields in the dummy feature that is generated when cucumber fails to execute. --- core/pom.xml | 27 +++++++ .../cucumber/core/plugin/JsonFormatter.java | 76 ++++++++++++++++--- .../stepexpression/StepExpressionFactory.java | 2 +- .../StepExpressionFactoryTest.java | 2 +- 4 files changed, 95 insertions(+), 12 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 5d9cdb1eb8..2cc1d505a6 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -137,6 +137,33 @@ + + + org.apache.maven.plugins + maven-shade-plugin + + + package + + shade + + + + + io.cucumber:gherkin + + + + + gherkin + io.cucumber.core.internal.gherkin + + + + + + + diff --git a/core/src/main/java/io/cucumber/core/plugin/JsonFormatter.java b/core/src/main/java/io/cucumber/core/plugin/JsonFormatter.java index 5bd5c4523f..a38e033ab5 100644 --- a/core/src/main/java/io/cucumber/core/plugin/JsonFormatter.java +++ b/core/src/main/java/io/cucumber/core/plugin/JsonFormatter.java @@ -36,6 +36,7 @@ import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.Arrays; import java.util.Base64; import java.util.HashMap; import java.util.LinkedHashMap; @@ -43,6 +44,7 @@ import java.util.Map; import static io.cucumber.core.exception.ExceptionUtils.printStackTrace; +import static java.util.Collections.singletonList; import static java.util.Locale.ROOT; import static java.util.stream.Collectors.toList; @@ -140,16 +142,7 @@ private void handleTestStepFinished(TestStepFinished event) { private void finishReport(TestRunFinished event) { Throwable exception = event.getResult().getError(); if (exception != null) { - Map feature = new LinkedHashMap<>(); - feature.put("description", "Test run failed"); - Map elements = new LinkedHashMap<>(); - feature.put("elements", elements); - elements.put("description", "There were errors during the execution"); - Map result = new LinkedHashMap<>(); - elements.put("result", result); - result.put("error_message", exception.getMessage()); - result.put("status", "failed"); - featureMaps.add(feature); + featureMaps.add(createDummyFeatureForFailure(event)); } gson.toJson(featureMaps, out); @@ -160,6 +153,69 @@ private void finishReport(TestRunFinished event) { } } + private Map createDummyFeatureForFailure(TestRunFinished event) { + Throwable exception = event.getResult().getError(); + + Map feature = new LinkedHashMap<>(); + feature.put("line", 1); + { + Map scenario = new LinkedHashMap<>(); + feature.put("elements", singletonList(scenario)); + + scenario.put("start_timestamp", getDateTimeFromTimeStamp(event.getInstant())); + scenario.put("line", 2); + scenario.put("name", "Could not execute Cucumber"); + scenario.put("description", ""); + scenario.put("id", "failure;could-not-execute-cucumber"); + scenario.put("type", "scenario"); + scenario.put("keyword", "Scenario"); + + Map when = new LinkedHashMap<>(); + Map then = new LinkedHashMap<>(); + scenario.put("steps", Arrays.asList(when, then)); + { + + { + Map whenResult = new LinkedHashMap<>(); + when.put("result", whenResult); + whenResult.put("duration", 0); + whenResult.put("status", "passed"); + } + when.put("line", 3); + when.put("name", "Cucumber could not execute"); + Map whenMatch = new LinkedHashMap<>(); + when.put("match", whenMatch); + whenMatch.put("arguments", new ArrayList<>()); + whenMatch.put("location", "io.cucumber.core.Failure.cucumber_could_not_execute()"); + when.put("keyword", "When "); + + { + Map thenResult = new LinkedHashMap<>(); + then.put("result", thenResult); + thenResult.put("duration", 0); + thenResult.put("error_message", exception.getMessage()); + thenResult.put("status", "failed"); + } + then.put("line", 4); + then.put("name", "Cucumber will report this error:"); + Map thenMatch = new LinkedHashMap<>(); + then.put("match", thenMatch); + thenMatch.put("arguments", new ArrayList<>()); + thenMatch.put("location", "io.cucumber.core.Failure.cucumber_reports_this_error()"); + then.put("keyword", "Then "); + } + + feature.put("name", "Test run failed"); + feature.put("description", "There were errors during the execution"); + feature.put("id", "failure"); + feature.put("keyword", "Feature"); + feature.put("uri", "classpath:io/cucumber/core/failure.feature"); + feature.put("tags", new ArrayList<>()); + } + + return feature; + } + private Map createFeatureMap(TestCase testCase) { Map featureMap = new HashMap<>(); featureMap.put("uri", TestSourcesModel.relativize(testCase.getUri())); diff --git a/core/src/main/java/io/cucumber/core/stepexpression/StepExpressionFactory.java b/core/src/main/java/io/cucumber/core/stepexpression/StepExpressionFactory.java index 317acf9cdf..5febcb05d9 100644 --- a/core/src/main/java/io/cucumber/core/stepexpression/StepExpressionFactory.java +++ b/core/src/main/java/io/cucumber/core/stepexpression/StepExpressionFactory.java @@ -102,7 +102,7 @@ private static Supplier stepDefinitionDoesNotTakeAnyParameter(StepDefiniti private CucumberException registerTypeInConfiguration(String expressionString, UndefinedParameterTypeException e) { return new CucumberException(format("" + "Could not create a cucumber expression for '%s'.\n" + - "It appears you did not register parameter type.", + "It appears you did not register a parameter type.", expressionString ), e); } diff --git a/core/src/test/java/io/cucumber/core/stepexpression/StepExpressionFactoryTest.java b/core/src/test/java/io/cucumber/core/stepexpression/StepExpressionFactoryTest.java index af2f133e35..e69f77ac54 100644 --- a/core/src/test/java/io/cucumber/core/stepexpression/StepExpressionFactoryTest.java +++ b/core/src/test/java/io/cucumber/core/stepexpression/StepExpressionFactoryTest.java @@ -97,7 +97,7 @@ void throws_for_unknown_parameter_types() { ); assertThat(exception.getMessage(), is("" + "Could not create a cucumber expression for 'Given a {unknownParameterType}'.\n" + - "It appears you did not register parameter type." + "It appears you did not register a parameter type." )); assertThat(events, iterableWithSize(1));