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-19 21:37:20 +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-19 21:37:20 +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-19 21:37:20 +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-19 21:37:20 +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-19 21:37:20 +00:00
|
|
|
fs.open('/myfile', 'w+', function (error, fd) {
|
|
|
|
if (error) throw error;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2018-09-19 21:37:20 +00:00
|
|
|
fs.close(fd, function (error) {
|
|
|
|
if (error) throw error;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2018-09-19 21:37:20 +00:00
|
|
|
fs.rename('/myfile', '/myotherfile', function (error) {
|
|
|
|
if (error) throw error;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2018-09-19 21:37:20 +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-19 21:37:20 +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-19 21:37:20 +00:00
|
|
|
it('should rename an existing directory', function (done) {
|
2014-10-16 02:04:03 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
|
2018-09-19 21:37:20 +00:00
|
|
|
fs.mkdir('/mydir', function (error) {
|
|
|
|
if (error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-19 21:37:20 +00:00
|
|
|
fs.rename('/mydir', '/myotherdir', function (error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
2018-09-19 21:37:20 +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-19 21:37:20 +00:00
|
|
|
fs.stat('/myotherdir', function (error, result) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
2018-09-19 21:20:54 +00:00
|
|
|
expect(result.nlinks).to.equal(1);
|
2014-10-16 02:04:03 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-09-19 21:37:20 +00:00
|
|
|
it('should return error if the file or directory to rename doesnt exist', function (done) {
|
2018-09-19 21:08:48 +00:00
|
|
|
var fs = util.fs();
|
2018-09-19 21:37:20 +00:00
|
|
|
fs.rename('/fakeDirectory', '/myotherdir', function (error) {
|
|
|
|
expect(error).to.exist;
|
|
|
|
done();
|
|
|
|
});
|
2018-09-19 21:08:48 +00:00
|
|
|
});
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-19 21:37:20 +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-19 21:37:20 +00:00
|
|
|
fs.mkdir('/mydir', function (error) {
|
|
|
|
if (error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-19 21:37:20 +00:00
|
|
|
fs.mkdir('/myotherdir', function (error) {
|
|
|
|
if (error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-19 21:37:20 +00:00
|
|
|
fs.rename('/mydir', '/myotherdir', function (error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
2018-09-19 21:37:20 +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-19 21:37:20 +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-19 21:37:20 +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-19 21:37:20 +00:00
|
|
|
fs.mkdir('/mydir', function (error) {
|
|
|
|
if (error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-19 21:37:20 +00:00
|
|
|
fs.mkdir('/myotherdir', function (error) {
|
|
|
|
if (error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-19 21:37:20 +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-19 21:37:20 +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-19 21:37:20 +00:00
|
|
|
fs.stat('/mydir', function (error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
|
2018-09-19 21:37:20 +00:00
|
|
|
fs.stat('/myotherdir', function (error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-19 21:37:20 +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-19 21:37:20 +00:00
|
|
|
fs.mkdir('/mydir', function (error) {
|
|
|
|
if (error) throw error;
|
2014-10-16 02:04:03 +00:00
|
|
|
|
2018-09-19 21:37:20 +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-19 21:37:20 +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-19 21:37:20 +00:00
|
|
|
fs.stat('/mydir', function (error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
|
2018-09-19 21:37:20 +00:00
|
|
|
fs.stat('/myfile', function (error) {
|
2014-10-16 02:04:03 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|