Skip to content

Commit

Permalink
cukexit saving re-factoring of execution-units ref #444 more to come
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Aug 29, 2018
1 parent d6e5f4b commit 0964e27
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 222 deletions.
43 changes: 27 additions & 16 deletions karate-core/src/main/java/com/intuit/karate/core/Scenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,58 +23,69 @@
*/
package com.intuit.karate.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
*
* @author pthomas3
*/
public class Scenario {

public static final String KEYWORD = "Scenario";

private final Feature feature;
private final FeatureSection section;
private final FeatureSection section;
private final int index;

private List<Tag> tags;
private int line;
private int line;
private String name;
private String description;
private List<Step> steps;
private boolean outline;

public Scenario(Feature feature, FeatureSection section, int index) {
this.feature = feature;
this.section = section;
this.index = index;
}
}

public List<Step> getStepsIncludingBackground() {
List<Step> background = feature.getBackground() == null ? null : feature.getBackground().getSteps();
int count = background == null ? steps.size() : steps.size() + background.size();
List<Step> temp = new ArrayList(count);
if (background != null) {
temp.addAll(background);
}
temp.addAll(steps);
return temp;
}

public FeatureSection getSection() {
return section;
}
}

public Feature getFeature() {
return feature;
}
}

public Collection<Tag> getTagsEffective() {
return Tags.merge(feature.getTags(), tags);
}

public int getIndex() {
return index;
}
}

public int getLine() {
return line;
}

public void setLine(int line) {
this.line = line;
}
}

public List<Tag> getTags() {
return tags;
Expand All @@ -90,7 +101,7 @@ public String getName() {

public void setName(String name) {
this.name = name;
}
}

public String getDescription() {
return description;
Expand All @@ -106,7 +117,7 @@ public List<Step> getSteps() {

public void setSteps(List<Step> steps) {
this.steps = steps;
}
}

public boolean isOutline() {
return outline;
Expand All @@ -115,5 +126,5 @@ public boolean isOutline() {
public void setOutline(boolean outline) {
this.outline = outline;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,82 +25,109 @@

import com.intuit.karate.LogAppender;
import com.intuit.karate.StepDefs;
import com.intuit.karate.StringUtils;
import com.intuit.karate.exception.KarateException;
import java.util.Iterator;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
*
* @author pthomas3
*/
public class ScenarioExecutionUnit implements ExecutionUnit<FeatureResult> {
public class ScenarioExecutionUnit implements ExecutionUnit<Void> {

private final Scenario scenario;
private final StepDefs stepDefs;
private final FeatureResult featureResult;
private final LogAppender appender;

private boolean backgroundDone = false;
private final Iterator<Step> iterator;

private BackgroundResult backgroundResult;
private ScenarioResult scenarioResult;

private boolean stopped = false;
private KarateException error;

public ScenarioExecutionUnit(Scenario scenario, StepDefs stepDefs, ExecutionContext exec) {
this.scenario = scenario;
this.stepDefs = stepDefs;
this.featureResult = exec.result;
this.appender = exec.appender;
if (scenario.getFeature().getBackground() == null) {
backgroundDone = true;
iterator = scenario.getStepsIncludingBackground().iterator();
}

void addStepResult(StepResult stepResult) {
if (stepResult.getStep().isBackground()) {
if (backgroundResult == null) {
backgroundResult = new BackgroundResult(scenario.getFeature().getBackground());
}
backgroundResult.addStepResult(stepResult);
} else {
if (scenarioResult == null) {
scenarioResult = new ScenarioResult(scenario);
}
scenarioResult.addStepResult(stepResult);
}
}

@Override
public void submit(Consumer<Runnable> system, BiConsumer<FeatureResult, KarateException> next) {
public void submit(Consumer<Runnable> system, BiConsumer<Void, KarateException> next) {
// before-scenario hook
if (stepDefs.callContext.executionHook != null) {
try {
stepDefs.callContext.executionHook.beforeScenario(scenario, stepDefs);
} catch (Exception e) {
String message = "scenario hook threw fatal error: " + e.getMessage();
stepDefs.context.logger.error(message);
featureResult.addError(e);
next.accept(featureResult, new KarateException(message, e));
next.accept(null, new KarateException(message, e));
return;
}
}
if (!backgroundDone) {
backgroundDone = true;
Background background = scenario.getFeature().getBackground();
BackgroundResult backgroundResult = new BackgroundResult(background);
system.accept(() -> {
StepListExecutionUnit unit = new StepListExecutionUnit(background.getSteps(), scenario, stepDefs, backgroundResult, appender, stopped);
unit.submit(system, (r, e) -> {
// the timing of this line below is important, it collects errors in the feature-result
featureResult.addResult(backgroundResult);
if (e != null) {
// we failed in the Background itself !
stepDefs.context.setScenarioError(e);
next.accept(featureResult, e);
} else {
stopped = r; // unlikely we ever abort in a Background, but still
if (iterator.hasNext()) {
Step step = iterator.next();
if (stopped) {
addStepResult(new StepResult(step, Result.skipped()));
ScenarioExecutionUnit.this.submit(system, next);
} else {
system.accept(() -> {
StepExecutionUnit unit = new StepExecutionUnit(step, scenario, stepDefs);
unit.submit(system, (stepResult, e) -> {
addStepResult(stepResult);
// log appender collection for each step happens here
if (step.getDocString() == null) {
String log = StringUtils.trimToNull(appender.collect());
if (log != null) {
stepResult.putDocString(log);
}
}
if (stepResult.getResult().isAborted()) {
stopped = true;
}
if (e != null) { // failed
stopped = true;
error = e;
}
ScenarioExecutionUnit.this.submit(system, next);
}
});
});
});
}
} else {
ScenarioResult scenarioResult = new ScenarioResult(scenario);
system.accept(() -> {
StepListExecutionUnit unit = new StepListExecutionUnit(scenario.getSteps(), scenario, stepDefs, scenarioResult, appender, stopped);
unit.submit(system, (r, e) -> {
// the timing of this line below is important, it collects errors in the feature-result
featureResult.addResult(scenarioResult);
if (e != null) {
stepDefs.context.setScenarioError(e);
}
if (stepDefs.callContext.executionHook != null) {
stepDefs.callContext.executionHook.afterScenario(scenarioResult, stepDefs);
}
next.accept(featureResult, e);
});
});
// these have to be done at the end after they are fully populated
// else the feature-result will not "collect" stats correctly
if (backgroundResult != null) {
featureResult.addResult(backgroundResult);
}
if (scenarioResult != null) {
featureResult.addResult(scenarioResult);
}
// after-scenario hook
if (stepDefs.callContext.executionHook != null) {
stepDefs.callContext.executionHook.afterScenario(scenarioResult, stepDefs);
}
next.accept(null, error);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public List<Scenario> getScenarios() {
Table t = et.getTable();
int rowCount = t.getRows().size();
for (int i = 1; i < rowCount; i++) { // don't include header row
Scenario s = new Scenario(feature, section, i - 1);
list.add(s);
s.setOutline(true);
s.setLine(t.getLineNumberForRow(i));
Scenario scenario = new Scenario(feature, section, i - 1);
list.add(scenario);
scenario.setOutline(true);
scenario.setLine(t.getLineNumberForRow(i));
if (tags != null || et.getTags() != null) {
List<Tag> temp = new ArrayList();
if (tags != null) {
Expand All @@ -67,10 +67,10 @@ public List<Scenario> getScenarios() {
if (et.getTags() != null) {
temp.addAll(et.getTags());
}
s.setTags(temp);
scenario.setTags(temp);
}
s.setName(name);
s.setDescription(description);
scenario.setName(name);
scenario.setDescription(description);
List<Step> replaced = new ArrayList(steps.size());
for (Step original : steps) {
String text = original.getText();
Expand All @@ -86,14 +86,14 @@ public List<Scenario> getScenarios() {
table = table.replace(token, value);
}
}
Step step = new Step(s, original.getIndex());
Step step = new Step(scenario, original.getIndex());
step.setPrefix(original.getPrefix());
step.setText(text);
step.setDocString(docString);
step.setTable(table);
replaced.add(step);
}
s.setSteps(replaced);
scenario.setSteps(replaced);
}
}
return list;
Expand Down
Loading

0 comments on commit 0964e27

Please sign in to comment.