filer/tests/spec/fs.unlink.spec.js

143 lines
3.6 KiB
JavaScript
Raw Permalink 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.unlink', 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.unlink).to.be.a('function');
});
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should remove a link to an existing file', function(done) {
var fs = util.fs();
var complete1, complete2;
2014-05-23 20:53:50 +00:00
function maybeDone() {
if(complete1 && complete2) {
done();
}
2014-05-23 20:53:50 +00:00
}
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.close(fd, function(error) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.link('/myfile', '/myotherfile', function(error) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.unlink('/myfile', function(error) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.stat('/myfile', function(error, result) {
expect(error).to.exist;
2018-07-26 22:02:16 +00:00
expect(result).not.to.exist;
2014-05-23 20:53:50 +00:00
complete1 = true;
maybeDone();
});
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.stat('/myotherfile', function(error, result) {
if(error) throw error;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
expect(result.nlinks).to.equal(1);
complete2 = true;
maybeDone();
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 not follow symbolic links', function(done) {
var fs = util.fs();
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.symlink('/', '/myFileLink', function (error) {
if (error) throw error;
fs.link('/myFileLink', '/myotherfile', function (error) {
2013-11-27 17:18:09 +00:00
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.unlink('/myFileLink', function (error) {
2013-11-27 17:18:09 +00:00
if (error) throw error;
2014-05-23 20:53:50 +00:00
fs.lstat('/myFileLink', function (error, result) {
expect(error).to.exist;
2018-07-26 22:02:16 +00:00
expect(result).not.to.exist;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.lstat('/myotherfile', function (error, result) {
if (error) throw error;
expect(result.nlinks).to.equal(1);
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.stat('/', function (error, result) {
2013-11-27 17:18:09 +00:00
if (error) throw error;
expect(result.nlinks).to.equal(1);
2014-05-23 20:53:50 +00:00
done();
2013-11-27 17:18:09 +00:00
});
});
});
});
});
});
});
it('should not unlink directories', function (done) {
var fs = util.fs();
fs.mkdir('/mydir', function (error) {
if(error) throw error;
fs.unlink('/mydir', function (error) {
expect(error).to.exist;
expect(error.code).to.equal('EPERM');
fs.stat('/mydir', function (error, stats) {
expect(error).not.to.exist;
expect(stats).to.exist;
expect(stats.isDirectory()).to.be.true;
done();
});
});
});
});
2013-11-27 17:18:09 +00:00
});
describe('fs.promises.unlink', function () {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function () {
var fs = util.fs();
expect(fs.promises.unlink).to.be.a('function');
});
it('should return an error if trying to delete a file that does not exist', function() {
var fsPromises = util.fs().promises;
return fsPromises.unlink('/myFile')
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
});
});
it('should not unlink directories', () => {
var fs = util.fs().promises;
return fs.mkdir('/mydir')
.then(() => fs.unlink('/mydir'))
.catch(error => {
expect(error).to.exist;
expect(error.code).to.equal('EPERM');
})
.then(() => fs.stat('/mydir'))
.then(stats => {
expect(stats).to.exist;
expect(stats.type).to.equal('DIRECTORY');
});
});
});