From ef6b28b5fea22db0708ebbef6838334c438132ae Mon Sep 17 00:00:00 2001 From: Peter Thomas Date: Thu, 22 Jul 2021 08:44:45 +0530 Subject: [PATCH] resolve variable access in called features #1687 introduced by #1685 --- .../intuit/karate/core/ScenarioEngine.java | 9 ++++---- .../karate/core/FeatureRuntimeTest.java | 21 ++++++++++++------- .../karate/core/call-jsonpath-called.feature | 6 ++++++ .../intuit/karate/core/call-jsonpath.feature | 5 +++++ 4 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 karate-core/src/test/java/com/intuit/karate/core/call-jsonpath-called.feature create mode 100644 karate-core/src/test/java/com/intuit/karate/core/call-jsonpath.feature diff --git a/karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java b/karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java index 1d42ab11c..9862341ff 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java +++ b/karate-core/src/main/java/com/intuit/karate/core/ScenarioEngine.java @@ -1627,7 +1627,7 @@ private void set(String name, String path, boolean isWithinParentheses, Variable name = nameAndPath.left; path = nameAndPath.right; } - Variable target = vars.get(name); + Variable target = JS.bindings.hasMember(name) ? new Variable(JS.get(name)) : null; // should work in called features if (isXmlPath(path)) { if (target == null || target.isNull()) { if (viaTable) { // auto create if using set via cucumber table as a convenience @@ -1678,7 +1678,6 @@ private void set(String name, String path, boolean isWithinParentheses, Variable json.set(path, value.getValue()); } } - } private static final String PATH = "path"; @@ -2112,11 +2111,13 @@ private static Variable nodeToValue(Node node) { } public Variable evalJsonPathOnVariableByName(String name, String path) { - return evalJsonPath(vars.get(name), path); + Variable v = new Variable(JS.get(name)); // should work in called features + return evalJsonPath(v, path); } public Variable evalXmlPathOnVariableByName(String name, String path) { - return evalXmlPath(vars.get(name), path); + Variable v = new Variable(JS.get(name)); // should work in called features + return evalXmlPath(v, path); } public Variable evalKarateExpression(String text) { diff --git a/karate-core/src/test/java/com/intuit/karate/core/FeatureRuntimeTest.java b/karate-core/src/test/java/com/intuit/karate/core/FeatureRuntimeTest.java index b3e2c988c..afe1099a5 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/FeatureRuntimeTest.java +++ b/karate-core/src/test/java/com/intuit/karate/core/FeatureRuntimeTest.java @@ -240,7 +240,7 @@ void testOutlineConfigJsParallel() { .parallel(2); assertEquals(0, results.getFailCount()); } - + @Test void testOutlineConfigJsCallSingleParallel() { Results results = Runner.path("classpath:com/intuit/karate/core/outline-config-js.feature") @@ -278,25 +278,30 @@ void testIgnoreStepFailure() { report.render("target/report-test"); // error log will should have logs on all failures } - + @Test void testKarateFork() { run("fork.feature"); - } - + } + @Test void testCsv() { run("csv.feature"); - } - + } + @Test void testXmlPretty() { run("xml-pretty.feature"); } - + @Test void testMatchStep() { run("match-step.feature"); - } + } + + @Test + void testCallJsonPath() { + run("call-jsonpath.feature"); + } } diff --git a/karate-core/src/test/java/com/intuit/karate/core/call-jsonpath-called.feature b/karate-core/src/test/java/com/intuit/karate/core/call-jsonpath-called.feature new file mode 100644 index 000000000..8995e5ed4 --- /dev/null +++ b/karate-core/src/test/java/com/intuit/karate/core/call-jsonpath-called.feature @@ -0,0 +1,6 @@ +@ignore +Feature: + +Scenario: +* def vals = $foo[*].id +* print vals diff --git a/karate-core/src/test/java/com/intuit/karate/core/call-jsonpath.feature b/karate-core/src/test/java/com/intuit/karate/core/call-jsonpath.feature new file mode 100644 index 000000000..36f9dbbfd --- /dev/null +++ b/karate-core/src/test/java/com/intuit/karate/core/call-jsonpath.feature @@ -0,0 +1,5 @@ +Feature: + +Scenario: +* def foo = [{a: 1}, {a: 2}] +* def bar = call read('call-jsonpath-called.feature')