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

Update tail node tests to use resource file

This commit is contained in:
Nick O'Leary 2014-08-07 14:55:34 +01:00
parent fde77cec5d
commit 43ad8706aa
3 changed files with 17 additions and 32 deletions

View File

@ -27,20 +27,27 @@ module.exports = function(RED) {
var node = this; var node = this;
var err = ""; var err = "";
// TODO: rewrite to use node-tail
var tail = spawn("tail", ["-F", this.filename]); var tail = spawn("tail", ["-F", this.filename]);
tail.stdout.on("data", function (data) { tail.stdout.on("data", function (data) {
var msg = {topic:node.filename};
if (node.split) { if (node.split) {
// TODO: allow customisation of the line break - as we do elsewhere
var strings = data.toString().split("\n"); var strings = data.toString().split("\n");
for (var s in strings) { for (var s in strings) {
//TODO: should we really filter blanks? Is that expected?
if (strings[s] !== "") { if (strings[s] !== "") {
msg.payload = strings[s]; node.send({
node.send(msg); topic: node.filename,
payload: strings[s]
});
} }
} }
} }
else { else {
msg.payload = data.toString(); var msg = {
topic:node.filename,
payload: data.toString()
};
node.send(msg); node.send(msg);
} }
}); });

View File

@ -24,42 +24,17 @@ var helper = require("../../helper.js");
describe('TailNode', function() { describe('TailNode', function() {
var tempDir = path.join(__dirname, ".tmp"); var resourcesDir = path.join(__dirname,"..","..","..","resources");
var fileToTail = path.join(tempDir, "tailMe.txt"); var fileToTail = path.join(resourcesDir,"28-tail-test-file.txt");
// function which writes to the file creating all missing directories
function writeFile(path, contents) {
mkdirp(tempDir, function(err) {
if(err) {
return err;
}
try {
fs.writeFileSync(path, contents, 'utf8');
} catch (error) {
console.log("Unexpected error writing file: " +error.message);
return error;
}
});
}
beforeEach(function(done) { beforeEach(function(done) {
writeFile(fileToTail, "Tail message line1\nTail message line2\n");
helper.startServer(done); helper.startServer(done);
}); });
afterEach(function(done) { afterEach(function(done) {
helper.unload(); helper.unload();
fs.remove(tempDir, function(err) {
if (err) {
console.log("error occurred removing " + tempDir + ": " +err);
return err;
}
fs.exists(tempDir, function(exists) {
exists.should.be.false;
helper.stopServer(done); helper.stopServer(done);
}); });
});
});
it('should be loaded', function(done) { it('should be loaded', function(done) {
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail}]; var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail}];
@ -78,6 +53,7 @@ describe('TailNode', function() {
var helperNode1 = helper.getNode("helperNode1"); var helperNode1 = helper.getNode("helperNode1");
var inputCounter = 0; var inputCounter = 0;
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
console.log(msg);
msg.should.have.property('topic', fileToTail); msg.should.have.property('topic', fileToTail);
msg.payload.should.equal("Tail message line" + (++inputCounter)); msg.payload.should.equal("Tail message line" + (++inputCounter));
if(inputCounter === 2) { if(inputCounter === 2) {

View File

@ -0,0 +1,2 @@
Tail message line1
Tail message line2