Skip to content

Commit

Permalink
cukexit almost done - ui and gatling ported and deleted old cucumber …
Browse files Browse the repository at this point in the history
…wrappers

finally. this is a big milestone and what is remaining is mainly the junit-html report
there is scope for improvement, e.g. the stepdefs, context and callContext are managing the same state
but we can declare success - that we no longer depend on cucumber
  • Loading branch information
ptrthomas committed Aug 27, 2018
1 parent f22cfa0 commit d6e5f4b
Show file tree
Hide file tree
Showing 77 changed files with 602 additions and 4,244 deletions.
17 changes: 7 additions & 10 deletions karate-core/src/main/java/com/intuit/karate/CallContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
*/
package com.intuit.karate;

import com.intuit.karate.core.ScenarioHook;
import com.intuit.karate.cucumber.ScenarioInfo;
import com.intuit.karate.cucumber.StepInterceptor;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import com.intuit.karate.core.ExecutionHook;

/**
*
Expand All @@ -45,8 +44,7 @@ public class CallContext {
public final String httpClientClass;
public final Consumer<Runnable> asyncSystem;
public final Runnable asyncNext;
public final StepInterceptor stepInterceptor;
public final ScenarioHook scenarioHook;
public final ExecutionHook executionHook;

private List<String> tags;
private Map<String, List<String>> tagValues;
Expand Down Expand Up @@ -81,16 +79,16 @@ public boolean isCalled() {
}

public CallContext(Map<String, Object> callArg, boolean evalKarateConfig) {
this(null, 0, callArg, -1, false, evalKarateConfig, null, null, null, null, null);
this(null, 0, callArg, -1, false, evalKarateConfig, null, null, null, null);
}

public CallContext(ScenarioHook scenarioHook) {
this(null, 0, null, -1, false, true, null, null, null, null, scenarioHook);
public CallContext(ExecutionHook scenarioHook) {
this(null, 0, null, -1, false, true, null, null, null, scenarioHook);
}

public CallContext(ScriptContext parentContext, int callDepth, Map<String, Object> callArg, int loopIndex,
boolean reuseParentContext, boolean evalKarateConfig, String httpClientClass,
Consumer<Runnable> asyncSystem, Runnable asyncNext, StepInterceptor stepInterceptor, ScenarioHook scenarioHook) {
Consumer<Runnable> asyncSystem, Runnable asyncNext, ExecutionHook scenarioHook) {
this.parentContext = parentContext;
this.callDepth = callDepth;
this.callArg = callArg;
Expand All @@ -100,8 +98,7 @@ public CallContext(ScriptContext parentContext, int callDepth, Map<String, Objec
this.httpClientClass = httpClientClass;
this.asyncSystem = asyncSystem;
this.asyncNext = asyncNext;
this.stepInterceptor = stepInterceptor;
this.scenarioHook = scenarioHook;
this.executionHook = scenarioHook;
}

}
45 changes: 3 additions & 42 deletions karate-core/src/main/java/com/intuit/karate/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import com.intuit.karate.cucumber.FeatureFilePath;
import com.intuit.karate.exception.KarateFileNotFoundException;
import com.jayway.jsonpath.DocumentContext;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
import com.intuit.karate.core.Feature;
import com.intuit.karate.core.FeatureParser;
import java.net.URI;
Expand Down Expand Up @@ -239,6 +235,9 @@ public static ClassLoader createClassLoader(String... paths) {
}

public static String toPackageQualifiedName(String path) {
if (path == null) {
return "(in memory)";
}
path = removePrefix(path);
String packagePath = path.replace("/", "."); // assumed to be already in non-windows form
if (packagePath.endsWith(".feature")) {
Expand All @@ -260,40 +259,6 @@ public static String getFeaturePath(String commandLine, String cwd) {
return commandLine.substring(start, end + 8);
}

private static String searchPattern(String one, String two, char c) {
return c + "src" + c + one + c + two + c;
}

private static final String[] SEARCH_PATTERNS = {
searchPattern("test", "java", '/'),
searchPattern("test", "resources", '/'),
searchPattern("main", "java", '/'),
searchPattern("main", "resources", '/')
};

private static String[] getSearchPaths(String rootPath) {
String[] res = new String[SEARCH_PATTERNS.length];
for (int i = 0; i < SEARCH_PATTERNS.length; i++) {
res[i] = new File(rootPath + SEARCH_PATTERNS[i]).getPath();
}
return res;
}

public static FeatureFilePath parseFeaturePath(File file) {
String path = file.getAbsolutePath();
path = path.replace('\\', '/'); // normalize windows
for (String pattern : SEARCH_PATTERNS) {
int pos = path.lastIndexOf(pattern);
if (pos != -1) { // found
String rootPath = path.substring(0, pos);
String[] searchPaths = getSearchPaths(rootPath);
return new FeatureFilePath(file, searchPaths);
}
}
String[] searchPaths = {file.getParentFile().getPath()};
return new FeatureFilePath(file, searchPaths);
}

public static String toString(File file) {
try {
return toString(new FileInputStream(file));
Expand Down Expand Up @@ -381,10 +346,6 @@ public static InputStream toInputStream(String text) {
return new ByteArrayInputStream(text.getBytes(UTF8));
}

public static List<String> toStringLines(String text) {
return new BufferedReader(new StringReader(text)).lines().collect(Collectors.toList());
}

public static String replaceFileExtension(String path, String extension) {
int pos = path.lastIndexOf('.');
if (pos == -1) {
Expand Down
2 changes: 1 addition & 1 deletion karate-core/src/main/java/com/intuit/karate/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static Match parse(String exp) {
private Match() {
ScriptEnv env = ScriptEnv.forEnvAndCurrentWorkingDir(null);
CallContext callContext = new CallContext(null, 0, null, -1, false, false,
DummyHttpClient.class.getName(), null, null, null, null);
DummyHttpClient.class.getName(), null, null, null);
context = new ScriptContext(env, callContext);
}

Expand Down
6 changes: 4 additions & 2 deletions karate-core/src/main/java/com/intuit/karate/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -1688,11 +1688,13 @@ public static ScriptValue evalFeatureCall(Feature feature, Object callArg, Scrip
private static ScriptValue evalFeatureCall(Feature feature, ScriptContext context,
Map<String, Object> callArg, int loopIndex, boolean reuseParentConfig) {
CallContext callContext = new CallContext(context, context.callDepth + 1, callArg, loopIndex,
reuseParentConfig, false, null, context.asyncSystem, null, context.stepInterceptor, null);
reuseParentConfig, false, null, context.asyncSystem, null, context.executionHook);
// if (context.env.reporter != null) { TODO call reporting
// context.env.reporter.callBegin(feature, callContext);
// }
FeatureResult result = Engine.executeSync(null, feature, null, callContext);
// the call is going to execute synchronously ! TODO improve
// which is why the context.asyncSystem, context.asyncNext is not really needed
FeatureResult result = Engine.execute(null, feature, null, callContext);
if (result.isFailed()) {
Throwable error = result.getErrors().get(0);
if (error instanceof KarateException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/
package com.intuit.karate;

import com.intuit.karate.core.ExecutionHook;
import com.intuit.karate.cucumber.ScenarioInfo;
import com.intuit.karate.cucumber.StepInterceptor;
import com.intuit.karate.exception.KarateFileNotFoundException;
import com.intuit.karate.http.Cookie;
import com.intuit.karate.http.HttpClient;
Expand Down Expand Up @@ -56,7 +56,7 @@ public class ScriptContext {
protected final ScriptEnv env;
protected final Consumer<Runnable> asyncSystem;
protected final Runnable asyncNext;
protected final StepInterceptor stepInterceptor;
protected final ExecutionHook executionHook;

protected final ScenarioInfo scenarioInfo;

Expand Down Expand Up @@ -118,7 +118,7 @@ public ScriptContext(ScriptEnv env, CallContext call) {
logger = env.logger;
callDepth = call.callDepth;
asyncSystem = call.asyncSystem;
stepInterceptor = call.stepInterceptor;
executionHook = call.executionHook;
asyncNext = call.asyncNext;
tags = call.getTags();
tagValues = call.getTagValues();
Expand Down
20 changes: 11 additions & 9 deletions karate-core/src/main/java/com/intuit/karate/ScriptEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package com.intuit.karate;

import com.intuit.karate.cucumber.KarateReporter;
import java.io.File;

/**
Expand All @@ -39,23 +38,21 @@ public class ScriptEnv {
public final String featureName;
public final ClassLoader fileClassLoader;
public final CallCache callCache;
public final KarateReporter reporter;

public ScriptEnv(String env, String tagSelector, File featureDir, String featureName, ClassLoader fileClassLoader,
CallCache callCache, Logger logger, KarateReporter reporter) {
CallCache callCache, Logger logger) {
this.env = env;
this.tagSelector = tagSelector;
this.featureDir = featureDir;
this.featureName = featureName;
this.fileClassLoader = fileClassLoader;
this.callCache = callCache;
this.logger = logger;
this.reporter = reporter;
}

public ScriptEnv(String env, String tagSelector, File featureDir, String featureName, ClassLoader fileClassLoader, KarateReporter reporter) {
public ScriptEnv(String env, String tagSelector, File featureDir, String featureName, ClassLoader fileClassLoader) {
this(env, tagSelector, featureDir, featureName, fileClassLoader, new CallCache(),
new Logger(), reporter);
new Logger());
}

public static ScriptEnv forEnvAndCurrentWorkingDir(String env) {
Expand All @@ -67,13 +64,18 @@ public static ScriptEnv forEnvAndClass(String env, Class clazz) {
}

private static ScriptEnv forEnvAndWorkingDir(String env, File workingDir) {
return new ScriptEnv(env, null, workingDir, null, Thread.currentThread().getContextClassLoader(), null);
return new ScriptEnv(env, null, workingDir, null, Thread.currentThread().getContextClassLoader());
}

public static ScriptEnv forEnvAndFeatureFile(String env, File featureFile) {
return forFeatureFile(env, null, featureFile, Thread.currentThread().getContextClassLoader());
}

public static ScriptEnv forEnvFeatureFileAndLogger(String env, File featureFile, Logger logger) {
return new ScriptEnv(env, null, featureFile.getParentFile(), featureFile.getName(),
Thread.currentThread().getContextClassLoader(), new CallCache(), logger);
}

public static ScriptEnv forEnvTagsAndFeatureFile(String env, String tagSelector, File featureFile) {
return forFeatureFile(env, tagSelector, featureFile, Thread.currentThread().getContextClassLoader());
}
Expand All @@ -83,7 +85,7 @@ public static ScriptEnv forEnvAndFeatureFile(String env, File featureFile, Strin
}

private static ScriptEnv forFeatureFile(String env, String tagSelector, File featureFile, ClassLoader classLoader) {
return new ScriptEnv(env, tagSelector, featureFile.getParentFile(), featureFile.getName(), classLoader, null);
return new ScriptEnv(env, tagSelector, featureFile.getParentFile(), featureFile.getName(), classLoader);
}

public ScriptEnv refresh(String in) { // immutable
Expand All @@ -94,7 +96,7 @@ public ScriptEnv refresh(String in) { // immutable
karateEnv = StringUtils.trimToNull(System.getProperty(ScriptBindings.KARATE_ENV));
}
}
return new ScriptEnv(karateEnv, tagSelector, featureDir, featureName, fileClassLoader, callCache, logger, reporter);
return new ScriptEnv(karateEnv, tagSelector, featureDir, featureName, fileClassLoader, callCache, logger);
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions karate-core/src/main/java/com/intuit/karate/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
*/
package com.intuit.karate;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
*
Expand Down Expand Up @@ -156,5 +159,9 @@ public static StringUtils.Pair splitByFirstLineFeed(String text) {
}
return StringUtils.pair(left, right);
}

public static List<String> toStringLines(String text) {
return new BufferedReader(new StringReader(text)).lines().collect(Collectors.toList());
}

}
15 changes: 8 additions & 7 deletions karate-core/src/main/java/com/intuit/karate/core/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ public static String getBuildDir() {
return command.contains("org.gradle.") ? "build" : "target";
}

public static FeatureResult executeSync(String envString, Feature feature, String tagSelector, CallContext callContext) {
public static FeatureResult execute(String envString, Feature feature, String tagSelector, CallContext callContext) {
File file = feature.getFile();
ScriptEnv env = ScriptEnv.forEnvTagsAndFeatureFile(envString, tagSelector, file);
if (callContext == null) {
callContext = new CallContext(null, true);
}
ExecutionContext exec = new ExecutionContext(feature, env, callContext);
boolean enableFileLogAppender = callContext.asyncSystem == null;
ExecutionContext exec = new ExecutionContext(feature, env, callContext, enableFileLogAppender);
FeatureExecutionUnit unit = new FeatureExecutionUnit(exec);
unit.submit(SYNC_EXECUTOR, NO_OP);
return exec.result;
Expand Down Expand Up @@ -290,16 +291,16 @@ public static File saveResultHtml(String targetDir, FeatureResult result) {
Iterator<ResultElement> iterator = result.getElements().iterator();
ResultElement prev = null;
while (iterator.hasNext()) {
ResultElement element = iterator.next();
if (element.isBackground()) {
ResultElement element = iterator.next();
if (element.isBackground()) {
prev = element;
} else {
Node scenarioDiv = div(doc, "scenario");
append(doc, "/html/body/div", scenarioDiv);
append(doc, "/html/body/div", scenarioDiv);
Node scenarioHeadingDiv = div(doc, "scenario-heading",
node(doc, "span", "scenario-keyword", element.getKeyword() + ": "),
node(doc, "span", "scenario-name", element.getName()));
scenarioDiv.appendChild(scenarioHeadingDiv);
scenarioDiv.appendChild(scenarioHeadingDiv);
prev = null;
}
}
Expand Down Expand Up @@ -331,7 +332,7 @@ private static List<MethodMatch> findMethodsMatching(String text) {
}
return matches;
}

public static String fromCucumberOptionsTags(List<String> tags) {
if (tags == null || tags.isEmpty()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,32 @@ public class ExecutionContext {
public final FeatureResult result;
public final LogAppender appender;

public ExecutionContext(Feature feature, ScriptEnv env, CallContext callContext) {
public ExecutionContext(Feature feature, ScriptEnv env, CallContext callContext, boolean enableFileLogAppender) {
this.feature = feature;
result = new FeatureResult(feature);
this.env = env;
this.callContext = callContext;
String basePath = feature.getPackageQualifiedName();
this.appender = new FileLogAppender(Engine.getBuildDir() + "/surefire-reports/" + basePath + ".log", env.logger);
if (enableFileLogAppender) {
String basePath = feature.getPackageQualifiedName();
appender = new FileLogAppender(Engine.getBuildDir() + "/surefire-reports/" + basePath + ".log", env.logger);
} else {
appender = new LogAppender() {
@Override
public String collect() {
return "";
}

@Override
public void append(String text) {

}

@Override
public void close() {

}
};
}
}

}
Loading

0 comments on commit d6e5f4b

Please sign in to comment.