Skip to content

Commit

Permalink
multi run hash detection
Browse files Browse the repository at this point in the history
  • Loading branch information
bischoffdev committed Sep 12, 2024
1 parent 721cf60 commit 34d33bc
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 31 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

Back to [Readme](README.md).

## [3.8.1] - 2024-09-12

### Changed

* Much safer detection of multi-run scenarios when using `groupPreviousScenarioRuns`

## [3.8.0] - 2024-09-06

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
package com.trivago.cluecumber.engine.json.processors;

import com.trivago.cluecumber.engine.json.pojo.Argument;
import com.trivago.cluecumber.engine.json.pojo.Element;
import com.trivago.cluecumber.engine.json.pojo.Report;
import com.trivago.cluecumber.engine.json.pojo.Row;
import com.trivago.cluecumber.engine.json.pojo.Step;
import com.trivago.cluecumber.engine.json.pojo.Tag;

import javax.inject.Inject;
Expand Down Expand Up @@ -53,17 +56,7 @@ public void addMultipleRunsInformationToScenarios(final List<Report> reports) {
// Group elements by unique ID
for (Report report : reports) {
for (Element element : report.getElements()) {
System.out.println("---------------------");
System.out.println("element.getFeatureIndex(): " + element.getFeatureIndex());
System.out.println("element.getDescription(): " + element.getDescription());
System.out.println("element.getLine(): " + element.getLine());
System.out.println("element.getFeatureName(): " + element.getFeatureName());
System.out.println("element.getName(): " + element.getName());
System.out.println("element.getId(): " + element.getId());
System.out.println("element.getTags(): " + element.getTags().stream().map(Tag::getName).collect(Collectors.joining(", ")));
System.out.println("element.getTotalNumberOfSteps(): " + element.getTotalNumberOfSteps());

String combinedId = element.getId() + element.getLine();
String combinedId = generateCombinedId(element);
elementsByUniqueId.computeIfAbsent(combinedId, k -> new ArrayList<>()).add(element);
}
}
Expand All @@ -72,19 +65,57 @@ public void addMultipleRunsInformationToScenarios(final List<Report> reports) {
for (List<Element> group : elementsByUniqueId.values()) {
if (group.size() < 2) continue;

group.sort(Comparator.comparing(Element::getStartDateTime, Comparator.nullsLast(Comparator.naturalOrder()))
.reversed());

// Remove first entry since it is the parent and add all children to it
group.sort(Comparator.comparing(Element::getStartDateTime, Comparator.nullsLast(Comparator.naturalOrder())).reversed());
Element parentElement = group.remove(0);
parentElement.setMultiRunParent(true);
group.forEach(element -> element.isMultiRunChild(true));
parentElement.setMultiRunChildren(group);
}

// Remove children from reports so they are not displayed or counted in charts and statistics
// Remove child elements from reports
for (Report report : reports) {
report.getElements().removeIf(Element::isMultiRunChild);
}
}

private String generateCombinedId(Element element) {
List<String> argumentValues = new ArrayList<>();
List<String> docStrings = new ArrayList<>();
List<List<String>> outputs = new ArrayList<>();
List<String> rows = new ArrayList<>();

for (Step step : element.getSteps()) {
step.getMatch().getArguments().stream()
.map(argument -> argument.getOffset() + ": " + argument.getVal())
.forEach(argumentValues::add);

if (step.getDocString() != null) {
docStrings.add(step.getDocString().getLine() + ": " + step.getDocString().getValue());
}

if (step.getOutput() != null) {
outputs.add(step.getOutput());
}

if (step.getRows() != null) {
step.getRows().forEach(row -> rows.add(String.join(", ", row.getCells())));
}
}

return String.valueOf((element.getFeatureIndex() +
element.getDescription() +
element.getLine() +
element.getFeatureName() +
element.getName() +
element.getId() +
element.getTags().stream().map(Tag::getName).collect(Collectors.joining(",")) +
element.getTotalNumberOfSteps() +
element.getSteps().stream().map(Step::getKeyword).collect(Collectors.joining(", ")) +
element.getSteps().stream().map(Step::getName).collect(Collectors.joining(", ")) +
String.join(", ", docStrings) +
String.join(", ", argumentValues) +
String.join(", ", rows) +
outputs.stream().map(output -> String.join(", ", output)).collect(Collectors.joining(", ")))
.hashCode());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.trivago.cluecumber.engine.json.processors;

import com.trivago.cluecumber.engine.json.pojo.Argument;
import com.trivago.cluecumber.engine.json.pojo.DocString;
import com.trivago.cluecumber.engine.json.pojo.Element;
import com.trivago.cluecumber.engine.json.pojo.Match;
import com.trivago.cluecumber.engine.json.pojo.Report;
import com.trivago.cluecumber.engine.json.pojo.Row;
import com.trivago.cluecumber.engine.json.pojo.Step;
Expand Down Expand Up @@ -98,7 +100,7 @@ public void addMultipleRunsInformationToSameScenariosWithDifferentKeywords() {
elementMultipleRunsPreProcessor.addMultipleRunsInformationToScenarios(reports);

List<Element> newElements = reports.get(0).getElements();
assertEquals(newElements.size(), 2);
assertEquals(2, newElements.size());
}

@Test
Expand All @@ -107,6 +109,40 @@ public void addMultipleRunsInformationToSameScenariosWithDifferentParameterValue
Report report = new Report();
List<Element> elements = new ArrayList<>();

Step step = new Step();
Argument argument = new Argument();
argument.setOffset(1);
argument.setVal("abc");
Match match = new Match();
match.setArguments(List.of(argument));
step.setMatch(match);
Element element = createNewElement(List.of(step));
elements.add(element);

step = new Step();
argument = new Argument();
argument.setOffset(1);
argument.setVal("def");
match = new Match();
match.setArguments(List.of(argument));
step.setMatch(match); element = createNewElement(List.of(step));
elements.add(element);

report.setElements(elements);
reports.add(report);

elementMultipleRunsPreProcessor.addMultipleRunsInformationToScenarios(reports);

List<Element> newElements = reports.get(0).getElements();
assertEquals(2, newElements.size());
}

@Test
public void addMultipleRunsInformationToSameScenariosWithDifferentNames() {
List<Report> reports = new ArrayList<>();
Report report = new Report();
List<Element> elements = new ArrayList<>();

Step step = new Step();
step.setName("Given I have 4 apples");
Element element = createNewElement(List.of(step));
Expand All @@ -123,7 +159,7 @@ public void addMultipleRunsInformationToSameScenariosWithDifferentParameterValue
elementMultipleRunsPreProcessor.addMultipleRunsInformationToScenarios(reports);

List<Element> newElements = reports.get(0).getElements();
assertEquals(newElements.size(), 2);
assertEquals(2, newElements.size());
}

@Test
Expand Down Expand Up @@ -152,7 +188,7 @@ public void addMultipleRunsInformationToSameScenariosWithDifferentDocStrings() {
elementMultipleRunsPreProcessor.addMultipleRunsInformationToScenarios(reports);

List<Element> newElements = reports.get(0).getElements();
assertEquals(newElements.size(), 2);
assertEquals(2, newElements.size());
}

@Test
Expand All @@ -177,7 +213,7 @@ public void addMultipleRunsInformationToSameScenariosWithDifferentOutputs() {
elementMultipleRunsPreProcessor.addMultipleRunsInformationToScenarios(reports);

List<Element> newElements = reports.get(0).getElements();
assertEquals(newElements.size(), 2);
assertEquals(2, newElements.size());
}

@Test
Expand All @@ -189,15 +225,17 @@ public void addMultipleRunsInformationToSameScenariosWithDifferentDataTableRows(
Step step = new Step();
List<Row> rows = new ArrayList<>();
Row row = new Row();
row.setCells(List.of("abc"));
row.setCells(List.of("aaa", "bbb"));
rows.add(row);
step.setRows(rows);
Element element = createNewElement(List.of(step));
elements.add(element);

step = new Step();
rows = new ArrayList<>();
row = new Row();
row.setCells(List.of("def"));
row.setCells(List.of("aaa", "ddd"));
rows.add(row);
step.setRows(rows);
element = createNewElement(List.of(step));
elements.add(element);
Expand All @@ -207,8 +245,8 @@ public void addMultipleRunsInformationToSameScenariosWithDifferentDataTableRows(

elementMultipleRunsPreProcessor.addMultipleRunsInformationToScenarios(reports);

List<Element> newElements = reports.get(0).getElements();
assertEquals(newElements.size(), 2);
List<Element> newElements = reports.get(0). getElements();
assertEquals(2, newElements.size());
}

@Test
Expand Down Expand Up @@ -237,7 +275,7 @@ public void addMultipleRunsInformationToSameScenariosWithDifferentTags() {
elementMultipleRunsPreProcessor.addMultipleRunsInformationToScenarios(reports);

List<Element> newElements = reports.get(0).getElements();
assertEquals(newElements.size(), 2);
assertEquals(2, newElements.size());
}

@Test
Expand All @@ -262,7 +300,7 @@ public void addMultipleRunsInformationToSameScenariosWithDifferentDescription()
elementMultipleRunsPreProcessor.addMultipleRunsInformationToScenarios(reports);

List<Element> newElements = reports.get(0).getElements();
assertEquals(newElements.size(), 2);
assertEquals(2, newElements.size());
}

@Test
Expand All @@ -287,7 +325,7 @@ public void addMultipleRunsInformationToSameScenariosWithDifferentFeatureNames()
elementMultipleRunsPreProcessor.addMultipleRunsInformationToScenarios(reports);

List<Element> newElements = reports.get(0).getElements();
assertEquals(newElements.size(), 2);
assertEquals(2, newElements.size());
}

private Element createNewElement(final List<Step> steps) {
Expand Down
2 changes: 1 addition & 1 deletion examples/core-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>blog.softwaretester</groupId>
<artifactId>core-example</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
<packaging>jar</packaging>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions examples/maven-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>blog.softwaretester</groupId>
<artifactId>maven-example</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
<packaging>pom</packaging>

<properties>
Expand Down Expand Up @@ -37,7 +37,7 @@
</executions>
<configuration>
<!-- The only two mandatory properties -->
<sourceJsonReportDirectory>${cucumber.report.json.location}/rerun2</sourceJsonReportDirectory>
<sourceJsonReportDirectory>${cucumber.report.json.location}</sourceJsonReportDirectory>
<generatedHtmlReportDirectory>${generated.report.location}</generatedHtmlReportDirectory>

<!-- Optional custom parameters that are shown on the start page (if they have a value) -->
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</modules>

<properties>
<revision>3.8.0</revision>
<revision>3.8.1</revision>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
Expand Down

0 comments on commit 34d33bc

Please sign in to comment.