Search in sources :

Example 1 with CucablePluginException

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.");
}
Also used : CucablePluginException(com.trivago.exceptions.CucablePluginException) File(java.io.File)

Example 2 with CucablePluginException

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;
}
Also used : AstBuilder(gherkin.AstBuilder) ParserException(gherkin.ParserException) CucablePluginException(com.trivago.exceptions.CucablePluginException) GherkinDocument(gherkin.ast.GherkinDocument) TagExpressionParser(io.cucumber.tagexpressions.TagExpressionParser) Parser(gherkin.Parser)

Example 3 with CucablePluginException

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.");
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) CucablePluginException(com.trivago.exceptions.CucablePluginException)

Example 4 with CucablePluginException

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());
    }
}
Also used : TagExpressionException(io.cucumber.tagexpressions.TagExpressionException) Expression(io.cucumber.tagexpressions.Expression) CucablePluginException(com.trivago.exceptions.CucablePluginException) ArrayList(java.util.ArrayList)

Example 5 with CucablePluginException

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;
}
Also used : SingleScenario(com.trivago.vo.SingleScenario) Background(gherkin.ast.Background) CucablePluginException(com.trivago.exceptions.CucablePluginException) ArrayList(java.util.ArrayList) GherkinDocument(gherkin.ast.GherkinDocument) Step(com.trivago.vo.Step) Feature(gherkin.ast.Feature) Scenario(gherkin.ast.Scenario) SingleScenario(com.trivago.vo.SingleScenario) FeatureFileParseException(com.trivago.exceptions.filesystem.FeatureFileParseException) ScenarioOutline(gherkin.ast.ScenarioOutline) ScenarioDefinition(gherkin.ast.ScenarioDefinition)

Aggregations

CucablePluginException (com.trivago.exceptions.CucablePluginException)6 GherkinDocument (gherkin.ast.GherkinDocument)3 ArrayList (java.util.ArrayList)3 AstBuilder (gherkin.AstBuilder)2 Parser (gherkin.Parser)2 FeatureFileParseException (com.trivago.exceptions.filesystem.FeatureFileParseException)1 GherkinDocumentParser (com.trivago.gherkin.GherkinDocumentParser)1 SingleScenario (com.trivago.vo.SingleScenario)1 Step (com.trivago.vo.Step)1 ParserException (gherkin.ParserException)1 Background (gherkin.ast.Background)1 Feature (gherkin.ast.Feature)1 Scenario (gherkin.ast.Scenario)1 ScenarioDefinition (gherkin.ast.ScenarioDefinition)1 ScenarioOutline (gherkin.ast.ScenarioOutline)1 Expression (io.cucumber.tagexpressions.Expression)1 TagExpressionException (io.cucumber.tagexpressions.TagExpressionException)1 TagExpressionParser (io.cucumber.tagexpressions.TagExpressionParser)1 File (java.io.File)1 List (java.util.List)1