1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

expand existing env var test for all scenarios

This commit is contained in:
Steve-Mcl 2023-06-17 22:11:02 +01:00
parent ee8b2a0b58
commit ceb9a320ba

View File

@ -1311,33 +1311,42 @@ describe('Flow', function() {
}) })
process.env.V0 = "gv0"; process.env.V0 = "gv0";
process.env.V1 = "gv1"; process.env.V1 = "gv1";
process.env.V3 = "gv3";
var config = flowUtils.parseConfig([ var config = flowUtils.parseConfig([
{id:"t1",type:"tab",env:[ {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:[ {id:"g1",type:"group",z:"t1",env:[
{"name": "V0", value: "v1", type: "str"}, {"name": "V0", value: "g1v0", type: "str"},
{"name": "V1", value: "v2", type: "str"} {"name": "V1", value: "g1v1", type: "str"}
]}, ]},
{id:"g2",type:"group",z:"t1",g:"g1",env:[ {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:"t1__V0",x:10,y:10,z:"t1",type:"test",foo:"${V0}",wires:[]}, // V0 will come from tab env V0
{id:"2",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"$(V0)",wires:[]}, {id:"t1g1V0",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"${V0}",wires:[]}, // V0 will come from group 1 env V0
{id:"3",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"$(V1)",wires:[]}, {id:"t1g1V1",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"${V1}",wires:[]}, // V1 will come from group 1 env V1
{id:"4",x:10,y:10,z:"t1",g:"g2",type:"test",foo:"$(V1)",wires:[]}, {id:"t1g2V0",x:10,y:10,z:"t1",g:"g2",type:"test",foo:"${V0}",wires:[]}, // V0 will come from group 1 env V0
{id:"5",x:10,y:10,z:"t1",type:"test",foo:"$(V1)",wires:[]}, {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"]); var flow = Flow.create({getSetting:v=>process.env[v]},config,config.flows["t1"]);
flow.start(); flow.start();
var activeNodes = flow.getActiveNodes(); var activeNodes = flow.getActiveNodes();
activeNodes["1"].foo.should.equal("v0"); activeNodes.t1__V0.foo.should.equal("t1v0"); // node in tab 1, get tab 1 env V0
activeNodes["2"].foo.should.equal("v1"); activeNodes.t1__V1.foo.should.equal("gv1"); // node in tab 1, get V1, (tab 1 no V1) --> parent (global has V1)
activeNodes["3"].foo.should.equal("v2"); activeNodes.t1g1V0.foo.should.equal("g1v0"); // node in group 1, get V0, (group 1 has V0)
activeNodes["4"].foo.should.equal("v3"); activeNodes.t1g1V1.foo.should.equal("g1v1"); // node in group 1, get V1, (group 1 has V1)
activeNodes["5"].foo.should.equal("gv1"); 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() { flow.stop().then(function() {
done(); done();
@ -1347,7 +1356,6 @@ describe('Flow', function() {
console.log(e.stack); console.log(e.stack);
done(e); done(e);
} }
}); });
it("can access environment variable property using $parent", function (done) { it("can access environment variable property using $parent", function (done) {
try { try {
@ -1393,7 +1401,6 @@ describe('Flow', function() {
console.log(e.stack); console.log(e.stack);
done(e); done(e);
} }
}); });
it("can define environment variable using JSONata", function (done) { it("can define environment variable using JSONata", function (done) {
@ -1427,9 +1434,40 @@ describe('Flow', function() {
console.log(e.stack); console.log(e.stack);
done(e); 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);
}
});
}); });
}); });