Skip to content

Commit

Permalink
fixed #160 and #173
Browse files Browse the repository at this point in the history
  • Loading branch information
bischoffdev committed Nov 26, 2020
1 parent 3339fa2 commit 41515e7
Show file tree
Hide file tree
Showing 19 changed files with 581 additions and 420 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

Back to [Readme](README.md).

## [1.9.0] - 2020-11-26

### Added
* Better error messages when features cannot be parsed (#173)
* `Background` steps are now preserved as `Background` in generated scenarios (#160)

## [1.8.0] - 2020-08-15

### Fixed
Expand Down Expand Up @@ -311,6 +317,7 @@ Back to [Readme](README.md).
Initial project version on GitHub and Maven Central.
[1.9.0]: https://github.com/trivago/cucable-plugin/compare/1.8.0...1.9.0
[1.8.0]: https://github.com/trivago/cucable-plugin/compare/1.7.2...1.8.0
[1.7.2]: https://github.com/trivago/cucable-plugin/compare/1.7.1...1.7.2
[1.7.1]: https://github.com/trivago/cucable-plugin/compare/1.7.0...1.7.1
Expand Down
4 changes: 2 additions & 2 deletions example-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

<groupId>com.trivago.rta</groupId>
<artifactId>cucable-test-project</artifactId>
<version>1.8.0</version>
<version>1.9.0</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.failsafe.plugin.version>3.0.0-M3</maven.failsafe.plugin.version>
<maven.build.helper.plugin.version>3.0.0</maven.build.helper.plugin.version>
<cucumber.version>5.6.0</cucumber.version>
<cucumber.version>6.9.0</cucumber.version>
<maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>
<maven.jar.plugin.version>3.1.2</maven.jar.plugin.version>

Expand Down
1 change: 1 addition & 0 deletions example-project/src/test/resources/cucumber.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cucumber.publish.quiet=true
10 changes: 5 additions & 5 deletions plugin-code/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.trivago.rta</groupId>
<artifactId>cucable-plugin</artifactId>
<version>1.8.0</version>
<version>1.9.0</version>
<url>https://github.com/trivago/cucable-plugin</url>

<name>Cucable Maven Plugin</name>
Expand Down Expand Up @@ -77,11 +77,11 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<gherkin.version>5.2.0</gherkin.version>
<tag.expressions.version>2.0.4</tag.expressions.version>
<apache.commons.version>3.10</apache.commons.version>
<tag.expressions.version>3.0.0</tag.expressions.version>
<apache.commons.version>3.11</apache.commons.version>

<mockito.version>3.3.3</mockito.version>
<junit5.vintage.version>5.6.1</junit5.vintage.version>
<mockito.version>3.6.28</mockito.version>
<junit5.vintage.version>5.7.0</junit5.vintage.version>
<openpojo.version>0.8.13</openpojo.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class FeatureFileParseException extends CucablePluginException {
*
* @param featureFileName The name of the unparsable feature file.
*/
public FeatureFileParseException(final String featureFileName) {
super("Could not parse feature file '" + featureFileName + "'.");
public FeatureFileParseException(final String featureFileName, final String message) {
super("Could not parse feature file '" + featureFileName + "': " + message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ private String getRenderedFeatureFileContent(List<SingleScenario> singleScenario
firstScenario.getFeatureDescription()
);

addBackgroundSteps(renderedContent, firstScenario.getBackgroundSteps());

for (SingleScenario singleScenario : singleScenarios) {
renderedContent.append(LINE_SEPARATOR);
List<String> scenarioTags = singleScenario.getScenarioTags();
Expand All @@ -61,7 +63,6 @@ private String getRenderedFeatureFileContent(List<SingleScenario> singleScenario
singleScenario.getScenarioName(),
singleScenario.getScenarioDescription()
);
addSteps(renderedContent, singleScenario.getBackgroundSteps());
addSteps(renderedContent, singleScenario.getSteps());
}

Expand Down Expand Up @@ -89,9 +90,9 @@ private void addLanguage(final StringBuilder stringBuilder, final String feature
return;
}
stringBuilder.append("# language: ")
.append(featureLanguage)
.append(LINE_SEPARATOR)
.append(LINE_SEPARATOR);
.append(featureLanguage)
.append(LINE_SEPARATOR)
.append(LINE_SEPARATOR);
}

/**
Expand All @@ -102,11 +103,11 @@ private void addLanguage(final StringBuilder stringBuilder, final String feature
*/
private void addComments(final StringBuilder stringBuilder, String featureFilePath) {
stringBuilder.append(LINE_SEPARATOR)
.append("# Source feature: ")
.append(featureFilePath.replace("\\", "/"))
.append(LINE_SEPARATOR)
.append("# Generated by Cucable")
.append(LINE_SEPARATOR);
.append("# Source feature: ")
.append(featureFilePath.replace("\\", "/"))
.append(LINE_SEPARATOR)
.append("# Generated by Cucable")
.append(LINE_SEPARATOR);
}

/**
Expand All @@ -126,6 +127,20 @@ private void addSteps(final StringBuilder stringBuilder, final List<Step> steps)
}
}

/**
* Adds the rendered background steps to the generated feature file content.
*
* @param stringBuilder The current feature {@link StringBuilder} instance.
* @param backgroundSteps The background {@link Step} list.
*/
private void addBackgroundSteps(StringBuilder stringBuilder, List<Step> backgroundSteps) {
if (backgroundSteps == null || backgroundSteps.isEmpty()) {
return;
}
stringBuilder.append(LINE_SEPARATOR).append("Background:").append(LINE_SEPARATOR);
addSteps(stringBuilder, backgroundSteps);
}

/**
* Adds the feature or scenario name and description to the generated feature file content.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.trivago.gherkin;

import com.trivago.exceptions.CucablePluginException;
import com.trivago.exceptions.filesystem.FeatureFileParseException;
import com.trivago.logging.CucableLogger;
import com.trivago.properties.PropertyManager;
import com.trivago.vo.DataTable;
Expand Down Expand Up @@ -51,7 +52,6 @@ public class GherkinDocumentParser {

private static final Pattern SCENARIO_OUTLINE_PLACEHOLDER_PATTERN = Pattern.compile("<.+?>");

private final TagExpressionParser tagExpressionParser = new TagExpressionParser();
private final GherkinToCucableConverter gherkinToCucableConverter;
private final GherkinTranslations gherkinTranslations;
private final PropertyManager propertyManager;
Expand All @@ -62,7 +62,8 @@ public class GherkinDocumentParser {
final GherkinToCucableConverter gherkinToCucableConverter,
final GherkinTranslations gherkinTranslations,
final PropertyManager propertyManager,
final CucableLogger logger) {
final CucableLogger logger
) {
this.gherkinToCucableConverter = gherkinToCucableConverter;
this.gherkinTranslations = gherkinTranslations;
this.propertyManager = propertyManager;
Expand All @@ -80,10 +81,16 @@ public class GherkinDocumentParser {
public List<SingleScenario> getSingleScenariosFromFeature(
final String featureContent,
final String featureFilePath,
final List<Integer> scenarioLineNumbers) throws CucablePluginException {
final List<Integer> scenarioLineNumbers
) throws CucablePluginException {

String escapedFeatureContent = featureContent.replace("\\n", "\\\\n");
GherkinDocument gherkinDocument = getGherkinDocumentFromFeatureFileContent(escapedFeatureContent);
GherkinDocument gherkinDocument;
try {
gherkinDocument = getGherkinDocumentFromFeatureFileContent(escapedFeatureContent);
} catch (CucablePluginException e) {
throw new FeatureFileParseException(featureFilePath, e.getMessage());
}

Feature feature = gherkinDocument.getFeature();
if (feature == null) {
Expand Down Expand Up @@ -113,8 +120,8 @@ public List<SingleScenario> getSingleScenariosFromFeature(
if (scenarioDefinition instanceof Scenario) {
Scenario scenario = (Scenario) scenarioDefinition;
if (scenarioLineNumbers == null
|| scenarioLineNumbers.isEmpty()
|| scenarioLineNumbers.contains(scenario.getLocation().getLine())) {
|| scenarioLineNumbers.isEmpty()
|| scenarioLineNumbers.contains(scenario.getLocation().getLine())) {
SingleScenario singleScenario =
new SingleScenario(
featureName,
Expand All @@ -138,8 +145,8 @@ public List<SingleScenario> getSingleScenariosFromFeature(
ScenarioOutline scenarioOutline = (ScenarioOutline) scenarioDefinition;

if (scenarioLineNumbers == null
|| scenarioLineNumbers.isEmpty()
|| scenarioLineNumbers.contains(scenarioOutline.getLocation().getLine())) {
|| scenarioLineNumbers.isEmpty()
|| scenarioLineNumbers.contains(scenarioOutline.getLocation().getLine())) {
List<SingleScenario> outlineScenarios =
getSingleScenariosFromOutline(
scenarioOutline,
Expand Down Expand Up @@ -243,7 +250,8 @@ private List<SingleScenario> getSingleScenariosFromOutline(
* @return A {@link Step} list with substituted names.
*/
private List<Step> substituteStepExamplePlaceholders(
final List<Step> steps, final Map<String, List<String>> exampleMap, final int rowIndex) {
final List<Step> steps, final Map<String, List<String>> exampleMap, final int rowIndex
) {

List<Step> substitutedSteps = new ArrayList<>();
for (Step step : steps) {
Expand Down Expand Up @@ -297,7 +305,8 @@ private DataTable replaceDataTableExamplePlaceholder(
* @param singleScenario an existing Cucable {@link SingleScenario}.
*/
private void addGherkinScenarioInformationToSingleScenario(
final Scenario gherkinScenario, final SingleScenario singleScenario) {
final Scenario gherkinScenario, final SingleScenario singleScenario
) {

List<String> tags = gherkinToCucableConverter.convertGherkinTagsToCucableTags(gherkinScenario.getTags());
singleScenario.setScenarioTags(tags);
Expand All @@ -322,7 +331,7 @@ private GherkinDocument getGherkinDocumentFromFeatureFileContent(final String fe
try {
gherkinDocument = gherkinDocumentParser.parse(featureContent);
} catch (ParserException parserException) {
throw new CucablePluginException("Could not parse feature: " + parserException.getMessage());
throw new CucablePluginException(parserException.getMessage());
}

if (gherkinDocument == null || gherkinDocument.getFeature() == null) {
Expand Down Expand Up @@ -356,10 +365,11 @@ private boolean scenarioShouldBeIncluded(final SingleScenario singleScenario) th
}

try {
Expression tagExpression = tagExpressionParser.parse(includeScenarioTags);
Expression tagExpression = TagExpressionParser.parse(includeScenarioTags);
return tagExpression.evaluate(combinedScenarioTags) && scenarioNameMatchExists;
} catch (TagExpressionException e) {
throw new CucablePluginException("The tag expression '" + includeScenarioTags + "' is invalid: " + e.getMessage());
throw new CucablePluginException(
"The tag expression '" + includeScenarioTags + "' is invalid: " + e.getMessage());
}

}
Expand Down Expand Up @@ -405,7 +415,8 @@ public int matchScenarioWithScenarioNames(String language, String stringToMatch)
private String replacePlaceholderInString(
final String sourceString,
final Map<String, List<String>> exampleMap,
final int rowIndex) {
final int rowIndex
) {

String result = sourceString;
Matcher m = SCENARIO_OUTLINE_PLACEHOLDER_PATTERN.matcher(sourceString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
class GherkinTranslations {

private static final String SCENARIO = "Scenario";
private static final String BACKGROUND = "Background";

private final GherkinDialectProvider gherkinDialectProvider;

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class FeatureFileParseExceptionTest {

@Test
public void testErrorMessage() {
FeatureFileParseException exception = new FeatureFileParseException("Filename");
assertThat(exception.getMessage(), is("Could not parse feature file 'Filename'."));
FeatureFileParseException exception = new FeatureFileParseException("Filename", "error message");
assertThat(exception.getMessage(), is("Could not parse feature file 'Filename': error message"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ public void testErrorOnePropertyMessage() {
List<String> properties = new ArrayList<>();
properties.add("OneProperty");
WrongOrMissingPropertiesException exception = new WrongOrMissingPropertiesException(properties);
assertThat(exception.getMessage(),
is("Property not specified correctly in the configuration section of your pom file: [OneProperty]"));
assertThat(
exception.getMessage(),
is("Property not specified correctly in the configuration section of your pom file: [OneProperty]")
);
}

@Test
Expand All @@ -41,7 +43,9 @@ public void testErrorMultiplePropertiesMessage() {
properties.add("OneProperty");
properties.add("AnotherProperty");
WrongOrMissingPropertiesException exception = new WrongOrMissingPropertiesException(properties);
assertThat(exception.getMessage(),
is("Properties not specified correctly in the configuration section of your pom file: [OneProperty, AnotherProperty]"));
assertThat(
exception.getMessage(),
is("Properties not specified correctly in the configuration section of your pom file: [OneProperty, AnotherProperty]")
);
}
}
Loading

0 comments on commit 41515e7

Please sign in to comment.