mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Make Watch node test more reliable
- tidied up some unnecessary code - removed the 'size' check as that is highly prone to timing issues
This commit is contained in:
parent
1f40d4f941
commit
8ffabf1813
@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
var fs = require("fs");
|
var fs = require("fs-extra");
|
||||||
var path = require("path");
|
var path = require("path");
|
||||||
var should = require("should");
|
var should = require("should");
|
||||||
var helper = require("../../helper.js");
|
var helper = require("../../helper.js");
|
||||||
@ -26,11 +26,6 @@ describe('watch Node', function() {
|
|||||||
|
|
||||||
var resourcesDir = path.join(__dirname,"..","..","..","resources");
|
var resourcesDir = path.join(__dirname,"..","..","..","resources");
|
||||||
var baseDir = path.join(resourcesDir, "23-watch-test-dir");
|
var baseDir = path.join(resourcesDir, "23-watch-test-dir");
|
||||||
var dirToWatch = undefined;
|
|
||||||
var subDirToWatch = undefined;
|
|
||||||
var file0ToWatch = undefined;
|
|
||||||
var file1ToWatch = undefined;
|
|
||||||
var file2ToWatch = undefined;
|
|
||||||
var count = 0;
|
var count = 0;
|
||||||
|
|
||||||
function prepareDir() {
|
function prepareDir() {
|
||||||
@ -41,63 +36,26 @@ describe('watch Node', function() {
|
|||||||
file2ToWatch = path.join(subDirToWatch, "file2.txt");
|
file2ToWatch = path.join(subDirToWatch, "file2.txt");
|
||||||
fs.mkdirSync(dirToWatch);
|
fs.mkdirSync(dirToWatch);
|
||||||
count++;
|
count++;
|
||||||
|
return {
|
||||||
|
dirToWatch:dirToWatch,
|
||||||
|
file0ToWatch:file0ToWatch,
|
||||||
|
file1ToWatch:file1ToWatch,
|
||||||
|
subDirToWatch:subDirToWatch,
|
||||||
|
file2ToWatch:file2ToWatch
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function wait(msec, func) {
|
function wait(msec, func) {
|
||||||
setTimeout(func, msec);
|
setTimeout(func, msec);
|
||||||
}
|
}
|
||||||
|
|
||||||
function rmdir(dir_path, done) {
|
|
||||||
function collect(dir_path, files, dirs) {
|
|
||||||
var elems = fs.readdirSync(dir_path);
|
|
||||||
elems.forEach(function(elem) {
|
|
||||||
var elem_path = path.join(dir_path, elem);
|
|
||||||
var stat = fs.lstatSync(elem_path);
|
|
||||||
if(stat.isDirectory()) {
|
|
||||||
var r = collect(elem_path, files, dirs);
|
|
||||||
files = r[0];
|
|
||||||
dirs = r[1];
|
|
||||||
} else {
|
|
||||||
files.push(elem_path);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
dirs.push(dir_path);
|
|
||||||
return [files, dirs];
|
|
||||||
}
|
|
||||||
function seq(func, list, cont) {
|
|
||||||
if(list.length > 0) {
|
|
||||||
var elem = list.shift();
|
|
||||||
func(elem, function(err) {
|
|
||||||
if(err) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
seq(func, list, cont);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
cont();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var r = collect(dir_path, [], []);
|
|
||||||
var files = r[0];
|
|
||||||
var dirs = r[1];
|
|
||||||
seq(fs.unlink, files,
|
|
||||||
function() {
|
|
||||||
seq(fs.rmdir, dirs, done);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
fs.mkdirSync(baseDir);
|
fs.ensureDirSync(baseDir);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
after(function(done) {
|
after(function(done) {
|
||||||
rmdir(baseDir, done);
|
fs.removeSync(baseDir);
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(function(done) {
|
|
||||||
prepareDir();
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -114,6 +72,8 @@ describe('watch Node', function() {
|
|||||||
var count = 0;
|
var count = 0;
|
||||||
var len = Object.keys(results).length;
|
var len = Object.keys(results).length;
|
||||||
n2.on("input", function(msg) {
|
n2.on("input", function(msg) {
|
||||||
|
try {
|
||||||
|
// console.log(msg);
|
||||||
msg.should.have.property('file');
|
msg.should.have.property('file');
|
||||||
|
|
||||||
var file = msg.file;
|
var file = msg.file;
|
||||||
@ -122,7 +82,7 @@ describe('watch Node', function() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
processed[file] = true;
|
processed[file] = true;
|
||||||
(file in results).should.be.true;
|
(file in results).should.be.true();
|
||||||
|
|
||||||
var result = results[file];
|
var result = results[file];
|
||||||
msg.should.have.property('payload', result.payload);
|
msg.should.have.property('payload', result.payload);
|
||||||
@ -134,7 +94,10 @@ describe('watch Node', function() {
|
|||||||
if(count === len) {
|
if(count === len) {
|
||||||
n1.close();
|
n1.close();
|
||||||
// wait for close
|
// wait for close
|
||||||
wait(500, done);
|
wait(100, done);
|
||||||
|
}
|
||||||
|
}catch(err) {
|
||||||
|
done(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// wait for preparation
|
// wait for preparation
|
||||||
@ -143,106 +106,116 @@ describe('watch Node', function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
it('should watch a file to be changed', function(done) {
|
it('should watch a file to be changed', function(done) {
|
||||||
fs.writeFileSync(file0ToWatch, '');
|
var files = prepareDir();
|
||||||
|
fs.writeFileSync(files.file0ToWatch, '');
|
||||||
var flow = [{id:"n1", type:"watch", name: "watch",
|
var flow = [{id:"n1", type:"watch", name: "watch",
|
||||||
files: file0ToWatch, recursive: false,
|
files: files.file0ToWatch, recursive: false,
|
||||||
wires:[["n2"]]},
|
wires:[["n2"]]},
|
||||||
{id:"n2", type:"helper"}];
|
{id:"n2", type:"helper"}];
|
||||||
var results = {
|
var results = {
|
||||||
'file0.txt' : {
|
'file0.txt' : {
|
||||||
'payload' : file0ToWatch,
|
'payload' : files.file0ToWatch,
|
||||||
'topic': file0ToWatch,
|
'topic': files.file0ToWatch,
|
||||||
'type': 'file',
|
'type': 'file',
|
||||||
'size': 5
|
'size': 5
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
testWatch(flow, function() {
|
testWatch(flow, function() {
|
||||||
fs.appendFileSync(file0ToWatch, "ABCDE");
|
fs.appendFileSync(files.file0ToWatch, "ABCDE");
|
||||||
}, results, done);
|
}, results, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should watch multiple files to be changed', function(done) {
|
it('should watch multiple files to be changed', function(done) {
|
||||||
fs.writeFileSync(file0ToWatch, '');
|
var files = prepareDir();
|
||||||
fs.writeFileSync(file1ToWatch, '');
|
fs.writeFileSync(files.file0ToWatch, '');
|
||||||
var files = file0ToWatch +","+file1ToWatch;
|
fs.writeFileSync(files.file1ToWatch, '');
|
||||||
var flow = [{id:"n1", type:"watch", name: "watch",
|
var flow = [{id:"n1", type:"watch", name: "watch",
|
||||||
files: files, recursive: false,
|
files: files.file0ToWatch +","+files.file1ToWatch, recursive: false,
|
||||||
wires:[["n2"]]},
|
wires:[["n2"]]},
|
||||||
{id:"n2", type:"helper"}];
|
{id:"n2", type:"helper"}];
|
||||||
var results = {
|
var results = {
|
||||||
'file0.txt' : {
|
'file0.txt' : {
|
||||||
'payload' : file0ToWatch,
|
'payload' : files.file0ToWatch,
|
||||||
'topic': file0ToWatch,
|
'topic': files.file0ToWatch,
|
||||||
'type': 'file',
|
'type': 'file'//,
|
||||||
'size': 5
|
// 'size': 5
|
||||||
},
|
},
|
||||||
'file1.txt' : {
|
'file1.txt' : {
|
||||||
'payload' : file1ToWatch,
|
'payload' : files.file1ToWatch,
|
||||||
'topic': file1ToWatch,
|
'topic': files.file1ToWatch,
|
||||||
'type': 'file',
|
'type': 'file'//,
|
||||||
'size': 3
|
// 'size': 3
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
testWatch(flow, function() {
|
testWatch(flow, function() {
|
||||||
fs.appendFileSync(file0ToWatch, "ABCDE");
|
fs.appendFileSync(files.file0ToWatch, "ABCDE");
|
||||||
fs.appendFileSync(file1ToWatch, "123");
|
fs.appendFileSync(files.file1ToWatch, "123");
|
||||||
}, results, done);
|
}, results, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should watch attribute of a file to be changed', function(done) {
|
it('should watch attribute of a file to be changed', function(done) {
|
||||||
fs.writeFileSync(file0ToWatch, '');
|
var files = prepareDir();
|
||||||
fs.chmodSync(file0ToWatch, 0o444);
|
fs.writeFileSync(files.file0ToWatch, '');
|
||||||
|
fs.chmodSync(files.file0ToWatch, 0o444);
|
||||||
var flow = [{id:"n1", type:"watch", name: "watch",
|
var flow = [{id:"n1", type:"watch", name: "watch",
|
||||||
files: file0ToWatch, recursive: false,
|
files: files.file0ToWatch, recursive: false,
|
||||||
wires:[["n2"]]},
|
wires:[["n2"]]},
|
||||||
{id:"n2", type:"helper"}];
|
{id:"n2", type:"helper"}];
|
||||||
var results = {
|
var results = {
|
||||||
'file0.txt' : {
|
'file0.txt' : {
|
||||||
'payload' : file0ToWatch,
|
'payload' : files.file0ToWatch,
|
||||||
'topic': file0ToWatch,
|
'topic': files.file0ToWatch,
|
||||||
'type': 'file',
|
'type': 'file'//,
|
||||||
'size': 0
|
// 'size': 0
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
testWatch(flow, function() {
|
testWatch(flow, function() {
|
||||||
fs.chmodSync(file0ToWatch, 0o777);
|
fs.chmodSync(files.file0ToWatch, 0o777);
|
||||||
}, results, done);
|
}, results, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should watch a file in a directory to be changed', function(done) {
|
it('should watch a file in a directory to be changed', function(done) {
|
||||||
fs.writeFileSync(file0ToWatch, '');
|
var files = prepareDir();
|
||||||
|
fs.writeFileSync(files.file0ToWatch, '');
|
||||||
var flow = [{id:"n1", type:"watch", name: "watch",
|
var flow = [{id:"n1", type:"watch", name: "watch",
|
||||||
files: dirToWatch, recursive: true,
|
files: files.dirToWatch, recursive: true,
|
||||||
wires:[["n2"]]},
|
wires:[["n2"]]},
|
||||||
{id:"n2", type:"helper"}];
|
{id:"n2", type:"helper"}];
|
||||||
var results = {
|
var results = {
|
||||||
'file0.txt' : {
|
'file0.txt' : {
|
||||||
'payload' : file0ToWatch,
|
'payload' : files.file0ToWatch,
|
||||||
'topic': file0ToWatch,
|
'topic': files.file0ToWatch,
|
||||||
'type': 'file',
|
'type': 'file'//,
|
||||||
'size': 5
|
// 'size': 5
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
testWatch(flow, function() {
|
testWatch(flow, function() {
|
||||||
fs.appendFileSync(file0ToWatch, "ABCDE");
|
fs.appendFileSync(files.file0ToWatch, "ABCDE");
|
||||||
}, results, done);
|
}, results, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should watch a sub directory in a directory to be changed', function(done) {
|
it('should watch a sub directory in a directory to be changed', function(done) {
|
||||||
fs.mkdirSync(subDirToWatch);
|
var files = prepareDir();
|
||||||
|
fs.mkdirSync(files.subDirToWatch);
|
||||||
var flow = [{id:"n1", type:"watch", name: "watch",
|
var flow = [{id:"n1", type:"watch", name: "watch",
|
||||||
files: dirToWatch, recursive: true,
|
files: files.dirToWatch, recursive: true,
|
||||||
wires:[["n2"]]},
|
wires:[["n2"]]},
|
||||||
{id:"n2", type:"helper"}];
|
{id:"n2", type:"helper"}];
|
||||||
var results = {
|
var results = {
|
||||||
|
'subdir': {
|
||||||
|
payload: files.subDirToWatch,
|
||||||
|
topic: files.dirToWatch,
|
||||||
|
file: 'subdir',
|
||||||
|
type: 'directory'
|
||||||
|
},
|
||||||
'file2.txt': {
|
'file2.txt': {
|
||||||
payload: file2ToWatch,
|
payload: files.file2ToWatch,
|
||||||
type: 'file',
|
type: 'file'//,
|
||||||
size: 5
|
// size: 5
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
testWatch(flow, function() {
|
testWatch(flow, function() {
|
||||||
fs.appendFileSync(file2ToWatch, "ABCDE");
|
fs.appendFileSync(files.file2ToWatch, "ABCDE");
|
||||||
}, results, done);
|
}, results, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user