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

Merge pull request #1833 from node-red-hitachi/update-test-for-file-node

Update test for file node for new output port
This commit is contained in:
Nick O'Leary 2018-07-20 11:45:39 +01:00 committed by GitHub
commit 29d386cc51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -53,37 +53,38 @@ 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");
setTimeout(function() { var count = 0;
n1.emit("input", {payload:true}); // boolean var data = ["test2", true, 999, [2]];
},30);
setTimeout(function() { n2.on("input", function (msg) {
n1.emit("input", {payload:999}); // number msg.should.have.property("payload", data[count]);
},60); if (count === 3) {
setTimeout(function() {
n1.emit("input", {payload:[2]}); // object (array)
},90);
setTimeout(function() {
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(19); f.should.have.length(19);
@ -94,63 +95,87 @@ describe('file Nodes', function() {
f.should.equal("test2\r\ntrue\r\n999\r\n[2]\r\n"); f.should.equal("test2\r\ntrue\r\n999\r\n[2]\r\n");
} }
done(); done();
},wait); }
count++;
});
n1.receive({payload:"test2"}); // string
setTimeout(function() {
n1.receive({payload:true}); // boolean
},30);
setTimeout(function() {
n1.receive({payload:999}); // number
},60);
setTimeout(function() {
n1.receive({payload:[2]}); // object (array)
},90);
}); });
}); });
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 {
if (count === 1) {
// Check they got appended as expected // Check they got appended as expected
var f = fs.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
f.should.equal("onetwo"); f.should.equal("onetwo");
// Delete the file // Delete the file
fs.unlinkSync(fileToTest); fs.unlinkSync(fileToTest);
// Send two more messages to the file
n1.emit("input", {payload:"three"});
n1.emit("input", {payload:"four"});
setTimeout(function() { setTimeout(function() {
// Check the file was updated // Send two more messages to the file
try { n1.receive({payload:"three"});
n1.receive({payload:"four"});
}, wait);
}
if (count === 3) {
var f = fs.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
f.should.equal("threefour"); f.should.equal("threefour");
fs.unlinkSync(fileToTest); fs.unlinkSync(fileToTest);
done(); done();
}
} catch(err) { } catch(err) {
done(err); done(err);
} }
},wait); count++;
} catch(err) { });
done(err);
} // Send two messages to the file
},wait); n1.receive({payload:"one"});
n1.receive({payload:"two"});
}); });
}); });
it('should append to a file after it has been recreated ', function(done) { 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}]; 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) {
try { try {
msg.should.have.property("payload", data[count]);
if (count == 1) {
// Check they got appended as expected // Check they got appended as expected
var f = fs.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
f.should.equal("onetwo"); f.should.equal("onetwo");
@ -163,10 +188,27 @@ describe('file Nodes', function() {
} else { } else {
recreateTest(n1, fileToTest); recreateTest(n1, fileToTest);
} }
}
if (count == 3) {
// Check the file was updated
try {
var f = fs.readFileSync(fileToTest).toString();
f.should.equal("threefour");
fs.unlinkSync(fileToTest);
done();
} catch(err) { } catch(err) {
done(err); done(err);
} }
},wait); }
} catch(err) {
done(err);
}
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"});
}); });
}); });