Skip to content

Commit

Permalink
part 3 hopefully the final fix for karatelabs#1515
Browse files Browse the repository at this point in the history
problem 1: very bad bug in initMagicVariables where we were setting __arg to a wrapper not the raw object
problem 2: we had completely missed hydrating / re-attaching magic variables
hopefully that explains the randomness, the incoming __arg may contain nested objects etc
  • Loading branch information
ptrthomas committed Mar 27, 2021
1 parent d8a416a commit 78ed24f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ public Object callSingle(String fileName, Value arg) throws Exception {
if (arg == null || arg.isNull()) {
argVar = null;
} else {
engine.recurseAndAttach(arg);
argVar = new Variable(arg);
}
Variable resultVar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ private void httpInvokeOnce() {
}
if (hooks != null) {
hooks.forEach(h -> h.afterHttpCall(request, response, runtime));
}
}
byte[] bytes = response.getBody();
Object body;
String responseType;
Expand Down Expand Up @@ -996,7 +996,12 @@ public void doc(String exp) {
public void init() { // not in constructor because it has to be on Runnable.run() thread
JS = JsEngine.local();
logger.trace("js context: {}", JS);
runtime.magicVariables.forEach((k, v) -> setHiddenVariable(k, v));
runtime.magicVariables.forEach((k, v) -> {
// even hidden variables may need pre-processing
// for e.g. the __arg may contain functions that originated in a different js context
recurseAndAttach(v);
setHiddenVariable(k, v);
});
attachVariables(); // re-hydrate any functions from caller or background
setHiddenVariable(KARATE, bridge);
setHiddenVariable(READ, readFunction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private Map<String, Object> initMagicVariables() {
// so we inject the parent magic variables
// but they will be over-written by what is local to this scenario
map.putAll(caller.parentRuntime.magicVariables);
map.put("__arg", caller.arg);
map.put("__arg", caller.arg == null ? null : caller.arg.getValue());
map.put("__loop", caller.getLoopIndex());
if (caller.arg != null && caller.arg.isMap()) {
engine.setVariables(caller.arg.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ Feature:

Scenario:
* match functionFromKarateConfig() == 'resultFromFunctionFromKarateConfig'
* match __arg.functionFromKarateConfig() == 'resultFromFunctionFromKarateConfig'
* match functionFromCallSingleFromConfig() == 'resultFromFunctionFromCallSingleFromConfig'
* match __arg.functionFromCallSingleFromConfig() == 'resultFromFunctionFromCallSingleFromConfig'
* def message = 'fromCallSingleFromConfig2'

0 comments on commit 78ed24f

Please sign in to comment.