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

update test for file node for new output port

This commit is contained in:
Hiroyasu Nishiyama 2018-07-20 18:28:49 +09:00
parent 8226f1fa75
commit 054c7a76a4

View File

@ -53,75 +53,143 @@ describe('file Nodes', function() {
}); });
it('should write to a file', function(done) { it('should write to a file', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":true}]; var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":true, wires: [["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
helper.load(fileNode, flow, function() { helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1"); var n1 = helper.getNode("fileNode1");
n1.emit("input", {payload:"test"}); var n2 = helper.getNode("helperNode1");
setTimeout(function() { n2.on("input", function(msg) {
var f = fs.readFileSync(fileToTest); var f = fs.readFileSync(fileToTest);
f.should.have.length(4); f.should.have.length(4);
fs.unlinkSync(fileToTest); fs.unlinkSync(fileToTest);
msg.should.have.property("payload", "test");
done(); done();
},wait); });
n1.receive({payload:"test"});
}); });
}); });
it('should append to a file and add newline', function(done) { it('should append to a file and add newline', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":false}]; var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":false, wires: [["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
try { try {
fs.unlinkSync(fileToTest); fs.unlinkSync(fileToTest);
}catch(err) {} } catch(err) {
}
helper.load(fileNode, flow, function() { helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1"); var n1 = helper.getNode("fileNode1");
n1.emit("input", {payload:"test2"}); // string var n2 = helper.getNode("helperNode1");
var count = 0;
var data = ["test2", true, 999, [2]];
n2.on("input", function (msg) {
msg.should.have.property("payload", data[count]);
if (count === 3) {
var f = fs.readFileSync(fileToTest).toString();
if (os.type() !== "Windows_NT") {
f.should.have.length(19);
f.should.equal("test2\ntrue\n999\n[2]\n");
}
else {
f.should.have.length(23);
f.should.equal("test2\r\ntrue\r\n999\r\n[2]\r\n");
}
done();
}
count++;
});
n1.receive({payload:"test2"}); // string
setTimeout(function() { setTimeout(function() {
n1.emit("input", {payload:true}); // boolean n1.receive({payload:true}); // boolean
},30); },30);
setTimeout(function() { setTimeout(function() {
n1.emit("input", {payload:999}); // number n1.receive({payload:999}); // number
},60); },60);
setTimeout(function() { setTimeout(function() {
n1.emit("input", {payload:[2]}); // object (array) n1.receive({payload:[2]}); // object (array)
},90); },90);
setTimeout(function() {
var f = fs.readFileSync(fileToTest).toString();
if (os.type() !== "Windows_NT") {
f.should.have.length(19);
f.should.equal("test2\ntrue\n999\n[2]\n");
}
else {
f.should.have.length(23);
f.should.equal("test2\r\ntrue\r\n999\r\n[2]\r\n");
}
done();
},wait);
}); });
}); });
it('should append to a file after it has been deleted ', function(done) { it('should append to a file after it has been deleted ', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":false}]; var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":false, wires: [["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
try { try {
fs.unlinkSync(fileToTest); fs.unlinkSync(fileToTest);
} catch(err) {} } catch(err) {
}
helper.load(fileNode, flow, function() { helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1"); var n1 = helper.getNode("fileNode1");
// Send two messages to the file var n2 = helper.getNode("helperNode1");
n1.emit("input", {payload:"one"}); var data = ["one", "two", "three", "four"];
n1.emit("input", {payload:"two"}); var count = 0;
setTimeout(function() {
n2.on("input", function (msg) {
msg.should.have.property("payload", data[count]);
try { try {
// Check they got appended as expected if (count === 1) {
var f = fs.readFileSync(fileToTest).toString(); // Check they got appended as expected
f.should.equal("onetwo"); var f = fs.readFileSync(fileToTest).toString();
f.should.equal("onetwo");
// Delete the file // Delete the file
fs.unlinkSync(fileToTest); fs.unlinkSync(fileToTest);
setTimeout(function() {
// Send two more messages to the file
n1.receive({payload:"three"});
n1.receive({payload:"four"});
}, wait);
}
if (count === 3) {
var f = fs.readFileSync(fileToTest).toString();
f.should.equal("threefour");
fs.unlinkSync(fileToTest);
done();
}
} catch(err) {
done(err);
}
count++;
});
// Send two more messages to the file // Send two messages to the file
n1.emit("input", {payload:"three"}); n1.receive({payload:"one"});
n1.emit("input", {payload:"four"}); n1.receive({payload:"two"});
});
});
setTimeout(function() { it('should append to a file after it has been recreated ', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":false, wires: [["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
try {
fs.unlinkSync(fileToTest);
} catch(err) {
}
helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1");
var n2 = helper.getNode("helperNode1");
var data = ["one", "two", "three", "four"];
var count = 0;
n2.on("input", function (msg) {
try {
msg.should.have.property("payload", data[count]);
if (count == 1) {
// Check they got appended as expected
var f = fs.readFileSync(fileToTest).toString();
f.should.equal("onetwo");
if (os.type() === "Windows_NT") {
var dummyFile = path.join(resourcesDir,"50-file-test-dummy.txt");
fs.rename(fileToTest, dummyFile, function() {
recreateTest(n1, dummyFile);
});
} else {
recreateTest(n1, fileToTest);
}
}
if (count == 3) {
// Check the file was updated // Check the file was updated
try { try {
var f = fs.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
@ -131,42 +199,16 @@ describe('file Nodes', function() {
} catch(err) { } catch(err) {
done(err); done(err);
} }
},wait);
} catch(err) {
done(err);
}
},wait);
});
});
it('should append to a file after it has been recreated ', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":false}];
try {
fs.unlinkSync(fileToTest);
} catch(err) {}
helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1");
// Send two messages to the file
n1.emit("input", {payload:"one"});
n1.emit("input", {payload:"two"});
setTimeout(function() {
try {
// Check they got appended as expected
var f = fs.readFileSync(fileToTest).toString();
f.should.equal("onetwo");
if (os.type() === "Windows_NT") {
var dummyFile = path.join(resourcesDir,"50-file-test-dummy.txt");
fs.rename(fileToTest, dummyFile, function() {
recreateTest(n1, dummyFile);
});
} else {
recreateTest(n1, fileToTest);
} }
} catch(err) { } catch(err) {
done(err); done(err);
} }
},wait); count++;
});
// Send two messages to the file
n1.receive({payload:"one"});
n1.receive({payload:"two"});
}); });
function recreateTest(n1, fileToDelete) { function recreateTest(n1, fileToDelete) {
@ -177,30 +219,23 @@ describe('file Nodes', function() {
fs.writeFileSync(fileToTest,""); fs.writeFileSync(fileToTest,"");
// Send two more messages to the file // Send two more messages to the file
n1.emit("input", {payload:"three"}); n1.receive({payload:"three"});
n1.emit("input", {payload:"four"}); n1.receive({payload:"four"});
setTimeout(function() {
// Check the file was updated
try {
var f = fs.readFileSync(fileToTest).toString();
f.should.equal("threefour");
fs.unlinkSync(fileToTest);
done();
} catch(err) {
done(err);
}
},wait);
} }
}); });
it('should use msg.filename if filename not set in node', function(done) { it('should use msg.filename if filename not set in node', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "appendNewline":true, "overwriteFile":true}]; var flow = [{id:"fileNode1", type:"file", name: "fileNode", "appendNewline":true, "overwriteFile":true, wires: [["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
helper.load(fileNode, flow, function() { helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1"); var n1 = helper.getNode("fileNode1");
n1.emit("input", {payload:"fine", filename:fileToTest}); var n2 = helper.getNode("helperNode1");
setTimeout(function() {
n2.on("input", function (msg) {
msg.should.have.property("payload", "fine");
msg.should.have.property("filename", fileToTest);
var f = fs.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
if (os.type() !== "Windows_NT") { if (os.type() !== "Windows_NT") {
f.should.have.length(5); f.should.have.length(5);
@ -211,16 +246,20 @@ describe('file Nodes', function() {
f.should.equal("fine\r\n"); f.should.equal("fine\r\n");
} }
done(); done();
},wait); });
n1.receive({payload:"fine", filename:fileToTest});
}); });
}); });
it('should be able to delete the file', function(done) { it('should be able to delete the file', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":"delete"}]; var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":"delete", wires: [["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
helper.load(fileNode, flow, function() { helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1"); var n1 = helper.getNode("fileNode1");
n1.emit("input", {payload:"fine"}); var n2 = helper.getNode("helperNode1");
setTimeout(function() {
n2.on("input", function (msg) {
try { try {
var f = fs.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
f.should.not.equal("fine"); f.should.not.equal("fine");
@ -230,7 +269,9 @@ describe('file Nodes', function() {
e.code.should.equal("ENOENT"); e.code.should.equal("ENOENT");
done(); done();
} }
},wait); });
n1.receive({payload:"fine"});
}); });
}); });