ndir
Version:
The lost dir util tools for Nodejs. Handle dir and file in Event
108 lines (96 loc) • 3.25 kB
Plain Text
/**
* Module dependencies.
*/
var dir = require('../lib/dir');
var should = require('should');
var path = require('path');
var fs = require('fs');
var exec = require('child_process').exec;
describe('dir', function() {
describe('#walk()', function() {
var root = path.dirname(__dirname) + '/';
function check(dir, files) {
fs.statSync(dir).isDirectory().should.be.true;
files.should.be.a('object');
Object.keys(files).length.should.equal(fs.readdirSync(dir).length);
for (var k in files) {
k.should.be.a('string');
k.should.include(dir);
var stats = files[k];
stats.should.be.an.instanceof(fs.Stats);
}
}
it('should walk dir all', function(done) {
var walker = new dir.Walk(root);
walker.on('dir', check);
walker.on('end', done);
});
it('should walk dir all in callback', function(done) {
var walker = new dir.Walk(root, check, done);
});
it('walk(dir, ondir, onend, onerr) should worked', function(done) {
dir.walk(root, check, done, function(err, p) {
console.log(p);
throw err;
});
});
it('should success when walk empty dir', function(done) {
dir.walk(__dirname + '/emptydir', check, done);
});
});
describe('#copyfile()', function() {
var from = __dirname + '/dir.test.foo.txt';
var to = __dirname + '/dir.test.bar.txt';
var toParentNotExists = '/tmp/' + new Date().getTime() + '/dir.test.bar.txt';
before(function() {
path.existsSync(to) && fs.unlinkSync(to);
});
it('should worked', function(done) {
dir.copyfile(from, to, function(err) {
should.not.exist(err);
fs.statSync(to).isFile().should.be.true;
fs.readFileSync(to).toString().should.equal(fs.readFileSync(from).toString());
dir.copyfile(to, to, function(err) {
err.should.be.an.instanceof(Error);
done();
});
});
});
it('should copy toParentNotExists', function(done) {
dir.copyfile(from, toParentNotExists, function(exists, next) {
exists.should.be.false;
next(true);
}, function(err) {
should.not.exist(err);
fs.statSync(toParentNotExists).isFile().should.be.true;
fs.readFileSync(toParentNotExists).toString().should.equal(fs.readFileSync(from).toString());
done();
});
});
});
describe('#mkdir()', function() {
var existsDir = '/tmp/dir.test.exists.dir';
var notExistsDir = '/tmp/dir.test/not.exists.dir';
before(function(done) {
!path.existsSync(existsDir) && fs.mkdirSync(existsDir);
exec('rm -rf /tmp/dir.test', done);
});
after(function() {
fs.rmdirSync(existsDir);
});
it('should make exists dir success', function(done) {
path.existsSync(existsDir).should.be.true;
dir.mkdir(existsDir, function(err) {
path.existsSync(existsDir).should.be.true;
done(err);
});
});
it('should make not exists dir success', function(done) {
path.existsSync(notExistsDir).should.be.false;
dir.mkdir(notExistsDir, function(err) {
path.existsSync(notExistsDir).should.be.true;
done(err);
});
});
});
});