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

Merge pull request #3379 from node-red-hitachi/nr_node_path-support

add NR_NODE_PATH path support
This commit is contained in:
Nick O'Leary 2022-01-26 14:29:03 +00:00 committed by GitHub
commit 5b428bb8e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 0 deletions

View File

@ -378,6 +378,9 @@ class Subflow extends Flow {
if (name === "NR_NODE_ID") { if (name === "NR_NODE_ID") {
return node.id; return node.id;
} }
if (name === "NR_NODE_PATH") {
return node._path;
}
} }
if (node.g) { if (node.g) {
const group = this.getGroupNode(node.g); const group = this.getGroupNode(node.g);

View File

@ -529,6 +529,9 @@ function getSetting(node, name, flow_) {
if (name === "NR_NODE_ID") { if (name === "NR_NODE_ID") {
return node.id; return node.id;
} }
if (name === "NR_NODE_PATH") {
return node._path;
}
} }
var flow = (flow_ ? flow_ : (node ? node._flow : null)); var flow = (flow_ ? flow_ : (node ? node._flow : null));
if (flow) { if (flow) {

View File

@ -142,6 +142,25 @@ describe('inject node', function() {
}); });
}); });
it('inject path of node as environment variable ', function (done) {
var flow = [{id: "n1", type: "inject", name: "NAME", topnic: "t1", payload: "NR_NODE_PATH", payloadType: "env", wires: [["n2"]], z: "flow"},
{id: "n2", type: "helper"}];
helper.load(injectNode, flow, function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function (msg) {
try {
msg.should.have.property("payload", "flow/n1");
done();
} catch (err) {
done(err);
}
});
n1.receive({});
});
});
it('inject name of flow as environment variable ', function (done) { it('inject name of flow as environment variable ', function (done) {
var flow = [{id: "n1", type: "inject", name: "NAME", topnic: "t1", payload: "NR_FLOW_NAME", payloadType: "env", wires: [["n2"]], z: "flow"}, var flow = [{id: "n1", type: "inject", name: "NAME", topnic: "t1", payload: "NR_FLOW_NAME", payloadType: "env", wires: [["n2"]], z: "flow"},
{id: "n2", type: "helper"}, {id: "n2", type: "helper"},

View File

@ -567,4 +567,43 @@ describe('subflow', function() {
}); });
}); });
it('should access NR_NODE_PATH env var within subflow instance', function(done) {
var flow = [
{id:"t0", type:"tab", label:"", disabled:false, info:""},
{id:"n1", x:10, y:10, z:"t0", type:"subflow:s1",
env: [], wires:[["n2"]]},
{id:"n2", x:10, y:10, z:"t0", type:"helper", wires:[]},
// Subflow
{id:"s1", type:"subflow", name:"Subflow", info:"",
in:[{
x:10, y:10,
wires:[ {id:"s1-n1"} ]
}],
out:[{
x:10, y:10,
wires:[ {id:"s1-n1", port:0} ]
}]
},
{id:"s1-n1", x:10, y:10, z:"s1", type:"function",
func:"msg.payload = env.get('NR_NODE_PATH'); return msg;",
wires:[]}
];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.should.have.property("payload", "t0/n1/s1-n1");
done();
}
catch (e) {
console.log(e);
done(e);
}
});
n1.receive({payload:"foo"});
});
});
}); });