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
|
|
|
|
2018-10-16 13:43:44 +00:00
|
|
|
it.skip('should be able to rename an existing file to the same filename', function(done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
|
|
|
|
fs.open('/myfile', 'w+', function(error, fd) {
|
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
fs.close(fd, function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
fs.rename('/myfile', '/myfile', function(error) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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-01-21 21:25:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
function maybeDone() {
|
|
|
|
if(complete1 && complete2) {
|
|
|
|
done();
|
2014-01-21 21:25:09 +00:00
|
|
|
}
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-16 02:04:03 +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;
|
|
|
|
expect(result.nlinks).to.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-16 15:07:10 +00:00
|
|
|
it('should rename an existing directory (using promises)', () => {
|
|
|
|
var fsPromises = util.fs().promises;
|
|
|
|
|
|
|
|
return fsPromises.mkdir('/mydir')
|
|
|
|
.then(() => fsPromises.rename('/mydir', '/myotherdir'))
|
|
|
|
.then(() => { fsPromises.stat('/mydir')
|
|
|
|
.catch((error) => {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('ENOENT');
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => { fsPromises.stat('/myotherdir')
|
|
|
|
.then(result => {
|
|
|
|
expect(result.nlinks).to.equal(1);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
if (error) throw error;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-16 02:04:03 +00:00
|
|
|
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
|
|
|
});
|