Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

fake-fs pull request #1

Merged
merged 7 commits into from

2 participants

@tgeorgiev

Hi!

Thanks for the cool library, it is just what I needed!

I have made some bugfixes and introduced rmdir and unlink as per http://nodejs.org/api/fs.html. Can you check them?

Thanks

@eldargab
Owner

We should update stat times in .rmdir() and unlink(), otherwise LGTM. Thank you.

@eldargab eldargab merged commit b4bc30c into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 117 additions and 4 deletions.
  1. +47 −4 lib/fake-fs.js
  2. +70 −0 test/fake-fs.js
View
51 lib/fake-fs.js
@@ -3,9 +3,10 @@ var PATH = require('path')
var normalize = PATH.normalize
var join = PATH.join
var dirname = PATH.dirname
+var basename = PATH.basename
function resolve (p) {
- return PATH.resolve(p).replace('\\', '/') // Windows support
+ return PATH.resolve(p).replace(/\\/g, '/') // Windows support
}
function FsError (code) {
@@ -31,6 +32,11 @@ Fs.prototype.file = function (path, content, encoding) {
Fs.prototype._add = function (path, item) {
var segs = path.split('/'); segs.shift()
+
+ if (segs[segs.length - 1] === "") {
+ segs.pop();
+ }
+
var dir = this.root
for (var i = 0; i < segs.length - 1; i++) {
dir = dir.childs[segs[i]] || (dir.childs[segs[i]] = new stat.Dir)
@@ -44,6 +50,11 @@ Fs.prototype._add = function (path, item) {
Fs.prototype._itemAt = function (path) {
var segs = resolve(path).split('/'); segs.shift()
+
+ if (segs[segs.length - 1] === "") {
+ segs.pop();
+ }
+
var item = this.root
for (var i = 0; i < segs.length; i++) {
item = item.childs && item.childs[segs[i]]
@@ -58,6 +69,14 @@ Fs.prototype._get = function (path) {
return item
}
+Fs.prototype._rem = function (path) {
+ var parent = this._get(dirname(path));
+ if (!parent.isDirectory()) throw FsError('ENOTDIR');
+
+ var itemName = basename(path);
+ delete parent.childs[itemName];
+}
+
Fs.prototype.statSync = function (path) {
return this._get(path)
@@ -96,7 +115,29 @@ Fs.prototype.mkdirSync = function (dir, mode) {
this.dir(dir)
}
-;['readdir', 'stat'].forEach(function (meth) {
+Fs.prototype.rmdirSync = function (path) {
+ if (!this.existsSync(path)) throw FsError('ENOENT');
+
+ var item = this._get(path);
+ if (!item.isDirectory()) throw FsError('ENOTDIR');
+
+ var parent = this._get(dirname(path));
+ updateTimes(parent);
+ this._rem(path);
+}
+
+Fs.prototype.unlinkSync = function (path) {
+ if (!this.existsSync(path)) throw FsError('ENOENT');
+
+ var item = this._get(path);
+ if (item.isDirectory()) throw FsError('EISDIR');
+
+ var parent = this._get(dirname(path));
+ updateTimes(parent);
+ this._rem(path);
+}
+
+;['readdir', 'stat', 'rmdir', 'unlink'].forEach(function (meth) {
var sync = meth + 'Sync'
Fs.prototype[meth] = function (p, cb) {
var res, err
@@ -177,7 +218,9 @@ var methods = [
'readdir',
'mkdir',
'readFile',
- 'writeFile'
+ 'writeFile',
+ 'rmdir',
+ 'unlink'
].reduce(function (res, meth) {
res.push(meth)
res.push(meth + 'Sync')
@@ -214,4 +257,4 @@ function updateTimes (stat) {
var now = new Date
stat.mtime = now
stat.ctime = now
-}
+}
View
70 test/fake-fs.js
@@ -180,6 +180,13 @@ describe('Fake FS', function () {
done()
})
})
+
+ it('Should return true for root path', function (done) {
+ fs.exists('/', function (exists) {
+ exists.should.be.true
+ done()
+ })
+ })
})
describe('.mkdir()', function () {
@@ -218,6 +225,69 @@ describe('Fake FS', function () {
})
})
+ describe('.rmdir()', function () {
+ it('Should remove an existing direcory', function () {
+ fs.dir('a/b')
+ fs.rmdirSync('a/b')
+ fs.existsSync('a/b').should.be.false
+ })
+
+ 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
+ fs.existsSync('a/b/file.txt').should.be.false
+ fs.existsSync('a/b').should.be.false
+
+ fs.existsSync('a').should.be.true
+ })
+
+ it('Should throw an ENOTDIR error on file', function () {
+ fs.file('a/file.txt')
+
+ fs.rmdir('a/file.txt', cb)
+
+ cb.error('ENOTDIR')
+ })
+
+ it('Should update dir times on directory removal', function (done) {
+ fs.dir('a/b')
+
+ testTimesUpdated('a', function () {
+ fs.rmdir('a/b')
+ }, done)
+ })
+ })
+
+ describe('.unlink()', function () {
+ it('Should remove an existing file', function () {
+ fs.file('a/file.txt')
+
+ fs.unlinkSync('a/file.txt')
+
+ fs.existsSync('a/file.txt').should.be.false
+ })
+
+ it('Should throw an EISDIR error on directory', function () {
+ fs.dir('a/b')
+
+ fs.unlink('a/b', cb)
+
+ cb.error('EISDIR')
+ })
+
+ it('Should update dir times on file removal', function (done) {
+ fs.file('a/file.txt')
+
+ testTimesUpdated('a', function () {
+ fs.unlink('a/file.txt')
+ }, done)
+ })
+ })
+
describe('.readFile()', function () {
it('Should read file contents', function () {
var content = new Buffer([1, 2, 3])
Something went wrong with that request. Please try again.