filer/tests/spec/fs.rmdir.spec.js

180 lines
4.8 KiB
JavaScript
Raw Normal View History

2014-05-23 20:53:50 +00:00
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
describe('fs.rmdir', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should be a function', function() {
var fs = util.fs();
expect(fs.rmdir).to.be.a('function');
});
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should return an error if the path does not exist', function(done) {
var fs = util.fs();
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.rmdir('/tmp/mydir', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
2014-05-23 20:53:50 +00:00
done();
2013-11-27 17:18:09 +00:00
});
2014-05-23 20:53:50 +00:00
});
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should return an error if attempting to remove the root directory', function(done) {
var fs = util.fs();
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.rmdir('/', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EBUSY');
2014-05-23 20:53:50 +00:00
done();
2013-11-27 17:18:09 +00:00
});
2014-05-23 20:53:50 +00:00
});
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should return an error if the directory is not empty', function(done) {
var fs = util.fs();
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.mkdir('/tmp', function(error) {
if(error) throw error;
fs.mkdir('/tmp/mydir', function(error) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.rmdir('/', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EBUSY');
2014-05-23 20:53:50 +00:00
done();
2013-11-27 17:18:09 +00:00
});
});
});
2014-05-23 20:53:50 +00:00
});
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should return an error if the path is not a directory', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.mkdir('/tmp', function(error) {
if(error) throw error;
fs.open('/tmp/myfile', 'w', function(error, fd) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.close(fd, function(error) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.rmdir('/tmp/myfile', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOTDIR');
2014-05-23 20:53:50 +00:00
done();
2013-11-27 17:18:09 +00:00
});
});
});
});
2014-05-23 20:53:50 +00:00
});
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should return an error if the path is a symbolic link', function(done) {
var fs = util.fs();
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.mkdir('/tmp', function (error) {
if(error) throw error;
fs.symlink('/tmp', '/tmp/myfile', function (error) {
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.rmdir('/tmp/myfile', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOTDIR');
2014-05-23 20:53:50 +00:00
done();
2013-11-27 17:18:09 +00:00
});
});
});
2014-05-23 20:53:50 +00:00
});
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should remove an existing directory', function(done) {
var fs = util.fs();
2014-05-23 20:53:50 +00:00
fs.mkdir('/tmp', function(error) {
if(error) throw error;
fs.rmdir('/tmp', function(error) {
expect(error).not.to.exist;
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.stat('/tmp', function(error, stats) {
expect(error).to.exist;
expect(stats).not.to.exist;
done();
2013-11-27 17:18:09 +00:00
});
});
});
});
it('(promise) should be a function', function() {
var fsPromises = util.fs().promises;
expect(fsPromises.rmdir).to.be.a('function');
});
it('(promise) should return an error if the path does not exist', function() {
var fsPromises = util.fs().promises;
return fsPromises.rmdir('/tmp/mydir')
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
});
});
it('(promise) should return an error if attempting to remove the root directory', function() {
var fsPromises = util.fs().promises;
return fsPromises.rmdir('/')
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('EBUSY');
});
});
2013-11-27 17:18:09 +00:00
});
describe('fs.promises.rmdir', function(){
beforeEach(util.setup);
afterEach(util.cleanup);
it('should return an error if the directory is not empty', function() {
var fs = util.fs();
var fsPromises = fs.promises;
return fsPromises.mkdir('/tmp')
.then(() => fsPromises.mkdir('/tmp/mydir'))
.then(() => fsPromises.rmdir('/'))
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('EBUSY');
});
});
it('should return an error if the path is not a directory', function() {
var fs = util.fs();
var fsPromises = fs.promises;
return fsPromises.mkdir('/tmp')
.then(() => fsPromises.writeFile('/tmp/myfile','Hello World'))
.then(() => fsPromises.rmdir('/tmp/myfile'))
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOTDIR');
});
});
});
2018-12-15 03:17:15 +00:00
describe('fsPromises.rmdir', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should return an error if the path does not exist', function() {
var fs = util.fs().promises;
return fs.rmdir('/tmp/mydir')
2018-12-15 03:17:15 +00:00
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
});
});
2018-12-15 03:17:15 +00:00
it('should return an error if attempting to remove the root directory', function() {
var fs = util.fs().promises;
return fs.rmdir('/')
2018-12-15 03:17:15 +00:00
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('EBUSY');
});
});
2018-09-25 03:27:37 +00:00
});