Skip to content

Commit

Permalink
implemented most of ref #387
Browse files Browse the repository at this point in the history
karate.config.dir can set the dir in which config files will be looked for
if karate.env is set, karate-config-<env>.js will be looked for in the config dir which defaults to .karate in working dir
working dir should be the maven project base dir / or within the root
so teams can keep env over-rides in this one file
  • Loading branch information
ptrthomas committed Jun 5, 2018
1 parent b401201 commit 7d95b2c
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 20 deletions.
34 changes: 29 additions & 5 deletions karate-core/src/main/java/com/intuit/karate/ScriptBindings.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.intuit.karate.exception.KarateAbortException;
import com.intuit.karate.exception.KarateFileNotFoundException;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -54,9 +55,12 @@ public class ScriptBindings implements Bindings {
private final Map<String, Object> adds;

public static final String KARATE = "karate";
public static final String DOT_KARATE = ".karate";
public static final String KARATE_ENV = "karate.env";
public static final String KARATE_CONFIG = "karate.config";
public static final String KARATE_CONFIG_JS = "karate-config.js";
public static final String KARATE_CONFIG_DIR = "karate.config.dir";
private static final String KARATE_DASH_CONFIG = "karate-config";
private static final String DOT_JS = ".js";
public static final String KARATE_CONFIG_JS = KARATE_DASH_CONFIG + DOT_JS;
public static final String READ = "read";
public static final String PATH_MATCHES = "pathMatches";
public static final String METHOD_IS = "methodIs";
Expand All @@ -66,6 +70,8 @@ public class ScriptBindings implements Bindings {
public static final String PATH_PARAMS = "pathParams";
public static final String BODY_PATH = "bodyPath";

private static final String READ_FUNCTION = String.format("function(path){ return %s.%s(path) }", KARATE, READ);

public ScriptBindings(ScriptContext context) {
this.vars = context.vars;
this.adds = new HashMap(6); // read, karate, self, root, parent, nashorn.global
Expand All @@ -78,9 +84,27 @@ public ScriptBindings(ScriptContext context) {
adds.put(READ, readFunction.getValue());
}

private static final String READ_FUNCTION = String.format("function(path){ return %s.%s(path) }", KARATE, READ);

public static final String READ_KARATE_CONFIG = String.format("%s('%s')", READ, FileUtils.CLASSPATH_COLON + KARATE_CONFIG_JS);
private static final String READ_INVOKE = "%s('%s%s')";
private static final String READ_KARATE_CONFIG_DEFAULT = String.format(READ_INVOKE, READ, FileUtils.CLASSPATH_COLON, KARATE_CONFIG_JS);


public static final String readKarateConfigForEnv(boolean isDefault, String configDir, String env) {
if (isDefault) {
if (configDir == null) {
return READ_KARATE_CONFIG_DEFAULT;
} else {
File configFile = new File(configDir + "/" + KARATE_CONFIG_JS);
if (configFile.exists()) {
return String.format(READ_INVOKE, READ, FileUtils.FILE_COLON, configFile.getPath());
} else {
return READ_KARATE_CONFIG_DEFAULT;
}
}
} else { // configDir and env are both expected to be not null
File configFile = new File(configDir + "/" + KARATE_DASH_CONFIG + "-" + env + DOT_JS);
return String.format(READ_INVOKE, READ, FileUtils.FILE_COLON, configFile.getPath());
}
}

public static ScriptValue evalInNashorn(String exp, ScriptContext context, ScriptEvalContext evalContext) {
if (context == null) {
Expand Down
35 changes: 24 additions & 11 deletions karate-core/src/main/java/com/intuit/karate/ScriptContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public boolean isPrintEnabled() {
}

public ScriptContext(ScriptEnv env, CallContext call) {
this.env = env.refresh(null);
env = env.refresh(null);
this.env = env; // make sure references below to env.env use the updated one
logger = env.logger;
callDepth = call.callDepth;
asyncSystem = call.asyncSystem;
Expand All @@ -149,23 +150,35 @@ public ScriptContext(ScriptEnv env, CallContext call) {
client = HttpClient.construct(config, this);
bindings = new ScriptBindings(this);
if (call.parentContext == null && call.evalKarateConfig) {
try {
String configScript;
String configPath = System.getProperty(ScriptBindings.KARATE_CONFIG);
if (configPath != null) { // over-ridden by user or command-line / stand-alone jar
File configFile = new File(configPath);
configScript = String.format("%s('%s')", ScriptBindings.READ, FileUtils.FILE_COLON + configFile.getPath());
} else {
configScript = ScriptBindings.READ_KARATE_CONFIG;
}
String configDir = System.getProperty(ScriptBindings.KARATE_CONFIG_DIR);
String configScript = ScriptBindings.readKarateConfigForEnv(true, configDir, null);
try {
Script.callAndUpdateConfigAndAlsoVarsIfMapReturned(false, configScript, null, this);
} catch (Exception e) {
if (e instanceof KarateFileNotFoundException) {
logger.warn("skipping bootstrap configuration: {}", e.getMessage());
} else {
throw new RuntimeException("evaluation of " + ScriptBindings.KARATE_CONFIG_JS + " failed:", e);
throw new RuntimeException("evaluation of '" + ScriptBindings.KARATE_CONFIG_JS + "' failed", e);
}
}
if (env.env != null) {
if (configDir == null) {
configDir = ScriptBindings.DOT_KARATE;
}
File configDirFile = new File(configDir);
if (configDirFile.exists()) {
configScript = ScriptBindings.readKarateConfigForEnv(false, configDir, env.env);
try {
Script.callAndUpdateConfigAndAlsoVarsIfMapReturned(false, configScript, null, this);
} catch (Exception e) {
if (e instanceof KarateFileNotFoundException) {
logger.debug("skipping bootstrap configuration for env: {} - {}", env.env, e.getMessage());
} else {
throw new RuntimeException("evaluation of 'karate-config-" + env.env + ".js' failed", e);
}
}
}
}
}
if (call.callArg != null) { // if call.reuseParentContext is true, arg will clobber parent context
for (Map.Entry<String, Object> entry : call.callArg.entrySet()) {
Expand Down
3 changes: 3 additions & 0 deletions karate-junit4/.karate/karate-config-confdemo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function() {
return { confoverride: 'success' };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.intuit.karate.junit4.config;

import com.intuit.karate.cucumber.CucumberRunner;
import org.junit.Test;

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

@Test
public void testOverrideDir() {
System.setProperty("karate.config.dir", "src/test/java/com/intuit/karate/junit4/config");
System.setProperty("karate.env", "custom");
CucumberRunner.runFeature(this.getClass(), "config-dir.feature", null, true);
System.clearProperty("karate.config.dir");
}

@Test
public void testOverrideEnv() {
System.setProperty("karate.env", "confdemo");
CucumberRunner.runFeature(this.getClass(), "config-env.feature", null, true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@ignore
Feature: config dir over-ride

Scenario: check if ./karate-config-custom.js was invoked
* match diroverride == 'worked'
* match envoverride == 'done'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@ignore
Feature: config over-ride per environment

Scenario: check if .karate/karate-config-confdemo.js was invoked
* match confoverride == 'success'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function() {
return { envoverride: 'done' };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function() {
return { diroverride: 'worked' };
}
6 changes: 3 additions & 3 deletions karate-netty/src/main/java/com/intuit/karate/netty/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public Void call() throws Exception {
if (env != null) {
System.setProperty(ScriptBindings.KARATE_ENV, env);
}
String configPath = System.getProperty(ScriptBindings.KARATE_CONFIG);
if (configPath == null) {
System.setProperty(ScriptBindings.KARATE_CONFIG, new File(ScriptBindings.KARATE_CONFIG_JS).getPath() + "");
String configDir = System.getProperty(ScriptBindings.KARATE_CONFIG_DIR);
if (configDir == null) {
System.setProperty(ScriptBindings.KARATE_CONFIG_DIR, new File(".").getPath());
}
KarateRuntimeOptions kro = new KarateRuntimeOptions(tags, tests);
List<KarateFeature> karateFeatures = KarateFeature.loadFeatures(kro);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MainTestRunner {

@Test
public void testMain() {
System.setProperty("karate.config", "src/test/java/karate-config.js");
System.setProperty("karate.config.dir", "src/test/java");
Main.main(new String[]{"-t", "~@ignore", "src/test/java/com/intuit/karate/netty"});
}

Expand Down

0 comments on commit 7d95b2c

Please sign in to comment.