filer/tests/spec/shell/rm.spec.js

139 lines
3.3 KiB
JavaScript
Raw Normal View History

2014-05-23 20:53:50 +00:00
var Filer = require('../../..');
var util = require('../../lib/test-utils.js');
var expect = require('chai').expect;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
describe('FileSystemShell.rm', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
it('should be a function', function() {
var shell = util.shell();
expect(shell.rm).to.be.a('function');
});
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
it('should fail when path argument is absent', function(done) {
var fs = util.fs();
2014-10-24 18:19:17 +00:00
var shell = new fs.Shell();
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
shell.rm(null, function(error, list) {
expect(error).to.exist;
expect(error.code).to.equal("EINVAL");
expect(list).not.to.exist;
done();
2014-02-19 16:25:30 +00:00
});
2014-05-23 20:53:50 +00:00
});
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
it('should remove a single file', function(done) {
var fs = util.fs();
2014-10-24 18:19:17 +00:00
var shell = new fs.Shell();
2014-05-23 20:53:50 +00:00
var contents = "a";
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
fs.writeFile('/file', contents, function(err) {
if(err) throw err;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
shell.rm('/file', function(err) {
expect(err).not.to.exist;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
fs.stat('/file', function(err, stats) {
expect(err).to.exist;
expect(stats).not.to.exist;
done();
2014-02-19 16:25:30 +00:00
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
it('should remove an empty dir', function(done) {
var fs = util.fs();
2014-10-24 18:19:17 +00:00
var shell = new fs.Shell();
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
fs.mkdir('/dir', function(err) {
if(err) throw err;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
shell.rm('/dir', function(err) {
expect(err).not.to.exist;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
fs.stat('/dir', function(err, stats) {
expect(err).to.exist;
expect(stats).not.to.exist;
done();
2014-02-19 16:25:30 +00:00
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
it('should fail to remove a non-empty dir', function(done) {
var fs = util.fs();
2014-10-24 18:19:17 +00:00
var shell = new fs.Shell();
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
fs.mkdir('/dir', function(err) {
if(err) throw err;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
shell.touch('/dir/file', function(err) {
if(err) throw err;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
shell.rm('/dir', function(err) {
expect(err).to.exist;
expect(err.code).to.equal('ENOTEMPTY');
done();
2014-02-19 16:25:30 +00:00
});
});
});
2014-05-23 20:53:50 +00:00
});
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
it('should remove a non-empty dir with option.recursive set', function(done) {
var fs = util.fs();
2014-10-24 18:19:17 +00:00
var shell = new fs.Shell();
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
fs.mkdir('/dir', function(err) {
if(err) throw err;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
shell.touch('/dir/file', function(err) {
if(err) throw err;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
shell.rm('/dir', { recursive: true }, function(err) {
expect(err).not.to.exist;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
fs.stat('/dir', function(err, stats) {
expect(err).to.exist;
expect(stats).not.to.exist;
done();
2014-02-19 16:25:30 +00:00
});
});
});
});
2014-05-23 20:53:50 +00:00
});
it('should work on a complex dir structure', function(done) {
var fs = util.fs();
2014-10-24 18:19:17 +00:00
var shell = new fs.Shell();
2014-05-23 20:53:50 +00:00
var contents = "a";
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
fs.mkdir('/dir', function(err) {
if(err) throw err;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
fs.mkdir('/dir/dir2', function(err) {
2014-02-19 16:25:30 +00:00
if(err) throw err;
2014-05-23 20:53:50 +00:00
fs.writeFile('/dir/file', contents, function(err) {
2014-02-19 16:25:30 +00:00
if(err) throw err;
2014-05-23 20:53:50 +00:00
fs.writeFile('/dir/file2', contents, function(err) {
2014-02-19 16:25:30 +00:00
if(err) throw err;
2014-05-23 20:53:50 +00:00
shell.rm('/dir', { recursive: true }, function(err) {
expect(err).not.to.exist;
2014-02-19 16:25:30 +00:00
2014-05-23 20:53:50 +00:00
fs.stat('/dir', function(err, stats) {
expect(err).to.exist;
expect(stats).not.to.exist;
done();
2014-02-19 16:25:30 +00:00
});
});
});
});
});
});
});
});