From c331da732333ced40c6881a392638b0cb6746966 Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Sat, 27 Nov 2021 19:29:57 +0900 Subject: [PATCH] fix env var access using $env for groups --- .../@node-red/runtime/lib/flows/Flow.js | 21 +++++---- .../@node-red/runtime/lib/flows/Subflow.js | 3 +- .../node_modules/@node-red/util/lib/util.js | 3 +- .../@node-red/runtime/lib/flows/Flow_spec.js | 46 +++++++++++++++++++ 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/packages/node_modules/@node-red/runtime/lib/flows/Flow.js b/packages/node_modules/@node-red/runtime/lib/flows/Flow.js index 23f41e0ed..5fb21157e 100644 --- a/packages/node_modules/@node-red/runtime/lib/flows/Flow.js +++ b/packages/node_modules/@node-red/runtime/lib/flows/Flow.js @@ -439,8 +439,6 @@ class Flow { } } return [env.name, env]; - - return [env.name, env]; }); group._env = Object.fromEntries(entries); } @@ -457,24 +455,24 @@ class Flow { const val = ((value === "true") || (value === true)); - return { + return [{ val: val - }; + }, null]; } if (type === "cred") { - return { + return [{ val: value - }; + }, null]; } try { var val = redUtil.evaluateNodeProperty(value, type, node, null, null); - return { + return [{ val: val - }; + }, null]; } catch (e) { this.error(e); - return null; + return [null, null]; } } } @@ -488,7 +486,7 @@ class Flow { return this.getGroupEnvSetting(node, parent, name); } } - return null; + return [null, name]; } @@ -545,6 +543,9 @@ class Flow { } } } + else { + key = key.substring(8); + } } return this.parent.getSetting(key); } diff --git a/packages/node_modules/@node-red/runtime/lib/flows/Subflow.js b/packages/node_modules/@node-red/runtime/lib/flows/Subflow.js index 0f3aefe26..3d2bc3afe 100644 --- a/packages/node_modules/@node-red/runtime/lib/flows/Subflow.js +++ b/packages/node_modules/@node-red/runtime/lib/flows/Subflow.js @@ -373,10 +373,11 @@ class Subflow extends Flow { const node = this.subflowInstance; if (node.g) { const group = this.getGroupNode(node.g); - const result = this.getGroupEnvSetting(node, group, name); + const [result, newName] = this.getGroupEnvSetting(node, group, name); if (result) { return result.val; } + name = newName; } diff --git a/packages/node_modules/@node-red/util/lib/util.js b/packages/node_modules/@node-red/util/lib/util.js index 60a674261..bfea58f0f 100644 --- a/packages/node_modules/@node-red/util/lib/util.js +++ b/packages/node_modules/@node-red/util/lib/util.js @@ -526,10 +526,11 @@ function getSetting(node, name, flow_) { if (flow) { if (node && node.g) { const group = flow.getGroupNode(node.g); - const result = flow.getGroupEnvSetting(node, group, name); + const [result, newName] = flow.getGroupEnvSetting(node, group, name); if (result) { return result.val; } + name = newName; } return flow.getSetting(name); } diff --git a/test/unit/@node-red/runtime/lib/flows/Flow_spec.js b/test/unit/@node-red/runtime/lib/flows/Flow_spec.js index 95086c296..eed4810c4 100644 --- a/test/unit/@node-red/runtime/lib/flows/Flow_spec.js +++ b/test/unit/@node-red/runtime/lib/flows/Flow_spec.js @@ -1275,6 +1275,52 @@ describe('Flow', function() { } }); + it("can access environment variable property using $parent", function (done) { + try { + after(function() { + delete process.env.V0; + delete process.env.V1; + }) + process.env.V0 = "gv0"; + process.env.V1 = "gv1"; + var config = flowUtils.parseConfig([ + {id:"t1",type:"tab",env:[ + {"name": "V0", value: "v0", type: "str"} + ]}, + {id:"g1",type:"group",z:"t1",env:[ + {"name": "V0", value: "v1", type: "str"}, + {"name": "V1", value: "v2", type: "str"} + ]}, + {id:"g2",type:"group",z:"t1",g:"g1",env:[ + {"name": "V1", value: "v3", type: "str"} + ]}, + {id:"1",x:10,y:10,z:"t1",type:"test",foo:"${$parent.V0}",wires:[]}, + {id:"2",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"${$parent.V0}",wires:[]}, + {id:"3",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"${$parent.V1}",wires:[]}, + {id:"4",x:10,y:10,z:"t1",g:"g2",type:"test",foo:"${$parent.V1}",wires:[]}, + {id:"5",x:10,y:10,z:"t1",type:"test",foo:"${$parent.V1}",wires:[]}, + ]); + var flow = Flow.create({getSetting:v=>process.env[v]},config,config.flows["t1"]); + flow.start(); + + var activeNodes = flow.getActiveNodes(); + + activeNodes["1"].foo.should.equal("gv0"); + activeNodes["2"].foo.should.equal("v0"); + activeNodes["3"].foo.should.equal("gv1"); + activeNodes["4"].foo.should.equal("v2"); + activeNodes["5"].foo.should.equal("gv1"); + + flow.stop().then(function() { + done(); + }); + } + catch (e) { + console.log(e.stack); + done(e); + } + + }); });