use of com.trivago.exceptions.CucablePluginException in project cucable-plugin by trivago.
the class FileSystemManager method getPathsFromCucableFeature.
/**
* Returns a list of feature file paths located in the specified source feature directory.
*
* @return a list of feature file paths.
* @throws CucablePluginException see {@link CucablePluginException}
*/
public List<Path> getPathsFromCucableFeature(final CucableFeature cucableFeature) throws CucablePluginException {
if (cucableFeature == null) {
return Collections.emptyList();
}
String sourceFeatures = cucableFeature.getName().replace("file://", "");
File sourceFeaturesFile = new File(sourceFeatures);
if (sourceFeatures.trim().isEmpty()) {
return Collections.emptyList();
}
// Check if the property value is a single file or a directory
if (sourceFeaturesFile.isFile() && sourceFeatures.endsWith(FEATURE_FILE_EXTENSION)) {
return Collections.singletonList(Paths.get(sourceFeatures));
}
if (sourceFeaturesFile.isDirectory()) {
return getFilesWithFeatureExtension(sourceFeatures);
}
throw new CucablePluginException(sourceFeatures + " is not a feature file or a directory.");
}
use of com.trivago.exceptions.CucablePluginException in project cucable-plugin by trivago.
the class GherkinDocumentParser method getGherkinDocumentFromFeatureFileContent.
/**
* Get a {@link GherkinDocument} from a feature file for further processing.
*
* @param featureContent a feature string.
* @return a {@link GherkinDocument}.
* @throws CucablePluginException see {@link CucablePluginException}.
*/
private GherkinDocument getGherkinDocumentFromFeatureFileContent(final String featureContent) throws CucablePluginException {
Parser<GherkinDocument> gherkinDocumentParser = new Parser<>(new AstBuilder());
GherkinDocument gherkinDocument;
try {
gherkinDocument = gherkinDocumentParser.parse(featureContent);
} catch (ParserException parserException) {
throw new CucablePluginException(parserException.getMessage());
}
if (gherkinDocument == null || gherkinDocument.getFeature() == null) {
cucableLogger.warn("No parsable gherkin.");
}
return gherkinDocument;
}
use of com.trivago.exceptions.CucablePluginException in project cucable-plugin by trivago.
the class RunnerFileContentRenderer method checkForPlaceholderErrors.
/**
* Check for placeholder exceptions in the specified template.
*
* @param runnerFileContentString The source string.
* @throws CucablePluginException see {@link CucablePluginException}.
*/
private void checkForPlaceholderErrors(final String runnerFileContentString) throws CucablePluginException {
// Catch legacy placeholder usage
if (runnerFileContentString.contains(FEATURE_FILE_NAME_PLACEHOLDER)) {
throw new CucablePluginException("The " + FEATURE_FILE_NAME_PLACEHOLDER + " placeholder is deprecated. Please use " + CUCABLE_FEATURE_PLACEHOLDER + " or " + CUCABLE_RUNNER_PLACEHOLDER + " accordingly.");
}
// Check if at least one feature placeholder is present
final String regex = "\\" + CUCABLE_FEATURE_PLACEHOLDER;
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(runnerFileContentString);
if (!matcher.find()) {
throw new CucablePluginException("At least one " + CUCABLE_FEATURE_PLACEHOLDER + " placeholder is needed in your template.");
}
}
use of com.trivago.exceptions.CucablePluginException in project cucable-plugin by trivago.
the class GherkinDocumentParser method scenarioShouldBeIncluded.
/**
* Checks if a scenario should be included in the runner and feature generation based on the tag settings and
* scenarioNames settings.
*
* @param singleScenario a single scenario object.
* @return true if the combined tags match the given tag expression and the scenario name (if specified) matches.
*/
private boolean scenarioShouldBeIncluded(final SingleScenario singleScenario) throws CucablePluginException {
String includeScenarioTags = propertyManager.getIncludeScenarioTags();
String language = singleScenario.getFeatureLanguage();
String scenarioName = singleScenario.getScenarioName();
boolean scenarioNameMatchExists = matchScenarioWithScenarioNames(language, scenarioName) >= 0;
List<String> combinedScenarioTags = new ArrayList<>(singleScenario.getScenarioTags());
combinedScenarioTags.addAll(singleScenario.getFeatureTags());
combinedScenarioTags.addAll(singleScenario.getExampleTags());
combinedScenarioTags = combinedScenarioTags.stream().distinct().collect(Collectors.toList());
if (includeScenarioTags == null || includeScenarioTags.isEmpty()) {
return scenarioNameMatchExists;
}
try {
Expression tagExpression = TagExpressionParser.parse(includeScenarioTags);
return tagExpression.evaluate(combinedScenarioTags) && scenarioNameMatchExists;
} catch (TagExpressionException e) {
throw new CucablePluginException("The tag expression '" + includeScenarioTags + "' is invalid: " + e.getMessage());
}
}
use of com.trivago.exceptions.CucablePluginException in project cucable-plugin by trivago.
the class GherkinDocumentParser method getSingleScenariosFromFeature.
/**
* Returns a {@link SingleScenario} list from a given feature file.
*
* @param featureContent A feature string.
* @param featureFilePath The path to the source feature file.
* @return A {@link SingleScenario} list.
* @throws CucablePluginException see {@link CucablePluginException}.
*/
public List<SingleScenario> getSingleScenariosFromFeature(final String featureContent, final String featureFilePath, final List<Integer> scenarioLineNumbers) throws CucablePluginException {
String escapedFeatureContent = featureContent.replace("\\n", "\\\\n");
GherkinDocument gherkinDocument;
try {
gherkinDocument = getGherkinDocumentFromFeatureFileContent(escapedFeatureContent);
} catch (CucablePluginException e) {
throw new FeatureFileParseException(featureFilePath, e.getMessage());
}
Feature feature = gherkinDocument.getFeature();
if (feature == null) {
return Collections.emptyList();
}
String featureName = feature.getKeyword() + ": " + feature.getName();
String featureLanguage = feature.getLanguage();
String featureDescription = feature.getDescription();
List<String> featureTags = gherkinToCucableConverter.convertGherkinTagsToCucableTags(feature.getTags());
ArrayList<SingleScenario> singleScenarioFeatures = new ArrayList<>();
List<Step> backgroundSteps = new ArrayList<>();
List<ScenarioDefinition> scenarioDefinitions = feature.getChildren();
for (ScenarioDefinition scenarioDefinition : scenarioDefinitions) {
String scenarioName = scenarioDefinition.getKeyword() + ": " + scenarioDefinition.getName();
String scenarioDescription = scenarioDefinition.getDescription();
if (scenarioDefinition instanceof Background) {
// Save background steps in order to add them to every scenario inside the same feature
Background background = (Background) scenarioDefinition;
backgroundSteps = gherkinToCucableConverter.convertGherkinStepsToCucableSteps(background.getSteps());
continue;
}
if (scenarioDefinition instanceof Scenario) {
Scenario scenario = (Scenario) scenarioDefinition;
if (scenarioLineNumbers == null || scenarioLineNumbers.isEmpty() || scenarioLineNumbers.contains(scenario.getLocation().getLine())) {
SingleScenario singleScenario = new SingleScenario(featureName, featureFilePath, featureLanguage, featureDescription, scenarioName, scenarioDescription, featureTags, backgroundSteps);
addGherkinScenarioInformationToSingleScenario(scenario, singleScenario);
if (scenarioShouldBeIncluded(singleScenario)) {
singleScenarioFeatures.add(singleScenario);
}
}
continue;
}
if (scenarioDefinition instanceof ScenarioOutline) {
ScenarioOutline scenarioOutline = (ScenarioOutline) scenarioDefinition;
if (scenarioLineNumbers == null || scenarioLineNumbers.isEmpty() || scenarioLineNumbers.contains(scenarioOutline.getLocation().getLine())) {
List<SingleScenario> outlineScenarios = getSingleScenariosFromOutline(scenarioOutline, featureName, featureFilePath, featureLanguage, featureDescription, featureTags, backgroundSteps);
for (SingleScenario singleScenario : outlineScenarios) {
if (scenarioShouldBeIncluded(singleScenario)) {
singleScenarioFeatures.add(singleScenario);
}
}
}
}
}
return singleScenarioFeatures;
}
Aggregations