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
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
describe('fs.rename', function() {
|
2014-05-23 20:53:50 +00:00
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
it('should be a function', function() {
|
2014-05-23 20:53:50 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
expect(fs.rename).to.be.a('function');
|
|
|
|
});
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
it('should rename an existing file', function(done) {
|
2014-05-23 20:53:50 +00:00
|
|
|
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() {
|
2018-09-27 00:50:42 +00:00
|
|
|
if(complete1 && complete2) {
|
2014-05-23 20:53:50 +00:00
|
|
|
done();
|
2014-01-21 21:25:09 +00:00
|
|
|
}
|
2014-05-23 20:53:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.open('/myfile', 'w+', function(error, fd) {
|
|
|
|
if(error) throw error;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.close(fd, function(error) {
|
|
|
|
if(error) throw error;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.rename('/myfile', '/myotherfile', function(error) {
|
|
|
|
if(error) throw error;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.stat('/myfile', function(error, result) {
|
2014-05-23 20:53:50 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.stat('/myotherfile', function(error, result) {
|
2014-05-23 20:53:50 +00:00
|
|
|
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
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
it('should rename an existing directory', function(done) {
|
2014-10-16 02:04:03 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.mkdir('/mydir', function(error) {
|
|
|
|
if(error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.rename('/mydir', '/myotherdir', function(error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.stat('/mydir', function(error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('ENOENT');
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.stat('/myotherdir', function(error, result) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(result.nlinks).to.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
it('should rename an existing directory if the new path points to an existing directory', function(done) {
|
2014-10-16 02:04:03 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.mkdir('/mydir', function(error) {
|
|
|
|
if(error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.mkdir('/myotherdir', function(error) {
|
|
|
|
if(error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.rename('/mydir', '/myotherdir', function(error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.stat('/mydir', function(error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('ENOENT');
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.stat('/myotherdir', function(error, result) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(result.nlinks).to.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
it('should fail to rename an existing directory if the new path points to an existing directory that is not empty', function(done) {
|
2014-10-16 02:04:03 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.mkdir('/mydir', function(error) {
|
2018-09-26 04:21:43 +00:00
|
|
|
if (error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.mkdir('/myotherdir', function(error) {
|
2018-09-26 04:21:43 +00:00
|
|
|
if (error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.writeFile('/myotherdir/myfile', 'This is a file', function(error) {
|
|
|
|
if(error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.rename('/mydir', '/myotherdir', function(error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('ENOTEMPTY');
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.stat('/mydir', function(error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.stat('/myotherdir', function(error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
it('should fail to rename an existing directory if the new path points to an existing file', function(done) {
|
2014-10-16 02:04:03 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.mkdir('/mydir', function(error) {
|
|
|
|
if(error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.writeFile('/myfile', 'This is a file', function(error) {
|
|
|
|
if(error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.rename('/mydir', '/myfile', function(error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('ENOTDIR');
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.stat('/mydir', function(error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
fs.stat('/myfile', function(error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-09-20 03:46:48 +00:00
|
|
|
|
2018-09-27 00:50:42 +00:00
|
|
|
it('(promise version) should rename an existing file', function(done) {
|
2018-09-20 03:46:48 +00:00
|
|
|
var complete1 = false;
|
|
|
|
var complete2 = false;
|
|
|
|
var fs = util.fs();
|
|
|
|
|
|
|
|
function maybeDone() {
|
2018-09-27 00:50:42 +00:00
|
|
|
if(complete1 && complete2) {
|
2018-09-20 03:46:48 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 14:39:03 +00:00
|
|
|
|
2018-09-26 04:21:43 +00:00
|
|
|
Promise.all(
|
|
|
|
fs.promises.open('/myfile', 'w+')
|
|
|
|
.then((fd)=>fs.promises.close(fd)),
|
|
|
|
fs.promises.rename('/myfile', '/myotherfile'),
|
|
|
|
fs.promises.stat('/myfile')
|
|
|
|
.then( (result)=> expect(result).not.to.exist, (error) => expect(error).to.exist)
|
|
|
|
.finally(()=>{
|
|
|
|
complete1 = true;
|
|
|
|
maybeDone();
|
|
|
|
}),
|
|
|
|
fs.promises.stat('/myotherfile')
|
|
|
|
.then( (result) => expect(result.nlinks).to.equal(1), (error) => expect(error).not.to.exist)
|
|
|
|
.finally(()=>{
|
|
|
|
complete2 = true;
|
|
|
|
maybeDone();
|
|
|
|
})
|
2018-09-26 14:39:03 +00:00
|
|
|
).catch((error)=> {throw error;});
|
2018-09-20 03:46:48 +00:00
|
|
|
});
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|