From ceb9a320baef060dab40612ae11c940ce683eaea Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Sat, 17 Jun 2023 22:11:02 +0100 Subject: [PATCH] expand existing env var test for all scenarios --- .../@node-red/runtime/lib/flows/Flow_spec.js | 72 ++++++++++++++----- 1 file changed, 55 insertions(+), 17 deletions(-) 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 c376f839a..9f99830b0 100644 --- a/test/unit/@node-red/runtime/lib/flows/Flow_spec.js +++ b/test/unit/@node-red/runtime/lib/flows/Flow_spec.js @@ -1311,33 +1311,42 @@ describe('Flow', function() { }) process.env.V0 = "gv0"; process.env.V1 = "gv1"; + process.env.V3 = "gv3"; var config = flowUtils.parseConfig([ {id:"t1",type:"tab",env:[ - {"name": "V0", value: "v0", type: "str"} + {"name": "V0", value: "t1v0", type: "str"}, + {"name": "V2", value: "t1v2", type: "str"} ]}, {id:"g1",type:"group",z:"t1",env:[ - {"name": "V0", value: "v1", type: "str"}, - {"name": "V1", value: "v2", type: "str"} + {"name": "V0", value: "g1v0", type: "str"}, + {"name": "V1", value: "g1v1", type: "str"} ]}, {id:"g2",type:"group",z:"t1",g:"g1",env:[ - {"name": "V1", value: "v3", type: "str"} + {"name": "V1", value: "g2v1", type: "str"} ]}, - {id:"1",x:10,y:10,z:"t1",type:"test",foo:"$(V0)",wires:[]}, - {id:"2",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"$(V0)",wires:[]}, - {id:"3",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"$(V1)",wires:[]}, - {id:"4",x:10,y:10,z:"t1",g:"g2",type:"test",foo:"$(V1)",wires:[]}, - {id:"5",x:10,y:10,z:"t1",type:"test",foo:"$(V1)",wires:[]}, + {id:"t1__V0",x:10,y:10,z:"t1",type:"test",foo:"${V0}",wires:[]}, // V0 will come from tab env V0 + {id:"t1g1V0",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"${V0}",wires:[]}, // V0 will come from group 1 env V0 + {id:"t1g1V1",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"${V1}",wires:[]}, // V1 will come from group 1 env V1 + {id:"t1g2V0",x:10,y:10,z:"t1",g:"g2",type:"test",foo:"${V0}",wires:[]}, // V0 will come from group 1 env V0 + {id:"t1g2V1",x:10,y:10,z:"t1",g:"g2",type:"test",foo:"${V1}",wires:[]}, // V1 will come from group 2 env V1 + {id:"t1g2V2",x:10,y:10,z:"t1",g:"g2",type:"test",foo:"${V2}",wires:[]}, // V2 will come from tab 1 env V2 + {id:"t1g2V3",x:10,y:10,z:"t1",g:"g2",type:"test",foo:"${V3}",wires:[]}, // V3 will come from process env V3 + + {id:"t1__V1",x:10,y:10,z:"t1",type:"test",foo:"${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("v0"); - activeNodes["2"].foo.should.equal("v1"); - activeNodes["3"].foo.should.equal("v2"); - activeNodes["4"].foo.should.equal("v3"); - activeNodes["5"].foo.should.equal("gv1"); + activeNodes.t1__V0.foo.should.equal("t1v0"); // node in tab 1, get tab 1 env V0 + activeNodes.t1__V1.foo.should.equal("gv1"); // node in tab 1, get V1, (tab 1 no V1) --> parent (global has V1) + activeNodes.t1g1V0.foo.should.equal("g1v0"); // node in group 1, get V0, (group 1 has V0) + activeNodes.t1g1V1.foo.should.equal("g1v1"); // node in group 1, get V1, (group 1 has V1) + activeNodes.t1g2V0.foo.should.equal("g1v0"); // node in group 2, get V0, (group 2 no V0) --> parent (group 1 has V0) + activeNodes.t1g2V1.foo.should.equal("g2v1"); // node in group 2, get V1, (group 2 has V1) + activeNodes.t1g2V2.foo.should.equal("t1v2"); // node in group 2, get V2, (group 2 no V2) --> parent (tab 1 has V2) + activeNodes.t1g2V3.foo.should.equal("gv3"); // node in group 2, get V3, (group 2 no V3) --> parent (tab 1 no V2) --> parent (global has V3) flow.stop().then(function() { done(); @@ -1347,7 +1356,6 @@ describe('Flow', function() { console.log(e.stack); done(e); } - }); it("can access environment variable property using $parent", function (done) { try { @@ -1393,7 +1401,6 @@ describe('Flow', function() { console.log(e.stack); done(e); } - }); it("can define environment variable using JSONata", function (done) { @@ -1427,9 +1434,40 @@ describe('Flow', function() { console.log(e.stack); done(e); } - }); + it("can access global environment variables defined as JSONata values", function (done) { + try { + after(function() { + delete process.env.V0; + }) + var config = flowUtils.parseConfig([ + {id:"t1",type:"tab",env:[ + {"name": "V0", value: "1+2", type: "jsonata"} + ]}, + {id:"g1",type:"group",z:"t1",env:[ + {"name": "V1", value: "2+3", type: "jsonata"}, + ]}, + {id:"1",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"$(V0)",wires:[]}, + {id:"2",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"$(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(3); + activeNodes["2"].foo.should.equal(5); + + flow.stop().then(function() { + done(); + }); + } + catch (e) { + console.log(e.stack); + done(e); + } + }); }); });