2014-05-23 20:53:50 +00:00
|
|
|
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) {
|
2018-12-21 01:11:51 +00:00
|
|
|
var shell = util.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;
|
2018-07-15 17:25:35 +00:00
|
|
|
expect(error.code).to.equal('EINVAL');
|
2014-05-23 20:53:50 +00:00
|
|
|
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();
|
2018-12-21 01:11:51 +00:00
|
|
|
var shell = util.shell();
|
2018-07-15 17:25:35 +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();
|
2018-12-21 01:11:51 +00:00
|
|
|
var shell = util.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();
|
2018-12-21 01:11:51 +00:00
|
|
|
var shell = util.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();
|
2018-12-21 01:11:51 +00:00
|
|
|
var shell = util.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();
|
2018-12-21 01:11:51 +00:00
|
|
|
var shell = util.shell();
|
2018-07-15 17:25:35 +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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|