Permalink
Browse files

Merge pull request #1 from tgeorgiev/master

  • Loading branch information...
2 parents 3ccae73 + d726b46 commit b4bc30c8477bdc25fe8fb35355e95fe7ff94cf6f @eldargab committed Jan 28, 2013
Showing with 117 additions and 4 deletions.
  1. +47 −4 lib/fake-fs.js
  2. +70 −0 test/fake-fs.js
View
@@ -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
@@ -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])

0 comments on commit b4bc30c

Please sign in to comment.