Search in sources :

Example 6 with CucablePluginException

use of com.trivago.exceptions.CucablePluginException in project cucable-plugin by trivago.

the class FeatureFileConverter method generateRunnerClassesWithDesiredNumberOfRunners.

/**
 * Generate runner classes for a list of feature file paths.
 *
 * @param generatedFeatureNames  The list of generated feature file names.
 * @param numberOfDesiredRunners The number of desired runners (if set to 0, a runner is generated for each feature file path).
 * @return The number of generated runners.
 * @throws CucablePluginException see {@link CucablePluginException}.
 */
private int generateRunnerClassesWithDesiredNumberOfRunners(final List<String> generatedFeatureNames, final int numberOfDesiredRunners) throws CucablePluginException {
    int targetRunnerNumber = numberOfDesiredRunners;
    List<String> scenarioNames = propertyManager.getScenarioNames();
    if (targetRunnerNumber == 0) {
        targetRunnerNumber = generatedFeatureNames.size();
    }
    List<List<String>> generatedFeatureNamesPerRunner = new ArrayList<>(targetRunnerNumber);
    for (int i = 0; i < targetRunnerNumber; i++) {
        generatedFeatureNamesPerRunner.add(new ArrayList<>());
    }
    int currentRunnerIndex = 0;
    int matchCount = 0;
    for (String generatedFeatureName : generatedFeatureNames) {
        if (scenarioNames.isEmpty()) {
            generatedFeatureNamesPerRunner.get(currentRunnerIndex).add(generatedFeatureName);
            currentRunnerIndex++;
            if (currentRunnerIndex >= targetRunnerNumber) {
                currentRunnerIndex = 0;
            }
        } else {
            // Move all scenarios matching a scenario name into its own group.
            String scenarioText = fileIO.readContentFromFile(propertyManager.getGeneratedFeatureDirectory() + "/" + generatedFeatureName + ".feature");
            if (scenarioText != null) {
                Parser<GherkinDocument> parser = new Parser<>(new AstBuilder());
                String language = parser.parse(scenarioText).getFeature().getLanguage();
                int listIndex = gherkinDocumentParser.matchScenarioWithScenarioNames(language, scenarioText);
                if (listIndex >= 0) {
                    generatedFeatureNamesPerRunner.get(listIndex).add(generatedFeatureName);
                    matchCount++;
                }
            }
        }
    }
    if (!scenarioNames.isEmpty() && matchCount == 0) {
        throw new CucablePluginException("No matching scenarios found for specified scenario names - " + Arrays.toString(scenarioNames.toArray()) + "!");
    }
    int runnerFileCounter = 0;
    for (List<String> generatedFeatureNamesForSingleRunner : generatedFeatureNamesPerRunner) {
        if (generatedFeatureNamesForSingleRunner.size() > 0) {
            generateRunnerClass(generatedFeatureNamesForSingleRunner);
            runnerFileCounter++;
        }
    }
    return runnerFileCounter;
}
Also used : AstBuilder(gherkin.AstBuilder) CucablePluginException(com.trivago.exceptions.CucablePluginException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GherkinDocument(gherkin.ast.GherkinDocument) GherkinDocumentParser(com.trivago.gherkin.GherkinDocumentParser) Parser(gherkin.Parser)

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