Permalink
Browse files

.rename() do not throw when new path points to existing file

  • Loading branch information...
eldargab committed Jan 28, 2013
1 parent 81509e6 commit ea5da472f893510587c5f583a6246f7b8621b170
Showing with 18 additions and 21 deletions.
  1. +8 −7 lib/fake-fs.js
  2. +10 −14 test/fake-fs.js
View
@@ -23,17 +23,17 @@ function Fs (paths) {
}
Fs.prototype.dir = function (path, opts) {
- return this._add(resolve(path), new stat.Dir(opts))
+ return this._add(path, new stat.Dir(opts))
}
Fs.prototype.file = function (path, content, encoding) {
- return this._add(resolve(path), new stat.File(content, encoding))
+ return this._add(path, new stat.File(content, encoding))
}
Fs.prototype._add = function (path, item) {
var segs = path == '/'
? []
- : path.split('/').slice(1)
+ : resolve(path).split('/').slice(1)
var dir = this.root
for (var i = 0; i < segs.length - 1; i++) {
@@ -136,20 +136,21 @@ Fs.prototype.unlinkSync = function (path) {
Fs.prototype.renameSync = function (oldPath, newPath) {
if (!this.existsSync(oldPath)) throw FsError('ENOENT');
- if (this.existsSync(newPath)) throw FsError('EPERM');
+ if (this.existsSync(newPath) && this.statSync(newPath).isDirectory())
+ throw FsError('EPERM');
var newParent = this._get(dirname(newPath));
if (!newParent.isDirectory()) throw FsError('ENOTDIR');
-
-
+
+
var fileOrDir = this._get(oldPath);
var oldParent = this._get(dirname(oldPath));
updateTimes(oldParent);
this._rem(oldPath);
updateTimes(newParent);
- this._add(resolve(newPath), fileOrDir);
+ this._add(newPath, fileOrDir);
}
;['readdir', 'stat', 'rmdir', 'unlink'].forEach(function (meth) {
View
@@ -235,7 +235,7 @@ describe('Fake FS', function () {
it('Should remove an existing direcory, its subdirectories and files', function () {
fs.dir('a/b/c')
fs.file('a/b/file.txt')
-
+
fs.rmdirSync('a/b')
fs.existsSync('a/b/c').should.be.false
@@ -267,7 +267,7 @@ describe('Fake FS', function () {
fs.file('a/file.txt')
fs.unlinkSync('a/file.txt')
-
+
fs.existsSync('a/file.txt').should.be.false
})
@@ -293,7 +293,7 @@ describe('Fake FS', function () {
fs.file('a/file.txt')
fs.renameSync('a/file.txt', 'a/file-new.txt')
-
+
fs.existsSync('a/file.txt').should.be.false
fs.existsSync('a/file-new.txt').should.be.true
})
@@ -303,7 +303,7 @@ describe('Fake FS', function () {
fs.dir('c/d')
fs.renameSync('a/file.txt', 'c/d/file-new.txt')
-
+
fs.existsSync('a/file.txt').should.be.false
fs.existsSync('c/d/file-new.txt').should.be.true
})
@@ -312,7 +312,7 @@ describe('Fake FS', function () {
fs.dir('a/b')
fs.renameSync('a/b', 'a/b-new')
-
+
fs.existsSync('a/b').should.be.false
fs.existsSync('a/b-new').should.be.true
})
@@ -322,7 +322,7 @@ describe('Fake FS', function () {
fs.dir('c/d')
fs.renameSync('a/b', 'c/d/b-new')
-
+
fs.existsSync('a/b').should.be.false
fs.existsSync('c/d/b-new').should.be.true
})
@@ -336,17 +336,13 @@ describe('Fake FS', function () {
cb.error('EPERM')
})
- /*
- One could argue about this, since with node.js fs, when renaming a file or a directory
- you can overwrite an existing file (you CANNOT overwrite an existing directory).
- */
- it('Should throw EPERM when new path points to existing file', function () {
+ it('Should not throw EPERM when new path points to existing file', function () {
fs.file('a/file1.txt')
fs.file('c/file2.txt')
fs.rename('a/file1.txt', 'c/file2.txt', cb)
-
- cb.error('EPERM')
+
+ cb.result()
})
it('Should throw ENOENT when new (directory) path points to a non-existent parent', function () {
@@ -486,4 +482,4 @@ describe('Fake FS', function () {
origStat.should.equal(global.stat)
})
})
-})
+})

0 comments on commit ea5da47

Please sign in to comment.