Skip to content

Commit fcc9ab6

Browse files
authored
[Core] Include all fields in JsonFormatters failure feature (#1954)
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.
1 parent fa2d198 commit fcc9ab6

File tree

4 files changed

+95
-12
lines changed

4 files changed

+95
-12
lines changed

core/pom.xml

+27
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,33 @@
137137
</archive>
138138
</configuration>
139139
</plugin>
140+
141+
<plugin>
142+
<groupId>org.apache.maven.plugins</groupId>
143+
<artifactId>maven-shade-plugin</artifactId>
144+
<executions>
145+
<execution>
146+
<phase>package</phase>
147+
<goals>
148+
<goal>shade</goal>
149+
</goals>
150+
<configuration>
151+
<artifactSet>
152+
<includes>
153+
<include>io.cucumber:gherkin</include>
154+
</includes>
155+
</artifactSet>
156+
<relocations>
157+
<relocation>
158+
<pattern>gherkin</pattern>
159+
<shadedPattern>io.cucumber.core.internal.gherkin</shadedPattern>
160+
</relocation>
161+
</relocations>
162+
</configuration>
163+
</execution>
164+
</executions>
165+
</plugin>
166+
140167
</plugins>
141168
</build>
142169

core/src/main/java/io/cucumber/core/plugin/JsonFormatter.java

+66-10
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@
3636
import java.time.ZoneOffset;
3737
import java.time.format.DateTimeFormatter;
3838
import java.util.ArrayList;
39+
import java.util.Arrays;
3940
import java.util.Base64;
4041
import java.util.HashMap;
4142
import java.util.LinkedHashMap;
4243
import java.util.List;
4344
import java.util.Map;
4445

4546
import static io.cucumber.core.exception.ExceptionUtils.printStackTrace;
47+
import static java.util.Collections.singletonList;
4648
import static java.util.Locale.ROOT;
4749
import static java.util.stream.Collectors.toList;
4850

@@ -140,16 +142,7 @@ private void handleTestStepFinished(TestStepFinished event) {
140142
private void finishReport(TestRunFinished event) {
141143
Throwable exception = event.getResult().getError();
142144
if (exception != null) {
143-
Map<String, Object> feature = new LinkedHashMap<>();
144-
feature.put("description", "Test run failed");
145-
Map<String, Object> elements = new LinkedHashMap<>();
146-
feature.put("elements", elements);
147-
elements.put("description", "There were errors during the execution");
148-
Map<String, Object> result = new LinkedHashMap<>();
149-
elements.put("result", result);
150-
result.put("error_message", exception.getMessage());
151-
result.put("status", "failed");
152-
featureMaps.add(feature);
145+
featureMaps.add(createDummyFeatureForFailure(event));
153146
}
154147

155148
gson.toJson(featureMaps, out);
@@ -160,6 +153,69 @@ private void finishReport(TestRunFinished event) {
160153
}
161154
}
162155

156+
private Map<String, Object> createDummyFeatureForFailure(TestRunFinished event) {
157+
Throwable exception = event.getResult().getError();
158+
159+
Map<String, Object> feature = new LinkedHashMap<>();
160+
feature.put("line", 1);
161+
{
162+
Map<String, Object> scenario = new LinkedHashMap<>();
163+
feature.put("elements", singletonList(scenario));
164+
165+
scenario.put("start_timestamp", getDateTimeFromTimeStamp(event.getInstant()));
166+
scenario.put("line", 2);
167+
scenario.put("name", "Could not execute Cucumber");
168+
scenario.put("description", "");
169+
scenario.put("id", "failure;could-not-execute-cucumber");
170+
scenario.put("type", "scenario");
171+
scenario.put("keyword", "Scenario");
172+
173+
Map<String, Object> when = new LinkedHashMap<>();
174+
Map<String, Object> then = new LinkedHashMap<>();
175+
scenario.put("steps", Arrays.asList(when, then));
176+
{
177+
178+
{
179+
Map<String, Object> whenResult = new LinkedHashMap<>();
180+
when.put("result", whenResult);
181+
whenResult.put("duration", 0);
182+
whenResult.put("status", "passed");
183+
}
184+
when.put("line", 3);
185+
when.put("name", "Cucumber could not execute");
186+
Map<String, Object> whenMatch = new LinkedHashMap<>();
187+
when.put("match", whenMatch);
188+
whenMatch.put("arguments", new ArrayList<>());
189+
whenMatch.put("location", "io.cucumber.core.Failure.cucumber_could_not_execute()");
190+
when.put("keyword", "When ");
191+
192+
{
193+
Map<String, Object> thenResult = new LinkedHashMap<>();
194+
then.put("result", thenResult);
195+
thenResult.put("duration", 0);
196+
thenResult.put("error_message", exception.getMessage());
197+
thenResult.put("status", "failed");
198+
}
199+
then.put("line", 4);
200+
then.put("name", "Cucumber will report this error:");
201+
Map<String, Object> thenMatch = new LinkedHashMap<>();
202+
then.put("match", thenMatch);
203+
thenMatch.put("arguments", new ArrayList<>());
204+
thenMatch.put("location", "io.cucumber.core.Failure.cucumber_reports_this_error()");
205+
then.put("keyword", "Then ");
206+
}
207+
208+
feature.put("name", "Test run failed");
209+
feature.put("description", "There were errors during the execution");
210+
feature.put("id", "failure");
211+
feature.put("keyword", "Feature");
212+
feature.put("uri", "classpath:io/cucumber/core/failure.feature");
213+
feature.put("tags", new ArrayList<>());
214+
}
215+
216+
return feature;
217+
}
218+
163219
private Map<String, Object> createFeatureMap(TestCase testCase) {
164220
Map<String, Object> featureMap = new HashMap<>();
165221
featureMap.put("uri", TestSourcesModel.relativize(testCase.getUri()));

core/src/main/java/io/cucumber/core/stepexpression/StepExpressionFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private static Supplier<Type> stepDefinitionDoesNotTakeAnyParameter(StepDefiniti
102102
private CucumberException registerTypeInConfiguration(String expressionString, UndefinedParameterTypeException e) {
103103
return new CucumberException(format("" +
104104
"Could not create a cucumber expression for '%s'.\n" +
105-
"It appears you did not register parameter type.",
105+
"It appears you did not register a parameter type.",
106106
expressionString
107107
), e);
108108
}

core/src/test/java/io/cucumber/core/stepexpression/StepExpressionFactoryTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void throws_for_unknown_parameter_types() {
9797
);
9898
assertThat(exception.getMessage(), is("" +
9999
"Could not create a cucumber expression for 'Given a {unknownParameterType}'.\n" +
100-
"It appears you did not register parameter type."
100+
"It appears you did not register a parameter type."
101101

102102
));
103103
assertThat(events, iterableWithSize(1));

0 commit comments

Comments
 (0)