filer/tests/spec/fs.rename.spec.js

164 lines
4.3 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.rename', 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.rename).to.be.a('function');
});
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should rename an existing file', function(done) {
var complete1 = false;
var complete2 = false;
var fs = util.fs();
2014-05-23 20:53:50 +00:00
function maybeDone() {
if(complete1 && complete2) {
done();
}
2014-05-23 20:53:50 +00:00
}
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
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.rename('/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.stat('/myfile', function(error, result) {
expect(error).to.exist;
2018-07-27 14:34:21 +00:00
expect(result).not.to.exist;
2014-05-23 20:53:50 +00:00
complete1 = true;
maybeDone();
});
fs.stat('/myotherfile', function(error, result) {
expect(error).not.to.exist;
expect(result.nlinks).to.equal(1);
complete2 = true;
maybeDone();
2013-11-27 17:18:09 +00:00
});
});
});
});
});
it('should rename an existing directory', function(done) {
var fs = util.fs();
fs.mkdir('/mydir', function(error) {
if(error) throw error;
fs.rename('/mydir', '/myotherdir', function(error) {
expect(error).not.to.exist;
fs.stat('/mydir', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
fs.stat('/myotherdir', function(error, result) {
expect(error).not.to.exist;
2018-09-19 21:20:54 +00:00
expect(result.nlinks).to.equal(1);
done();
});
});
});
});
});
2018-09-19 21:18:54 +00:00
it('should return error if the file or directory to rename doesnt exist', function(done) {
var fs = util.fs();
fs.rename('/fakeDirectory', '/myotherdir', function(error) {
expect(error).to.exist;
done();
});
});
it('should rename an existing directory if the new path points to an existing directory', function(done) {
var fs = util.fs();
fs.mkdir('/mydir', function(error) {
if(error) throw error;
fs.mkdir('/myotherdir', function(error) {
if(error) throw error;
fs.rename('/mydir', '/myotherdir', function(error) {
expect(error).not.to.exist;
fs.stat('/mydir', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOENT');
fs.stat('/myotherdir', function(error, result) {
expect(error).not.to.exist;
expect(result.nlinks).to.equal(1);
done();
});
});
});
});
});
});
it('should fail to rename an existing directory if the new path points to an existing directory that is not empty', function(done) {
var fs = util.fs();
fs.mkdir('/mydir', function(error) {
if(error) throw error;
fs.mkdir('/myotherdir', function(error) {
if(error) throw error;
fs.writeFile('/myotherdir/myfile', 'This is a file', function(error) {
if(error) throw error;
fs.rename('/mydir', '/myotherdir', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOTEMPTY');
fs.stat('/mydir', function(error) {
expect(error).not.to.exist;
fs.stat('/myotherdir', function(error) {
expect(error).not.to.exist;
done();
});
});
});
});
});
});
});
it('should fail to rename an existing directory if the new path points to an existing file', function(done) {
var fs = util.fs();
fs.mkdir('/mydir', function(error) {
if(error) throw error;
fs.writeFile('/myfile', 'This is a file', function(error) {
if(error) throw error;
fs.rename('/mydir', '/myfile', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('ENOTDIR');
fs.stat('/mydir', function(error) {
expect(error).not.to.exist;
fs.stat('/myfile', function(error) {
expect(error).not.to.exist;
done();
});
});
});
});
});
});
2013-11-27 17:18:09 +00:00
});